|
1 | 1 | 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'; |
2 | 15 | import { Link } from '@tanstack/react-router';
|
3 | 16 | import * as classes from './user-detail.styles';
|
4 | 17 |
|
| 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 | + |
5 | 61 | 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 | + |
6 | 119 | return (
|
7 | 120 | <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> |
9 | 171 | <Link to="/users">Navegar a listado de usuarios</Link>
|
10 | 172 | </div>
|
11 | 173 | );
|
|
0 commit comments