-
Notifications
You must be signed in to change notification settings - Fork 21
Expand file tree
/
Copy pathindex.tsx
More file actions
274 lines (258 loc) · 12.3 KB
/
Copy pathindex.tsx
File metadata and controls
274 lines (258 loc) · 12.3 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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
import React from 'react';
import Box from '@mui/material/Box';
import Table from '@mui/material/Table';
import TableBody from '@mui/material/TableBody';
import TableCell from '@mui/material/TableCell';
import TableContainer from '@mui/material/TableContainer';
import TableHead from '@mui/material/TableHead';
import TablePagination from '@mui/material/TablePagination';
import TableRow from '@mui/material/TableRow';
import TableSortLabel from '@mui/material/TableSortLabel';
import Paper from '@mui/material/Paper';
import { visuallyHidden } from '@mui/utils';
import { HeadCell, PageInfo, PaginationOptions } from 'components/common/Table/types';
import { LinearProgress } from '@mui/material';
import { Unless, When } from 'react-if';
type Order = 'asc' | 'desc';
interface CustomTableHeadProps<T> {
onRequestSort: (event: React.MouseEvent<unknown>, property: keyof T, headCellIndex: number) => void;
order?: Order;
orderBy?: keyof T;
nestedSortKey?: string | null;
rowCount: number;
headCells: HeadCell<T>[];
loading: boolean;
}
/**
* CustomTableHead is a component that renders the header of a table with sortable columns.
* It uses Material-UI's TableHead, TableRow, TableCell, and TableSortLabel components.
* It allows for sorting by clicking on the column headers, and displays the current sort order.
* @param {CustomTableHeadProps<T>} props - The properties for the table head.
* @param {Order} [props.order] - The current sort order, either 'asc' or 'desc'.
* @param {keyof T} [props.orderBy] - The key of the column that is currently sorted.
* @param {function} props.onRequestSort - The function to call when a column header is clicked for sorting.
* @param {HeadCell<T>[]} props.headCells - An array of head cells that define the columns of the table.
* @param {boolean} props.loading - Indicates whether the table is currently loading data.
* @param {string | null} [props.nestedSortKey] - The nested sort key for sorting within a column.
* @returns {JSX.Element} A Material-UI TableHead component with sortable column headers.
*/
function CustomTableHead<T>({
order,
orderBy,
onRequestSort,
headCells,
loading,
nestedSortKey,
}: CustomTableHeadProps<T>) {
const createSortHandler = (property: keyof T, headCellIndex: number) => (event: React.MouseEvent<unknown>) => {
onRequestSort(event, property, headCellIndex);
};
return (
<TableHead>
<TableRow>
{headCells.map((headCell, index) => (
<TableCell
key={`${String(headCell.key)}${index}`}
align={headCell.align}
style={headCell.customStyle || {}}
sortDirection={orderBy === headCell.key ? order : false}
sx={{ borderBottom: '1.5px solid gray', fontWeight: 'bold' }}
>
<TableSortLabel
disabled={!headCell.allowSort || loading}
active={orderBy === headCell.key && nestedSortKey === headCell.nestedSortKey}
direction={
orderBy === headCell.key && nestedSortKey === headCell.nestedSortKey ? order : 'asc'
}
onClick={createSortHandler(headCell.key, index)}
hideSortIcon={Boolean(headCell.hideSorticon)}
>
{headCell.label}
{headCell.icon}
{orderBy === headCell.key && nestedSortKey === headCell.nestedSortKey && (
<Box component="span" sx={visuallyHidden}>
{order === 'desc' ? 'sorted descending' : 'sorted ascending'}
</Box>
)}
</TableSortLabel>
</TableCell>
))}
</TableRow>
</TableHead>
);
}
export interface CustomTableProps<T> {
headCells: HeadCell<T>[];
rows: T[];
hideHeader?: boolean;
noRowBorder?: boolean;
handleChangePagination?: (pagination: PaginationOptions<T>) => void;
loading?: boolean;
paginationOptions?: PaginationOptions<T>;
pageInfo?: PageInfo;
noPagination?: boolean;
emptyText?: string;
commentTable?: boolean;
}
/**
* CustomTable is a generic table component that displays data in a tabular format.
* It supports sorting, pagination, and customizable headers.
* The table can be configured to hide the header, disable row borders, and handle pagination changes.
* @param {CustomTableProps<T>} props - The properties for the table.
* @param {HeadCell<T>[]} props.headCells - An array of head cells that define the columns of the table.
* @param {T[]} props.rows - An array of data rows to be displayed in the table.
* @param {boolean} [props.hideHeader=false] - If true, the table header will not be displayed.
* @param {boolean} [props.noRowBorder=false] - If true, the table rows will not have borders.
* @param {function} [props.handleChangePagination] - A function to handle pagination changes.
* @param {boolean} [props.loading=false] - If true, the table will show a loading indicator.
* @param {PaginationOptions<T>} [props.paginationOptions] - Options for pagination, including page number, size, sort key, and sort order.
* @param {PageInfo} [props.pageInfo] - Information about the current page, including total records.
* @param {boolean} [props.noPagination=false] - If true, pagination controls will not be displayed.
* @param {string} [props.emptyText='No records were found'] - Text to display when there are no records in the table.
* @param {boolean} [props.commentTable=false] - If true, the table is styled for comments, typically with no padding in cells.
* @returns {JSX.Element} A Material-UI Paper component containing the table with the specified properties.
*/
function CustomTable<T>({
hideHeader = false,
headCells = [],
rows = [],
noRowBorder = false,
noPagination = false,
commentTable = false,
handleChangePagination = (_pagination: PaginationOptions<T>) => {},
loading = false,
paginationOptions = {
page: 1,
size: rows.length,
},
pageInfo = {
total: rows.length,
},
emptyText = 'No records were found',
}: Readonly<CustomTableProps<T>>) {
const { page = 1, size, sort_key, sort_order, nested_sort_key } = paginationOptions;
const { total } = pageInfo;
const order = sort_order;
const orderBy = sort_key;
const rowsPerPage = size && size > 0 ? size : 5;
const baseRowsPerPageOptions = [5, 10, 25];
const rowsPerPageOptions = baseRowsPerPageOptions.includes(rowsPerPage)
? baseRowsPerPageOptions
: [...baseRowsPerPageOptions, rowsPerPage].sort((a, b) => a - b);
const handleRequestSort = (_event: React.MouseEvent<unknown>, property: keyof T, headCellIndex: number) => {
const isAsc = orderBy === property && order === 'asc';
handleChangePagination({
...paginationOptions,
sort_key: property,
nested_sort_key: headCells[headCellIndex].nestedSortKey,
sort_order: isAsc ? 'desc' : 'asc',
});
};
const handleChangePage = (_event: unknown, newPage: number) => {
handleChangePagination({
...paginationOptions,
page: newPage + 1,
});
};
const handleChangeRowsPerPage = (event: React.ChangeEvent<HTMLInputElement>) => {
const newSize = parseInt(event.target.value, 10);
handleChangePagination({
...paginationOptions,
page: 1,
size: newSize,
});
};
// Avoid a layout jump when reaching the last page with empty filteredRows.
const emptyRows = page > 1 ? Math.max(0, page * rowsPerPage - total) : 0;
return (
<Box data-testid="listing-table">
<Paper sx={{ width: '100%', mb: 2 }} elevation={0}>
<TableContainer>
<Table aria-labelledby="Engagements">
<When condition={!hideHeader}>
<CustomTableHead
order={order}
orderBy={orderBy}
nestedSortKey={nested_sort_key}
onRequestSort={handleRequestSort}
rowCount={rows.length}
headCells={headCells}
loading={loading}
/>
</When>
<TableBody>
<TableRow
sx={{
height: 5,
}}
>
<TableCell
sx={{ padding: 0, border: 'none', verticalAlign: 'top' }}
colSpan={headCells.length}
>
<When condition={loading}>
<LinearProgress />
</When>
</TableCell>
</TableRow>
{rows.map((row, rowIndex) => {
return (
<TableRow hover tabIndex={-1} key={`row-${rowIndex}`}>
{headCells.map((cell, cellIndex) => (
<TableCell
align={cell.align}
key={`row-${rowIndex}-${cellIndex}`}
style={cell.customStyle || {}}
sx={{
paddingTop: commentTable ? 0 : '16px',
border: noRowBorder ? 'none' : '',
}}
>
{cell.renderCell ? cell.renderCell(row) : String(row[cell.key])}
</TableCell>
))}
</TableRow>
);
})}
{rows.length == 0 && (
<TableRow>
<TableCell colSpan={headCells.length} align={'center'}>
<When condition={loading}>Loading Records</When>
<When condition={!loading}>{emptyText}</When>
</TableCell>
</TableRow>
)}
{emptyRows > 0 && (
<TableRow
style={{
height: 53 * emptyRows,
}}
>
<TableCell
colSpan={headCells.length}
sx={{
border: noRowBorder ? 'none' : '',
}}
/>
</TableRow>
)}
</TableBody>
</Table>
</TableContainer>
<Unless condition={noPagination}>
<TablePagination
data-testid="Table-Pagination"
rowsPerPageOptions={rowsPerPageOptions}
component="div"
count={total}
rowsPerPage={rowsPerPage}
page={page - 1}
onPageChange={handleChangePage}
onRowsPerPageChange={handleChangeRowsPerPage}
/>
</Unless>
</Paper>
</Box>
);
}
export default CustomTable;