|
@@ -1,378 +0,0 @@
|
1
|
|
-import * as React from 'react';
|
2
|
|
-import PropTypes from 'prop-types';
|
3
|
|
-import { alpha } from '@mui/material/styles';
|
4
|
|
-import Box from '@mui/material/Box';
|
5
|
|
-import Table from '@mui/material/Table';
|
6
|
|
-import TableBody from '@mui/material/TableBody';
|
7
|
|
-import TableCell from '@mui/material/TableCell';
|
8
|
|
-import TableContainer from '@mui/material/TableContainer';
|
9
|
|
-import TableHead from '@mui/material/TableHead';
|
10
|
|
-import TablePagination from '@mui/material/TablePagination';
|
11
|
|
-import TableRow from '@mui/material/TableRow';
|
12
|
|
-import TableSortLabel from '@mui/material/TableSortLabel';
|
13
|
|
-import Toolbar from '@mui/material/Toolbar';
|
14
|
|
-import Typography from '@mui/material/Typography';
|
15
|
|
-import Paper from '@mui/material/Paper';
|
16
|
|
-import Checkbox from '@mui/material/Checkbox';
|
17
|
|
-import IconButton from '@mui/material/IconButton';
|
18
|
|
-import Tooltip from '@mui/material/Tooltip';
|
19
|
|
-import FormControlLabel from '@mui/material/FormControlLabel';
|
20
|
|
-import Switch from '@mui/material/Switch';
|
21
|
|
-import DeleteIcon from '@mui/icons-material/Delete';
|
22
|
|
-import FilterListIcon from '@mui/icons-material/FilterList';
|
23
|
|
-import { visuallyHidden } from '@mui/utils';
|
24
|
|
-
|
25
|
|
-function createData(name, calories, fat, carbs, protein) {
|
26
|
|
- return {
|
27
|
|
- name,
|
28
|
|
- calories,
|
29
|
|
- fat,
|
30
|
|
- carbs,
|
31
|
|
- protein,
|
32
|
|
- };
|
33
|
|
-}
|
34
|
|
-
|
35
|
|
-const rows = [
|
36
|
|
- createData('Cupcake', 305, 3.7, 67, 4.3),
|
37
|
|
- createData('Donut', 452, 25.0, 51, 4.9),
|
38
|
|
- createData('Eclair', 262, 16.0, 24, 6.0),
|
39
|
|
- createData('Frozen yoghurt', 159, 6.0, 24, 4.0),
|
40
|
|
- createData('Gingerbread', 356, 16.0, 49, 3.9),
|
41
|
|
- createData('Honeycomb', 408, 3.2, 87, 6.5),
|
42
|
|
- createData('Ice cream sandwich', 237, 9.0, 37, 4.3),
|
43
|
|
- createData('Jelly Bean', 375, 0.0, 94, 0.0),
|
44
|
|
- createData('KitKat', 518, 26.0, 65, 7.0),
|
45
|
|
- createData('Lollipop', 392, 0.2, 98, 0.0),
|
46
|
|
- createData('Marshmallow', 318, 0, 81, 2.0),
|
47
|
|
- createData('Nougat', 360, 19.0, 9, 37.0),
|
48
|
|
- createData('Oreo', 437, 18.0, 63, 4.0),
|
49
|
|
-];
|
50
|
|
-
|
51
|
|
-function descendingComparator(a, b, orderBy) {
|
52
|
|
- if (b[orderBy] < a[orderBy]) {
|
53
|
|
- return -1;
|
54
|
|
- }
|
55
|
|
- if (b[orderBy] > a[orderBy]) {
|
56
|
|
- return 1;
|
57
|
|
- }
|
58
|
|
- return 0;
|
59
|
|
-}
|
60
|
|
-
|
61
|
|
-function getComparator(order, orderBy) {
|
62
|
|
- return order === 'desc'
|
63
|
|
- ? (a, b) => descendingComparator(a, b, orderBy)
|
64
|
|
- : (a, b) => -descendingComparator(a, b, orderBy);
|
65
|
|
-}
|
66
|
|
-
|
67
|
|
-// This method is created for cross-browser compatibility, if you don't
|
68
|
|
-// need to support IE11, you can use Array.prototype.sort() directly
|
69
|
|
-function stableSort(array, comparator) {
|
70
|
|
- const stabilizedThis = array.map((el, index) => [el, index]);
|
71
|
|
- stabilizedThis.sort((a, b) => {
|
72
|
|
- const order = comparator(a[0], b[0]);
|
73
|
|
- if (order !== 0) {
|
74
|
|
- return order;
|
75
|
|
- }
|
76
|
|
- return a[1] - b[1];
|
77
|
|
- });
|
78
|
|
- return stabilizedThis.map((el) => el[0]);
|
79
|
|
-}
|
80
|
|
-
|
81
|
|
-const headCells = [
|
82
|
|
- {
|
83
|
|
- id: 'name',
|
84
|
|
- numeric: false,
|
85
|
|
- disablePadding: true,
|
86
|
|
- label: 'Dessert (100g serving)',
|
87
|
|
- },
|
88
|
|
- {
|
89
|
|
- id: 'calories',
|
90
|
|
- numeric: true,
|
91
|
|
- disablePadding: false,
|
92
|
|
- label: 'Calories',
|
93
|
|
- },
|
94
|
|
- {
|
95
|
|
- id: 'fat',
|
96
|
|
- numeric: true,
|
97
|
|
- disablePadding: false,
|
98
|
|
- label: 'Fat (g)',
|
99
|
|
- },
|
100
|
|
- {
|
101
|
|
- id: 'carbs',
|
102
|
|
- numeric: true,
|
103
|
|
- disablePadding: false,
|
104
|
|
- label: 'Carbs (g)',
|
105
|
|
- },
|
106
|
|
- {
|
107
|
|
- id: 'protein',
|
108
|
|
- numeric: true,
|
109
|
|
- disablePadding: false,
|
110
|
|
- label: 'Protein (g)',
|
111
|
|
- },
|
112
|
|
-];
|
113
|
|
-
|
114
|
|
-function EnhancedTableHead(props) {
|
115
|
|
-
|
116
|
|
- const { onSelectAllClick, order, orderBy, numSelected, rowCount, onRequestSort } =
|
117
|
|
- props;
|
118
|
|
- const createSortHandler = (property) => (event) => {
|
119
|
|
- onRequestSort(event, property);
|
120
|
|
- };
|
121
|
|
-
|
122
|
|
- return (
|
123
|
|
- <TableHead>
|
124
|
|
- <TableRow>
|
125
|
|
- <TableCell padding="checkbox">
|
126
|
|
- <Checkbox
|
127
|
|
- color="primary"
|
128
|
|
- indeterminate={numSelected > 0 && numSelected < rowCount}
|
129
|
|
- checked={rowCount > 0 && numSelected === rowCount}
|
130
|
|
- onChange={onSelectAllClick}
|
131
|
|
- inputProps={{
|
132
|
|
- 'aria-label': 'select all desserts',
|
133
|
|
- }}
|
134
|
|
- />
|
135
|
|
- </TableCell>
|
136
|
|
- {headCells.map((headCell) => (
|
137
|
|
- <TableCell
|
138
|
|
- key={headCell.id}
|
139
|
|
- align={headCell.numeric ? 'right' : 'left'}
|
140
|
|
- padding={headCell.disablePadding ? 'none' : 'normal'}
|
141
|
|
- sortDirection={orderBy === headCell.id ? order : false}
|
142
|
|
- >
|
143
|
|
- <TableSortLabel
|
144
|
|
- active={orderBy === headCell.id}
|
145
|
|
- direction={orderBy === headCell.id ? order : 'asc'}
|
146
|
|
- onClick={createSortHandler(headCell.id)}
|
147
|
|
- >
|
148
|
|
- {headCell.label}
|
149
|
|
- {orderBy === headCell.id ? (
|
150
|
|
- <Box component="span" sx={visuallyHidden}>
|
151
|
|
- {order === 'desc' ? 'sorted descending' : 'sorted ascending'}
|
152
|
|
- </Box>
|
153
|
|
- ) : null}
|
154
|
|
- </TableSortLabel>
|
155
|
|
- </TableCell>
|
156
|
|
- ))}
|
157
|
|
- </TableRow>
|
158
|
|
- </TableHead>
|
159
|
|
- );
|
160
|
|
-}
|
161
|
|
-
|
162
|
|
-EnhancedTableHead.propTypes = {
|
163
|
|
- numSelected: PropTypes.number.isRequired,
|
164
|
|
- onRequestSort: PropTypes.func.isRequired,
|
165
|
|
- onSelectAllClick: PropTypes.func.isRequired,
|
166
|
|
- order: PropTypes.oneOf(['asc', 'desc']).isRequired,
|
167
|
|
- orderBy: PropTypes.string.isRequired,
|
168
|
|
- rowCount: PropTypes.number.isRequired,
|
169
|
|
-};
|
170
|
|
-
|
171
|
|
-const EnhancedTableToolbar = (props) => {
|
172
|
|
-
|
173
|
|
- const { numSelected } = props;
|
174
|
|
-
|
175
|
|
- return (
|
176
|
|
- <Toolbar
|
177
|
|
- sx={{
|
178
|
|
- pl: { sm: 2 },
|
179
|
|
- pr: { xs: 1, sm: 1 },
|
180
|
|
- ...(numSelected > 0 && {
|
181
|
|
- bgcolor: (theme) =>
|
182
|
|
- alpha(theme.palette.primary.main, theme.palette.action.activatedOpacity),
|
183
|
|
- }),
|
184
|
|
- }}
|
185
|
|
- >
|
186
|
|
- {numSelected > 0 ? (
|
187
|
|
- <Typography
|
188
|
|
- sx={{ flex: '1 1 100%' }}
|
189
|
|
- color="inherit"
|
190
|
|
- variant="subtitle1"
|
191
|
|
- component="div"
|
192
|
|
- >
|
193
|
|
- {numSelected} selected
|
194
|
|
- </Typography>
|
195
|
|
- ) : (
|
196
|
|
- <Typography
|
197
|
|
- sx={{ flex: '1 1 100%' }}
|
198
|
|
- variant="h6"
|
199
|
|
- id="tableTitle"
|
200
|
|
- component="div"
|
201
|
|
- >
|
202
|
|
- Nutrition
|
203
|
|
- </Typography>
|
204
|
|
- )}
|
205
|
|
-
|
206
|
|
- {numSelected > 0 ? (
|
207
|
|
- <Tooltip title="Delete">
|
208
|
|
- <IconButton>
|
209
|
|
- <DeleteIcon />
|
210
|
|
- </IconButton>
|
211
|
|
- </Tooltip>
|
212
|
|
- ) : (
|
213
|
|
- <Tooltip title="Filter list">
|
214
|
|
- <IconButton>
|
215
|
|
- <FilterListIcon />
|
216
|
|
- </IconButton>
|
217
|
|
- </Tooltip>
|
218
|
|
- )}
|
219
|
|
- </Toolbar>
|
220
|
|
- );
|
221
|
|
-};
|
222
|
|
-
|
223
|
|
-EnhancedTableToolbar.propTypes = {
|
224
|
|
- numSelected: PropTypes.number.isRequired,
|
225
|
|
-};
|
226
|
|
-
|
227
|
|
-export default function EnhancedTable() {
|
228
|
|
-
|
229
|
|
- const [order, setOrder] = React.useState('asc');
|
230
|
|
- const [orderBy, setOrderBy] = React.useState('calories');
|
231
|
|
- const [selected, setSelected] = React.useState([]);
|
232
|
|
- const [page, setPage] = React.useState(0);
|
233
|
|
- const [dense, setDense] = React.useState(false);
|
234
|
|
- const [rowsPerPage, setRowsPerPage] = React.useState(5);
|
235
|
|
-
|
236
|
|
- const handleRequestSort = (event, property) => {
|
237
|
|
- const isAsc = orderBy === property && order === 'asc';
|
238
|
|
- setOrder(isAsc ? 'desc' : 'asc');
|
239
|
|
- setOrderBy(property);
|
240
|
|
- };
|
241
|
|
-
|
242
|
|
- const handleSelectAllClick = (event) => {
|
243
|
|
- if (event.target.checked) {
|
244
|
|
- const newSelecteds = rows.map((n) => n.name);
|
245
|
|
- setSelected(newSelecteds);
|
246
|
|
- return;
|
247
|
|
- }
|
248
|
|
- setSelected([]);
|
249
|
|
- };
|
250
|
|
-
|
251
|
|
- const handleClick = (event, name) => {
|
252
|
|
- const selectedIndex = selected.indexOf(name);
|
253
|
|
- let newSelected = [];
|
254
|
|
-
|
255
|
|
- if (selectedIndex === -1) {
|
256
|
|
- newSelected = newSelected.concat(selected, name);
|
257
|
|
- } else if (selectedIndex === 0) {
|
258
|
|
- newSelected = newSelected.concat(selected.slice(1));
|
259
|
|
- } else if (selectedIndex === selected.length - 1) {
|
260
|
|
- newSelected = newSelected.concat(selected.slice(0, -1));
|
261
|
|
- } else if (selectedIndex > 0) {
|
262
|
|
- newSelected = newSelected.concat(
|
263
|
|
- selected.slice(0, selectedIndex),
|
264
|
|
- selected.slice(selectedIndex + 1),
|
265
|
|
- );
|
266
|
|
- }
|
267
|
|
-
|
268
|
|
- setSelected(newSelected);
|
269
|
|
- };
|
270
|
|
-
|
271
|
|
- const handleChangePage = (event, newPage) => {
|
272
|
|
- setPage(newPage);
|
273
|
|
- };
|
274
|
|
-
|
275
|
|
- const handleChangeRowsPerPage = (event) => {
|
276
|
|
- setRowsPerPage(parseInt(event.target.value, 10));
|
277
|
|
- setPage(0);
|
278
|
|
- };
|
279
|
|
-
|
280
|
|
- const handleChangeDense = (event) => {
|
281
|
|
- setDense(event.target.checked);
|
282
|
|
- };
|
283
|
|
-
|
284
|
|
- const isSelected = (name) => selected.indexOf(name) !== -1;
|
285
|
|
-
|
286
|
|
- // Avoid a layout jump when reaching the last page with empty rows.
|
287
|
|
- const emptyRows = page > 0 ? Math.max(0, (1 + page) * rowsPerPage - rows.length) : 0;
|
288
|
|
-
|
289
|
|
- return (
|
290
|
|
- <Box sx={{ width: '100%' }}>
|
291
|
|
- <Paper sx={{ width: '100%', mb: 2 }}>
|
292
|
|
- <EnhancedTableToolbar numSelected={selected.length} />
|
293
|
|
- <TableContainer>
|
294
|
|
- <Table
|
295
|
|
- sx={{ minWidth: 750 }}
|
296
|
|
- aria-labelledby="tableTitle"
|
297
|
|
- size={dense ? 'small' : 'medium'}
|
298
|
|
- >
|
299
|
|
- <EnhancedTableHead
|
300
|
|
- numSelected={selected.length}
|
301
|
|
- order={order}
|
302
|
|
- orderBy={orderBy}
|
303
|
|
- onSelectAllClick={handleSelectAllClick}
|
304
|
|
- onRequestSort={handleRequestSort}
|
305
|
|
- rowCount={rows.length}
|
306
|
|
- />
|
307
|
|
- <TableBody>
|
308
|
|
- {/* if you don't need to support IE11, you can replace the `stableSort` call with:
|
309
|
|
- rows.slice().sort(getComparator(order, orderBy)) */}
|
310
|
|
- {stableSort(rows, getComparator(order, orderBy))
|
311
|
|
- .slice(page * rowsPerPage, page * rowsPerPage + rowsPerPage)
|
312
|
|
- .map((row, index) => {
|
313
|
|
- const isItemSelected = isSelected(row.name);
|
314
|
|
- const labelId = `enhanced-table-checkbox-${index}`;
|
315
|
|
-
|
316
|
|
- return (
|
317
|
|
- <TableRow
|
318
|
|
- hover
|
319
|
|
- onClick={(event) => handleClick(event, row.name)}
|
320
|
|
- role="checkbox"
|
321
|
|
- aria-checked={isItemSelected}
|
322
|
|
- tabIndex={-1}
|
323
|
|
- key={row.name}
|
324
|
|
- selected={isItemSelected}
|
325
|
|
- >
|
326
|
|
- <TableCell padding="checkbox">
|
327
|
|
- <Checkbox
|
328
|
|
- color="primary"
|
329
|
|
- checked={isItemSelected}
|
330
|
|
- inputProps={{
|
331
|
|
- 'aria-labelledby': labelId,
|
332
|
|
- }}
|
333
|
|
- />
|
334
|
|
- </TableCell>
|
335
|
|
- <TableCell
|
336
|
|
- component="th"
|
337
|
|
- id={labelId}
|
338
|
|
- scope="row"
|
339
|
|
- padding="none"
|
340
|
|
- >
|
341
|
|
- {row.name}
|
342
|
|
- </TableCell>
|
343
|
|
- <TableCell align="right">{row.calories}</TableCell>
|
344
|
|
- <TableCell align="right">{row.fat}</TableCell>
|
345
|
|
- <TableCell align="right">{row.carbs}</TableCell>
|
346
|
|
- <TableCell align="right">{row.protein}</TableCell>
|
347
|
|
- </TableRow>
|
348
|
|
- );
|
349
|
|
- })}
|
350
|
|
- {emptyRows > 0 && (
|
351
|
|
- <TableRow
|
352
|
|
- style={{
|
353
|
|
- height: (dense ? 33 : 53) * emptyRows,
|
354
|
|
- }}
|
355
|
|
- >
|
356
|
|
- <TableCell colSpan={6} />
|
357
|
|
- </TableRow>
|
358
|
|
- )}
|
359
|
|
- </TableBody>
|
360
|
|
- </Table>
|
361
|
|
- </TableContainer>
|
362
|
|
- <TablePagination
|
363
|
|
- rowsPerPageOptions={[5, 10, 25]}
|
364
|
|
- component="div"
|
365
|
|
- count={rows.length}
|
366
|
|
- rowsPerPage={rowsPerPage}
|
367
|
|
- page={page}
|
368
|
|
- onPageChange={handleChangePage}
|
369
|
|
- onRowsPerPageChange={handleChangeRowsPerPage}
|
370
|
|
- />
|
371
|
|
- </Paper>
|
372
|
|
- <FormControlLabel
|
373
|
|
- control={<Switch checked={dense} onChange={handleChangeDense} />}
|
374
|
|
- label="Dense padding"
|
375
|
|
- />
|
376
|
|
- </Box>
|
377
|
|
- );
|
378
|
|
-}
|