Debian Codegraph @

node/@patternfly/react-table/src/demos/examples/TableAutomaticPagination.tsx

raw
1 import { Fragment, useState } from 'react';
2 import { Pagination } from '@patternfly/react-core';
3 import { Table, Thead, Tr, Th, Tbody, Td } from '@patternfly/react-table';
4
5 export const TableAutomaticPagination: React.FunctionComponent = () => {
6 const columns = {
7 firstColumn: 'First column',
8 secondColumn: 'Second column',
9 thirdColumn: 'Third column'
10 };
11
12 const defaultRows = [
13 { firstColumn: 'Row 1 column 1', secondColumn: 'Row 1 column 2', thirdColumn: 'Row 1 column 3' },
14 { firstColumn: 'Row 2 column 1', secondColumn: 'Row 2 column 2', thirdColumn: 'Row 2 column 3' },
15 { firstColumn: 'Row 3 column 1', secondColumn: 'Row 3 column 2', thirdColumn: 'Row 3 column 3' },
16 { firstColumn: 'Row 4 column 1', secondColumn: 'Row 4 column 2', thirdColumn: 'Row 4 column 3' },
17 { firstColumn: 'Row 5 column 1', secondColumn: 'Row 5 column 2', thirdColumn: 'Row 5 column 3' },
18 { firstColumn: 'Row 6 column 1', secondColumn: 'Row 6 column 2', thirdColumn: 'Row 6 column 3' },
19 { firstColumn: 'Row 7 column 1', secondColumn: 'Row 7 column 2', thirdColumn: 'Row 7 column 3' },
20 { firstColumn: 'Row 8 column 1', secondColumn: 'Row 8 column 2', thirdColumn: 'Row 8 column 3' },
21 { firstColumn: 'Row 9 column 1', secondColumn: 'Row 9 column 2', thirdColumn: 'Row 9 column 3' },
22 { firstColumn: 'Row 10 column 1', secondColumn: 'Row 10 column 2', thirdColumn: 'Row 10 column 3' },
23 { firstColumn: 'Row 11 column 1', secondColumn: 'Row 11 column 2', thirdColumn: 'Row 11 column 3' },
24 { firstColumn: 'Row 12 column 1', secondColumn: 'Row 12 column 2', thirdColumn: 'Row 12 column 3' }
25 ];
26 const defaultPerPage = 10;
27
28 const [perPage, setPerPage] = useState(defaultPerPage);
29 const [page, setPage] = useState(1);
30 const [rows, setRows] = useState(defaultRows.slice(0, defaultPerPage));
31
32 const handleSetPage = (
33 _evt: React.MouseEvent | React.KeyboardEvent | MouseEvent,
34 newPage: number,
35 _perPage: number,
36 startIdx: number,
37 endIdx: number
38 ) => {
39 setPage(newPage);
40 setRows(defaultRows.slice(startIdx, endIdx));
41 };
42
43 const handlePerPageSelect = (
44 _evt: React.MouseEvent | React.KeyboardEvent | MouseEvent,
45 newPerPage: number,
46 newPage: number,
47 startIdx: number,
48 endIdx: number
49 ) => {
50 setPerPage(newPerPage);
51 setPage(newPage);
52 setRows(defaultRows.slice(startIdx, endIdx));
53 };
54
55 const renderPagination = (variant = 'top') => (
56 <Pagination
57 isCompact
58 itemCount={defaultRows.length}
59 page={page}
60 perPage={perPage}
61 isLastFullPageShown
62 onSetPage={handleSetPage}
63 onPerPageSelect={handlePerPageSelect}
64 perPageOptions={[
65 { title: '3', value: 3 },
66 { title: '5', value: 5 },
67 { title: '12', value: 12 },
68 { title: '20', value: 20 }
69 ]}
70 titles={{
71 paginationAriaLabel: `${variant} pagination`
72 }}
73 />
74 );
75
76 return (
77 <Fragment>
78 {renderPagination()}
79 <Table aria-label="Automated Pagination Table Demo">
80 <Thead>
81 <Tr>
82 <Th>{columns.firstColumn}</Th>
83 <Th>{columns.secondColumn}</Th>
84 <Th>{columns.thirdColumn}</Th>
85 </Tr>
86 </Thead>
87 <Tbody>
88 {rows.map((row, rowIndex) => (
89 <Tr key={rowIndex}>
90 <>
91 <Td dataLabel={columns.firstColumn}>{row.firstColumn}</Td>
92 <Td dataLabel={columns.secondColumn}>{row.secondColumn}</Td>
93 <Td dataLabel={columns.thirdColumn}>{row.thirdColumn}</Td>
94 </>
95 </Tr>
96 ))}
97 </Tbody>
98 </Table>
99 </Fragment>
100 );
101 };