-
-
Notifications
You must be signed in to change notification settings - Fork 263
refactor: 调整 MenuItem 渲染逻辑,将 label 包装在 span 中并优化条件判断 #843
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||||
|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -47,13 +47,11 @@ function convertItemsToNodes( | |||||||||
| if (type === 'divider') { | ||||||||||
| return <MergedDivider key={mergedKey} {...restProps} />; | ||||||||||
| } | ||||||||||
|
|
||||||||||
| const hasExtra = !!extra || extra === 0; | ||||||||||
| return ( | ||||||||||
| <MergedMenuItem key={mergedKey} {...restProps} extra={extra}> | ||||||||||
| {label} | ||||||||||
| {(!!extra || extra === 0) && ( | ||||||||||
| <span className={`${prefixCls}-item-extra`}>{extra}</span> | ||||||||||
| )} | ||||||||||
| {hasExtra ? <span className={`${prefixCls}-item-label`}>{label}</span> : label} | ||||||||||
| {hasExtra && <span className={`${prefixCls}-item-extra`}>{extra}</span>} | ||||||||||
|
Comment on lines
+53
to
+54
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 潜在问题:prefixCls 可能为 undefined 导致无效的 class 名称 当 建议添加保护逻辑: 🔎 建议的修复方案方案一:添加默认值 - {hasExtra ? <span className={`${prefixCls}-item-label`}>{label}</span> : label}
- {hasExtra && <span className={`${prefixCls}-item-extra`}>{extra}</span>}
+ {hasExtra ? <span className={prefixCls ? `${prefixCls}-item-label` : 'item-label'}>{label}</span> : label}
+ {hasExtra && <span className={prefixCls ? `${prefixCls}-item-extra` : 'item-extra'}>{extra}</span>}方案二:在函数开始处验证 prefixCls function convertItemsToNodes(
list: ItemType[],
components: Required<Components>,
prefixCls: string = 'rc-menu', // 添加默认值
) {
// ...
}📝 Committable suggestion
Suggested change
🤖 Prompt for AI Agents |
||||||||||
| </MergedMenuItem> | ||||||||||
| ); | ||||||||||
| } | ||||||||||
|
|
||||||||||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
为了使代码更简洁,这里的条件判断可以省略
!!。在 JavaScript 的布尔运算中,extra会被自动转换为布尔值进行判断,因此!!extra和extra在此处的逻辑效果是等价的。