-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathindex.tsx
More file actions
165 lines (159 loc) · 5.99 KB
/
index.tsx
File metadata and controls
165 lines (159 loc) · 5.99 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
import {
ColumnDef,
flexRender,
getCoreRowModel,
getSortedRowModel,
OnChangeFn,
RowSelectionState,
SortingState,
Row as TanstackRow,
Table as TanstackTable,
useReactTable,
} from '@tanstack/react-table'
import classNames from 'classnames'
import React, { ReactElement } from 'react'
import Card from 'components/common/Card'
import { SortAsc, SortDesc, SortNone } from 'components/common/Icons'
import Row from 'components/common/Table/Row'
import Text from 'components/common/Text'
import { LEFT_ALIGNED_ROWS } from 'constants/table'
import ConditionalWrapper from 'hocs/ConditionalWrapper'
interface Props<T> {
title: string | ReactElement
columns: ColumnDef<T>[]
data: T[]
initialSorting: SortingState
renderExpanded?: (row: TanstackRow<T>, table: TanstackTable<T>) => ReactElement
tableBodyClassName?: string
spacingClassName?: string
type?: TableType
hideCard?: boolean
setRowSelection?: OnChangeFn<RowSelectionState>
selectedRows?: RowSelectionState
onClickRow?: (id: string) => void
onSortingChange?: OnChangeFn<SortingState>
disableSortingRow?: boolean
titleComponent?: ReactElement
isBalancesTable?: boolean
}
export default function Table<T>(props: Props<T>) {
const [internalSorting, setInternalSorting] = React.useState<SortingState>(props.initialSorting)
const sorting = props.onSortingChange ? props.initialSorting : internalSorting
const onSortingChange = props.onSortingChange ?? setInternalSorting
// eslint-disable-next-line react-hooks/incompatible-library
const table = useReactTable({
data: props.data,
columns: props.columns,
state: {
sorting,
rowSelection: props.selectedRows,
},
enableRowSelection: true,
onRowSelectionChange: props.setRowSelection,
onSortingChange,
getCoreRowModel: getCoreRowModel(),
getSortedRowModel: getSortedRowModel(),
})
return (
<ConditionalWrapper
condition={!props.hideCard}
wrapper={(children) => (
<Card
className={classNames('w-full', props.type !== 'balances' && 'h-fit')}
contentClassName='max-w-full overflow-x-auto scrollbar-hide'
title={props.title}
>
{children}
</Card>
)}
>
<div className='w-full'>
<div className='w-full overflow-x-auto scrollbar-hide'>
<table className={classNames('w-[calc(100%-1px)]', props?.tableBodyClassName)}>
{!props.disableSortingRow && (
<thead className='bg-surface-dark border-b border-white/10'>
{table.getHeaderGroups().map((headerGroup) => (
<tr key={headerGroup.id}>
{headerGroup.headers.map((header) => {
return (
<th
key={header.id}
onClick={header.column.getToggleSortingHandler()}
className={classNames(
props.spacingClassName ?? 'px-4 py-2',
header.column.getCanSort() && 'hover:cursor-pointer',
LEFT_ALIGNED_ROWS.includes(header.id) ? 'text-left' : 'text-right',
header.column.columnDef.meta?.className,
)}
>
<div
className={classNames(
'flex',
LEFT_ALIGNED_ROWS.includes(header.id)
? 'justify-start'
: 'justify-end',
'align-center relative',
)}
>
<Text
tag='span'
size='xs'
className='flex items-center font-normal text-white/60'
>
{flexRender(header.column.columnDef.header, header.getContext())}
</Text>
{header.column.getCanSort() && (
<span
className={classNames(
'w-5 h-5 my-auto text-white',
!LEFT_ALIGNED_ROWS.includes(header.id) &&
'absolute -mr-4.5 -translate-y-1/2 top-1/2',
)}
>
{header.column.getCanSort()
? ({
asc: <SortAsc size={16} />,
desc: <SortDesc />,
false: <SortNone className='opacity-20' />,
}[header.column.getIsSorted() as string] ?? null)
: null}
</span>
)}
</div>
</th>
)
})}
</tr>
))}
</thead>
)}
{props.titleComponent && (
<tbody>
<tr>
<td colSpan={table.getAllColumns().length} className='p-0'>
{props.titleComponent}
</td>
</tr>
</tbody>
)}
<tbody>
{table.getRowModel().rows.map((row) => (
<Row
key={row.id}
row={row}
table={table}
renderExpanded={props.renderExpanded}
spacingClassName={props.spacingClassName}
isSelectable={!!props.setRowSelection}
type={props.type}
onClick={props.onClickRow}
isBalancesTable={props.isBalancesTable}
/>
))}
</tbody>
</table>
</div>
</div>
</ConditionalWrapper>
)
}