Skip to content
This repository has been archived by the owner on Nov 14, 2023. It is now read-only.

Commit

Permalink
fix: update DropdownButton item key
Browse files Browse the repository at this point in the history
Signed-off-by: zhanghuan <zhang.huan@xsky.com>
  • Loading branch information
xskyAlex committed Nov 16, 2022
1 parent f02646c commit fe07a1c
Showing 1 changed file with 18 additions and 10 deletions.
28 changes: 18 additions & 10 deletions src/components/DropdownButton/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -17,22 +17,29 @@ function randomId() {
.toString(36)
.substring(2);
}

function renderContent(menu: DropdownButtonMenuItem[] = [], setButtonOpen: Function, open?: boolean) {
if (menu instanceof Array) {
return menu.map(m => renderMenu(m, setButtonOpen, open));
}
return menu;
}

function renderMenu(menu: DropdownButtonMenuItem, setButtonOpen: Function, open?: boolean) {
const item = cloneDeep(menu);
const item = React.useMemo(() => {
if (typeof menu === 'string') {
return menu
} else {
return cloneDeep({...menu, key: menu.key || randomId()})
}
}, [menu]);
if (!item) {
return null;
}
if (typeof item === 'string') {
return <MenuItem key={item} onSelect={() => { setButtonOpen(!!open) }} >{item}</MenuItem>;
}
if (!item.key) {
item.key = randomId();
return <MenuItem key={item} onSelect={() => {
setButtonOpen(!!open)
}}>{item}</MenuItem>;
}
if (item.children && item.children.length) {
return (
Expand All @@ -42,16 +49,17 @@ function renderMenu(menu: DropdownButtonMenuItem, setButtonOpen: Function, open?
);
}
const handleItemSelect = (eventKey: any) => {
const { onSelect } = item;
const {onSelect} = item;
setButtonOpen(!!open);
// 如果有传入 onSelect 回调函数,会继续执行传入的回调函数
if (onSelect) onSelect(eventKey);
}
const menuProps = omit(item, 'toolTip');
return <MenuItem {...menuProps} onSelect={handleItemSelect} >
{ item.toolTip ? (<Tooltip {...item.toolTip} placement={item.toolTip.placement || 'right'}>{item.toolTip.children}</Tooltip>)
: item.title }
</MenuItem>;
return <MenuItem {...menuProps} onSelect={handleItemSelect}>
{item.toolTip ? (
<Tooltip {...item.toolTip} placement={item.toolTip.placement || 'right'}>{item.toolTip.children}</Tooltip>)
: item.title}
</MenuItem>;
}

const DropdownButton = (props: DropdownButtonProps) => {
Expand Down

0 comments on commit fe07a1c

Please sign in to comment.