@bsol-oss/react-datatable5 1.0.0 → 1.0.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/index.js +217 -0
- package/package.json +3 -2
- package/src/index.tsx +1 -1
package/dist/index.js
ADDED
|
@@ -0,0 +1,217 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
var jsxRuntime = require('react/jsx-runtime');
|
|
4
|
+
var react = require('react');
|
|
5
|
+
var reactTable = require('@tanstack/react-table');
|
|
6
|
+
var axios = require('axios');
|
|
7
|
+
var react$1 = require('@chakra-ui/react');
|
|
8
|
+
var io = require('react-icons/io');
|
|
9
|
+
var table = require('@chakra-ui/table');
|
|
10
|
+
var md = require('react-icons/md');
|
|
11
|
+
var icons = require('@chakra-ui/icons');
|
|
12
|
+
|
|
13
|
+
const TableContext = react.createContext({
|
|
14
|
+
table: undefined,
|
|
15
|
+
refreshData: () => { },
|
|
16
|
+
});
|
|
17
|
+
|
|
18
|
+
const useDataFromUrl = ({ url, params = {}, defaultData, }) => {
|
|
19
|
+
const [loading, setLoading] = react.useState(true);
|
|
20
|
+
const [hasError, setHasError] = react.useState(false);
|
|
21
|
+
const [data, setData] = react.useState(defaultData);
|
|
22
|
+
const refreshData = async () => {
|
|
23
|
+
await getData();
|
|
24
|
+
};
|
|
25
|
+
const getData = async () => {
|
|
26
|
+
try {
|
|
27
|
+
setLoading(true);
|
|
28
|
+
const { data } = await axios.get(url, { params: params });
|
|
29
|
+
console.log("get DataFromUrl success", data);
|
|
30
|
+
setLoading(false);
|
|
31
|
+
setData(data);
|
|
32
|
+
}
|
|
33
|
+
catch (e) {
|
|
34
|
+
console.log(e);
|
|
35
|
+
setLoading(false);
|
|
36
|
+
setHasError(true);
|
|
37
|
+
}
|
|
38
|
+
};
|
|
39
|
+
react.useEffect(() => {
|
|
40
|
+
getData().catch((e) => {
|
|
41
|
+
console.error(e);
|
|
42
|
+
});
|
|
43
|
+
}, []);
|
|
44
|
+
return { data, loading, hasError, refreshData };
|
|
45
|
+
};
|
|
46
|
+
|
|
47
|
+
const DataTable = ({ columns, url, children }) => {
|
|
48
|
+
const [sorting, setSorting] = react.useState([]);
|
|
49
|
+
const [columnFilters, setColumnFilters] = react.useState([]); // can set initial column filter state here
|
|
50
|
+
const [pagination, setPagination] = react.useState({
|
|
51
|
+
pageIndex: 0, //initial page index
|
|
52
|
+
pageSize: 10, //default page size
|
|
53
|
+
});
|
|
54
|
+
const { data, loading, hasError, refreshData } = useDataFromUrl({
|
|
55
|
+
url: url,
|
|
56
|
+
defaultData: {
|
|
57
|
+
success: false,
|
|
58
|
+
results: [],
|
|
59
|
+
count: 0,
|
|
60
|
+
filterCount: 0,
|
|
61
|
+
},
|
|
62
|
+
params: {
|
|
63
|
+
pagination: JSON.stringify({
|
|
64
|
+
offset: pagination.pageIndex * pagination.pageSize,
|
|
65
|
+
rows: pagination.pageSize,
|
|
66
|
+
}),
|
|
67
|
+
sorting: JSON.stringify(sorting.length > 0
|
|
68
|
+
? { field: sorting[0].id, sort: sorting[0].desc ? "desc" : "asc" }
|
|
69
|
+
: {}),
|
|
70
|
+
where: JSON.stringify(columnFilters.reduce((accumulator, filter) => {
|
|
71
|
+
const obj = {};
|
|
72
|
+
obj[filter.id] = filter.value;
|
|
73
|
+
return { ...accumulator, ...obj };
|
|
74
|
+
}, {})),
|
|
75
|
+
},
|
|
76
|
+
});
|
|
77
|
+
const table = reactTable.useReactTable({
|
|
78
|
+
data: data.results,
|
|
79
|
+
columns: columns,
|
|
80
|
+
getCoreRowModel: reactTable.getCoreRowModel(),
|
|
81
|
+
// getPaginationRowModel: getPaginationRowModel(),
|
|
82
|
+
manualPagination: true,
|
|
83
|
+
manualSorting: true,
|
|
84
|
+
onPaginationChange: setPagination,
|
|
85
|
+
onSortingChange: setSorting,
|
|
86
|
+
onColumnFiltersChange: setColumnFilters,
|
|
87
|
+
columnResizeMode: "onChange",
|
|
88
|
+
state: {
|
|
89
|
+
pagination,
|
|
90
|
+
sorting,
|
|
91
|
+
columnFilters,
|
|
92
|
+
},
|
|
93
|
+
defaultColumn: {
|
|
94
|
+
size: 10, //starting column size
|
|
95
|
+
minSize: 10, //enforced during column resizing
|
|
96
|
+
maxSize: 10000, //enforced during column resizing
|
|
97
|
+
},
|
|
98
|
+
});
|
|
99
|
+
react.useEffect(() => {
|
|
100
|
+
refreshData();
|
|
101
|
+
}, [pagination, sorting, columnFilters]);
|
|
102
|
+
return (jsxRuntime.jsx(TableContext.Provider, { value: { table: { ...table }, refreshData: refreshData }, children: children }));
|
|
103
|
+
};
|
|
104
|
+
|
|
105
|
+
const EditViewButton = () => {
|
|
106
|
+
const { table } = react.useContext(TableContext);
|
|
107
|
+
return (jsxRuntime.jsxs(react$1.Popover, { placement: "bottom-end", children: [jsxRuntime.jsx(react$1.PopoverTrigger, { children: jsxRuntime.jsx(react$1.IconButton, { "aria-label": "view", icon: jsxRuntime.jsx(io.IoMdEye, {}) }) }), jsxRuntime.jsxs(react$1.PopoverContent, { width: "auto", children: [jsxRuntime.jsx(react$1.PopoverArrow, {}), jsxRuntime.jsx(react$1.PopoverBody, { children: jsxRuntime.jsx(react$1.Flex, { flexFlow: "column", gap: "1rem", children: table.getAllLeafColumns().map((column) => {
|
|
108
|
+
return (jsxRuntime.jsx(react$1.FormControl, { width: "auto", children: jsxRuntime.jsx(react$1.Checkbox, { isChecked: column.getIsVisible(), onChange: column.getToggleVisibilityHandler(), children: column.id }) }, crypto.randomUUID()));
|
|
109
|
+
}) }) })] })] }));
|
|
110
|
+
};
|
|
111
|
+
|
|
112
|
+
const PageSizeControl = ({ pageSizes = [10, 20, 30, 40, 50], }) => {
|
|
113
|
+
const { table } = react.useContext(TableContext);
|
|
114
|
+
return (jsxRuntime.jsx("select", { value: table.getState().pagination.pageSize, onChange: (e) => {
|
|
115
|
+
table.setPageSize(Number(e.target.value));
|
|
116
|
+
}, children: pageSizes.map((pageSize) => (jsxRuntime.jsx("option", { value: pageSize, children: pageSize }, pageSize))) }));
|
|
117
|
+
};
|
|
118
|
+
|
|
119
|
+
const ResetFilteringButton = () => {
|
|
120
|
+
const { table } = react.useContext(TableContext);
|
|
121
|
+
return (jsxRuntime.jsx(react$1.Button, { onClick: () => {
|
|
122
|
+
table.resetColumnFilters();
|
|
123
|
+
}, children: "Reset Filtering" }));
|
|
124
|
+
};
|
|
125
|
+
|
|
126
|
+
const ResetSortingButton = () => {
|
|
127
|
+
const { table } = react.useContext(TableContext);
|
|
128
|
+
return (jsxRuntime.jsx(react$1.Button, { onClick: () => {
|
|
129
|
+
table.resetSorting();
|
|
130
|
+
}, children: "Reset Sorting" }));
|
|
131
|
+
};
|
|
132
|
+
|
|
133
|
+
const useDataTable = () => {
|
|
134
|
+
const { table, refreshData } = react.useContext(TableContext);
|
|
135
|
+
return { table, refreshData };
|
|
136
|
+
};
|
|
137
|
+
|
|
138
|
+
const Table = ({ children }) => {
|
|
139
|
+
const { table } = useDataTable();
|
|
140
|
+
return (jsxRuntime.jsx(react$1.Container, { maxW: "100%", overflowY: "scroll", children: jsxRuntime.jsx(react$1.Table, { width: table.getCenterTotalSize(), variant: "simple", children: children }) }));
|
|
141
|
+
};
|
|
142
|
+
|
|
143
|
+
const TableBody = () => {
|
|
144
|
+
const { table: table$1 } = react.useContext(TableContext);
|
|
145
|
+
return (jsxRuntime.jsx(table.Tbody, { children: table$1.getRowModel().rows.map((row) => (jsxRuntime.jsx(table.Tr, { children: row.getVisibleCells().map((cell) => (jsxRuntime.jsx(table.Td, { width: `${cell.column.getSize()}px`, children: reactTable.flexRender(cell.column.columnDef.cell, cell.getContext()) }, crypto.randomUUID()))) }, crypto.randomUUID()))) }));
|
|
146
|
+
};
|
|
147
|
+
|
|
148
|
+
const TableFilter = () => {
|
|
149
|
+
const { table } = useDataTable();
|
|
150
|
+
return (jsxRuntime.jsx(jsxRuntime.Fragment, { children: table.getLeafHeaders().map((header) => {
|
|
151
|
+
return (jsxRuntime.jsx(jsxRuntime.Fragment, { children: header.column.getCanFilter() && (jsxRuntime.jsxs(react$1.Box, { children: [jsxRuntime.jsx(react$1.Text, { children: header.column.id }), jsxRuntime.jsx(react$1.Input, { value: header.column.getFilterValue()
|
|
152
|
+
? String(header.column.getFilterValue())
|
|
153
|
+
: "", onChange: (e) => {
|
|
154
|
+
header.column.setFilterValue(e.target.value);
|
|
155
|
+
} })] })) }));
|
|
156
|
+
}) }));
|
|
157
|
+
};
|
|
158
|
+
|
|
159
|
+
const TableFooter = () => {
|
|
160
|
+
const table = useDataTable().table;
|
|
161
|
+
return (jsxRuntime.jsx(react$1.Tfoot, { children: table.getFooterGroups().map((footerGroup) => (jsxRuntime.jsx(react$1.Tr, { children: footerGroup.headers.map((header) => (jsxRuntime.jsx(react$1.Th, { colSpan: header.colSpan, children: header.isPlaceholder
|
|
162
|
+
? null
|
|
163
|
+
: reactTable.flexRender(header.column.columnDef.footer, header.getContext()) }, crypto.randomUUID()))) }, crypto.randomUUID()))) }));
|
|
164
|
+
};
|
|
165
|
+
|
|
166
|
+
const TableHeader = ({ canResize }) => {
|
|
167
|
+
const { table } = useDataTable();
|
|
168
|
+
return (jsxRuntime.jsx(react$1.Thead, { children: table.getHeaderGroups().map((headerGroup) => (jsxRuntime.jsx(react$1.Tr, { style: { columnSpan: "all" }, children: headerGroup.headers.map((header) => {
|
|
169
|
+
const resizeProps = {
|
|
170
|
+
onClick: () => header.column.resetSize(),
|
|
171
|
+
onMouseDown: header.getResizeHandler(),
|
|
172
|
+
onTouchStart: header.getResizeHandler(),
|
|
173
|
+
cursor: "col-resize",
|
|
174
|
+
};
|
|
175
|
+
return (jsxRuntime.jsx(react$1.Th, { padding: "0rem", colSpan: header.colSpan, width: `${header.getSize()}px`, children: jsxRuntime.jsxs(react$1.Flex, { alignItems: "center", gap: "0.5rem", padding: "0.5rem", children: [jsxRuntime.jsx(react$1.Box, { children: header.isPlaceholder
|
|
176
|
+
? null
|
|
177
|
+
: reactTable.flexRender(header.column.columnDef.header, header.getContext()) }), header.column.getCanSort() && (jsxRuntime.jsxs(jsxRuntime.Fragment, { children: [jsxRuntime.jsxs(react$1.Button, { onClick: (e) => {
|
|
178
|
+
header.column.toggleSorting();
|
|
179
|
+
}, children: [header.column.getNextSortingOrder() === false && (
|
|
180
|
+
// <Text>To No sort</Text>
|
|
181
|
+
jsxRuntime.jsx(icons.ChevronUpIcon, {})), header.column.getNextSortingOrder() === "asc" && (
|
|
182
|
+
// <Text>To asc</Text>
|
|
183
|
+
jsxRuntime.jsx(icons.UpDownIcon, {})), header.column.getNextSortingOrder() === "desc" && (
|
|
184
|
+
// <Text>To desc</Text>
|
|
185
|
+
jsxRuntime.jsx(icons.ChevronDownIcon, {}))] }), header.column.getIsSorted() && (jsxRuntime.jsx(react$1.Button, { onClick: (e) => {
|
|
186
|
+
header.column.clearSorting();
|
|
187
|
+
}, children: jsxRuntime.jsx(icons.CloseIcon, {}) }))] })), header.column.getIsFiltered() && jsxRuntime.jsx(md.MdFilterListAlt, {}), canResize && (jsxRuntime.jsx(react$1.Box, { borderRight: header.column.getIsResizing()
|
|
188
|
+
? "0.25rem solid black"
|
|
189
|
+
: "0.25rem solid grey", height: "5rem", width: "5px", userSelect: "none", style: { touchAction: "none" }, ...resizeProps }))] }) }, crypto.randomUUID()));
|
|
190
|
+
}) }, crypto.randomUUID()))) }));
|
|
191
|
+
};
|
|
192
|
+
|
|
193
|
+
const TablePagination = (props) => {
|
|
194
|
+
const { firstPage, getCanPreviousPage, previousPage, getState, nextPage, getCanNextPage, lastPage, } = useDataTable().table;
|
|
195
|
+
return (jsxRuntime.jsxs(react$1.ButtonGroup, { isAttached: true, children: [jsxRuntime.jsx(react$1.IconButton, { icon: jsxRuntime.jsx(md.MdFirstPage, {}), onClick: () => firstPage(), disabled: !getCanPreviousPage(), "aria-label": "first-page" }), jsxRuntime.jsx(react$1.IconButton, { icon: jsxRuntime.jsx(md.MdArrowBack, {}), onClick: () => previousPage(), disabled: !getCanPreviousPage(), "aria-label": "previous-page" }), jsxRuntime.jsx(react$1.Button, { onClick: () => { }, disabled: !getCanPreviousPage(), children: getState().pagination.pageIndex + 1 }), jsxRuntime.jsx(react$1.IconButton, { onClick: () => nextPage(), disabled: !getCanNextPage(), "aria-label": "next-page", children: jsxRuntime.jsx(md.MdArrowForward, {}) }), jsxRuntime.jsx(react$1.IconButton, { onClick: () => lastPage(), disabled: !getCanNextPage(), "aria-label": "last-page", children: jsxRuntime.jsx(md.MdLastPage, {}) })] }));
|
|
196
|
+
};
|
|
197
|
+
|
|
198
|
+
const TextCell = ({ label, children }) => {
|
|
199
|
+
return (jsxRuntime.jsx(react$1.Tooltip, { label: label, children: jsxRuntime.jsx(react$1.Text, { as: "span", overflow: "hidden", textOverflow: "ellipsis", noOfLines: [1, 2, 3], children: children }) }));
|
|
200
|
+
};
|
|
201
|
+
|
|
202
|
+
var index = {
|
|
203
|
+
DataTable,
|
|
204
|
+
EditViewButton,
|
|
205
|
+
PageSizeControl,
|
|
206
|
+
ResetFilteringButton,
|
|
207
|
+
ResetSortingButton,
|
|
208
|
+
Table,
|
|
209
|
+
TableBody,
|
|
210
|
+
TableFilter,
|
|
211
|
+
TableFooter,
|
|
212
|
+
TableHeader,
|
|
213
|
+
TablePagination,
|
|
214
|
+
TextCell,
|
|
215
|
+
};
|
|
216
|
+
|
|
217
|
+
module.exports = index;
|
package/package.json
CHANGED
|
@@ -1,9 +1,10 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@bsol-oss/react-datatable5",
|
|
3
|
-
"version": "1.0.
|
|
3
|
+
"version": "1.0.2",
|
|
4
4
|
"type": "module",
|
|
5
|
+
"main": "dist/index.js",
|
|
5
6
|
"repository": "https://github.com/bsol-oss/react-datatable5.git",
|
|
6
|
-
"homepage": "https://github.com/bsol-oss/react-datatabl5#react-
|
|
7
|
+
"homepage": "https://github.com/bsol-oss/react-datatabl5#react-datatable5",
|
|
7
8
|
"author": "screw123",
|
|
8
9
|
"license": "MIT",
|
|
9
10
|
"keywords": [
|
package/src/index.tsx
CHANGED