-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathDataTable.tsx
178 lines (169 loc) · 5.42 KB
/
DataTable.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
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
// @ts-nocheck
import React, { useState, useMemo, useEffect } from 'react';
import { useTranslation } from 'react-i18next';
import {
useGlobalFilter,
usePagination,
useSortBy,
useTable,
} from 'react-table';
import DataPagination from './DataPagination';
interface TableData {
data: any[];
columns: any;
title: string;
loading?: boolean;
className?: string;
}
function DataTable({ data, columns, title, loading, className }: TableData) {
const [pageIndex, setPageIndex] = useState(0);
const [filterInput, setFilterInput] = useState('');
const { t } = useTranslation();
// Memoize columns and data to prevent unnecessary re-renders
const memoizedColumns = useMemo(() => [...columns], [columns]);
const memoizedData = useMemo(() => [...data], [data]);
// Table instance
const tableInstance = useTable(
{
data: memoizedData,
columns: memoizedColumns,
initialState: { pageIndex, pageSize: 3, globalFilter: filterInput },
},
useGlobalFilter,
useSortBy,
usePagination,
);
const {
getTableProps,
setGlobalFilter,
getTableBodyProps,
page,
nextPage,
previousPage,
canPreviousPage,
canNextPage,
gotoPage,
pageCount,
setPageSize,
pageOptions,
headerGroups,
prepareRow,
state: { pageIndex: currentPageIndex, pageSize },
} = tableInstance;
useEffect(() => {
setPageIndex(currentPageIndex);
}, [currentPageIndex]);
const handleFilterChange = (e) => {
const value = e.target.value || '';
setGlobalFilter(value);
setFilterInput(value);
};
return (
<div
className={`relative font-serif bg-indigo-100 dark:bg-dark-bg shadow-lg h-fit px-5 md:py-4 lg:py-5 rounded-md w-[100%] overflow-auto custom-scrollbar "lg:ml-60 mx-auto"} lg:mb-10 ${className}`}
>
<div className="flex flex-col md:flex-row items-center justify-between pb-6 space-y-4 md:space-y-0">
<div>
<h2 className="text-lg md:text-xl font-semibold text-gray-800 dark:text-white">
{t(title)}
</h2>
{/* Uncomment if you want a filter input */}
{/* <input
value={filterInput}
aria-label="Filter table data"
placeholder="Filter"
className="px-4 py-2 mt-4 font-sans text-xs md:text-sm border rounded outline-none border-primary dark:bg-neutral-600 dark:text-white w-full sm:w-52 md:w-96"
onChange={handleFilterChange}
/> */}
</div>
</div>
<div className="overflow-x-auto custom-scrollbar">
<table
className="min-w-full leading-normal text-xs md:text-sm"
{...getTableProps()}
>
<thead>
{headerGroups.map((headerGroup) => (
<tr {...headerGroup.getHeaderGroupProps()} key={headerGroup.id}>
{headerGroup.headers.map((column) => (
<th
className={`thead w-1/${columns.length} text-center ${
column.isSorted ? 'sort-asc' : ''
}`}
{...column.getHeaderProps(column.getSortByToggleProps())}
key={column.id}
>
{column.render('Header')}
</th>
))}
</tr>
))}
</thead>
<tbody {...getTableBodyProps()}>
{memoizedData.length ? (
!loading &&
page.map((row) => {
prepareRow(row);
return (
<tr
className={`border-b dark:border-gray-700 ${
row.index % 2 === 0
? 'bg-gray-600 dark:bg-neutral-600'
: 'bg-transparent'
}`}
{...row.getRowProps()}
key={row.id}
>
{row.cells.map((cell) => (
<td
className={`w-1/${columns.length} data-cell px-4 py-2 text-center`}
{...cell.getCellProps()}
key={cell.column.id}
>
{cell.render('Cell')}
</td>
))}
</tr>
);
})
) : (
<tr>
<td colSpan={columns.length} className="p-4 text-center h-24">
<p className="text-sm md:text-lg font-medium text-gray-600 dark:text-gray-400">
No records available
</p>
</td>
</tr>
)}
{loading && (
<tr>
<td
colSpan={columns.length}
className="px-6 py-4 text-sm text-center text-gray-500 dark:text-gray-300"
>
Loading...
</td>
</tr>
)}
</tbody>
</table>
</div>
<div className="px-4 md:px-6 py-4">
<DataPagination
pageOptions={pageOptions}
canNextPage={canNextPage}
gotoPage={gotoPage}
columnLength={columns.length}
canPreviousPage={canPreviousPage}
pageSize={pageSize}
setPageSize={setPageSize}
previousPage={previousPage}
nextPage={nextPage}
pageCount={pageCount}
pageIndex={pageIndex}
/>
</div>
</div>
);
}
export default DataTable;