Skip to content
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

fix: Items unique key warning in dropdown menu to avoid rerenders #10925

Merged
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
54 changes: 31 additions & 23 deletions packages/uikit/src/components/DropdownMenu/DropdownMenu.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -70,8 +70,28 @@ const MenuItem: React.FC<{
return disabled || isDisabled ? "disabled" : "textSubtle";
}, [activeSubItemChildItem, disabled, hasChildItems, isDisabled, item]);

const handleClick = useCallback(
(e: React.MouseEvent<HTMLButtonElement>) => {
if (hasChildItems) {
handleToggleSubMenu(e);
} else {
setIsOpen(false);
itemProps?.onClick?.(e);
}
},
[hasChildItems, handleToggleSubMenu, setIsOpen, itemProps?.onClick]
);

const handleExternalClick = useCallback(
(e: any) => {
setIsOpen(false);
itemProps?.onClick?.(e);
},
[setIsOpen, itemProps?.onClick]
);

return (
<StyledDropdownMenuItemContainer key={label?.toString()}>
<StyledDropdownMenuItemContainer>
{type === DropdownMenuItemType.BUTTON && (
<DropdownMenuItem $isActive={isActive} disabled={disabled || isDisabled} type="button" {...itemProps}>
{MenuItemContent}
Expand All @@ -84,14 +104,7 @@ const MenuItem: React.FC<{
href={href}
{...itemProps}
$isActive={isActive}
onClick={(e: React.MouseEvent<HTMLButtonElement>) => {
if (hasChildItems) {
handleToggleSubMenu(e);
} else {
setIsOpen(false);
itemProps.onClick?.(e);
}
}}
onClick={handleClick}
>
{MenuItemContent}
{hasChildItems && (
Expand All @@ -112,10 +125,7 @@ const MenuItem: React.FC<{
as="a"
href={href}
target="_blank"
onClick={(e: any) => {
setIsOpen(false);
itemProps.onClick?.(e);
}}
onClick={handleExternalClick}
>
<Flex alignItems="center" justifyContent="space-between" width="100%">
{MenuItemContent}
Expand All @@ -130,10 +140,10 @@ const MenuItem: React.FC<{
hasChildItems &&
items
?.filter((childItem) => ((isMobile || isMd) && childItem.isMobileOnly) || !childItem.isMobileOnly)
?.map((childItem) => (
?.map((childItem, index) => (
<MenuItem
isChildItems
key={childItem?.label?.toString()}
key={childItem?.label?.toString() || `delimiter${index}`}
item={childItem}
isDisabled={isDisabled}
linkComponent={linkComponent}
Expand Down Expand Up @@ -211,15 +221,13 @@ const DropdownMenu: React.FC<React.PropsWithChildren<DropdownMenuProps>> = ({
}, [setIsOpen])
);

const handlePointerDown = useCallback(() => {
setIsOpen((s) => !s);
}, []);

return (
<Box ref={setTargetRef} {...props}>
<Box
onPointerDown={() => {
setIsOpen((s) => !s);
}}
>
{children}
</Box>
<Box onPointerDown={handlePointerDown}>{children}</Box>
{hasItems && (
<StyledDropdownMenu
ref={setTooltipRef}
Expand All @@ -232,7 +240,7 @@ const DropdownMenu: React.FC<React.PropsWithChildren<DropdownMenuProps>> = ({
.filter((item) => ((isMobile || isMd) && item.isMobileOnly) || !item.isMobileOnly)
.map((item) => (
<MenuItem
key={item?.label?.toString()}
key={item?.label?.toString() || `delimiter${index}`}
item={item}
activeItem={activeItem}
activeSubItemChildItem={activeSubItemChildItem}
Expand Down
Loading