From 1022f4fb052bf65a2054d0ccc6ee2b150fa216be Mon Sep 17 00:00:00 2001 From: Giovanni Gonzaga Date: Thu, 21 Sep 2023 16:53:01 +0200 Subject: [PATCH] add default row height an d simplify config --- packages/bento-design-system/src/Table/Table.tsx | 13 ++++++++++--- .../stories/Components/Table.stories.tsx | 2 +- 2 files changed, 11 insertions(+), 4 deletions(-) diff --git a/packages/bento-design-system/src/Table/Table.tsx b/packages/bento-design-system/src/Table/Table.tsx index 41c0f0792..1c12675af 100644 --- a/packages/bento-design-system/src/Table/Table.tsx +++ b/packages/bento-design-system/src/Table/Table.tsx @@ -87,7 +87,7 @@ type Props>> = { onRowPress?: (row: Row>) => void; } & ( | { groupBy?: C[number]["accessor"]; virtualizeRows?: never } - | { groupBy?: never; virtualizeRows?: { estimateRowHeight: (index: number) => number } } + | { groupBy?: never; virtualizeRows?: boolean | { estimateRowHeight: (index: number) => number } } ) & SortingProps; @@ -123,7 +123,7 @@ export function Table>>({ stickyHeaders, height, onRowPress, - virtualizeRows, + virtualizeRows: virtualizeRowsConfig, }: Props) { const config = useBentoConfig().table; const customOrderByFn = useMemo( @@ -243,10 +243,17 @@ export function Table>>({ const tableContainerRef = useRef(null); + const virtualizeRows = + typeof virtualizeRowsConfig === "boolean" ? virtualizeRowsConfig : virtualizeRowsConfig != null; + const estimateSize = + typeof virtualizeRowsConfig === "boolean" + ? () => 52 + : virtualizeRowsConfig?.estimateRowHeight ?? (() => 0); + const rowVirtualizer = useVirtualizer({ count: rows.length, getScrollElement: () => tableContainerRef.current, - estimateSize: virtualizeRows?.estimateRowHeight ?? (() => 0), + estimateSize, }); const virtualRows = rowVirtualizer.getVirtualItems(); diff --git a/packages/bento-design-system/stories/Components/Table.stories.tsx b/packages/bento-design-system/stories/Components/Table.stories.tsx index 511774acb..8a656a6c7 100644 --- a/packages/bento-design-system/stories/Components/Table.stories.tsx +++ b/packages/bento-design-system/stories/Components/Table.stories.tsx @@ -487,7 +487,7 @@ export const VirtualizedRows = { args: { stickyHeaders: true, height: { custom: 340 }, - virtualizeRows: { estimateRowHeight: () => 92 }, + virtualizeRows: true, data: repeatToLength(exampleData, 1_000), }, } satisfies Story;