@campxdev/shared 0.3.1 → 0.3.3

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.3",
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: '16px', padding: '16px'}}>
25
25
  <StyledAlert severity='error'>
26
26
  {error?.message}
27
27
  <Button
@@ -1,293 +1,293 @@
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
+ <>
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
+ 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
+ )
246
246
  }
247
247
 
248
- const Limit = ({ pageSize, setPageSize }) => {
249
- const [anchorEl, setAnchorEl] = useState<null | HTMLElement>(null)
250
- const open = Boolean(anchorEl)
248
+ const Limit = ({pageSize, setPageSize}) => {
249
+ const [anchorEl, setAnchorEl] = useState<null | HTMLElement>(null)
250
+ const open = Boolean(anchorEl)
251
251
 
252
- const handleClick = (event) => {
253
- setAnchorEl(event.currentTarget)
254
- }
252
+ const handleClick = (event) => {
253
+ setAnchorEl(event.currentTarget)
254
+ }
255
255
 
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
- ]
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
+ ]
265
265
 
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
- )
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
+ )
293
293
  }
@@ -1,65 +1,64 @@
1
- import { Box, TableBody, TableCell, Typography } from '@mui/material'
1
+ import {Box, TableBody, TableCell, Typography} from '@mui/material'
2
2
  import Spinner from '../Spinner'
3
3
  import noDataImage from './no-data-illu.svg'
4
- import { StyledTableRow } from './styles'
4
+ import {StyledTableRow} from './styles'
5
5
 
6
6
  export const RenderTableBody = ({
7
- getTableBodyProps,
8
- prepareRow,
9
- rows,
10
- loading,
11
- currentRows,
12
- colLength,
7
+ getTableBodyProps,
8
+ prepareRow,
9
+ rows,
10
+ loading,
11
+ colLength,
13
12
  }) => {
14
- if (loading)
15
- return (
16
- <TableCell colSpan={colLength}>
17
- <Spinner />
18
- </TableCell>
19
- )
13
+ if (loading)
14
+ return (
15
+ <TableCell colSpan={colLength}>
16
+ <Spinner />
17
+ </TableCell>
18
+ )
20
19
 
21
- if (!loading && currentRows === 0)
22
- return (
23
- <TableCell colSpan={colLength}>
24
- <Box
25
- sx={{
26
- display: 'flex',
27
- flexDirection: 'column',
28
- alignItems: 'center',
29
- justifyContent: 'center',
30
- }}
31
- >
32
- <img
33
- style={{ width: '200px', height: 'auto', textAlign: 'center' }}
34
- src={noDataImage}
35
- />
36
- <Typography variant="h6" align="center">
37
- No Records Found
38
- </Typography>
39
- </Box>
40
- </TableCell>
41
- )
20
+ if (!loading && rows?.length < 1)
21
+ return (
22
+ <TableCell colSpan={colLength}>
23
+ <Box
24
+ sx={{
25
+ display: 'flex',
26
+ flexDirection: 'column',
27
+ alignItems: 'center',
28
+ justifyContent: 'center',
29
+ }}
30
+ >
31
+ <img
32
+ style={{width: '200px', height: 'auto', textAlign: 'center'}}
33
+ src={noDataImage}
34
+ />
35
+ <Typography variant='h6' align='center'>
36
+ No Records Found
37
+ </Typography>
38
+ </Box>
39
+ </TableCell>
40
+ )
42
41
 
43
- return (
44
- <TableBody {...getTableBodyProps()}>
45
- {rows?.map((row, index) => {
46
- prepareRow(row)
47
- return (
48
- <StyledTableRow
49
- key={index}
50
- {...row.getRowProps()}
51
- isSelected={row?.isSelected}
52
- >
53
- {row.cells?.map((cell, index) => {
54
- return (
55
- <TableCell key={index} {...cell.getCellProps()}>
56
- {cell?.render('Cell')}
57
- </TableCell>
58
- )
59
- })}
60
- </StyledTableRow>
61
- )
62
- })}
63
- </TableBody>
64
- )
42
+ return (
43
+ <TableBody {...getTableBodyProps()}>
44
+ {rows?.map((row, index) => {
45
+ prepareRow(row)
46
+ return (
47
+ <StyledTableRow
48
+ key={index}
49
+ {...row.getRowProps()}
50
+ isSelected={row?.isSelected}
51
+ >
52
+ {row.cells?.map((cell, index) => {
53
+ return (
54
+ <TableCell key={index} {...cell.getCellProps()}>
55
+ {cell?.render('Cell')}
56
+ </TableCell>
57
+ )
58
+ })}
59
+ </StyledTableRow>
60
+ )
61
+ })}
62
+ </TableBody>
63
+ )
65
64
  }
