Skip to content

Commit cf895fd

Browse files
ManoloManolo
Manolo
authored and
Manolo
committed
Implement manual pagination for user list and update styles
1 parent b419811 commit cf895fd

File tree

2 files changed

+45
-21
lines changed

2 files changed

+45
-21
lines changed

src/modules/users/list/users.pod.tsx

Lines changed: 35 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,7 @@
11
import React from 'react';
22
import { Link } from '@tanstack/react-router';
33
import { ColumnDef, useReactTable, getCoreRowModel } from '@tanstack/react-table';
4-
import {
5-
Table,
6-
TableBody,
7-
TableCell,
8-
TableContainer,
9-
TableHead,
10-
TableRow,
11-
TablePagination,
12-
Paper,
13-
} from '@mui/material';
4+
import { Table, TableBody, TableCell, TableContainer, TableHead, TableRow, Paper, Pagination } from '@mui/material';
145
import * as classes from './users.styles';
156

167
interface Data {
@@ -55,19 +46,45 @@ const columns: ColumnDef<Data>[] = [
5546
];
5647

5748
export const UsersPod: React.FC = () => {
49+
const [pagination, setPagination] = React.useState({
50+
pageIndex: 0,
51+
pageSize: 10,
52+
});
53+
54+
const [paginatedData, setPaginatedData] = React.useState<Data[]>([]);
55+
56+
React.useEffect(() => {
57+
const start = pagination.pageIndex * pagination.pageSize;
58+
const end = start + pagination.pageSize;
59+
setPaginatedData(data.slice(start, end));
60+
}, [pagination]);
61+
5862
const tableInstance = useReactTable({
59-
data,
63+
data: paginatedData,
6064
columns,
6165
getCoreRowModel: getCoreRowModel(),
66+
manualPagination: true,
67+
rowCount: data.length,
68+
onPaginationChange: setPagination,
69+
state: {
70+
pagination,
71+
},
6272
});
6373

64-
const { getAllColumns, getRowModel } = tableInstance;
74+
const { getAllColumns, getRowModel, getPageCount, getState } = tableInstance;
75+
76+
const handleChangePage = (_: React.ChangeEvent<unknown>, newPage: number) => {
77+
setPagination(prev => ({
78+
...prev,
79+
pageIndex: newPage - 1,
80+
}));
81+
};
6582

6683
return (
6784
<div className={classes.root}>
6885
<h1>Soy la página de listado de usuarios</h1>
6986
<TableContainer component={Paper}>
70-
<Table sx={{ minWidth: 1200 }}>
87+
<Table className={classes.table}>
7188
<TableHead>
7289
<TableRow>
7390
{getAllColumns().map(column => (
@@ -88,14 +105,11 @@ export const UsersPod: React.FC = () => {
88105
))}
89106
</TableBody>
90107
</Table>
91-
<TablePagination
92-
rowsPerPageOptions={[5, 10, 25]}
93-
component="div"
94-
count={data.length}
95-
rowsPerPage={5}
96-
page={0}
97-
onPageChange={() => {}}
98-
onRowsPerPageChange={() => {}}
108+
<Pagination
109+
count={getPageCount()}
110+
page={getState().pagination.pageIndex + 1}
111+
onChange={handleChangePage}
112+
className={classes.pagination}
99113
/>
100114
</TableContainer>
101115
<Link to="/users/$id" params={{ id: '1' }}>

src/modules/users/list/users.styles.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,10 @@ export const root = css`
88
align-items: center;
99
`;
1010

11+
export const table = css`
12+
min-width: 1200px;
13+
`;
14+
1115
export const head = css`
1216
background-color: ${theme.palette.common.black};
1317
color: ${theme.palette.common.white};
@@ -18,3 +22,9 @@ export const row = css`
1822
background-color: ${theme.palette.action.hover};
1923
}
2024
`;
25+
26+
export const pagination = css`
27+
display: flex;
28+
justify-content: center;
29+
padding: 10px;
30+
`;

0 commit comments

Comments
 (0)