From 776670a36aadb37eb9f37c23e81e88957091544c 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 d1d5f9104..5823c51bf 100644 --- a/packages/bento-design-system/src/Table/Table.tsx +++ b/packages/bento-design-system/src/Table/Table.tsx @@ -110,7 +110,7 @@ type Props< : never; virtualizeRows?: never; } - | { groupBy?: never; virtualizeRows?: { estimateRowHeight: (index: number) => number } } + | { groupBy?: never; virtualizeRows?: boolean | { estimateRowHeight: (index: number) => number } } ) & SortingProps; @@ -150,7 +150,7 @@ export function Table< stickyHeaders, height, onRowPress, - virtualizeRows, + virtualizeRows: virtualizeRowsConfig, }: Props) { const config = useBentoConfig().table; const customOrderByFn = useMemo( @@ -302,10 +302,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 90f4d11df..7ac70ebda 100644 --- a/packages/bento-design-system/stories/Components/Table.stories.tsx +++ b/packages/bento-design-system/stories/Components/Table.stories.tsx @@ -573,7 +573,7 @@ export const VirtualizedRows = { args: { stickyHeaders: true, height: { custom: 340 }, - virtualizeRows: { estimateRowHeight: () => 92 }, + virtualizeRows: true, data: repeatToLength(exampleData, 1_000), }, } satisfies Story;