|
| 1 | +import React, { FC, useCallback } from 'react'; |
| 2 | +import { orderBy } from 'lodash'; |
| 3 | +import { Table, TableCell, TableSortableHeaderCell, TableRow } from '..'; |
| 4 | +import { Text } from '../..'; |
| 5 | +import { useSortBy } from '../hooks/useSortBy'; |
| 6 | + |
| 7 | +const data = [ |
| 8 | + { |
| 9 | + id: 1, |
| 10 | + name: 'Zaha', |
| 11 | + emailAddress: '[email protected]', |
| 12 | + role: 'Admin', |
| 13 | + costCenter: 'Design System' |
| 14 | + }, |
| 15 | + { |
| 16 | + id: 2, |
| 17 | + name: 'Alex', |
| 18 | + emailAddress: '[email protected]', |
| 19 | + role: 'Booker', |
| 20 | + costCenter: 'Product' |
| 21 | + }, |
| 22 | + { |
| 23 | + id: 3, |
| 24 | + name: 'Britta', |
| 25 | + emailAddress: '[email protected]', |
| 26 | + role: 'Passenger', |
| 27 | + costCenter: 'Customer' |
| 28 | + }, |
| 29 | + { |
| 30 | + id: 4, |
| 31 | + name: 'Caio', |
| 32 | + emailAddress: '[email protected]', |
| 33 | + role: 'Admin', |
| 34 | + costCenter: 'Design System' |
| 35 | + } |
| 36 | +]; |
| 37 | + |
| 38 | +export const SortableTable: FC = () => { |
| 39 | + const { sortBy, setSortBy } = useSortBy(); |
| 40 | + const handleSortingChange = useCallback( |
| 41 | + field => { |
| 42 | + setSortBy(field); |
| 43 | + }, |
| 44 | + [setSortBy] |
| 45 | + ); |
| 46 | + |
| 47 | + const direction = sortBy.direction?.toLowerCase() || 'asc'; |
| 48 | + // @ts-expect-error Argument of type 'string' is not assignable to parameter of type 'Many<boolean | "asc" | "desc"> | undefined'.ts(2769) |
| 49 | + const sortedData = orderBy(data, sortBy.field, direction); |
| 50 | + |
| 51 | + return ( |
| 52 | + <Table rowStyle="zebra" rowSize="small"> |
| 53 | + <thead> |
| 54 | + <TableRow> |
| 55 | + <TableSortableHeaderCell |
| 56 | + field="name" |
| 57 | + active={sortBy.field === 'name'} |
| 58 | + direction={sortBy.direction} |
| 59 | + onSortChange={handleSortingChange} |
| 60 | + > |
| 61 | + Name |
| 62 | + </TableSortableHeaderCell> |
| 63 | + <TableSortableHeaderCell |
| 64 | + field="emailAddress" |
| 65 | + active={sortBy.field === 'emailAddress'} |
| 66 | + direction={sortBy.direction} |
| 67 | + onSortChange={handleSortingChange} |
| 68 | + > |
| 69 | + E-Mail |
| 70 | + </TableSortableHeaderCell> |
| 71 | + <TableSortableHeaderCell |
| 72 | + field="role" |
| 73 | + active={sortBy.field === 'role'} |
| 74 | + direction={sortBy.direction} |
| 75 | + onSortChange={handleSortingChange} |
| 76 | + > |
| 77 | + Role |
| 78 | + </TableSortableHeaderCell> |
| 79 | + <TableSortableHeaderCell |
| 80 | + field="costCenter" |
| 81 | + active={sortBy.field === 'costCenter'} |
| 82 | + direction={sortBy.direction} |
| 83 | + onSortChange={handleSortingChange} |
| 84 | + > |
| 85 | + Cost Center |
| 86 | + </TableSortableHeaderCell> |
| 87 | + </TableRow> |
| 88 | + </thead> |
| 89 | + <tbody> |
| 90 | + {sortedData.map(entry => ( |
| 91 | + <TableRow key={entry.id}> |
| 92 | + <TableCell> |
| 93 | + <Text fontWeight="semibold" fontSize={1}> |
| 94 | + {entry.name} |
| 95 | + </Text> |
| 96 | + </TableCell> |
| 97 | + <TableCell>{entry.emailAddress}</TableCell> |
| 98 | + <TableCell>{entry.role}</TableCell> |
| 99 | + <TableCell>{entry.costCenter}</TableCell> |
| 100 | + </TableRow> |
| 101 | + ))} |
| 102 | + </tbody> |
| 103 | + </Table> |
| 104 | + ); |
| 105 | +}; |
0 commit comments