diff --git a/apps/docs/src/usages/table.tsx b/apps/docs/src/usages/table.tsx index 68cc5e0..879f862 100644 --- a/apps/docs/src/usages/table.tsx +++ b/apps/docs/src/usages/table.tsx @@ -1,7 +1,6 @@ import { Cell, Column, - Pagination, Row, Table, TableBody, @@ -65,13 +64,7 @@ export default () => { sortDescriptor={list.sortDescriptor} aria-label="Example static collection table" style={{ height: '210px', maxWidth: '400px' }} - paginator={ - - } + paginator={{ pageSizes: [5, 10, 20] }} > diff --git a/packages/actify/src/components/Table/Table.tsx b/packages/actify/src/components/Table/Table.tsx index 5dbd00a..2fa5cb3 100644 --- a/packages/actify/src/components/Table/Table.tsx +++ b/packages/actify/src/components/Table/Table.tsx @@ -3,6 +3,8 @@ import { AriaTableProps, useTable } from 'react-aria' import { SelectionBehavior, useTableState } from 'react-stately' +import { Pagination, type PaginationProps } from '../Pagination' +import React from 'react' import { TableCell } from './TableCell' import { TableCheckboxCell } from './TableCheckboxCell' import { TableColumnHeader } from './TableColumnHeader' @@ -12,11 +14,10 @@ import { TableRow } from './TableRow' import { TableRowGroup } from './TableRowGroup' import { TableSelectAllCell } from './TableSelectAllCell' import styles from './table.module.css' -import { useRef } from 'react' interface TableComponentProps extends AriaTableProps, TableProps { children: any - paginator?: React.ReactNode + paginator?: PaginationProps style?: React.CSSProperties selectionBehavior?: SelectionBehavior } @@ -28,10 +29,43 @@ const Table = (props: TableComponentProps) => { selectionMode === 'multiple' && selectionBehavior !== 'replace' }) - const ref = useRef(null) const { collection } = state + const ref = React.useRef(null) const { gridProps } = useTable(props, state, ref) + // paginator + const pageSizes = paginator?.pageSizes ?? [5, 10, 20] + const [currentPage, setCurrentPage] = React.useState(1) + const [selectedPageSize, setSelectedPageSize] = React.useState(pageSizes[0]) + + const onPageChange = (page: number) => { + setCurrentPage(page) + } + + React.useEffect(() => { + const totalPages = Math.ceil(collection.size / selectedPageSize) + if (totalPages < currentPage) { + setCurrentPage(1) + } + }, [currentPage, selectedPageSize]) + + const paginationProps = React.useMemo(() => { + const totalPages = Math.ceil(collection.size / selectedPageSize) + return { + pageSizes, + currentPage, + totalPages, + selectedPageSize, + setSelectedPageSize + } + }, [ + pageSizes, + currentPage, + collection, + selectedPageSize, + setSelectedPageSize + ]) + return ( <> @@ -57,20 +91,29 @@ const Table = (props: TableComponentProps) => { ))} - {[...collection.body.childNodes].map((row) => ( - - {[...row.childNodes].map((cell) => - cell.props.isSelectionCell ? ( - - ) : ( - - ) - )} - - ))} + {[...collection.body.childNodes] + .slice( + selectedPageSize * (currentPage - 1), + currentPage * selectedPageSize + ) + .map((row) => ( + + {[...row.childNodes].map((cell) => + cell.props.isSelectionCell ? ( + + ) : ( + + ) + )} + + ))}
- {paginator} + {paginator && } ) }