|
| 1 | +/*--------------------------------------------------------------------------------------------- |
| 2 | + * Copyright (c) Bentley Systems, Incorporated. All rights reserved. |
| 3 | + * See LICENSE.md in the project root for license terms and full copyright notice. |
| 4 | + *--------------------------------------------------------------------------------------------*/ |
| 5 | + |
| 6 | +import React from "react"; |
| 7 | +import Paper from "@mui/material/Paper"; |
| 8 | +import Table from "@mui/material/Table"; |
| 9 | +import TableBody from "@mui/material/TableBody"; |
| 10 | +import TableCell from "@mui/material/TableCell"; |
| 11 | +import TableContainer from "@mui/material/TableContainer"; |
| 12 | +import TableHead from "@mui/material/TableHead"; |
| 13 | +import TableRow from "@mui/material/TableRow"; |
| 14 | +import TableSortLabel from "@mui/material/TableSortLabel"; |
| 15 | + |
| 16 | +const rows = [ |
| 17 | + { name: "Cupcake", calories: 305, fat: 3.7, carbs: 67, protein: 4.3 }, |
| 18 | + { name: "Eclair", calories: 262, fat: 16.0, carbs: 24, protein: 6.0 }, |
| 19 | + { |
| 20 | + name: "Frozen yoghurt", |
| 21 | + calories: 159, |
| 22 | + fat: 6.0, |
| 23 | + carbs: 24, |
| 24 | + protein: 4.0, |
| 25 | + }, |
| 26 | + { name: "Gingerbread", calories: 356, fat: 16.0, carbs: 49, protein: 3.9 }, |
| 27 | + { |
| 28 | + name: "Ice cream sandwich", |
| 29 | + calories: 237, |
| 30 | + fat: 9.0, |
| 31 | + carbs: 37, |
| 32 | + protein: 4.3, |
| 33 | + }, |
| 34 | +]; |
| 35 | + |
| 36 | +export default () => { |
| 37 | + const sortButtonDescriptionId = React.useId(); |
| 38 | + |
| 39 | + const [direction, setDirection] = |
| 40 | + React.useState<React.ComponentProps<typeof TableSortLabel>["direction"]>( |
| 41 | + "asc", |
| 42 | + ); |
| 43 | + const [sortedColumn, setSortedColumn] = React.useState<"calories" | "fat">( |
| 44 | + "calories", |
| 45 | + ); |
| 46 | + |
| 47 | + const updateSort = (column: "calories" | "fat") => { |
| 48 | + if (column === sortedColumn) { |
| 49 | + setDirection((current) => (current === "asc" ? "desc" : "asc")); |
| 50 | + } else { |
| 51 | + setSortedColumn(column); |
| 52 | + setDirection("asc"); |
| 53 | + } |
| 54 | + }; |
| 55 | + |
| 56 | + const sortedRows = React.useMemo( |
| 57 | + () => |
| 58 | + Array.from(rows).sort((a, b) => |
| 59 | + direction === "asc" |
| 60 | + ? a[sortedColumn] - b[sortedColumn] |
| 61 | + : b[sortedColumn] - a[sortedColumn], |
| 62 | + ), |
| 63 | + [direction, sortedColumn], |
| 64 | + ); |
| 65 | + |
| 66 | + return ( |
| 67 | + <TableContainer render={<Paper />}> |
| 68 | + <span hidden id={sortButtonDescriptionId}> |
| 69 | + change sort |
| 70 | + </span> |
| 71 | + <Table> |
| 72 | + <caption>Dessert nutrition</caption> |
| 73 | + <TableHead> |
| 74 | + <TableRow> |
| 75 | + <TableCell>Dessert (100g serving)</TableCell> |
| 76 | + <TableCell |
| 77 | + align="right" |
| 78 | + sortDirection={sortedColumn === "calories" && direction} |
| 79 | + > |
| 80 | + <TableSortLabel |
| 81 | + direction={sortedColumn === "calories" ? direction : "asc"} |
| 82 | + active={sortedColumn === "calories"} |
| 83 | + onClick={() => updateSort("calories")} |
| 84 | + aria-describedby={sortButtonDescriptionId} |
| 85 | + > |
| 86 | + Calories |
| 87 | + </TableSortLabel> |
| 88 | + </TableCell> |
| 89 | + <TableCell |
| 90 | + align="right" |
| 91 | + sortDirection={sortedColumn === "fat" && direction} |
| 92 | + > |
| 93 | + <TableSortLabel |
| 94 | + direction={sortedColumn === "fat" ? direction : "asc"} |
| 95 | + active={sortedColumn === "fat"} |
| 96 | + onClick={() => updateSort("fat")} |
| 97 | + aria-describedby={sortButtonDescriptionId} |
| 98 | + > |
| 99 | + Fat (g) |
| 100 | + </TableSortLabel> |
| 101 | + </TableCell> |
| 102 | + </TableRow> |
| 103 | + </TableHead> |
| 104 | + <TableBody> |
| 105 | + {sortedRows.map((row) => ( |
| 106 | + <TableRow key={row.name}> |
| 107 | + <TableCell render={<th />} scope="row"> |
| 108 | + {row.name} |
| 109 | + </TableCell> |
| 110 | + <TableCell align="right">{row.calories}</TableCell> |
| 111 | + <TableCell align="right">{row.fat}</TableCell> |
| 112 | + </TableRow> |
| 113 | + ))} |
| 114 | + </TableBody> |
| 115 | + </Table> |
| 116 | + </TableContainer> |
| 117 | + ); |
| 118 | +}; |
0 commit comments