-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCustomTable.tsx
More file actions
153 lines (142 loc) · 4.54 KB
/
Copy pathCustomTable.tsx
File metadata and controls
153 lines (142 loc) · 4.54 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
import { useCallback, type FC, useState, useEffect } from 'react'
import {
Box,
Paper,
TableContainer,
Table,
TableHead,
TableRow,
TableBody,
TablePagination,
TableCell,
Radio,
TableSortLabel
} from '@mui/material'
import { useTranslation } from 'react-i18next'
import PersonIcon from '@mui/icons-material/Person'
import GroupIcon from '@mui/icons-material/Group'
import SportsSoccerIcon from '@mui/icons-material/SportsSoccer'
interface CustomTableProps {
columns: Column[]
rows: any[]
selectedRow?: number | null
selectable?: boolean
defaultOrderBy?: string
defaultOrder?: 'asc' | 'desc'
tableHeight?: string
handleRowSelect?: (event: React.ChangeEvent<HTMLInputElement>) => void
}
const CustomTable: FC<CustomTableProps> = ({
selectable = true,
tableHeight = '83%',
columns,
rows,
selectedRow,
defaultOrder,
defaultOrderBy,
handleRowSelect
}) => {
const { t } = useTranslation()
const [page, setPage] = useState(0)
const [rowsPerPage, setRowsPerPage] = useState(10)
const [orderBy, setOrderBy] = useState(defaultOrderBy ?? 'id')
const [order, setOrder] = useState<'asc' | 'desc'>(defaultOrder ?? 'asc')
const handleChangePage = useCallback((_: unknown, newPage: number) => {
setPage(newPage)
}, [])
const handleChangeRowsPerPage = useCallback((event: React.ChangeEvent<HTMLInputElement>) => {
setRowsPerPage(+event.target.value)
setPage(0)
}, [])
const handleRequestSort = useCallback((property: string) => {
setOrder((orderBy === property && order === 'asc') ? 'desc' : 'asc')
setOrderBy(property)
}, [order, orderBy])
useEffect(() => {
setPage(0)
}, [rowsPerPage])
const sortedRows = [...rows].sort((a, b) =>
order === 'asc'
? a[orderBy] > b[orderBy] ? 1 : -1
: a[orderBy] < b[orderBy] ? 1 : -1)
return <Box display='flex' width='100%' flexDirection='column' height={tableHeight}>
<Paper sx={{ width: '100%', overflow: 'hidden', height: '92%' }}>
<TableContainer sx={{ maxHeight: '100%' }} data-testid="table-container">
<Table stickyHeader aria-label="sticky table" size='small'>
<TableHead>
<TableRow>
{selectable &&
<TableCell>
{t('table.select')}
</TableCell>
}
{columns.map((column, index) =>
<TableCell key={index}>
{column.sortable
? (
<TableSortLabel
active={orderBy === column.id}
direction={orderBy === column.id ? order : 'asc'}
onClick={() => { handleRequestSort(column.id) }}
>
{column.label}
</TableSortLabel>
)
: (
column.label
)}
</TableCell>
)}
</TableRow>
</TableHead>
<TableBody>
{sortedRows
.slice(page * rowsPerPage, page * rowsPerPage + rowsPerPage)
.map((row, index) => (
<TableRow hover role="checkbox" tabIndex={-1} key={index}>
{selectable &&
<TableCell>
<Radio
name={`col_${index}`}
checked={selectedRow === row.id}
onChange={handleRowSelect}
value={row.id} />
</TableCell>
}
{columns.map((column, index) => (
<TableCell key={index}>
{column.id === 'type' ? chooseTableIcon(row[column.id] as TableType) : row[column.id]}
</TableCell>
))}
</TableRow>
))}
</TableBody>
</Table>
</TableContainer>
</Paper>
<Paper sx={{ width: '100%', overflow: 'hidden', height: '8%', margin: '0.5vh 0px' }}>
<TablePagination
rowsPerPageOptions={[10, 25, 100]}
component="div"
count={rows.length}
rowsPerPage={rowsPerPage}
page={page}
onPageChange={handleChangePage}
onRowsPerPageChange={handleChangeRowsPerPage}
/>
</Paper>
</Box>
}
export default CustomTable
const chooseTableIcon = (id: TableType): JSX.Element => {
switch (id) {
case 'Match':
return <SportsSoccerIcon />
case 'Player':
return <PersonIcon />
case 'Team':
return <GroupIcon />
default:
return <PersonIcon />
}
}