@@ -1,184 +1,186 @@
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
17
  import Spinner from '../Spinner'
18
- import { SortAscIcon, SortDescIcon, SortIcon } from './Icons'
18
+ import {SortAscIcon, SortDescIcon, SortIcon} from './Icons'
19
19
  import TableFooter from './TableFooter'
20
20
 
21
21
  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
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
29
29
  }
30
30
 
31
31
  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
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
46
46
  }
47
47
 
48
48
  type Order = 'asc' | 'desc' | ''
49
49
  type Sort = {}
50
50
 
51
51
  export default function Table({
52
- columns,
53
- dataSource,
54
- onRowClick,
55
- pagination,
56
- loading,
57
- onSort,
58
- dense = false,
52
+ columns,
53
+ dataSource,
54
+ onRowClick,
55
+ pagination,
56
+ loading,
57
+ onSort,
58
+ dense = false,
59
59
  }: TableProps) {
60
- const [sort, setSort] = useState<Sort>({})
60
+ const [sort, setSort] = useState<Sort>({})
61
61
 
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
- }
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
+ }
78
78
 
79
- useEffect(() => {
80
- if (!onSort) return
79
+ useEffect(() => {
80
+ if (!onSort) return
81
81
 
82
- onSort({
83
- sortBy: Object.keys(sort).join(','),
84
- sortOrder: Object.keys(sort)
85
- .map((item) => sort[item])
86
- .join(','),
87
- })
88
- }, [sort])
82
+ onSort({
83
+ sortBy: Object.keys(sort).join(','),
84
+ sortOrder: Object.keys(sort)
85
+ .map((item) => sort[item])
86
+ .join(','),
87
+ })
88
+ }, [sort])
89
89
 
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
- )
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
+ {dataSource && (
159
+ <Typography sx={{padding: '10px'}}>
160
+ No Records Found !..
161
+ </Typography>
162
+ )}
163
+ </Box>
164
+ )}
165
+ </>
166
+ ) : (
167
+ <Spinner />
168
+ )}
169
+ </MuiTable>
170
+ </>
171
+ <>
172
+ {pagination && (
173
+ <TableFooter
174
+ page={pagination.page}
175
+ limit={pagination.limit}
176
+ totalCount={pagination.totalCount}
177
+ handlePagination={pagination.onChange}
178
+ handlePageLimit={pagination.onChangeLimit}
179
+ />
180
+ )}
181
+ </>
182
+ </TableContainer>
183
+ )
182
184
  }
183
185
 
184
186
  // const StyledSortButton = styled(IconButton)<{
@@ -207,13 +209,13 @@ export default function Table({
207
209
  // }),
208
210
  // }))
209
211
 
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
- }),
212
+ const StyledTableRow = styled(TableRow)<{canRowClick: boolean}>(
213
+ ({theme, canRowClick}) => ({
214
+ ...(canRowClick && {
215
+ cursor: 'pointer',
216
+ '&.MuiTableRow-hover:hover': {
217
+ backgroundColor: alpha('#f2f2f2', 0.4),
218
+ },
219
+ }),
220
+ })
219
221
  )