add more controlled pagination to table (#276)

This commit is contained in:
aleka 2024-02-06 11:48:49 -05:00 committed by GitHub
parent 42595a3667
commit 44ed52d5e2
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 19 additions and 8 deletions

View File

@ -95,7 +95,10 @@ export type ElementProps<TableRowData extends object | CustomRowConfig, TableRow
selectionBehavior?: 'replace' | 'toggle';
onRowAction?: (key: TableRowKey, row: TableRowData) => void;
slotEmpty?: React.ReactNode;
initialNumRowsToShow?: number;
viewMoreConfig?: {
initialNumRowsToShow: number;
numRowsPerPage?: number;
};
// collection: TableCollection<string>;
// children: React.ReactNode;
};
@ -125,7 +128,7 @@ export const Table = <TableRowData extends object, TableRowKey extends Key>({
selectionMode = 'single',
selectionBehavior = 'toggle',
slotEmpty,
initialNumRowsToShow,
viewMoreConfig,
// shouldRowRender,
// collection,
@ -141,8 +144,18 @@ export const Table = <TableRowData extends object, TableRowKey extends Key>({
style,
}: ElementProps<TableRowData, TableRowKey> & StyleProps) => {
const [selectedKeys, setSelectedKeys] = useState(new Set<TableRowKey>());
const [numRowsToShow, setNumRowsToShow] = useState(initialNumRowsToShow);
const enableViewMore = numRowsToShow !== undefined;
const [numRowsToShow, setNumRowsToShow] = useState(viewMoreConfig?.initialNumRowsToShow);
const enableViewMore = viewMoreConfig !== undefined;
const onViewMoreClick = () => {
if (!viewMoreConfig) return;
const { numRowsPerPage } = viewMoreConfig;
if (numRowsPerPage) {
setNumRowsToShow((prev) => (prev ?? 0) + numRowsPerPage);
} else {
setNumRowsToShow(data.length);
}
};
const currentBreakpoints = useBreakpoints();
const shownColumns = columns.filter(
@ -218,9 +231,7 @@ export const Table = <TableRowData extends object, TableRowKey extends Key>({
}
numColumns={shownColumns.length}
onViewMoreClick={
enableViewMore && numRowsToShow < data.length
? () => setNumRowsToShow(data.length)
: undefined
enableViewMore && numRowsToShow! < data.length ? onViewMoreClick : undefined
}
// shouldRowRender={shouldRowRender}
hideHeader={hideHeader}

View File

@ -121,7 +121,7 @@ export const TradingRewardHistoryTable = ({
selectionBehavior="replace"
withOuterBorder={withOuterBorder}
withInnerBorders={withInnerBorders}
initialNumRowsToShow={5}
viewMoreConfig={{ initialNumRowsToShow: 5, numRowsPerPage: 10 }}
withScrollSnapColumns
withScrollSnapRows
/>