From 8dda731db677fd8045ce1e7aa363fc223af557c6 Mon Sep 17 00:00:00 2001 From: Himanshu Singh Date: Mon, 24 Jun 2024 16:43:03 +0200 Subject: [PATCH] fix(compass-connections-navigation): use updated reference of required props while rendering NavigationItem (#5955) --- .../src/connections-navigation-tree.tsx | 13 +++++- .../src/navigation-item.tsx | 3 +- .../src/virtual-list/virtual-list.spec.tsx | 4 +- .../src/virtual-list/virtual-list.tsx | 44 +++++++++++++++++-- 4 files changed, 55 insertions(+), 9 deletions(-) diff --git a/packages/compass-connections-navigation/src/connections-navigation-tree.tsx b/packages/compass-connections-navigation/src/connections-navigation-tree.tsx index 122cfa64997..a41f0459782 100644 --- a/packages/compass-connections-navigation/src/connections-navigation-tree.tsx +++ b/packages/compass-connections-navigation/src/connections-navigation-tree.tsx @@ -182,9 +182,18 @@ const ConnectionsNavigationTree: React.FunctionComponent< height={height} itemHeight={ROW_HEIGHT} onDefaultAction={onDefaultAction} - onExpandedChange={onItemExpand} + onItemAction={onItemAction} + onItemExpand={onItemExpand} + getItemActions={getItemActions} getItemKey={(item) => item.id} - renderItem={({ item, isActive, isFocused }) => { + renderItem={({ + item, + isActive, + isFocused, + onItemAction, + onItemExpand, + getItemActions, + }) => { return ( NavigationItemActions; onItemAction: (item: SidebarActionableItem, action: Actions) => void; - onItemExpand: OnExpandedChange; + onItemExpand(item: SidebarActionableItem, isExpanded: boolean): void; }; export function NavigationItem({ diff --git a/packages/compass-connections-navigation/src/virtual-list/virtual-list.spec.tsx b/packages/compass-connections-navigation/src/virtual-list/virtual-list.spec.tsx index 525976bed5c..905a384e7e8 100644 --- a/packages/compass-connections-navigation/src/virtual-list/virtual-list.spec.tsx +++ b/packages/compass-connections-navigation/src/virtual-list/virtual-list.spec.tsx @@ -91,7 +91,9 @@ function NavigationTree({ height={400} itemHeight={30} onDefaultAction={() => {}} - onExpandedChange={onExpandedChange} + onItemExpand={onExpandedChange} + onItemAction={() => {}} + getItemActions={() => []} width={100} renderItem={({ item }) => item.name} __TEST_OVER_SCAN_COUNT={Infinity} diff --git a/packages/compass-connections-navigation/src/virtual-list/virtual-list.tsx b/packages/compass-connections-navigation/src/virtual-list/virtual-list.tsx index 41afa8fe08b..7abfb1a1215 100644 --- a/packages/compass-connections-navigation/src/virtual-list/virtual-list.tsx +++ b/packages/compass-connections-navigation/src/virtual-list/virtual-list.tsx @@ -15,6 +15,9 @@ import { useFocusRing, useId, } from '@mongodb-js/compass-components'; +import { type SidebarActionableItem, type SidebarTreeItem } from '../tree-data'; +import { type Actions } from '../constants'; +import { type NavigationItemActions } from '../item-actions'; function useDefaultAction( item: T, @@ -55,12 +58,18 @@ type RenderItem = (props: { isActive: boolean; isFocused: boolean; item: T; + onItemAction(this: void, item: SidebarActionableItem, action: Actions): void; + onItemExpand( + this: void, + item: SidebarActionableItem, + isExpanded: boolean + ): void; + getItemActions(this: void, item: SidebarTreeItem): NavigationItemActions; }) => React.ReactNode; export type OnDefaultAction = ( item: T, evt: React.MouseEvent | React.KeyboardEvent ) => void; -export type OnExpandedChange = (item: T, expanded: boolean) => void; type VirtualTreeProps = { dataTestId?: string; @@ -72,7 +81,13 @@ type VirtualTreeProps = { renderItem: RenderItem; getItemKey?: (item: T) => string; onDefaultAction: OnDefaultAction>; - onExpandedChange: OnExpandedChange>; + onItemExpand( + this: void, + item: SidebarActionableItem, + isExpanded: boolean + ): void; + onItemAction(this: void, item: SidebarActionableItem, action: Actions): void; + getItemActions(this: void, item: SidebarTreeItem): NavigationItemActions; __TEST_OVER_SCAN_COUNT?: number; }; @@ -99,7 +114,9 @@ export function VirtualTree({ getItemKey: _getItemKey, renderItem: _renderItem, onDefaultAction, - onExpandedChange, + onItemExpand, + onItemAction, + getItemActions, __TEST_OVER_SCAN_COUNT, }: VirtualTreeProps) { const listRef = useRef(null); @@ -122,7 +139,7 @@ export function VirtualTree({ useVirtualNavigationTree({ items, activeItemId, - onExpandedChange, + onExpandedChange: onItemExpand, onFocusMove, }); @@ -136,6 +153,9 @@ export function VirtualTree({ activeItemId, renderItem, onDefaultAction, + onItemAction, + onItemExpand, + getItemActions, }; }, [ items, @@ -144,6 +164,9 @@ export function VirtualTree({ onDefaultAction, activeItemId, isTreeItemFocused, + onItemAction, + getItemActions, + onItemExpand, ]); const getItemKey = useCallback( @@ -187,6 +210,13 @@ type VirtualItemData = { activeItemId?: string; renderItem: RenderItem; onDefaultAction: OnDefaultAction>; + onItemAction(this: void, item: SidebarActionableItem, action: Actions): void; + onItemExpand( + this: void, + item: SidebarActionableItem, + isExpanded: boolean + ): void; + getItemActions(this: void, item: SidebarTreeItem): NavigationItemActions; }; function TreeItem({ index, @@ -206,6 +236,9 @@ function TreeItem({ data.isTreeItemFocused && !isPlaceholderItem(item) && item.id === data.currentTabbable, + onItemAction: data.onItemAction, + onItemExpand: data.onItemExpand, + getItemActions: data.getItemActions, }); }, [ renderItem, @@ -214,6 +247,9 @@ function TreeItem({ activeItemId, data.currentTabbable, data.isTreeItemFocused, + data.onItemAction, + data.getItemActions, + data.onItemExpand, ]); const actionProps = useDefaultAction(