-
Notifications
You must be signed in to change notification settings - Fork 13
Expand file tree
/
Copy pathindex.tsx
More file actions
172 lines (162 loc) · 4.73 KB
/
index.tsx
File metadata and controls
172 lines (162 loc) · 4.73 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
import { ComponentProps, FC, ReactNode } from 'react'
import { styled } from '@mui/material/styles'
import { Skeleton } from '@oasisprotocol/ui-library/src/components/ui/skeleton'
import {
Table as BaseTable,
TableHeader,
TableHead,
TableBody,
TableRow,
TableCell,
} from '@oasisprotocol/ui-library/src/components/table'
import { TablePagination, TablePaginationProps } from './TablePagination'
import { backgroundColorAnimation } from '../../../styles/theme/animations'
import { CardEmptyState } from '../CardEmptyState'
import { cn } from '@oasisprotocol/ui-library/src/lib/utils'
type SkeletonTableRowsProps = {
rowsNumber: number
columnsNumber: number
}
const SkeletonTableRows: FC<SkeletonTableRowsProps> = ({ rowsNumber, columnsNumber }) => (
<>
{[...Array(rowsNumber)].map((item, index) => (
<TableRow key={index}>
<TableCell colSpan={columnsNumber}>
{/* In designs table row has 61px height, but we need to take into account border and padding here */}
<Skeleton className="h-[28px]" />
</TableCell>
</TableRow>
))}
</>
)
type StyledTableRowProps = ComponentProps<typeof TableRow> & {
highlight?: boolean
backgroundColor?: string
}
const StyledTableRow = styled(TableRow, {
shouldForwardProp: prop => prop !== 'highlight' && prop !== 'backgroundColor',
})<StyledTableRowProps>(({ backgroundColor, highlight }) => ({
...(highlight && backgroundColorAnimation),
...(backgroundColor && { backgroundColor }),
}))
export enum TableCellAlign {
Center = 'center',
Left = 'left',
Right = 'right',
}
const getAlignClass = (align?: TableCellAlign): string => {
switch (align) {
case TableCellAlign.Center:
return 'text-center'
case TableCellAlign.Right:
return 'text-right'
case TableCellAlign.Left:
default:
return 'text-left'
}
}
type TableCellProps = {
align?: TableCellAlign
content: ReactNode
hide?: boolean
key: string
}
export type TableRowProps = {
key: string
data: TableCellProps[]
highlight?: boolean
backgroundColor?: string
}
export type TableColProps = {
align?: TableCellAlign
content: ReactNode
hide?: boolean
key: string
width?: string
}
type TableProps = {
columns: TableColProps[]
name: string
isLoading: boolean
pagination: false | TablePaginationProps
rows?: TableRowProps[]
rowsNumber?: number
extraHorizontalSpaceOnMobile?: boolean | undefined
alwaysWaitWhileLoading?: boolean | undefined
/**
* Optional message to display when there are zero entries, instead of the table.
*
* This will only be displayed when loading finishes and there are no records.
* If not given, the table will simply be hidden without any trace.
*/
emptyMessage?: string | undefined
}
export const Table: FC<TableProps> = ({
columns,
isLoading,
name,
pagination,
rows,
rowsNumber = 5,
extraHorizontalSpaceOnMobile = false,
alwaysWaitWhileLoading = false,
emptyMessage = undefined,
}) => {
return !isLoading && !rows?.length ? ( // This is known to be empty
emptyMessage ? ( // If we have a message, show it
<CardEmptyState label={emptyMessage} />
) : null // otherwise just show nothing
) : (
<>
<BaseTable aria-label={name}>
<TableHeader>
<TableRow>
{columns.map((column, index) => {
if (column.hide) {
return null
}
return (
<TableHead
key={column.key}
className={getAlignClass(column.align)}
style={{
width: column.width || 'auto',
}}
>
{column.content}
</TableHead>
)
})}
</TableRow>
</TableHeader>
<TableBody>
{(alwaysWaitWhileLoading || !rows) && isLoading && (
<SkeletonTableRows rowsNumber={rowsNumber} columnsNumber={columns.length} />
)}
{rows?.map(row => (
<StyledTableRow key={row.key} highlight={row.highlight} backgroundColor={row.backgroundColor}>
{row.data.map((cell, index) => {
if (cell.hide) {
return null
}
return (
<TableCell
key={cell.key}
className={cn(getAlignClass(cell.align), extraHorizontalSpaceOnMobile && 'sm:px-8')}
>
{cell.content}
</TableCell>
)
})}
</StyledTableRow>
))}
</TableBody>
</BaseTable>
{pagination && (
<div className="flex justify-center">
<TablePagination {...pagination} />
</div>
)}
</>
)
}