From 870d2e3e0734e98434272702f31b348f4ff0d6a9 Mon Sep 17 00:00:00 2001 From: memoyil <2213635+memoyil@users.noreply.github.com> Date: Thu, 7 Nov 2024 11:44:10 +0100 Subject: [PATCH] fix: Items unique key warning in dropdown menu to avoid rerenders --- .../components/DropdownMenu/DropdownMenu.tsx | 54 +++++++++++-------- 1 file changed, 31 insertions(+), 23 deletions(-) diff --git a/packages/uikit/src/components/DropdownMenu/DropdownMenu.tsx b/packages/uikit/src/components/DropdownMenu/DropdownMenu.tsx index c12bf1d0c2353..536c6c6aa54c9 100644 --- a/packages/uikit/src/components/DropdownMenu/DropdownMenu.tsx +++ b/packages/uikit/src/components/DropdownMenu/DropdownMenu.tsx @@ -70,8 +70,28 @@ const MenuItem: React.FC<{ return disabled || isDisabled ? "disabled" : "textSubtle"; }, [activeSubItemChildItem, disabled, hasChildItems, isDisabled, item]); + const handleClick = useCallback( + (e: React.MouseEvent) => { + 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 ( - + {type === DropdownMenuItemType.BUTTON && ( {MenuItemContent} @@ -84,14 +104,7 @@ const MenuItem: React.FC<{ href={href} {...itemProps} $isActive={isActive} - onClick={(e: React.MouseEvent) => { - if (hasChildItems) { - handleToggleSubMenu(e); - } else { - setIsOpen(false); - itemProps.onClick?.(e); - } - }} + onClick={handleClick} > {MenuItemContent} {hasChildItems && ( @@ -112,10 +125,7 @@ const MenuItem: React.FC<{ as="a" href={href} target="_blank" - onClick={(e: any) => { - setIsOpen(false); - itemProps.onClick?.(e); - }} + onClick={handleExternalClick} > {MenuItemContent} @@ -130,10 +140,10 @@ const MenuItem: React.FC<{ hasChildItems && items ?.filter((childItem) => ((isMobile || isMd) && childItem.isMobileOnly) || !childItem.isMobileOnly) - ?.map((childItem) => ( + ?.map((childItem, index) => ( > = ({ }, [setIsOpen]) ); + const handlePointerDown = useCallback(() => { + setIsOpen((s) => !s); + }, []); + return ( - { - setIsOpen((s) => !s); - }} - > - {children} - + {children} {hasItems && ( > = ({ .filter((item) => ((isMobile || isMd) && item.isMobileOnly) || !item.isMobileOnly) .map((item) => (