Skip to content

Commit 6883749

Browse files
ManoloManolo
Manolo
authored and
Manolo
committed
add select rows
1 parent 97dfb3a commit 6883749

File tree

2 files changed

+194
-1
lines changed

2 files changed

+194
-1
lines changed

src/modules/users/detail/user-detail.pod.tsx

+163-1
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,173 @@
11
import React from 'react';
2+
import {
3+
Pagination,
4+
Paper,
5+
Table,
6+
TableBody,
7+
TableCell,
8+
TableContainer,
9+
TableHead,
10+
TableRow,
11+
Checkbox,
12+
Button,
13+
} from '@mui/material';
14+
import { ColumnDef, getCoreRowModel, RowSelectionState, useReactTable } from '@tanstack/react-table';
215
import { Link } from '@tanstack/react-router';
316
import * as classes from './user-detail.styles';
417

18+
interface Data {
19+
id: number;
20+
name: string;
21+
age: number;
22+
city: string;
23+
exported: boolean;
24+
unavailable?: boolean;
25+
}
26+
27+
const data: Data[] = [
28+
{ id: 1, name: 'Alice', age: 25, city: 'New York', exported: false, unavailable: false },
29+
{ id: 2, name: 'Bob', age: 30, city: 'San Francisco', exported: false, unavailable: false },
30+
{ id: 3, name: 'Charlie', age: 35, city: 'Chicago', exported: true, unavailable: false },
31+
{ id: 4, name: 'Diana', age: 28, city: 'Los Angeles', exported: true, unavailable: false },
32+
{ id: 5, name: 'Eve', age: 22, city: 'Miami', exported: false, unavailable: false },
33+
{ id: 6, name: 'Frank', age: 27, city: 'Seattle', exported: false, unavailable: false },
34+
{ id: 7, name: 'Grace', age: 32, city: 'Boston', exported: false, unavailable: true },
35+
{ id: 8, name: 'Hank', age: 29, city: 'Denver', exported: false, unavailable: false },
36+
{ id: 9, name: 'Ivy', age: 24, city: 'Portland', exported: false, unavailable: true },
37+
{ id: 10, name: 'Jack', age: 31, city: 'Austin', exported: false, unavailable: false },
38+
{ id: 11, name: 'Kate', age: 26, city: 'Houston', exported: false, unavailable: false },
39+
{ id: 12, name: 'Lance', age: 33, city: 'Atlanta', exported: false, unavailable: false },
40+
];
41+
42+
const columns: ColumnDef<Data>[] = [
43+
{
44+
accessorKey: 'id',
45+
header: 'ID',
46+
},
47+
{
48+
accessorKey: 'name',
49+
header: 'Name',
50+
},
51+
{
52+
accessorKey: 'age',
53+
header: 'Age',
54+
},
55+
{
56+
accessorKey: 'city',
57+
header: 'City',
58+
},
59+
];
60+
561
export const UserDetailPod: React.FC = () => {
62+
const [pagination, setPagination] = React.useState({
63+
pageIndex: 0,
64+
pageSize: 10,
65+
});
66+
67+
const [paginatedData, setPaginatedData] = React.useState<Data[]>([]);
68+
const [rowSelection, setRowSelection] = React.useState<RowSelectionState>({});
69+
70+
React.useEffect(() => {
71+
const start = pagination.pageIndex * pagination.pageSize;
72+
const end = start + pagination.pageSize;
73+
setPaginatedData(data.slice(start, end));
74+
}, [pagination]);
75+
76+
React.useEffect(() => {
77+
const initialSelection: RowSelectionState = {};
78+
data.forEach(user => {
79+
if (user.exported) {
80+
initialSelection[user.id] = true;
81+
}
82+
});
83+
setRowSelection(initialSelection);
84+
}, []);
85+
86+
const tableInstance = useReactTable({
87+
data: paginatedData,
88+
columns,
89+
getCoreRowModel: getCoreRowModel(),
90+
manualPagination: true,
91+
rowCount: data.length,
92+
onPaginationChange: setPagination,
93+
onRowSelectionChange: setRowSelection,
94+
enableRowSelection: row => row.original.unavailable !== true,
95+
getRowId: row => row.id.toString(),
96+
state: {
97+
pagination,
98+
rowSelection,
99+
},
100+
});
101+
102+
const {
103+
getAllColumns,
104+
getRowModel,
105+
getPageCount,
106+
getState,
107+
getIsSomePageRowsSelected,
108+
getIsAllRowsSelected,
109+
getToggleAllRowsSelectedHandler,
110+
} = tableInstance;
111+
112+
const handleChangePage = (_: React.ChangeEvent<unknown>, newPage: number) => {
113+
setPagination(prev => ({
114+
...prev,
115+
pageIndex: newPage - 1,
116+
}));
117+
};
118+
6119
return (
7120
<div className={classes.root}>
8-
<h1>Soy la página de detalle de usuario</h1>
121+
<div className={classes.title}>
122+
<h1>Soy la página de detalle de usuario</h1>
123+
<Button variant="contained" color="primary" onClick={() => console.log(getState().rowSelection)}>
124+
Select users
125+
</Button>
126+
</div>
127+
<TableContainer component={Paper}>
128+
<Table className={classes.table}>
129+
<TableHead>
130+
<TableRow>
131+
<TableCell className={classes.head}>
132+
<Checkbox
133+
color="error"
134+
checked={getIsAllRowsSelected()}
135+
indeterminate={getIsSomePageRowsSelected()}
136+
onChange={getToggleAllRowsSelectedHandler()}
137+
/>
138+
</TableCell>
139+
{getAllColumns().map(column => (
140+
<TableCell key={column.id} className={classes.head}>
141+
{column.id}
142+
</TableCell>
143+
))}
144+
</TableRow>
145+
</TableHead>
146+
<TableBody>
147+
{getRowModel().rows.map(row => (
148+
<TableRow key={row.original.id} className={classes.row}>
149+
<TableCell>
150+
<Checkbox
151+
checked={row.getIsSelected() ?? false}
152+
onChange={row.getToggleSelectedHandler()}
153+
disabled={row.original.unavailable}
154+
/>
155+
</TableCell>
156+
<TableCell>{row.original.id}</TableCell>
157+
<TableCell>{row.original.name}</TableCell>
158+
<TableCell>{row.original.age}</TableCell>
159+
<TableCell>{row.original.city}</TableCell>
160+
</TableRow>
161+
))}
162+
</TableBody>
163+
</Table>
164+
<Pagination
165+
count={getPageCount()}
166+
page={getState().pagination.pageIndex + 1}
167+
onChange={handleChangePage}
168+
className={classes.pagination}
169+
/>
170+
</TableContainer>
9171
<Link to="/users">Navegar a listado de usuarios</Link>
10172
</div>
11173
);
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,39 @@
11
import { css } from '@emotion/css';
2+
import { theme } from '#core/theme/theme.ts';
23

34
export const root = css`
45
display: flex;
56
flex-direction: column;
67
gap: 30px;
78
align-items: center;
89
`;
10+
11+
export const title = css`
12+
display: flex;
13+
gap: 10px;
14+
height: 50px;
15+
align-items: center;
16+
justify-content: space-around;
17+
width: 100%;
18+
`;
19+
20+
export const table = css`
21+
min-width: 1200px;
22+
`;
23+
24+
export const head = css`
25+
background-color: ${theme.palette.primary.dark};
26+
color: ${theme.palette.primary.contrastText};
27+
`;
28+
29+
export const row = css`
30+
&:nth-of-type(odd) {
31+
background-color: ${theme.palette.grey[100]};
32+
}
33+
`;
34+
35+
export const pagination = css`
36+
display: flex;
37+
justify-content: center;
38+
padding: 10px;
39+
`;

0 commit comments

Comments
 (0)