From 2a3eccc3225695dfc98b6a53a989bbf652e888d4 Mon Sep 17 00:00:00 2001 From: Kirill Dyachkovskiy <81510334+KirillDyachkovskiy@users.noreply.github.com> Date: Mon, 13 Nov 2023 20:17:51 +0300 Subject: [PATCH] fix(DropdownMenu): fix inner usages of switcher prop (#1116) --- .../Breadcrumbs/BreadcrumbsMore.tsx | 19 +++--- src/components/DropdownMenu/DropdownMenu.tsx | 58 +++++++++++-------- .../__stories__/DropdownMenu.stories.tsx | 10 +++- .../TableColumnSetup/TableColumnSetup.tsx | 33 +++++++---- .../withTableSettings/withTableSettings.tsx | 10 +++- 5 files changed, 79 insertions(+), 51 deletions(-) diff --git a/src/components/Breadcrumbs/BreadcrumbsMore.tsx b/src/components/Breadcrumbs/BreadcrumbsMore.tsx index 9a67c8e038..d4db1684fa 100644 --- a/src/components/Breadcrumbs/BreadcrumbsMore.tsx +++ b/src/components/Breadcrumbs/BreadcrumbsMore.tsx @@ -11,14 +11,6 @@ interface Props extends Pick - ... - - ); -} - export function BreadcrumbsMore({popupStyle, popupPlacement, items}: Props) { return ( } + renderSwitcher={({onClick}) => ( + + ... + + )} /> ); } diff --git a/src/components/DropdownMenu/DropdownMenu.tsx b/src/components/DropdownMenu/DropdownMenu.tsx index 986f51f781..74b9467a73 100644 --- a/src/components/DropdownMenu/DropdownMenu.tsx +++ b/src/components/DropdownMenu/DropdownMenu.tsx @@ -138,17 +138,29 @@ const DropdownMenu = ({ [items], ); - const handleSwitcherClick: React.MouseEventHandler = (event) => { - if (disabled) { - return; - } + const handleSwitcherClick = React.useCallback>( + (event) => { + if (disabled) { + return; + } - onSwitcherClick?.(event); - togglePopup(); - }; + onSwitcherClick?.(event); + + togglePopup(); + }, + [disabled, onSwitcherClick, togglePopup], + ); const {onKeyDown: handleSwitcherKeyDown} = useActionHandlers(handleSwitcherClick); + const switcherProps = React.useMemo( + () => ({ + onClick: handleSwitcherClick, + onKeyDown: handleSwitcherKeyDown, + }), + [handleSwitcherClick, handleSwitcherKeyDown], + ); + return ( {/* FIXME remove switcher prop and this wrapper */} @@ -156,25 +168,21 @@ const DropdownMenu = ({
- {renderSwitcher?.({ - onClick: handleSwitcherClick, - onKeyDown: handleSwitcherKeyDown, - }) || - switcher || ( - - )} + {renderSwitcher?.(switcherProps) || switcher || ( + + )}
+ renderSwitcher={({onClick}) => ( + - } + )} popupProps={{className: b('label-switcher-menu')}} /> ); diff --git a/src/components/Table/hoc/withTableSettings/TableColumnSetup/TableColumnSetup.tsx b/src/components/Table/hoc/withTableSettings/TableColumnSetup/TableColumnSetup.tsx index 5b696b2715..2a1e73832c 100644 --- a/src/components/Table/hoc/withTableSettings/TableColumnSetup/TableColumnSetup.tsx +++ b/src/components/Table/hoc/withTableSettings/TableColumnSetup/TableColumnSetup.tsx @@ -116,13 +116,13 @@ export const TableColumnSetup = (props: TableColumnSetupProps) => { const handleClosePopup = () => setInitialState(); - const handleControlClick = () => { + const handleControlClick = React.useCallback(() => { if (!disabled) { setFocused(!focused); setRequiredItems(getRequiredItems(items)); setCurrentItems(getConfigurableItems(items)); } - }; + }, [disabled, focused, items]); const handleApplyClick = () => { setInitialState(); @@ -212,19 +212,30 @@ export const TableColumnSetup = (props: TableColumnSetupProps) => { const {onKeyDown: handleControlKeyDown} = useActionHandlers(handleControlClick); + const switcherProps = React.useMemo( + () => ({ + onClick: handleControlClick, + onKeyDown: handleControlKeyDown, + }), + [handleControlClick, handleControlKeyDown], + ); + return (
{/* FIXME remove switcher prop and this wrapper */} {/* eslint-disable-next-line jsx-a11y/click-events-have-key-events, jsx-a11y/no-static-element-interactions */} -
- {renderSwitcher?.({onClick: handleControlClick, onKeyDown: handleControlKeyDown}) || - switcher || ( - - )} +
+ {renderSwitcher?.(switcherProps) || switcher || ( + + )}
( sortable={sortable} onUpdate={onUpdateColumns} items={columnSetupItems} - switcher={ - - } + )} />
);