forked from Kuechlin/mantine-data-grid
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPagination.tsx
95 lines (87 loc) · 2.83 KB
/
Pagination.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
import { Box, Group, MantineNumberSize, Pagination as MantinePagination, Select, Text } from '@mantine/core';
import { Table } from '@tanstack/react-table';
import { DataGridLocale, PaginationMode } from './types';
export const DEFAULT_PAGE_SIZES = ['10', '25', '50', '100'];
export const DEFAULT_INITIAL_PAGE = 0;
export const DEFAULT_INITIAL_SIZE = 10;
export type PaginationProps<TData> = {
table: Table<TData>;
classes: string[];
pageSizes?: string[];
fontSize?: MantineNumberSize;
color: string;
total?: number;
locale?: DataGridLocale;
mode?: PaginationMode;
};
export function Pagination<TData>({
table,
classes,
fontSize = 'md',
pageSizes = DEFAULT_PAGE_SIZES,
color = '', // Empty color will follow the primary button color
total,
locale,
mode = 'default',
}: PaginationProps<TData>) {
const pageIndex = table.getState().pagination.pageIndex;
const pageSize = table.getState().pagination.pageSize;
const maxRows = total ? total : table.getPrePaginationRowModel().rows.length;
const currentRowAmount = table.getRowModel().rows.length;
const firstRowNum = maxRows === 0 ? 0 : pageIndex * pageSize + 1;
const lastRowNum = maxRows === 0 ? 0 : firstRowNum + currentRowAmount - 1;
const handlePageSizeChange = (value: string) => {
table.setPageSize(Number(value));
};
const handlePageChange = (pageNum: number) => {
table.setPageIndex(Number(pageNum) - 1);
};
return (
<Box className={classes[0]}>
{mode === 'default' ? (
<Text size={fontSize} className={classes[1]}>
{locale?.pagination ? (
locale.pagination(firstRowNum, lastRowNum, maxRows)
) : (
<>
Showing <b>{firstRowNum}</b> - <b>{lastRowNum}</b> of <b>{maxRows}</b> result{maxRows === 1 ? '' : 's'}
</>
)}
</Text>
) : null}
<Group>
<Box className={classes[2]}>
{mode === 'default' ? (
<>
<Text size={fontSize}>{locale?.pageSize || 'Rows per page:'}</Text>
<Select
data={pageSizes}
value={`${table.getState().pagination.pageSize}`}
onChange={handlePageSizeChange}
sx={() => ({
width: '72px',
})}
/>
</>
) : null}
</Box>
<MantinePagination
size={fontSize}
/** Read: https://github.com/mantinedev/mantine/discussions/2001 */
sx={() => ({
'> button': {
height: '36px',
minWidth: '36px',
},
})}
value={table.getState().pagination.pageIndex + 1}
total={table.getPageCount()}
onChange={handlePageChange}
className={classes[3]}
siblings={1}
color={color}
/>
</Group>
</Box>
);
}