Compare commits

...

1 Commits

Author SHA1 Message Date
Aleka Cheung
b0946688b1
add more controlled pagination to table 2024-02-05 18:48:09 -05:00
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'; selectionBehavior?: 'replace' | 'toggle';
onRowAction?: (key: TableRowKey, row: TableRowData) => void; onRowAction?: (key: TableRowKey, row: TableRowData) => void;
slotEmpty?: React.ReactNode; slotEmpty?: React.ReactNode;
initialNumRowsToShow?: number; viewMoreConfig?: {
initialNumRowsToShow: number;
numRowsPerPage?: number;
};
// collection: TableCollection<string>; // collection: TableCollection<string>;
// children: React.ReactNode; // children: React.ReactNode;
}; };
@ -125,7 +128,7 @@ export const Table = <TableRowData extends object, TableRowKey extends Key>({
selectionMode = 'single', selectionMode = 'single',
selectionBehavior = 'toggle', selectionBehavior = 'toggle',
slotEmpty, slotEmpty,
initialNumRowsToShow, viewMoreConfig,
// shouldRowRender, // shouldRowRender,
// collection, // collection,
@ -141,8 +144,18 @@ export const Table = <TableRowData extends object, TableRowKey extends Key>({
style, style,
}: ElementProps<TableRowData, TableRowKey> & StyleProps) => { }: ElementProps<TableRowData, TableRowKey> & StyleProps) => {
const [selectedKeys, setSelectedKeys] = useState(new Set<TableRowKey>()); const [selectedKeys, setSelectedKeys] = useState(new Set<TableRowKey>());
const [numRowsToShow, setNumRowsToShow] = useState(initialNumRowsToShow); const [numRowsToShow, setNumRowsToShow] = useState(viewMoreConfig?.initialNumRowsToShow);
const enableViewMore = numRowsToShow !== undefined; 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 currentBreakpoints = useBreakpoints();
const shownColumns = columns.filter( const shownColumns = columns.filter(
@ -218,9 +231,7 @@ export const Table = <TableRowData extends object, TableRowKey extends Key>({
} }
numColumns={shownColumns.length} numColumns={shownColumns.length}
onViewMoreClick={ onViewMoreClick={
enableViewMore && numRowsToShow < data.length enableViewMore && numRowsToShow! < data.length ? onViewMoreClick : undefined
? () => setNumRowsToShow(data.length)
: undefined
} }
// shouldRowRender={shouldRowRender} // shouldRowRender={shouldRowRender}
hideHeader={hideHeader} hideHeader={hideHeader}

View File

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