@campxdev/shared 0.3.0 → 0.3.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/package.json +1 -1
- package/src/components/ErrorBoundary/ErrorBoundary.tsx +1 -1
- package/src/components/ErrorBoundary/ErrorFallback.tsx +1 -1
- package/src/components/ErrorBoundary/index.tsx +1 -1
- package/src/components/Layout/Header/UserBox.tsx +3 -0
- package/src/components/Spinner.tsx +14 -14
- package/src/components/TableComponent/ReactTable.tsx +258 -257
- package/src/components/TableComponent/index.tsx +174 -169
package/package.json
CHANGED
|
@@ -2,7 +2,7 @@ import {ErrorBoundary as ReactErrorBoundary} from 'react-error-boundary'
|
|
|
2
2
|
import {QueryErrorResetBoundary} from 'react-query'
|
|
3
3
|
import ErrorFallback from './ErrorFallback'
|
|
4
4
|
|
|
5
|
-
export default function
|
|
5
|
+
export default function ErrorBoundary({children}) {
|
|
6
6
|
return (
|
|
7
7
|
<QueryErrorResetBoundary>
|
|
8
8
|
{({reset}) => (
|
|
@@ -21,7 +21,7 @@ const StyledAlert = styled(Alert)(({theme}) => ({
|
|
|
21
21
|
|
|
22
22
|
export default function ErrorFallback({error, resetErrorBoundary}) {
|
|
23
23
|
return (
|
|
24
|
-
<Box sx={{padding: '16px'}}>
|
|
24
|
+
<Box sx={{marginTop: '80px', padding: '16px'}}>
|
|
25
25
|
<StyledAlert severity='error'>
|
|
26
26
|
{error?.message}
|
|
27
27
|
<Button
|
|
@@ -1 +1 @@
|
|
|
1
|
-
export {default} from './
|
|
1
|
+
export {default} from './ErrorBoundary'
|
|
@@ -1,18 +1,18 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import {CircularProgress} from '@mui/material'
|
|
2
2
|
|
|
3
|
-
function Spinner() {
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
3
|
+
function Spinner({height = '200px'}: {height?: string | number}) {
|
|
4
|
+
return (
|
|
5
|
+
<div
|
|
6
|
+
style={{
|
|
7
|
+
justifyContent: 'center',
|
|
8
|
+
alignItems: 'center',
|
|
9
|
+
height,
|
|
10
|
+
display: 'flex',
|
|
11
|
+
}}
|
|
12
|
+
>
|
|
13
|
+
<CircularProgress />
|
|
14
|
+
</div>
|
|
15
|
+
)
|
|
16
16
|
}
|
|
17
17
|
|
|
18
18
|
export default Spinner
|
|
@@ -1,293 +1,294 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import {ExpandMore} from '@mui/icons-material'
|
|
2
2
|
import {
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
3
|
+
Box,
|
|
4
|
+
Checkbox,
|
|
5
|
+
IconButton,
|
|
6
|
+
ListItemIcon,
|
|
7
|
+
MenuItem,
|
|
8
|
+
Table,
|
|
9
|
+
TableCell,
|
|
10
|
+
TableHead,
|
|
11
|
+
TableRow,
|
|
12
|
+
Typography,
|
|
13
13
|
} from '@mui/material'
|
|
14
|
-
import {
|
|
15
|
-
import {
|
|
14
|
+
import {useEffect, useMemo, useState} from 'react'
|
|
15
|
+
import {usePagination, useRowSelect, useTable} from 'react-table'
|
|
16
|
+
import ErrorBoundary from '../ErrorBoundary'
|
|
16
17
|
import BatchActionsHeader from './BatchActionsHeader'
|
|
17
|
-
import {
|
|
18
|
-
import {
|
|
18
|
+
import {SortAscIcon, SortDescIcon, SortIcon} from './Icons'
|
|
19
|
+
import {RenderTableBody} from './RenderTableBody'
|
|
19
20
|
import {
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
21
|
+
StyledLimitBox,
|
|
22
|
+
StyledLimitMenu,
|
|
23
|
+
StyledPagination,
|
|
24
|
+
StyledTableFooter,
|
|
24
25
|
} from './styles'
|
|
25
|
-
import {
|
|
26
|
-
import {
|
|
26
|
+
import {TableStats} from './TableFooter/TableFooter'
|
|
27
|
+
import {ColumnProps, Sort, TableProps} from './types'
|
|
27
28
|
|
|
28
29
|
type ReactTableCell = {
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
30
|
+
Header: any
|
|
31
|
+
accessor: any
|
|
32
|
+
Cell?: any
|
|
33
|
+
sort?: boolean
|
|
33
34
|
} & ColumnProps
|
|
34
35
|
|
|
35
36
|
const selectColumn = {
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
37
|
+
id: 'selection',
|
|
38
|
+
Header: ({getToggleAllPageRowsSelectedProps}) => (
|
|
39
|
+
<Checkbox size='small' {...getToggleAllPageRowsSelectedProps()} />
|
|
40
|
+
),
|
|
41
|
+
Cell: ({row}) => (
|
|
42
|
+
<Checkbox size='small' {...row.getToggleRowSelectedProps()} />
|
|
43
|
+
),
|
|
43
44
|
}
|
|
44
45
|
|
|
45
46
|
const getTableCol = (headerItem: ColumnProps) => {
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
47
|
+
let col: ReactTableCell = {
|
|
48
|
+
Header: headerItem?.title,
|
|
49
|
+
accessor: headerItem?.dataIndex,
|
|
50
|
+
...headerItem,
|
|
51
|
+
}
|
|
51
52
|
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
53
|
+
if (headerItem?.render) {
|
|
54
|
+
return {
|
|
55
|
+
...col,
|
|
56
|
+
Cell: ({row}) =>
|
|
57
|
+
headerItem?.render(
|
|
58
|
+
row?.original[headerItem?.dataIndex],
|
|
59
|
+
row?.original,
|
|
60
|
+
row?.index
|
|
61
|
+
),
|
|
62
|
+
}
|
|
63
|
+
}
|
|
64
|
+
return col
|
|
64
65
|
}
|
|
65
66
|
|
|
66
67
|
export default function TableComponent({
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
68
|
+
columns,
|
|
69
|
+
dataSource,
|
|
70
|
+
loading,
|
|
71
|
+
pagination,
|
|
72
|
+
refetching,
|
|
73
|
+
onSort,
|
|
74
|
+
select = {
|
|
75
|
+
enable: false,
|
|
76
|
+
},
|
|
76
77
|
}: TableProps) {
|
|
77
|
-
|
|
78
|
+
const [sort, setSort] = useState<Sort>({})
|
|
78
79
|
|
|
79
|
-
|
|
80
|
-
|
|
80
|
+
const headers = useMemo(() => {
|
|
81
|
+
if (!columns) return []
|
|
81
82
|
|
|
82
|
-
|
|
83
|
-
|
|
83
|
+
return columns?.map((item) => getTableCol(item))
|
|
84
|
+
}, [columns])
|
|
84
85
|
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
86
|
+
const tableData = useMemo(() => {
|
|
87
|
+
if (!dataSource) return []
|
|
88
|
+
return [...dataSource]
|
|
89
|
+
}, [dataSource])
|
|
89
90
|
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
91
|
+
const {
|
|
92
|
+
getTableProps,
|
|
93
|
+
getTableBodyProps,
|
|
94
|
+
headerGroups,
|
|
95
|
+
rows,
|
|
96
|
+
prepareRow,
|
|
97
|
+
pageCount,
|
|
98
|
+
gotoPage,
|
|
99
|
+
setPageSize,
|
|
100
|
+
selectedFlatRows,
|
|
101
|
+
toggleAllRowsSelected,
|
|
102
|
+
state: {pageIndex, pageSize, selectedRowIds},
|
|
103
|
+
} = useTable(
|
|
104
|
+
{
|
|
105
|
+
columns: headers || [],
|
|
106
|
+
data: tableData || [],
|
|
107
|
+
stateReducer: (newState, action) => {
|
|
108
|
+
switch (action.type) {
|
|
109
|
+
case 'toggleAllRowsSelected':
|
|
110
|
+
return {
|
|
111
|
+
...newState,
|
|
112
|
+
selectedRowIds: {},
|
|
113
|
+
}
|
|
113
114
|
|
|
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
|
-
|
|
115
|
+
default:
|
|
116
|
+
return newState
|
|
117
|
+
}
|
|
118
|
+
},
|
|
119
|
+
initialState: {
|
|
120
|
+
pageIndex: pagination?.page ?? 0,
|
|
121
|
+
pageSize: pagination?.limit ?? 10,
|
|
122
|
+
},
|
|
123
|
+
manualPagination: true,
|
|
124
|
+
autoResetSelectedRows: false,
|
|
125
|
+
pageCount: pagination?.totalCount ?? dataSource?.length,
|
|
126
|
+
getRowId: select?.enable
|
|
127
|
+
? (row) => {
|
|
128
|
+
return row?.id
|
|
129
|
+
}
|
|
130
|
+
: undefined,
|
|
131
|
+
},
|
|
132
|
+
// useSortBy,
|
|
133
|
+
usePagination,
|
|
134
|
+
useRowSelect,
|
|
135
|
+
(hooks) => {
|
|
136
|
+
if (!select.enable) return
|
|
137
|
+
hooks.visibleColumns.push((columns) => [selectColumn, ...columns])
|
|
138
|
+
}
|
|
139
|
+
)
|
|
139
140
|
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
141
|
+
const handleSortClick = (sortBykey) => {
|
|
142
|
+
setSort((prev) => {
|
|
143
|
+
if (prev[sortBykey]) {
|
|
144
|
+
if (prev[sortBykey] === 'desc') return {...prev, [sortBykey]: 'asc'}
|
|
145
|
+
if (prev[sortBykey] === 'asc') {
|
|
146
|
+
delete prev[sortBykey]
|
|
147
|
+
return {...prev}
|
|
148
|
+
}
|
|
149
|
+
} else {
|
|
150
|
+
return {
|
|
151
|
+
...prev,
|
|
152
|
+
[sortBykey]: 'desc',
|
|
153
|
+
}
|
|
154
|
+
}
|
|
155
|
+
})
|
|
156
|
+
}
|
|
157
|
+
useEffect(() => {
|
|
158
|
+
if (!onSort) return
|
|
158
159
|
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
160
|
+
onSort({
|
|
161
|
+
sortBy: Object.keys(sort).join(','),
|
|
162
|
+
sortOrder: Object.keys(sort)
|
|
163
|
+
.map((item) => sort[item])
|
|
164
|
+
.join(','),
|
|
165
|
+
})
|
|
166
|
+
}, [sort])
|
|
166
167
|
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
168
|
+
const handlePagination = (e, value) => {
|
|
169
|
+
gotoPage(value - 1)
|
|
170
|
+
}
|
|
170
171
|
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
172
|
+
useEffect(() => {
|
|
173
|
+
if (!pagination) return
|
|
174
|
+
pagination.onChange(pageIndex * pageSize)
|
|
175
|
+
}, [pageIndex])
|
|
175
176
|
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
177
|
+
useEffect(() => {
|
|
178
|
+
if (!pagination) return
|
|
179
|
+
pagination.onChangeLimit(pageSize)
|
|
180
|
+
pagination.onChange(0)
|
|
181
|
+
}, [pageSize])
|
|
181
182
|
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
183
|
+
const clearSelection = () => {
|
|
184
|
+
toggleAllRowsSelected(false)
|
|
185
|
+
}
|
|
185
186
|
|
|
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
|
-
|
|
187
|
+
return (
|
|
188
|
+
<ErrorBoundary>
|
|
189
|
+
<BatchActionsHeader
|
|
190
|
+
actions={select?.actions || []}
|
|
191
|
+
selectedRowIds={selectedRowIds}
|
|
192
|
+
clearSelection={clearSelection}
|
|
193
|
+
/>
|
|
194
|
+
<Table sx={{position: 'relative'}} {...getTableProps()}>
|
|
195
|
+
<TableHead>
|
|
196
|
+
{headerGroups.map((headerGroup, index) => (
|
|
197
|
+
<TableRow key={index} {...headerGroup.getHeaderGroupProps()}>
|
|
198
|
+
{headerGroup.headers.map((column: any, index) => (
|
|
199
|
+
<TableCell key={index} {...column.getHeaderProps()}>
|
|
200
|
+
{column.render('Header')}
|
|
201
|
+
{column.sort && (
|
|
202
|
+
<IconButton onClick={() => handleSortClick(column.id)}>
|
|
203
|
+
<ListItemIcon>
|
|
204
|
+
{sort[column.id] === 'asc' ? (
|
|
205
|
+
<SortAscIcon />
|
|
206
|
+
) : sort[column.id] === 'desc' ? (
|
|
207
|
+
<SortDescIcon />
|
|
208
|
+
) : (
|
|
209
|
+
<SortIcon />
|
|
210
|
+
)}
|
|
211
|
+
</ListItemIcon>
|
|
212
|
+
</IconButton>
|
|
213
|
+
)}
|
|
214
|
+
</TableCell>
|
|
215
|
+
))}
|
|
216
|
+
</TableRow>
|
|
217
|
+
))}
|
|
218
|
+
</TableHead>
|
|
219
|
+
<RenderTableBody
|
|
220
|
+
colLength={columns?.length}
|
|
221
|
+
loading={loading || refetching}
|
|
222
|
+
currentRows={rows?.length}
|
|
223
|
+
getTableBodyProps={getTableBodyProps}
|
|
224
|
+
rows={rows}
|
|
225
|
+
prepareRow={prepareRow}
|
|
226
|
+
/>
|
|
227
|
+
</Table>
|
|
228
|
+
{pagination && dataSource && !loading && (
|
|
229
|
+
<StyledTableFooter>
|
|
230
|
+
<TableStats
|
|
231
|
+
limit={pageSize}
|
|
232
|
+
page={pageIndex + 1}
|
|
233
|
+
totalCount={pageCount}
|
|
234
|
+
/>
|
|
235
|
+
<StyledPagination
|
|
236
|
+
variant='outlined'
|
|
237
|
+
shape='rounded'
|
|
238
|
+
onChange={handlePagination}
|
|
239
|
+
count={Math.ceil(pageCount / pageSize)}
|
|
240
|
+
page={pageIndex + 1}
|
|
241
|
+
/>
|
|
242
|
+
<Limit pageSize={pageSize} setPageSize={setPageSize} />
|
|
243
|
+
</StyledTableFooter>
|
|
244
|
+
)}
|
|
245
|
+
</ErrorBoundary>
|
|
246
|
+
)
|
|
246
247
|
}
|
|
247
248
|
|
|
248
|
-
const Limit = ({
|
|
249
|
-
|
|
250
|
-
|
|
249
|
+
const Limit = ({pageSize, setPageSize}) => {
|
|
250
|
+
const [anchorEl, setAnchorEl] = useState<null | HTMLElement>(null)
|
|
251
|
+
const open = Boolean(anchorEl)
|
|
251
252
|
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
253
|
+
const handleClick = (event) => {
|
|
254
|
+
setAnchorEl(event.currentTarget)
|
|
255
|
+
}
|
|
255
256
|
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
|
|
257
|
+
const handleClose = () => {
|
|
258
|
+
setAnchorEl(null)
|
|
259
|
+
}
|
|
260
|
+
const menuItems = [
|
|
261
|
+
{label: '10', value: 10},
|
|
262
|
+
{label: '20', value: 20},
|
|
263
|
+
{label: '50', value: 50},
|
|
264
|
+
{label: '100', value: 100},
|
|
265
|
+
]
|
|
265
266
|
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
|
|
282
|
-
|
|
283
|
-
|
|
284
|
-
|
|
285
|
-
|
|
286
|
-
|
|
287
|
-
|
|
288
|
-
|
|
289
|
-
|
|
290
|
-
|
|
291
|
-
|
|
292
|
-
|
|
267
|
+
return (
|
|
268
|
+
<StyledLimitBox open={open}>
|
|
269
|
+
<Box onClick={handleClick}>
|
|
270
|
+
<Typography>{`${pageSize} / Page`}</Typography>
|
|
271
|
+
<ExpandMore />
|
|
272
|
+
</Box>
|
|
273
|
+
<StyledLimitMenu
|
|
274
|
+
id='basic-menu'
|
|
275
|
+
anchorEl={anchorEl}
|
|
276
|
+
open={open}
|
|
277
|
+
onClose={handleClose}
|
|
278
|
+
>
|
|
279
|
+
{menuItems?.map((item, index) => (
|
|
280
|
+
<MenuItem
|
|
281
|
+
onClick={() => {
|
|
282
|
+
setPageSize(item?.value)
|
|
283
|
+
handleClose()
|
|
284
|
+
}}
|
|
285
|
+
value={item?.value}
|
|
286
|
+
key={index}
|
|
287
|
+
>
|
|
288
|
+
{item?.label}
|
|
289
|
+
</MenuItem>
|
|
290
|
+
))}
|
|
291
|
+
</StyledLimitMenu>
|
|
292
|
+
</StyledLimitBox>
|
|
293
|
+
)
|
|
293
294
|
}
|
|
@@ -1,184 +1,189 @@
|
|
|
1
1
|
import {
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
2
|
+
alpha,
|
|
3
|
+
Box,
|
|
4
|
+
IconButton,
|
|
5
|
+
ListItemIcon,
|
|
6
|
+
styled,
|
|
7
|
+
Table as MuiTable,
|
|
8
|
+
TableBody,
|
|
9
|
+
TableCell,
|
|
10
|
+
TableContainer,
|
|
11
|
+
TableHead,
|
|
12
|
+
TableRow,
|
|
13
|
+
Typography,
|
|
14
14
|
} from '@mui/material'
|
|
15
15
|
import _ from 'lodash'
|
|
16
|
-
import {
|
|
16
|
+
import {useEffect, useState} from 'react'
|
|
17
|
+
import ErrorBoundary from '../ErrorBoundary'
|
|
17
18
|
import Spinner from '../Spinner'
|
|
18
|
-
import {
|
|
19
|
+
import {SortAscIcon, SortDescIcon, SortIcon} from './Icons'
|
|
19
20
|
import TableFooter from './TableFooter'
|
|
20
21
|
|
|
21
22
|
export interface ColumnProps {
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
23
|
+
dataIndex: string
|
|
24
|
+
key: string
|
|
25
|
+
title: any
|
|
26
|
+
render?: (cellData: any, row: any, index: number) => any
|
|
27
|
+
textColor?: string
|
|
28
|
+
sort?: boolean
|
|
29
|
+
width?: string //pixels
|
|
29
30
|
}
|
|
30
31
|
|
|
31
32
|
interface TableProps {
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
33
|
+
columns: Array<ColumnProps>
|
|
34
|
+
rowKey?: string
|
|
35
|
+
dataSource?: any[]
|
|
36
|
+
loading?: boolean
|
|
37
|
+
onRowClick?: (row: any) => void
|
|
38
|
+
pagination?: {
|
|
39
|
+
page: number
|
|
40
|
+
limit: number
|
|
41
|
+
totalCount: number
|
|
42
|
+
onChange: (v: number) => void
|
|
43
|
+
onChangeLimit?: (v: number) => void
|
|
44
|
+
}
|
|
45
|
+
onSort?: (sort: any) => void
|
|
46
|
+
dense?: boolean
|
|
46
47
|
}
|
|
47
48
|
|
|
48
49
|
type Order = 'asc' | 'desc' | ''
|
|
49
50
|
type Sort = {}
|
|
50
51
|
|
|
51
52
|
export default function Table({
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
53
|
+
columns,
|
|
54
|
+
dataSource,
|
|
55
|
+
onRowClick,
|
|
56
|
+
pagination,
|
|
57
|
+
loading,
|
|
58
|
+
onSort,
|
|
59
|
+
dense = false,
|
|
59
60
|
}: TableProps) {
|
|
60
|
-
|
|
61
|
+
const [sort, setSort] = useState<Sort>({})
|
|
61
62
|
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
63
|
+
const handleSortClick = (sortBykey) => {
|
|
64
|
+
setSort((prev) => {
|
|
65
|
+
if (prev[sortBykey]) {
|
|
66
|
+
if (prev[sortBykey] === 'desc') return {...prev, [sortBykey]: 'asc'}
|
|
67
|
+
if (prev[sortBykey] === 'asc') {
|
|
68
|
+
delete prev[sortBykey]
|
|
69
|
+
return {...prev}
|
|
70
|
+
}
|
|
71
|
+
} else {
|
|
72
|
+
return {
|
|
73
|
+
...prev,
|
|
74
|
+
[sortBykey]: 'desc',
|
|
75
|
+
}
|
|
76
|
+
}
|
|
77
|
+
})
|
|
78
|
+
}
|
|
78
79
|
|
|
79
|
-
|
|
80
|
-
|
|
80
|
+
useEffect(() => {
|
|
81
|
+
if (!onSort) return
|
|
81
82
|
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
83
|
+
onSort({
|
|
84
|
+
sortBy: Object.keys(sort).join(','),
|
|
85
|
+
sortOrder: Object.keys(sort)
|
|
86
|
+
.map((item) => sort[item])
|
|
87
|
+
.join(','),
|
|
88
|
+
})
|
|
89
|
+
}, [sort])
|
|
89
90
|
|
|
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
|
-
|
|
91
|
+
return (
|
|
92
|
+
<ErrorBoundary>
|
|
93
|
+
<TableContainer>
|
|
94
|
+
<>
|
|
95
|
+
<MuiTable>
|
|
96
|
+
<TableHead>
|
|
97
|
+
<TableRow>
|
|
98
|
+
{columns.map((col, index) => (
|
|
99
|
+
<TableCell
|
|
100
|
+
key={index}
|
|
101
|
+
sx={{
|
|
102
|
+
...(col?.width && {
|
|
103
|
+
width: col?.width,
|
|
104
|
+
}),
|
|
105
|
+
}}
|
|
106
|
+
>
|
|
107
|
+
<>
|
|
108
|
+
<Box>{col.title}</Box>
|
|
109
|
+
</>
|
|
110
|
+
{col.sort && (
|
|
111
|
+
<IconButton
|
|
112
|
+
onClick={() => handleSortClick(col.dataIndex)}
|
|
113
|
+
>
|
|
114
|
+
<ListItemIcon>
|
|
115
|
+
{sort[col.dataIndex] === 'asc' ? (
|
|
116
|
+
<SortAscIcon />
|
|
117
|
+
) : sort[col.dataIndex] === 'desc' ? (
|
|
118
|
+
<SortDescIcon />
|
|
119
|
+
) : (
|
|
120
|
+
<SortIcon />
|
|
121
|
+
)}
|
|
122
|
+
</ListItemIcon>
|
|
123
|
+
</IconButton>
|
|
124
|
+
)}
|
|
125
|
+
</TableCell>
|
|
126
|
+
))}
|
|
127
|
+
</TableRow>
|
|
128
|
+
</TableHead>
|
|
129
|
+
{!loading ? (
|
|
130
|
+
<>
|
|
131
|
+
{dataSource?.length ? (
|
|
132
|
+
<TableBody>
|
|
133
|
+
{dataSource?.map((row, index) => (
|
|
134
|
+
<StyledTableRow
|
|
135
|
+
canRowClick={!!onRowClick}
|
|
136
|
+
hover={!!onRowClick}
|
|
137
|
+
key={index}
|
|
138
|
+
onClick={() => {
|
|
139
|
+
return onRowClick && onRowClick(row)
|
|
140
|
+
}}
|
|
141
|
+
>
|
|
142
|
+
{columns.map((col, colIndex) => (
|
|
143
|
+
<TableCell
|
|
144
|
+
sx={{
|
|
145
|
+
color: col.textColor,
|
|
146
|
+
padding: dense ? '10px' : '15px',
|
|
147
|
+
}}
|
|
148
|
+
key={colIndex}
|
|
149
|
+
>
|
|
150
|
+
<>
|
|
151
|
+
{col?.render
|
|
152
|
+
? col.render(row[col.dataIndex], row, index)
|
|
153
|
+
: _.get(row, col.dataIndex)}
|
|
154
|
+
</>
|
|
155
|
+
</TableCell>
|
|
156
|
+
))}
|
|
157
|
+
</StyledTableRow>
|
|
158
|
+
))}
|
|
159
|
+
</TableBody>
|
|
160
|
+
) : (
|
|
161
|
+
<Box>
|
|
162
|
+
<Typography sx={{padding: '10px'}}>
|
|
163
|
+
No Records Found !..
|
|
164
|
+
</Typography>
|
|
165
|
+
</Box>
|
|
166
|
+
)}
|
|
167
|
+
</>
|
|
168
|
+
) : (
|
|
169
|
+
<Spinner />
|
|
170
|
+
)}
|
|
171
|
+
</MuiTable>
|
|
172
|
+
</>
|
|
173
|
+
<>
|
|
174
|
+
{pagination && (
|
|
175
|
+
<TableFooter
|
|
176
|
+
page={pagination.page}
|
|
177
|
+
limit={pagination.limit}
|
|
178
|
+
totalCount={pagination.totalCount}
|
|
179
|
+
handlePagination={pagination.onChange}
|
|
180
|
+
handlePageLimit={pagination.onChangeLimit}
|
|
181
|
+
/>
|
|
182
|
+
)}
|
|
183
|
+
</>
|
|
184
|
+
</TableContainer>
|
|
185
|
+
</ErrorBoundary>
|
|
186
|
+
)
|
|
182
187
|
}
|
|
183
188
|
|
|
184
189
|
// const StyledSortButton = styled(IconButton)<{
|
|
@@ -207,13 +212,13 @@ export default function Table({
|
|
|
207
212
|
// }),
|
|
208
213
|
// }))
|
|
209
214
|
|
|
210
|
-
const StyledTableRow = styled(TableRow)<{
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
215
|
+
const StyledTableRow = styled(TableRow)<{canRowClick: boolean}>(
|
|
216
|
+
({theme, canRowClick}) => ({
|
|
217
|
+
...(canRowClick && {
|
|
218
|
+
cursor: 'pointer',
|
|
219
|
+
'&.MuiTableRow-hover:hover': {
|
|
220
|
+
backgroundColor: alpha('#f2f2f2', 0.4),
|
|
221
|
+
},
|
|
222
|
+
}),
|
|
223
|
+
})
|
|
219
224
|
)
|