Skip to content

Commit

Permalink
add default row height an d simplify config
Browse files Browse the repository at this point in the history
  • Loading branch information
giogonzo committed Sep 21, 2023
1 parent 6a61e2e commit 1022f4f
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 4 deletions.
13 changes: 10 additions & 3 deletions packages/bento-design-system/src/Table/Table.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ type Props<C extends ReadonlyArray<ColumnType<string, {}, any>>> = {
onRowPress?: (row: Row<RowType<C>>) => void;
} & (
| { groupBy?: C[number]["accessor"]; virtualizeRows?: never }
| { groupBy?: never; virtualizeRows?: { estimateRowHeight: (index: number) => number } }
| { groupBy?: never; virtualizeRows?: boolean | { estimateRowHeight: (index: number) => number } }
) &
SortingProps<C>;

Expand Down Expand Up @@ -123,7 +123,7 @@ export function Table<C extends ReadonlyArray<ColumnType<string, {}, any>>>({
stickyHeaders,
height,
onRowPress,
virtualizeRows,
virtualizeRows: virtualizeRowsConfig,
}: Props<C>) {
const config = useBentoConfig().table;
const customOrderByFn = useMemo(
Expand Down Expand Up @@ -243,10 +243,17 @@ export function Table<C extends ReadonlyArray<ColumnType<string, {}, any>>>({

const tableContainerRef = useRef<HTMLDivElement>(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();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;

0 comments on commit 1022f4f

Please sign in to comment.