Skip to content

Commit dde7f36

Browse files
committed
Pass and call a memoized toggle callback
1 parent a2c3f42 commit dde7f36

File tree

2 files changed

+10
-10
lines changed

2 files changed

+10
-10
lines changed

packages/compass-connections-navigation/src/base-navigation-item.tsx

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ type NavigationBaseItemProps = {
2828
actions: NavigationItemActions;
2929
onAction: (action: Actions) => void;
3030
};
31-
onExpand: (toggle: boolean) => void;
31+
toggleExpand: () => void;
3232
};
3333

3434
const menuStyles = css({
@@ -98,7 +98,7 @@ export const NavigationBaseItem: React.FC<NavigationBaseItemProps> = ({
9898
isExpanded,
9999
isFocused,
100100
hasDefaultAction,
101-
onExpand,
101+
toggleExpand,
102102
children,
103103
}) => {
104104
const [hoverProps, isHovered] = useHoverState();
@@ -114,11 +114,7 @@ export const NavigationBaseItem: React.FC<NavigationBaseItemProps> = ({
114114
<div className={cx('item-wrapper', itemWrapperStyles)} style={style}>
115115
{isExpandVisible && (
116116
<ExpandButton
117-
onClick={(evt) => {
118-
if (isExpandDisabled) return;
119-
evt.stopPropagation();
120-
onExpand(!isExpanded);
121-
}}
117+
onClick={toggleExpand}
122118
isExpanded={isExpanded}
123119
disabled={isExpandDisabled}
124120
></ExpandButton>

packages/compass-connections-navigation/src/navigation-item.tsx

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -242,6 +242,12 @@ export function NavigationItem({
242242
return actions;
243243
}, [item, isDarkMode]);
244244

245+
const toggleExpand = useCallback(() => {
246+
if (item.type !== 'placeholder') {
247+
onItemExpand(item, !item.isExpanded);
248+
}
249+
}, [onItemExpand, item]);
250+
245251
return (
246252
<StyledNavigationItem item={item}>
247253
{item.type === 'placeholder' ? (
@@ -262,9 +268,7 @@ export function NavigationItem({
262268
isExpandDisabled={
263269
item.type === 'connection' && item.connectionStatus !== 'connected'
264270
}
265-
onExpand={(isExpanded: boolean) => {
266-
onItemExpand(item, isExpanded);
267-
}}
271+
toggleExpand={toggleExpand}
268272
actionProps={actionProps}
269273
>
270274
{!!connectionStaticActions.length && (

0 commit comments

Comments
 (0)