From d1897bea59638555fd5a0af479c1db27e417423c Mon Sep 17 00:00:00 2001 From: Andres Pinto <143480783+apsantiso@users.noreply.github.com> Date: Fri, 13 Nov 2026 16:40:52 +0100 Subject: [PATCH 1/6] feat(list): add getItemId prop for custom item identity --- package.json | 2 +- src/components/data-display/list/List.tsx | 24 +++++--- src/components/data-display/list/ListItem.tsx | 2 +- .../data-display/list/__test__/List.test.tsx | 58 +++++++++++++++++++ 4 files changed, 76 insertions(+), 10 deletions(-) diff --git a/package.json b/package.json index 18c618d..5375550 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@internxt/ui", - "version": "0.1.29", + "version": "0.1.30", "description": "Library of Internxt components", "repository": { "type": "git", diff --git a/src/components/data-display/list/List.tsx b/src/components/data-display/list/List.tsx index 7273645..1e15224 100644 --- a/src/components/data-display/list/List.tsx +++ b/src/components/data-display/list/List.tsx @@ -33,6 +33,7 @@ export interface ListProps { onMouseEnter?: () => void; onMouseLeave?: () => void; headerBackgroundColor?: string; + getItemId?: (item: T) => string | number; keyBoardShortcutActions?: { onShiftFKeysPressed?: () => void; onRKeyPressed?: () => void; @@ -121,6 +122,10 @@ export interface ListProps { * @property {string} [headerBackgroundColor] * - Optional background color for the header. * + * @property {(item: T) => string | number} [getItemId] + * - Optional function returning the unique identity of an item (used for selection, context menu and React keys). + * Defaults to `item.id`. + * * @property { * { onShiftFKeysPressed?: () => void; onRKeyPressed?: () => void; onBackspaceKeyPressed?: () => void } * } [keyBoardShortcutActions] @@ -130,7 +135,7 @@ export interface ListProps { * - The rendered list component. */ -const List = ({ +const List = ({ header, checkboxDataCy, items, @@ -157,15 +162,18 @@ const List = ({ headerBackgroundColor = 'bg-surface', keyBoardShortcutActions, disableKeyboardShortcuts, + getItemId, }: ListProps): JSX.Element => { const containerRef = useRef(null); + const getKey = getItemId ?? ((item: T) => item.id as string | number); const isItemSelected = (item: T) => { - return selectedItems.some((i) => item.id === i.id); + const key = getKey(item); + return selectedItems.some((i) => getKey(i) === key); }; const container = document.getElementById('scrollableList'); const isVerticalScrollbarVisible = container && container.scrollHeight > container.clientHeight; const isEmptyState = !hasMoreItems && items.length === 0 && !isLoading; - const [idItemContextMenuOpen, setIdItemContextMenuOpen] = useState(null); + const [idItemContextMenuOpen, setIdItemContextMenuOpen] = useState(null); const skeletonData = new Array(25).fill(0).map(() => ({ skeletonItem: skinSkeleton, @@ -213,7 +221,7 @@ const List = ({ }; const selectAllItems = () => { - const notSelectedItems = items.filter((item) => !selectedItems.some((s) => s.id === item.id)); + const notSelectedItems = items.filter((item) => !selectedItems.some((s) => getKey(s) === getKey(item))); const changesToMake = notSelectedItems.map((item) => ({ props: item, value: true })); onSelectedItemsChanged(changesToMake); }; @@ -278,7 +286,7 @@ const List = ({ if (!isItemSelected(props)) { unselectAllItemsAndSelectOne(props); } - setIdItemContextMenuOpen(props.id); + setIdItemContextMenuOpen(getKey(props)); }; const onCloseContextMenu = () => { @@ -288,7 +296,7 @@ const List = ({ const handleThreeDotsButtonClick = (e: React.MouseEvent, item: T) => { e.stopPropagation(); - setIdItemContextMenuOpen((prevId) => (prevId ? null : item.id)); + setIdItemContextMenuOpen((prevId) => (prevId !== null ? null : getKey(item))); if (!isItemSelected(item)) { unselectAllItemsAndSelectOne(item); } @@ -330,10 +338,10 @@ const List = ({ > {items.map((item, index) => ( - key={item.id} + key={getKey(item)} item={item} listIndex={index} - isOpen={idItemContextMenuOpen === item.id} + isOpen={idItemContextMenuOpen === getKey(item)} onClose={onCloseContextMenu} itemComposition={itemComposition} selected={isItemSelected(item)} diff --git a/src/components/data-display/list/ListItem.tsx b/src/components/data-display/list/ListItem.tsx index 17257bc..27f3291 100644 --- a/src/components/data-display/list/ListItem.tsx +++ b/src/components/data-display/list/ListItem.tsx @@ -26,7 +26,7 @@ interface ListItemProps { } const TOP_MIN_HEIGHT = 500; -const ListItem = ({ +const ListItem = ({ item, listIndex, itemComposition, diff --git a/src/components/data-display/list/__test__/List.test.tsx b/src/components/data-display/list/__test__/List.test.tsx index f67a73d..1461075 100644 --- a/src/components/data-display/list/__test__/List.test.tsx +++ b/src/components/data-display/list/__test__/List.test.tsx @@ -348,3 +348,61 @@ describe('List component', () => { expect(mockOnEnterPressed).toHaveBeenCalledWith(items[0]); }); }); + +describe('List component with getItemId', () => { + type UuidItem = { id?: number; uuid: string; name: string; age: number }; + + const uuidHeader: HeaderProps[] = [ + { label: 'Name', width: '100px', name: 'name', orderable: true, defaultDirection: 'ASC' }, + ]; + const uuidItems: UuidItem[] = [ + { id: 0, uuid: 'uuid-1', name: 'File 1', age: 25 }, + { id: 0, uuid: 'uuid-2', name: 'File 2', age: 30 }, + { uuid: 'uuid-3', name: 'File 3', age: 35 }, + ]; + + const renderUuidList = (selected: UuidItem[]) => + render( + + header={uuidHeader} + items={uuidItems} + selectedItems={selected} + itemComposition={[(props) =>
{props.name}
]} + menu={menu} + onSelectedItemsChanged={mockOnSelectedItemsChanged} + getItemId={(item) => item.uuid} + />, + ); + + afterEach(() => { + vi.clearAllMocks(); + }); + + it('should select all not selected items by uuid when ids collide or are missing', () => { + const { container } = renderUuidList([uuidItems[0]]); + + fireEvent.keyDown(container, { key: 'a', ctrlKey: true }); + + expect(mockOnSelectedItemsChanged).toHaveBeenCalledWith([ + { props: uuidItems[1], value: true }, + { props: uuidItems[2], value: true }, + ]); + }); + + it('should toggle selection by uuid when Ctrl key is pressed', () => { + const { getByText } = renderUuidList([uuidItems[0]]); + + fireEvent.click(getByText('File 2'), { ctrlKey: true }); + + expect(mockOnSelectedItemsChanged).toHaveBeenCalledWith([{ props: uuidItems[1], value: true }]); + }); + + it('should open context menu only for the right clicked item', () => { + const { getByText, getAllByText } = renderUuidList([uuidItems[1]]); + + fireEvent.contextMenu(getByText('File 2')); + + expect(mockOnSelectedItemsChanged).not.toHaveBeenCalled(); + expect(getAllByText('Option 1')).toHaveLength(1); + }); +}); From 0c764e4a2ab19a4c92ae9239e5d31b37021c8220 Mon Sep 17 00:00:00 2001 From: Andres Pinto <143480783+apsantiso@users.noreply.github.com> Date: Mon, 16 Nov 2026 17:22:02 +0100 Subject: [PATCH 2/6] fix(list): require getItemId when item id is not guaranteed --- src/components/data-display/list/List.tsx | 21 ++++++++++++++------- 1 file changed, 14 insertions(+), 7 deletions(-) diff --git a/src/components/data-display/list/List.tsx b/src/components/data-display/list/List.tsx index 1e15224..a8b51bf 100644 --- a/src/components/data-display/list/List.tsx +++ b/src/components/data-display/list/List.tsx @@ -6,7 +6,13 @@ import { SkeletonLoader } from '../../feedback/skeletonLoader'; import { InfiniteScroll } from '../../layout/infiniteScroll'; import { MenuItemsType } from '../../navigation/menu/Menu'; -export interface ListProps { +type ItemId = string | number; +type GetItemId = (item: T) => ItemId; + +// getItemId is optional only when T guarantees an id. [T] avoids TS narrowing T inside the component +type GetItemIdProp = [T] extends [{ id: ItemId }] ? { getItemId?: GetItemId } : { getItemId: GetItemId }; + +interface BaseListProps { header: HeaderProps[]; checkboxDataCy?: string; items: T[]; @@ -33,7 +39,6 @@ export interface ListProps { onMouseEnter?: () => void; onMouseLeave?: () => void; headerBackgroundColor?: string; - getItemId?: (item: T) => string | number; keyBoardShortcutActions?: { onShiftFKeysPressed?: () => void; onRKeyPressed?: () => void; @@ -41,6 +46,8 @@ export interface ListProps { }; } +export type ListProps = BaseListProps & GetItemIdProp; + /** * List component * @@ -123,8 +130,8 @@ export interface ListProps { * - Optional background color for the header. * * @property {(item: T) => string | number} [getItemId] - * - Optional function returning the unique identity of an item (used for selection, context menu and React keys). - * Defaults to `item.id`. + * - Function returning the unique identity of an item (used for selection, context menu and React keys). + * Optional when `T` has a required `id`, in which case it defaults to `item.id`. Required otherwise. * * @property { * { onShiftFKeysPressed?: () => void; onRKeyPressed?: () => void; onBackspaceKeyPressed?: () => void } @@ -135,7 +142,7 @@ export interface ListProps { * - The rendered list component. */ -const List = ({ +const List = ({ header, checkboxDataCy, items, @@ -165,7 +172,7 @@ const List = ({ getItemId, }: ListProps): JSX.Element => { const containerRef = useRef(null); - const getKey = getItemId ?? ((item: T) => item.id as string | number); + const getKey: GetItemId = getItemId ?? ((item: T) => item.id as ItemId); const isItemSelected = (item: T) => { const key = getKey(item); return selectedItems.some((i) => getKey(i) === key); @@ -173,7 +180,7 @@ const List = ({ const container = document.getElementById('scrollableList'); const isVerticalScrollbarVisible = container && container.scrollHeight > container.clientHeight; const isEmptyState = !hasMoreItems && items.length === 0 && !isLoading; - const [idItemContextMenuOpen, setIdItemContextMenuOpen] = useState(null); + const [idItemContextMenuOpen, setIdItemContextMenuOpen] = useState(null); const skeletonData = new Array(25).fill(0).map(() => ({ skeletonItem: skinSkeleton, From 4db6e86ba38653ad518a556b96739eea0947cbb3 Mon Sep 17 00:00:00 2001 From: Andres Pinto <143480783+apsantiso@users.noreply.github.com> Date: Mon, 16 Nov 2026 17:22:02 +0100 Subject: [PATCH 3/6] test(list): use items without id in getItemId tests --- src/components/data-display/list/__test__/List.test.tsx | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/components/data-display/list/__test__/List.test.tsx b/src/components/data-display/list/__test__/List.test.tsx index 1461075..1a7d17f 100644 --- a/src/components/data-display/list/__test__/List.test.tsx +++ b/src/components/data-display/list/__test__/List.test.tsx @@ -356,8 +356,8 @@ describe('List component with getItemId', () => { { label: 'Name', width: '100px', name: 'name', orderable: true, defaultDirection: 'ASC' }, ]; const uuidItems: UuidItem[] = [ - { id: 0, uuid: 'uuid-1', name: 'File 1', age: 25 }, - { id: 0, uuid: 'uuid-2', name: 'File 2', age: 30 }, + { uuid: 'uuid-1', name: 'File 1', age: 25 }, + { uuid: 'uuid-2', name: 'File 2', age: 30 }, { uuid: 'uuid-3', name: 'File 3', age: 35 }, ]; @@ -378,7 +378,7 @@ describe('List component with getItemId', () => { vi.clearAllMocks(); }); - it('should select all not selected items by uuid when ids collide or are missing', () => { + it('should select all not selected items by uuid when ids are missing', () => { const { container } = renderUuidList([uuidItems[0]]); fireEvent.keyDown(container, { key: 'a', ctrlKey: true }); From 6e767218b8aa34a090bc5d3ea88a01d341096269 Mon Sep 17 00:00:00 2001 From: Andres Pinto <143480783+apsantiso@users.noreply.github.com> Date: Mon, 16 Nov 2026 17:43:39 +0100 Subject: [PATCH 4/6] fix(list): allow item types without id --- src/components/data-display/list/List.tsx | 8 ++++---- src/components/data-display/list/ListItem.tsx | 2 +- src/components/data-display/list/__test__/List.test.tsx | 2 +- 3 files changed, 6 insertions(+), 6 deletions(-) diff --git a/src/components/data-display/list/List.tsx b/src/components/data-display/list/List.tsx index a8b51bf..eda0df5 100644 --- a/src/components/data-display/list/List.tsx +++ b/src/components/data-display/list/List.tsx @@ -9,7 +9,7 @@ import { MenuItemsType } from '../../navigation/menu/Menu'; type ItemId = string | number; type GetItemId = (item: T) => ItemId; -// getItemId is optional only when T guarantees an id. [T] avoids TS narrowing T inside the component +// getItemId is optional only when T guarantees an id. [T] prevents distribution over unions, so every member must have id type GetItemIdProp = [T] extends [{ id: ItemId }] ? { getItemId?: GetItemId } : { getItemId: GetItemId }; interface BaseListProps { @@ -142,7 +142,7 @@ export type ListProps = BaseListProps & GetItemIdProp; * - The rendered list component. */ -const List = ({ +const List = ({ header, checkboxDataCy, items, @@ -172,7 +172,7 @@ const List = ({ getItemId, }: ListProps): JSX.Element => { const containerRef = useRef(null); - const getKey: GetItemId = getItemId ?? ((item: T) => item.id as ItemId); + const getKey: GetItemId = getItemId ?? ((item: T) => (item as { id: ItemId }).id); const isItemSelected = (item: T) => { const key = getKey(item); return selectedItems.some((i) => getKey(i) === key); @@ -303,7 +303,7 @@ const List = ({ const handleThreeDotsButtonClick = (e: React.MouseEvent, item: T) => { e.stopPropagation(); - setIdItemContextMenuOpen((prevId) => (prevId !== null ? null : getKey(item))); + setIdItemContextMenuOpen((prevId) => (prevId ? null : getKey(item))); if (!isItemSelected(item)) { unselectAllItemsAndSelectOne(item); } diff --git a/src/components/data-display/list/ListItem.tsx b/src/components/data-display/list/ListItem.tsx index 27f3291..9f32470 100644 --- a/src/components/data-display/list/ListItem.tsx +++ b/src/components/data-display/list/ListItem.tsx @@ -26,7 +26,7 @@ interface ListItemProps { } const TOP_MIN_HEIGHT = 500; -const ListItem = ({ +const ListItem = ({ item, listIndex, itemComposition, diff --git a/src/components/data-display/list/__test__/List.test.tsx b/src/components/data-display/list/__test__/List.test.tsx index 1a7d17f..46225bd 100644 --- a/src/components/data-display/list/__test__/List.test.tsx +++ b/src/components/data-display/list/__test__/List.test.tsx @@ -350,7 +350,7 @@ describe('List component', () => { }); describe('List component with getItemId', () => { - type UuidItem = { id?: number; uuid: string; name: string; age: number }; + type UuidItem = { uuid: string; name: string; age: number }; const uuidHeader: HeaderProps[] = [ { label: 'Name', width: '100px', name: 'name', orderable: true, defaultDirection: 'ASC' }, From 57759ca62a0688961125ed36526b758b3db88ca3 Mon Sep 17 00:00:00 2001 From: Andres Pinto <143480783+apsantiso@users.noreply.github.com> Date: Mon, 16 Nov 2026 17:43:44 +0100 Subject: [PATCH 5/6] test(list): add context menu toggle and default id tests --- .../data-display/list/__test__/List.test.tsx | 44 +++++++++++++++++++ 1 file changed, 44 insertions(+) diff --git a/src/components/data-display/list/__test__/List.test.tsx b/src/components/data-display/list/__test__/List.test.tsx index 46225bd..ca0f389 100644 --- a/src/components/data-display/list/__test__/List.test.tsx +++ b/src/components/data-display/list/__test__/List.test.tsx @@ -405,4 +405,48 @@ describe('List component with getItemId', () => { expect(mockOnSelectedItemsChanged).not.toHaveBeenCalled(); expect(getAllByText('Option 1')).toHaveLength(1); }); + + it('should toggle context menu of the item when three dots button is clicked twice', () => { + const { getAllByRole, getAllByText, queryByText } = renderUuidList([uuidItems[1]]); + const dotsButtons = getAllByRole('button').slice(-uuidItems.length); + + fireEvent.click(dotsButtons[1]); + expect(getAllByText('Option 1')).toHaveLength(1); + + fireEvent.click(dotsButtons[1]); + expect(queryByText('Option 1')).toBeNull(); + }); +}); + +describe('List component with default item id', () => { + type StringIdItem = { id: string; name: string }; + + const stringIdHeader: HeaderProps[] = [ + { label: 'Name', width: '100px', name: 'name', orderable: true, defaultDirection: 'ASC' }, + ]; + const stringIdItems: StringIdItem[] = [ + { id: 'id-1', name: 'File 1' }, + { id: 'id-2', name: 'File 2' }, + ]; + + afterEach(() => { + vi.clearAllMocks(); + }); + + it('should use item id as default identity when getItemId is not provided', () => { + const { container } = render( + + header={stringIdHeader} + items={stringIdItems} + selectedItems={[stringIdItems[0]]} + itemComposition={[(props) =>
{props.name}
]} + menu={menu} + onSelectedItemsChanged={mockOnSelectedItemsChanged} + />, + ); + + fireEvent.keyDown(container, { key: 'a', ctrlKey: true }); + + expect(mockOnSelectedItemsChanged).toHaveBeenCalledWith([{ props: stringIdItems[1], value: true }]); + }); }); From 31e1f4d1c6f225e2fc7ec48b74e0f531e5483e8e Mon Sep 17 00:00:00 2001 From: Andres Pinto <143480783+apsantiso@users.noreply.github.com> Date: Mon, 16 Nov 2026 17:43:44 +0100 Subject: [PATCH 6/6] refactor(list): replace short-circuit with if in ListItem click --- src/components/data-display/list/ListItem.tsx | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/components/data-display/list/ListItem.tsx b/src/components/data-display/list/ListItem.tsx index 9f32470..caae848 100644 --- a/src/components/data-display/list/ListItem.tsx +++ b/src/components/data-display/list/ListItem.tsx @@ -123,7 +123,9 @@ const ListItem = ({ const handleClick = (event: React.MouseEvent) => { onClick?.(event); - openedFromRightClick && handleMenuClose(); + if (openedFromRightClick) { + handleMenuClose(); + } }; const handleMenuClose = () => {