-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathDataTable.jsx
More file actions
207 lines (197 loc) · 6.05 KB
/
DataTable.jsx
File metadata and controls
207 lines (197 loc) · 6.05 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
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
/* eslint-disable no-nested-ternary */
import React from "react";
import PropTypes from "prop-types";
import classnames from "classnames";
import { Table } from "reactstrap";
// react-table doc: https://react-table-v7-docs.netlify.app/
import { useTable, useMountedLayoutEffect } from "react-table";
import { FaSort, FaSortUp, FaSortDown } from "react-icons/fa";
import {
makeTableArgs,
defaultConfig,
defaultInitialState,
defaultColumn
} from "./utils";
import Paginator from "./Paginator";
/**
* Suitable when data is already available client side. Thus, pagination/filtering/sorting can be performed client side too.
*/
function DataTable({
config: userConfig,
onSelectedRowChange,
SubComponent,
tableProps,
tableEmptyNode,
TableBodyComponent,
...rest
}) {
// merge user specified config with default config
const config = React.useMemo(
() => ({ ...defaultConfig, ...userConfig, }),
[userConfig]
);
const tableArgs = React.useMemo(() => makeTableArgs(config), [config]);
const {
getTableProps,
getTableBodyProps,
headerGroups,
footerGroups,
prepareRow,
visibleColumns,
page,
gotoPage,
pageOptions,
selectedFlatRows,
state: { pageIndex, },
} = useTable(
{
defaultColumn,
initialState: defaultInitialState,
autoResetPage: false,
autoResetExpanded: false,
autoResetGroupBy: false,
// autoResetSelectedRows: false,
autoResetSortBy: false,
autoResetFilters: false,
autoResetRowState: false,
...rest,
},
...tableArgs
);
useMountedLayoutEffect(() => {
if (onSelectedRowChange) {
if (selectedFlatRows?.length) {
const originalRows = selectedFlatRows.map((r) => r.original);
onSelectedRowChange(originalRows);
} else {
onSelectedRowChange([]);
}
}
}, [onSelectedRowChange, selectedFlatRows]);
const footerAvailable = footerGroups[0]?.headers.filter(h => h.Footer.length).length !== 0;
// Use the state and functions returned from useTable to build your UI
return (
<div>
<Table striped hover responsive="xl" {...getTableProps(tableProps)}>
{/* Table Head */}
<thead>
{headerGroups.map((headerGroup) => (
<tr className="head-row" {...headerGroup.getHeaderGroupProps()}>
{headerGroup.headers.map((column) => (
<th
className={column.isSorted ? "text-primary" : ""}
{...column.getHeaderProps()}
>
<div
className="text-center"
{...(column.canSort ? column.getSortByToggleProps() : {})}
>
{column.render("Header")}
{column.canSort &&
(column.isSorted ? (
column.isSortedDesc ? (
<FaSortDown />
) : (
<FaSortUp />
)
) : (
<FaSort className="text-muted small" />
))}
</div>
<div className="d-flex mt-1">
{column.canFilter && column.render("Filter")}
</div>
</th>
))}
</tr>
))}
</thead>
{/* Table Body */}
<tbody {...getTableBodyProps()}>
{page?.length ? (
!TableBodyComponent ? (
page.map((row) => {
prepareRow(row);
const { key, ...rowProps } = row.getRowProps();
return (
<React.Fragment key={key}>
{/* table row */}
<tr
{...rowProps}
className={classnames(rowProps.className, {
"row-selected": row.isSelected,
})}
>
{row.cells.map((cell) => (
<td className="text-center" {...cell.getCellProps()}>
{cell.render("Cell")}
</td>
))}
</tr>
{/* SubComponent */}
{SubComponent && config?.enableExpanded && row?.isExpanded && (
<tr>
<td colSpan={visibleColumns.length}>
<SubComponent row={row} />
</td>
</tr>
)}
</React.Fragment>
);
})
) : (
<TableBodyComponent page={page} />
)
) : (
<tr>
<td
colSpan={visibleColumns.length}
className="text-large fw-bold text-center"
>
{tableEmptyNode}
</td>
</tr>
)}
</tbody>
{/* Table Footer */}
{footerAvailable &&
<tfoot>
{footerGroups.map(group => (
<tr {...group.getFooterGroupProps()}>
{group.headers.map(column => (
<th {...column.getFooterProps()}>{column.render("Footer")}</th>
))}
</tr>
))}
</tfoot>
}
</Table>
{/* Paginator */}
{pageOptions.length > 1 && (
<Paginator
pageIndex={pageIndex}
pageOptions={pageOptions}
onPaginate={gotoPage}
className="table-paginator"
/>
)}
</div>
);
}
DataTable.propTypes = {
config: PropTypes.object,
onSelectedRowChange: PropTypes.func,
SubComponent: PropTypes.oneOfType([PropTypes.func, PropTypes.elementType]),
tableProps: PropTypes.object,
tableEmptyNode: PropTypes.oneOfType([PropTypes.string, PropTypes.node]),
TableBodyComponent: PropTypes.func,
};
DataTable.defaultProps = {
config: defaultConfig,
onSelectedRowChange: undefined,
SubComponent: undefined,
tableProps: undefined,
tableEmptyNode: "No Data",
TableBodyComponent: undefined,
};
export default DataTable;