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