-
Notifications
You must be signed in to change notification settings - Fork 7
/
index.tsx
147 lines (135 loc) · 3.92 KB
/
index.tsx
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
import { FC, ReactNode } from 'react'
import { styled } from '@mui/material/styles'
import Box from '@mui/material/Box'
import Skeleton from '@mui/material/Skeleton'
import TableContainer from '@mui/material/TableContainer'
import MuiTable from '@mui/material/Table'
import TableHead from '@mui/material/TableHead'
import TableBody from '@mui/material/TableBody'
import TableRow, { TableRowProps as MuiTableRowProps } from '@mui/material/TableRow'
import TableCell from '@mui/material/TableCell'
import { useScreenSize } from '../../hooks/useScreensize'
import { COLORS } from '../../../styles/theme/colors'
import { TablePagination, TablePaginationProps } from './TablePagination'
import { backgroundColorAnimation } from '../../../styles/theme/animations'
type SkeletonTableRowsProps = {
rowsNumber: number
columnsNumber: number
}
// in designs table row has 61px height, but we need to take into account border and padding here
const textSkeletonHeight = 28
const SkeletonTableRows: FC<SkeletonTableRowsProps> = ({ rowsNumber, columnsNumber }) => (
<>
{[...Array(rowsNumber)].map((item, index) => (
<TableRow key={index}>
<TableCell colSpan={columnsNumber}>
<Skeleton variant="text" height={textSkeletonHeight} />
</TableCell>
</TableRow>
))}
</>
)
type StyledTableRowProps = MuiTableRowProps & {
highlight?: boolean
}
const StyledTableRow = styled(TableRow, {
shouldForwardProp: prop => prop !== 'highlight',
})<StyledTableRowProps>(({ highlight }) => ({
...(highlight && backgroundColorAnimation),
}))
export enum TableCellAlign {
Center = 'center',
Left = 'left',
Right = 'right',
}
type TableCellProps = {
align?: TableCellAlign
content: ReactNode
key: string
}
export type TableRowProps = {
key: string
data: TableCellProps[]
highlight?: boolean
}
export type TableColProps = {
content: string
align?: TableCellAlign
width?: string
}
type TableProps = {
columns: TableColProps[]
name: string
isLoading: boolean
pagination: false | TablePaginationProps
rows?: TableRowProps[]
rowsNumber?: number
stickyColumn?: boolean
}
const stickyColumnStyles = {
position: 'sticky',
left: 0,
backgroundColor: COLORS.antiFlashWhite,
zIndex: 1,
}
export const Table: FC<TableProps> = ({
columns,
isLoading,
name,
pagination,
rows,
rowsNumber = 5,
stickyColumn = false,
}) => {
const { isMobile } = useScreenSize()
if (!isLoading && !rows?.length) {
return null
}
return (
<>
<TableContainer>
<MuiTable aria-label={name}>
<TableHead>
<TableRow>
{columns.map((column, index) => (
<TableCell
key={column.content}
align={column.align}
sx={{
width: column.width || 'auto',
...(stickyColumn && !index && isMobile ? stickyColumnStyles : {}),
}}
>
{column.content}
</TableCell>
))}
</TableRow>
</TableHead>
<TableBody>
{!rows && isLoading && (
<SkeletonTableRows rowsNumber={rowsNumber} columnsNumber={columns.length} />
)}
{rows?.map(row => (
<StyledTableRow key={row.key} highlight={row.highlight}>
{row.data.map((cell, index) => (
<TableCell
key={cell.key}
align={cell.align}
sx={stickyColumn && !index && isMobile ? stickyColumnStyles : undefined}
>
{cell.content}
</TableCell>
))}
</StyledTableRow>
))}
</TableBody>
</MuiTable>
</TableContainer>
{pagination && (
<Box sx={{ display: 'flex', justifyContent: 'center' }}>
<TablePagination {...pagination} />
</Box>
)}
</>
)
}