@bsol-oss/react-datatable5 2.1.0 → 3.0.0
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.d.ts +68 -61
- package/dist/index.js +118 -69
- package/dist/index.mjs +118 -70
- package/dist/types/components/DataTable/DataTableServer.d.ts +4 -11
- package/dist/types/components/DataTable/DefaultTable.d.ts +1 -1
- package/dist/types/components/DataTable/TableControls.d.ts +2 -1
- package/dist/types/components/DataTable/useDataFromUrl.d.ts +3 -3
- package/dist/types/components/DataTable/useDataTable.d.ts +1 -1
- package/dist/types/components/DataTable/useDataTableServer.d.ts +16 -0
- package/dist/types/index.d.ts +1 -0
- package/package.json +1 -1
package/dist/index.d.ts
CHANGED
|
@@ -122,9 +122,70 @@ interface DataTableProps<TData> {
|
|
|
122
122
|
}
|
|
123
123
|
declare const DataTable: <TData>({ columns, data, enableRowSelection, enableMultiRowSelection, enableSubRowSelection, onRowSelect, columnOrder, columnFilters, columnVisibility, density, globalFilter, pagination, sorting, rowSelection, setPagination, setSorting, setColumnFilters, setRowSelection, setGlobalFilter, setColumnOrder, setDensity, setColumnVisibility, children, }: DataTableProps<TData>) => react_jsx_runtime.JSX.Element;
|
|
124
124
|
|
|
125
|
-
interface
|
|
126
|
-
|
|
125
|
+
interface UseDataFromUrlReturn<T> {
|
|
126
|
+
data: T;
|
|
127
|
+
loading: boolean;
|
|
128
|
+
hasError: boolean;
|
|
129
|
+
refreshData: () => void;
|
|
130
|
+
}
|
|
131
|
+
interface UseDataFromUrlProps<T> {
|
|
127
132
|
url: string;
|
|
133
|
+
params?: object;
|
|
134
|
+
defaultData: T;
|
|
135
|
+
disableFirstFetch?: boolean;
|
|
136
|
+
onFetchSuccess?: (data: T) => void;
|
|
137
|
+
}
|
|
138
|
+
declare const useDataFromUrl: <T>({ url, params, disableFirstFetch, onFetchSuccess, defaultData, }: UseDataFromUrlProps<T>) => UseDataFromUrlReturn<T>;
|
|
139
|
+
|
|
140
|
+
interface UseDataTableProps {
|
|
141
|
+
default?: {
|
|
142
|
+
sorting?: SortingState;
|
|
143
|
+
columnFilters?: ColumnFiltersState;
|
|
144
|
+
pagination?: PaginationState;
|
|
145
|
+
rowSelection?: RowSelectionState;
|
|
146
|
+
columnOrder?: ColumnOrderState;
|
|
147
|
+
globalFilter?: string;
|
|
148
|
+
columnVisibility?: VisibilityState;
|
|
149
|
+
density?: DensityState;
|
|
150
|
+
};
|
|
151
|
+
}
|
|
152
|
+
interface UseDataTableReturn {
|
|
153
|
+
sorting: SortingState;
|
|
154
|
+
columnFilters: ColumnFiltersState;
|
|
155
|
+
pagination: PaginationState;
|
|
156
|
+
rowSelection: RowSelectionState;
|
|
157
|
+
columnOrder: ColumnOrderState;
|
|
158
|
+
globalFilter: string;
|
|
159
|
+
columnVisibility: VisibilityState;
|
|
160
|
+
density: DensityState;
|
|
161
|
+
setPagination: OnChangeFn<PaginationState>;
|
|
162
|
+
setSorting: OnChangeFn<SortingState>;
|
|
163
|
+
setColumnFilters: OnChangeFn<ColumnFiltersState>;
|
|
164
|
+
setRowSelection: OnChangeFn<RowSelectionState>;
|
|
165
|
+
setGlobalFilter: OnChangeFn<string>;
|
|
166
|
+
setColumnOrder: OnChangeFn<ColumnOrderState>;
|
|
167
|
+
setDensity: OnChangeFn<DensityState>;
|
|
168
|
+
setColumnVisibility: OnChangeFn<VisibilityState>;
|
|
169
|
+
}
|
|
170
|
+
declare const useDataTable: ({ default: { sorting: defaultSorting, pagination: defaultPagination, rowSelection: defaultRowSelection, columnFilters: defaultColumnFilters, columnOrder: defaultColumnOrder, columnVisibility: defaultColumnVisibility, globalFilter: defaultGlobalFilter, density: defaultDensity, }, }?: UseDataTableProps) => UseDataTableReturn;
|
|
171
|
+
|
|
172
|
+
interface UseDataTableServerProps<TData> extends Omit<UseDataFromUrlProps<DataResponse<TData>>, keyof {
|
|
173
|
+
defaultData: any;
|
|
174
|
+
}>, UseDataTableProps {
|
|
175
|
+
}
|
|
176
|
+
interface UseDataTableServerReturn<TData> extends UseDataFromUrlReturn<DataResponse<TData>>, UseDataTableReturn {
|
|
177
|
+
}
|
|
178
|
+
interface Result<T> {
|
|
179
|
+
results: T[];
|
|
180
|
+
}
|
|
181
|
+
interface DataResponse<T> extends Result<T> {
|
|
182
|
+
success: boolean;
|
|
183
|
+
count: number;
|
|
184
|
+
}
|
|
185
|
+
declare const useDataTableServer: <TData>({ url, onFetchSuccess, default: { sorting: defaultSorting, pagination: defaultPagination, rowSelection: defaultRowSelection, columnFilters: defaultColumnFilters, columnOrder: defaultColumnOrder, columnVisibility: defaultColumnVisibility, globalFilter: defaultGlobalFilter, density: defaultDensity, }, }: UseDataTableServerProps<TData>) => UseDataTableServerReturn<TData>;
|
|
186
|
+
|
|
187
|
+
interface DataTableServerProps<TData> extends UseDataFromUrlReturn<DataResponse<TData>> {
|
|
188
|
+
children: JSX.Element | JSX.Element[];
|
|
128
189
|
columns: ColumnDef<TData, any>[];
|
|
129
190
|
enableRowSelection?: boolean;
|
|
130
191
|
enableMultiRowSelection?: boolean;
|
|
@@ -146,16 +207,8 @@ interface DataTableServerProps<TData> {
|
|
|
146
207
|
setColumnOrder: OnChangeFn<ColumnOrderState>;
|
|
147
208
|
setDensity: OnChangeFn<DensityState>;
|
|
148
209
|
setColumnVisibility: OnChangeFn<VisibilityState>;
|
|
149
|
-
onFetchSuccess?: (response: DataResponse<TData>) => void;
|
|
150
210
|
}
|
|
151
|
-
|
|
152
|
-
results: T[];
|
|
153
|
-
}
|
|
154
|
-
interface DataResponse<T> extends Result<T> {
|
|
155
|
-
success: boolean;
|
|
156
|
-
count: number;
|
|
157
|
-
}
|
|
158
|
-
declare const DataTableServer: <TData>({ columns, url, enableRowSelection, enableMultiRowSelection, enableSubRowSelection, onRowSelect, columnOrder, columnFilters, columnVisibility, density, globalFilter, pagination, sorting, rowSelection, setPagination, setSorting, setColumnFilters, setRowSelection, setGlobalFilter, setColumnOrder, setDensity, setColumnVisibility, onFetchSuccess, children, }: DataTableServerProps<TData>) => react_jsx_runtime.JSX.Element;
|
|
211
|
+
declare const DataTableServer: <TData>({ columns, enableRowSelection, enableMultiRowSelection, enableSubRowSelection, onRowSelect, columnOrder, columnFilters, columnVisibility, density, globalFilter, pagination, sorting, rowSelection, setPagination, setSorting, setColumnFilters, setRowSelection, setGlobalFilter, setColumnOrder, setDensity, setColumnVisibility, data, loading, hasError, refreshData, children, }: DataTableServerProps<TData>) => react_jsx_runtime.JSX.Element;
|
|
159
212
|
|
|
160
213
|
interface TableControlsProps {
|
|
161
214
|
totalText?: string;
|
|
@@ -168,13 +221,14 @@ interface TableControlsProps {
|
|
|
168
221
|
showFilterTags?: boolean;
|
|
169
222
|
showReload?: boolean;
|
|
170
223
|
filterOptions?: string[];
|
|
224
|
+
extraItems?: JSX.Element;
|
|
171
225
|
}
|
|
172
|
-
declare const TableControls: ({ totalText, showFilter, fitTableWidth, fitTableHeight, isMobile, children, showFilterName, showFilterTags, showReload, filterOptions, }: TableControlsProps) => react_jsx_runtime.JSX.Element;
|
|
226
|
+
declare const TableControls: ({ totalText, showFilter, fitTableWidth, fitTableHeight, isMobile, children, showFilterName, showFilterTags, showReload, filterOptions, extraItems, }: TableControlsProps) => react_jsx_runtime.JSX.Element;
|
|
173
227
|
|
|
174
228
|
interface DefaultTableProps extends TableControlsProps {
|
|
175
229
|
showFooter?: boolean;
|
|
176
230
|
}
|
|
177
|
-
declare const DefaultTable: ({ totalText, showFilter, showFooter, fitTableWidth, fitTableHeight, isMobile, filterOptions, showFilterTags, showFilterName, showReload, }: DefaultTableProps) => react_jsx_runtime.JSX.Element;
|
|
231
|
+
declare const DefaultTable: ({ totalText, showFilter, showFooter, fitTableWidth, fitTableHeight, isMobile, filterOptions, showFilterTags, showFilterName, showReload, extraItems, }: DefaultTableProps) => react_jsx_runtime.JSX.Element;
|
|
178
232
|
|
|
179
233
|
interface TableProps extends TableProps$1 {
|
|
180
234
|
showLoading?: boolean;
|
|
@@ -268,53 +322,6 @@ interface TextCellProps extends TextProps {
|
|
|
268
322
|
}
|
|
269
323
|
declare const TextCell: ({ label, noOfLines, padding, children, tooltipProps, ...props }: TextCellProps) => react_jsx_runtime.JSX.Element;
|
|
270
324
|
|
|
271
|
-
interface useDataFromUrlReturn<T> {
|
|
272
|
-
data: T;
|
|
273
|
-
loading: boolean;
|
|
274
|
-
hasError: boolean;
|
|
275
|
-
refreshData: () => void;
|
|
276
|
-
}
|
|
277
|
-
interface useDataFromUrlProps<T> {
|
|
278
|
-
url: string;
|
|
279
|
-
params?: object;
|
|
280
|
-
defaultData: T;
|
|
281
|
-
disableFirstFetch?: boolean;
|
|
282
|
-
onFetchSuccess?: (data: T) => void;
|
|
283
|
-
}
|
|
284
|
-
declare const useDataFromUrl: <T>({ url, params, disableFirstFetch, onFetchSuccess, defaultData, }: useDataFromUrlProps<T>) => useDataFromUrlReturn<T>;
|
|
285
|
-
|
|
286
|
-
interface UseDataTableProps {
|
|
287
|
-
default: {
|
|
288
|
-
sorting?: SortingState;
|
|
289
|
-
columnFilters?: ColumnFiltersState;
|
|
290
|
-
pagination?: PaginationState;
|
|
291
|
-
rowSelection?: RowSelectionState;
|
|
292
|
-
columnOrder?: ColumnOrderState;
|
|
293
|
-
globalFilter?: string;
|
|
294
|
-
columnVisibility?: VisibilityState;
|
|
295
|
-
density?: DensityState;
|
|
296
|
-
};
|
|
297
|
-
}
|
|
298
|
-
interface UseDataTableReturn {
|
|
299
|
-
sorting: SortingState;
|
|
300
|
-
columnFilters: ColumnFiltersState;
|
|
301
|
-
pagination: PaginationState;
|
|
302
|
-
rowSelection: RowSelectionState;
|
|
303
|
-
columnOrder: ColumnOrderState;
|
|
304
|
-
globalFilter: string;
|
|
305
|
-
columnVisibility: VisibilityState;
|
|
306
|
-
density: DensityState;
|
|
307
|
-
setPagination: OnChangeFn<PaginationState>;
|
|
308
|
-
setSorting: OnChangeFn<SortingState>;
|
|
309
|
-
setColumnFilters: OnChangeFn<ColumnFiltersState>;
|
|
310
|
-
setRowSelection: OnChangeFn<RowSelectionState>;
|
|
311
|
-
setGlobalFilter: OnChangeFn<string>;
|
|
312
|
-
setColumnOrder: OnChangeFn<ColumnOrderState>;
|
|
313
|
-
setDensity: OnChangeFn<DensityState>;
|
|
314
|
-
setColumnVisibility: OnChangeFn<VisibilityState>;
|
|
315
|
-
}
|
|
316
|
-
declare const useDataTable: ({ default: { sorting: defaultSorting, pagination: defaultPagination, rowSelection: defaultRowSelection, columnFilters: defaultColumnFilters, columnOrder: defaultColumnOrder, columnVisibility: defaultColumnVisibility, globalFilter: defaultGlobalFilter, density: defaultDensity, }, }?: UseDataTableProps) => UseDataTableReturn;
|
|
317
|
-
|
|
318
325
|
declare const useDataTableContext: () => {
|
|
319
326
|
table: _tanstack_table_core.Table<any>;
|
|
320
327
|
refreshData: () => void;
|
|
@@ -385,4 +392,4 @@ declare module "@tanstack/react-table" {
|
|
|
385
392
|
}
|
|
386
393
|
}
|
|
387
394
|
|
|
388
|
-
export { type DataResponse, DataTable, type DataTableProps, DataTableServer, type DataTableServerProps, DefaultTable, type DefaultTableProps, DensityToggleButton, type DensityToggleButtonProps, EditFilterButton, type EditFilterButtonProps, EditOrderButton, type EditOrderButtonProps, EditSortingButton, type EditSortingButtonProps, EditViewButton, type EditViewButtonProps, FilterOptions, type FilterOptionsProps, GlobalFilter, PageSizeControl, type PageSizeControlProps, type PaginationProps, ReloadButton, type ReloadButtonProps, ResetFilteringButton, type ResetFilteringButtonProps, ResetSelectionButton, type ResetSelectionButtonProps, ResetSortingButton, type ResetSortingButtonProps, type Result, RowCountText, Table, TableBody, type TableBodyProps, TableCardContainer, type TableCardContainerProps, TableCards, type TableCardsProps, TableComponent, TableControls, type TableControlsProps, TableFilter, TableFilterTags, TableFooter, type TableFooterProps, TableHeader, type TableHeaderProps, TableLoadingComponent, type TableLoadingComponentProps, TableOrderer, TablePagination, type TableProps, type TableRendererProps, type TableRowSelectorProps, TableSelector, TableSorter, TableViewer, TextCell, type TextCellProps, type
|
|
395
|
+
export { type DataResponse, DataTable, type DataTableProps, DataTableServer, type DataTableServerProps, DefaultTable, type DefaultTableProps, DensityToggleButton, type DensityToggleButtonProps, EditFilterButton, type EditFilterButtonProps, EditOrderButton, type EditOrderButtonProps, EditSortingButton, type EditSortingButtonProps, EditViewButton, type EditViewButtonProps, FilterOptions, type FilterOptionsProps, GlobalFilter, PageSizeControl, type PageSizeControlProps, type PaginationProps, ReloadButton, type ReloadButtonProps, ResetFilteringButton, type ResetFilteringButtonProps, ResetSelectionButton, type ResetSelectionButtonProps, ResetSortingButton, type ResetSortingButtonProps, type Result, RowCountText, Table, TableBody, type TableBodyProps, TableCardContainer, type TableCardContainerProps, TableCards, type TableCardsProps, TableComponent, TableControls, type TableControlsProps, TableFilter, TableFilterTags, TableFooter, type TableFooterProps, TableHeader, type TableHeaderProps, TableLoadingComponent, type TableLoadingComponentProps, TableOrderer, TablePagination, type TableProps, type TableRendererProps, type TableRowSelectorProps, TableSelector, TableSorter, TableViewer, TextCell, type TextCellProps, type UseDataFromUrlProps, type UseDataFromUrlReturn, type UseDataTableProps, type UseDataTableReturn, type UseDataTableServerProps, type UseDataTableServerReturn, useDataFromUrl, useDataTable, useDataTableContext, useDataTableServer };
|
package/dist/index.js
CHANGED
|
@@ -9,13 +9,13 @@ var react$1 = require('react');
|
|
|
9
9
|
var io = require('react-icons/io');
|
|
10
10
|
var reactTable = require('@tanstack/react-table');
|
|
11
11
|
var matchSorterUtils = require('@tanstack/match-sorter-utils');
|
|
12
|
-
var axios = require('axios');
|
|
13
12
|
var table = require('@chakra-ui/table');
|
|
14
13
|
var bs = require('react-icons/bs');
|
|
15
14
|
var gr = require('react-icons/gr');
|
|
16
15
|
var io5 = require('react-icons/io5');
|
|
17
16
|
var reactBeautifulDnd = require('react-beautiful-dnd');
|
|
18
17
|
var fa = require('react-icons/fa');
|
|
18
|
+
var axios = require('axios');
|
|
19
19
|
|
|
20
20
|
const DensityToggleButton = ({ text, icon = jsxRuntime.jsx(ai.AiOutlineColumnWidth, {}), }) => {
|
|
21
21
|
const { table } = useDataTableContext();
|
|
@@ -263,66 +263,7 @@ const DataTable = ({ columns, data, enableRowSelection = true, enableMultiRowSel
|
|
|
263
263
|
}, children: children }));
|
|
264
264
|
};
|
|
265
265
|
|
|
266
|
-
const
|
|
267
|
-
const [loading, setLoading] = react$1.useState(true);
|
|
268
|
-
const [hasError, setHasError] = react$1.useState(false);
|
|
269
|
-
const [data, setData] = react$1.useState(defaultData);
|
|
270
|
-
const refreshData = async () => {
|
|
271
|
-
await getData();
|
|
272
|
-
};
|
|
273
|
-
const getData = async () => {
|
|
274
|
-
try {
|
|
275
|
-
setHasError(false);
|
|
276
|
-
setLoading(true);
|
|
277
|
-
const { data } = await axios.get(url, { params: params });
|
|
278
|
-
console.debug("get DataFromUrl success", data);
|
|
279
|
-
onFetchSuccess(data);
|
|
280
|
-
setLoading(false);
|
|
281
|
-
setData(data);
|
|
282
|
-
}
|
|
283
|
-
catch (e) {
|
|
284
|
-
console.log("Error", e);
|
|
285
|
-
setLoading(false);
|
|
286
|
-
setHasError(true);
|
|
287
|
-
}
|
|
288
|
-
};
|
|
289
|
-
react$1.useEffect(() => {
|
|
290
|
-
if (disableFirstFetch) {
|
|
291
|
-
return;
|
|
292
|
-
}
|
|
293
|
-
getData().catch((e) => {
|
|
294
|
-
console.error(e);
|
|
295
|
-
});
|
|
296
|
-
}, [url]);
|
|
297
|
-
return { data, loading, hasError, refreshData };
|
|
298
|
-
};
|
|
299
|
-
|
|
300
|
-
const DataTableServer = ({ columns, url, enableRowSelection = true, enableMultiRowSelection = true, enableSubRowSelection = true, onRowSelect = () => { }, columnOrder, columnFilters, columnVisibility, density, globalFilter, pagination, sorting, rowSelection, setPagination, setSorting, setColumnFilters, setRowSelection, setGlobalFilter, setColumnOrder, setDensity, setColumnVisibility, onFetchSuccess, children, }) => {
|
|
301
|
-
const { data, loading, hasError, refreshData } = useDataFromUrl({
|
|
302
|
-
url: url,
|
|
303
|
-
defaultData: {
|
|
304
|
-
success: false,
|
|
305
|
-
results: [],
|
|
306
|
-
count: 0,
|
|
307
|
-
},
|
|
308
|
-
params: {
|
|
309
|
-
pagination: JSON.stringify({
|
|
310
|
-
offset: pagination.pageIndex * pagination.pageSize,
|
|
311
|
-
rows: pagination.pageSize,
|
|
312
|
-
}),
|
|
313
|
-
sorting: JSON.stringify(sorting.length > 0
|
|
314
|
-
? { field: sorting[0].id, sort: sorting[0].desc ? "desc" : "asc" }
|
|
315
|
-
: {}),
|
|
316
|
-
where: JSON.stringify(columnFilters.reduce((accumulator, filter) => {
|
|
317
|
-
const obj = {};
|
|
318
|
-
obj[filter.id] = filter.value;
|
|
319
|
-
return { ...accumulator, ...obj };
|
|
320
|
-
}, {})),
|
|
321
|
-
searching: globalFilter,
|
|
322
|
-
},
|
|
323
|
-
disableFirstFetch: true,
|
|
324
|
-
onFetchSuccess: onFetchSuccess,
|
|
325
|
-
});
|
|
266
|
+
const DataTableServer = ({ columns, enableRowSelection = true, enableMultiRowSelection = true, enableSubRowSelection = true, onRowSelect = () => { }, columnOrder, columnFilters, columnVisibility, density, globalFilter, pagination, sorting, rowSelection, setPagination, setSorting, setColumnFilters, setRowSelection, setGlobalFilter, setColumnOrder, setDensity, setColumnVisibility, data, loading, hasError, refreshData, children, }) => {
|
|
326
267
|
const table = reactTable.useReactTable({
|
|
327
268
|
_features: [DensityFeature],
|
|
328
269
|
data: data.results,
|
|
@@ -370,9 +311,6 @@ const DataTableServer = ({ columns, url, enableRowSelection = true, enableMultiR
|
|
|
370
311
|
},
|
|
371
312
|
// for tanstack-table ts bug end
|
|
372
313
|
});
|
|
373
|
-
react$1.useEffect(() => {
|
|
374
|
-
refreshData();
|
|
375
|
-
}, [pagination, sorting, columnFilters, globalFilter, url]);
|
|
376
314
|
react$1.useEffect(() => {
|
|
377
315
|
setColumnOrder(table.getAllLeafColumns().map((column) => column.id));
|
|
378
316
|
}, []);
|
|
@@ -381,7 +319,7 @@ const DataTableServer = ({ columns, url, enableRowSelection = true, enableMultiR
|
|
|
381
319
|
}, [table.getState().rowSelection]);
|
|
382
320
|
react$1.useEffect(() => {
|
|
383
321
|
table.resetPagination();
|
|
384
|
-
}, [sorting, columnFilters, globalFilter
|
|
322
|
+
}, [sorting, columnFilters, globalFilter]);
|
|
385
323
|
return (jsxRuntime.jsx(TableContext.Provider, { value: {
|
|
386
324
|
table: { ...table },
|
|
387
325
|
refreshData: refreshData,
|
|
@@ -441,9 +379,9 @@ const TableRowSelector = ({ index, row, hoveredRow, pinnedBgColor = { light: "gr
|
|
|
441
379
|
onChange: row.getToggleSelectedHandler() }) }))] }));
|
|
442
380
|
};
|
|
443
381
|
|
|
444
|
-
const TableControls = ({ totalText = "Total:", showFilter = false, fitTableWidth = false, fitTableHeight = false, isMobile = false, children = jsxRuntime.jsx(jsxRuntime.Fragment, {}), showFilterName = false, showFilterTags = false, showReload = false, filterOptions = [], }) => {
|
|
382
|
+
const TableControls = ({ totalText = "Total:", showFilter = false, fitTableWidth = false, fitTableHeight = false, isMobile = false, children = jsxRuntime.jsx(jsxRuntime.Fragment, {}), showFilterName = false, showFilterTags = false, showReload = false, filterOptions = [], extraItems = jsxRuntime.jsx(jsxRuntime.Fragment, {}), }) => {
|
|
445
383
|
const { loading, hasError } = useDataTableContext();
|
|
446
|
-
return (jsxRuntime.jsxs(react.Grid, { templateRows: "auto auto auto 1fr auto", templateColumns: "1fr 1fr", width: fitTableWidth ? "fit-content" : "100%", height: fitTableHeight ? "fit-content" : "100%", justifySelf: "center", alignSelf: "center", gap: "0.5rem", children: [jsxRuntime.jsxs(react.Flex, { justifyContent: "space-between", gridColumn: "1 / span 2", children: [jsxRuntime.jsx(react.Box, { children: jsxRuntime.jsx(EditViewButton, { text: isMobile ? undefined : "View", icon: jsxRuntime.jsx(md.MdOutlineViewColumn, {}) }) }), jsxRuntime.jsxs(react.Flex, { gap: "0.5rem", alignItems: "center", justifySelf: "end", children: [loading && jsxRuntime.jsx(react.Spinner, { size: "sm" }), hasError && (jsxRuntime.jsx(react.Tooltip, { label: "An error occurred while fetching data", children: jsxRuntime.jsx(react.Box, { children: jsxRuntime.jsx(react.Icon, { as: bs.BsExclamationCircleFill, color: "red.400" }) }) })), showFilter && (jsxRuntime.jsxs(jsxRuntime.Fragment, { children: [jsxRuntime.jsx(GlobalFilter, {}), jsxRuntime.jsx(EditFilterButton, { text: isMobile ? undefined : "Advanced Filter" })] })), showReload && jsxRuntime.jsx(ReloadButton, {})] })] }), jsxRuntime.jsx(react.Flex, { gridColumn: "1 / span 2", flexFlow: "column", gap: "0.5rem", children: filterOptions.map((column) => {
|
|
384
|
+
return (jsxRuntime.jsxs(react.Grid, { templateRows: "auto auto auto 1fr auto", templateColumns: "1fr 1fr", width: fitTableWidth ? "fit-content" : "100%", height: fitTableHeight ? "fit-content" : "100%", justifySelf: "center", alignSelf: "center", gap: "0.5rem", children: [jsxRuntime.jsxs(react.Flex, { justifyContent: "space-between", gridColumn: "1 / span 2", children: [jsxRuntime.jsx(react.Box, { children: jsxRuntime.jsx(EditViewButton, { text: isMobile ? undefined : "View", icon: jsxRuntime.jsx(md.MdOutlineViewColumn, {}) }) }), jsxRuntime.jsxs(react.Flex, { gap: "0.5rem", alignItems: "center", justifySelf: "end", children: [loading && jsxRuntime.jsx(react.Spinner, { size: "sm" }), hasError && (jsxRuntime.jsx(react.Tooltip, { label: "An error occurred while fetching data", children: jsxRuntime.jsx(react.Box, { children: jsxRuntime.jsx(react.Icon, { as: bs.BsExclamationCircleFill, color: "red.400" }) }) })), showFilter && (jsxRuntime.jsxs(jsxRuntime.Fragment, { children: [jsxRuntime.jsx(GlobalFilter, {}), jsxRuntime.jsx(EditFilterButton, { text: isMobile ? undefined : "Advanced Filter" })] })), showReload && jsxRuntime.jsx(ReloadButton, {}), extraItems] })] }), jsxRuntime.jsx(react.Flex, { gridColumn: "1 / span 2", flexFlow: "column", gap: "0.5rem", children: filterOptions.map((column) => {
|
|
447
385
|
return (jsxRuntime.jsxs(react.Flex, { alignItems: "center", flexFlow: "wrap", gap: "0.5rem", children: [showFilterName && jsxRuntime.jsxs(react.Text, { children: [column, ":"] }), jsxRuntime.jsx(FilterOptions, { column: column })] }));
|
|
448
386
|
}) }), jsxRuntime.jsx(react.Flex, { gridColumn: "1 / span 2", children: showFilterTags && jsxRuntime.jsx(TableFilterTags, {}) }), jsxRuntime.jsx(react.Box, { overflow: "auto", gridColumn: "1 / span 2", width: "100%", height: "100%", children: children }), jsxRuntime.jsxs(react.Flex, { gap: "1rem", alignItems: "center", children: [jsxRuntime.jsx(PageSizeControl, {}), jsxRuntime.jsxs(react.Flex, { children: [jsxRuntime.jsx(react.Text, { paddingRight: "0.5rem", children: totalText }), jsxRuntime.jsx(RowCountText, {})] })] }), jsxRuntime.jsx(react.Box, { justifySelf: "end", children: jsxRuntime.jsx(TablePagination, {}) })] }));
|
|
449
387
|
};
|
|
@@ -578,8 +516,8 @@ const TableHeader = ({ canResize, pinnedBgColor = { light: "gray.50", dark: "gra
|
|
|
578
516
|
})] }, crypto.randomUUID()))) }));
|
|
579
517
|
};
|
|
580
518
|
|
|
581
|
-
const DefaultTable = ({ totalText = "Total:", showFilter = false, showFooter = false, fitTableWidth = false, fitTableHeight = false, isMobile = false, filterOptions = [], showFilterTags = false, showFilterName = false, showReload = false, }) => {
|
|
582
|
-
return (jsxRuntime.jsx(TableControls, { totalText: totalText, showFilter: showFilter, fitTableWidth: fitTableWidth, fitTableHeight: fitTableHeight, isMobile: isMobile, filterOptions: filterOptions, showFilterName: showFilterName, showFilterTags: showFilterTags, showReload: showReload, children: jsxRuntime.jsxs(Table, { variant: "striped", children: [jsxRuntime.jsx(TableHeader, { canResize: true }), jsxRuntime.jsx(TableBody, {}), showFooter && jsxRuntime.jsx(TableFooter, {})] }) }));
|
|
519
|
+
const DefaultTable = ({ totalText = "Total:", showFilter = false, showFooter = false, fitTableWidth = false, fitTableHeight = false, isMobile = false, filterOptions = [], showFilterTags = false, showFilterName = false, showReload = false, extraItems = jsxRuntime.jsx(jsxRuntime.Fragment, {}), }) => {
|
|
520
|
+
return (jsxRuntime.jsx(TableControls, { totalText: totalText, showFilter: showFilter, fitTableWidth: fitTableWidth, fitTableHeight: fitTableHeight, isMobile: isMobile, filterOptions: filterOptions, showFilterName: showFilterName, showFilterTags: showFilterTags, showReload: showReload, extraItems: extraItems, children: jsxRuntime.jsxs(Table, { variant: "striped", children: [jsxRuntime.jsx(TableHeader, { canResize: true }), jsxRuntime.jsx(TableBody, {}), showFooter && jsxRuntime.jsx(TableFooter, {})] }) }));
|
|
583
521
|
};
|
|
584
522
|
|
|
585
523
|
const Table = ({ children, showLoading = false, loadingComponent = jsxRuntime.jsx(jsxRuntime.Fragment, { children: "Loading..." }), ...props }) => {
|
|
@@ -847,6 +785,40 @@ const TextCell = ({ label, noOfLines = [1], padding = "0rem", children, tooltipP
|
|
|
847
785
|
return (jsxRuntime.jsx(react.Flex, { alignItems: "center", height: "100%", padding: padding, children: jsxRuntime.jsx(react.Text, { as: "span", overflow: "hidden", textOverflow: "ellipsis", wordBreak: "break-all", noOfLines: noOfLines, ...props, children: children }) }));
|
|
848
786
|
};
|
|
849
787
|
|
|
788
|
+
const useDataFromUrl = ({ url, params = {}, disableFirstFetch = false, onFetchSuccess = () => { }, defaultData, }) => {
|
|
789
|
+
const [loading, setLoading] = react$1.useState(true);
|
|
790
|
+
const [hasError, setHasError] = react$1.useState(false);
|
|
791
|
+
const [data, setData] = react$1.useState(defaultData);
|
|
792
|
+
const refreshData = async () => {
|
|
793
|
+
await getData();
|
|
794
|
+
};
|
|
795
|
+
const getData = async () => {
|
|
796
|
+
try {
|
|
797
|
+
setHasError(false);
|
|
798
|
+
setLoading(true);
|
|
799
|
+
const { data } = await axios.get(url, { params: params });
|
|
800
|
+
console.debug("get DataFromUrl success", data);
|
|
801
|
+
onFetchSuccess(data);
|
|
802
|
+
setLoading(false);
|
|
803
|
+
setData(data);
|
|
804
|
+
}
|
|
805
|
+
catch (e) {
|
|
806
|
+
console.log("Error", e);
|
|
807
|
+
setLoading(false);
|
|
808
|
+
setHasError(true);
|
|
809
|
+
}
|
|
810
|
+
};
|
|
811
|
+
react$1.useEffect(() => {
|
|
812
|
+
if (disableFirstFetch) {
|
|
813
|
+
return;
|
|
814
|
+
}
|
|
815
|
+
getData().catch((e) => {
|
|
816
|
+
console.error(e);
|
|
817
|
+
});
|
|
818
|
+
}, [url]);
|
|
819
|
+
return { data, loading, hasError, refreshData };
|
|
820
|
+
};
|
|
821
|
+
|
|
850
822
|
const useDataTable = ({ default: { sorting: defaultSorting = [], pagination: defaultPagination = {
|
|
851
823
|
pageIndex: 0, //initial page index
|
|
852
824
|
pageSize: 10, //default page size
|
|
@@ -905,6 +877,82 @@ const useDataTable = ({ default: { sorting: defaultSorting = [], pagination: def
|
|
|
905
877
|
};
|
|
906
878
|
};
|
|
907
879
|
|
|
880
|
+
const useDataTableServer = ({ url, onFetchSuccess = () => { }, default: { sorting: defaultSorting = [], pagination: defaultPagination = {
|
|
881
|
+
pageIndex: 0, //initial page index
|
|
882
|
+
pageSize: 10, //default page size
|
|
883
|
+
}, rowSelection: defaultRowSelection = {}, columnFilters: defaultColumnFilters = [], columnOrder: defaultColumnOrder = [], columnVisibility: defaultColumnVisibility = {}, globalFilter: defaultGlobalFilter = "", density: defaultDensity = "sm", } = {
|
|
884
|
+
sorting: [],
|
|
885
|
+
pagination: {
|
|
886
|
+
pageIndex: 0, //initial page index
|
|
887
|
+
pageSize: 10, //age size
|
|
888
|
+
},
|
|
889
|
+
rowSelection: {},
|
|
890
|
+
columnFilters: [],
|
|
891
|
+
columnOrder: [],
|
|
892
|
+
columnVisibility: {},
|
|
893
|
+
globalFilter: "",
|
|
894
|
+
density: "sm",
|
|
895
|
+
}, }) => {
|
|
896
|
+
const [sorting, setSorting] = react$1.useState(defaultSorting);
|
|
897
|
+
const [columnFilters, setColumnFilters] = react$1.useState(defaultColumnFilters); // can set initial column filter state here
|
|
898
|
+
const [pagination, setPagination] = react$1.useState(defaultPagination);
|
|
899
|
+
const [rowSelection, setRowSelection] = react$1.useState(defaultRowSelection);
|
|
900
|
+
const [columnOrder, setColumnOrder] = react$1.useState(defaultColumnOrder);
|
|
901
|
+
const [globalFilter, setGlobalFilter] = react$1.useState(defaultGlobalFilter);
|
|
902
|
+
const [density, setDensity] = react$1.useState(defaultDensity);
|
|
903
|
+
const [columnVisibility, setColumnVisibility] = react$1.useState(defaultColumnVisibility);
|
|
904
|
+
const { data, loading, hasError, refreshData } = useDataFromUrl({
|
|
905
|
+
url: url,
|
|
906
|
+
defaultData: {
|
|
907
|
+
success: false,
|
|
908
|
+
results: [],
|
|
909
|
+
count: 0,
|
|
910
|
+
},
|
|
911
|
+
params: {
|
|
912
|
+
pagination: JSON.stringify({
|
|
913
|
+
offset: pagination.pageIndex * pagination.pageSize,
|
|
914
|
+
rows: pagination.pageSize,
|
|
915
|
+
}),
|
|
916
|
+
sorting: JSON.stringify(sorting.length > 0
|
|
917
|
+
? { field: sorting[0].id, sort: sorting[0].desc ? "desc" : "asc" }
|
|
918
|
+
: {}),
|
|
919
|
+
where: JSON.stringify(columnFilters.reduce((accumulator, filter) => {
|
|
920
|
+
const obj = {};
|
|
921
|
+
obj[filter.id] = filter.value;
|
|
922
|
+
return { ...accumulator, ...obj };
|
|
923
|
+
}, {})),
|
|
924
|
+
searching: globalFilter,
|
|
925
|
+
},
|
|
926
|
+
disableFirstFetch: true,
|
|
927
|
+
onFetchSuccess: onFetchSuccess,
|
|
928
|
+
});
|
|
929
|
+
react$1.useEffect(() => {
|
|
930
|
+
refreshData();
|
|
931
|
+
}, [pagination, sorting, columnFilters, globalFilter, url]);
|
|
932
|
+
return {
|
|
933
|
+
sorting,
|
|
934
|
+
setSorting,
|
|
935
|
+
columnFilters,
|
|
936
|
+
setColumnFilters,
|
|
937
|
+
pagination,
|
|
938
|
+
setPagination,
|
|
939
|
+
rowSelection,
|
|
940
|
+
setRowSelection,
|
|
941
|
+
columnOrder,
|
|
942
|
+
setColumnOrder,
|
|
943
|
+
globalFilter,
|
|
944
|
+
setGlobalFilter,
|
|
945
|
+
density,
|
|
946
|
+
setDensity,
|
|
947
|
+
columnVisibility,
|
|
948
|
+
setColumnVisibility,
|
|
949
|
+
data,
|
|
950
|
+
loading,
|
|
951
|
+
hasError,
|
|
952
|
+
refreshData,
|
|
953
|
+
};
|
|
954
|
+
};
|
|
955
|
+
|
|
908
956
|
const FilterOptions = ({ column }) => {
|
|
909
957
|
const { table } = useDataTableContext();
|
|
910
958
|
const tableColumn = table.getColumn(column);
|
|
@@ -971,3 +1019,4 @@ exports.TextCell = TextCell;
|
|
|
971
1019
|
exports.useDataFromUrl = useDataFromUrl;
|
|
972
1020
|
exports.useDataTable = useDataTable;
|
|
973
1021
|
exports.useDataTableContext = useDataTableContext;
|
|
1022
|
+
exports.useDataTableServer = useDataTableServer;
|
package/dist/index.mjs
CHANGED
|
@@ -7,13 +7,13 @@ import { createContext, useContext, useEffect, useState } from 'react';
|
|
|
7
7
|
import { IoMdEye, IoMdCheckbox } from 'react-icons/io';
|
|
8
8
|
import { makeStateUpdater, functionalUpdate, useReactTable, getCoreRowModel, getFilteredRowModel, getSortedRowModel, getPaginationRowModel, flexRender } from '@tanstack/react-table';
|
|
9
9
|
import { rankItem } from '@tanstack/match-sorter-utils';
|
|
10
|
-
import axios from 'axios';
|
|
11
10
|
import { Tbody, Tr, Td } from '@chakra-ui/table';
|
|
12
11
|
import { BsExclamationCircleFill } from 'react-icons/bs';
|
|
13
12
|
import { GrAscend, GrDescend } from 'react-icons/gr';
|
|
14
13
|
import { IoReload } from 'react-icons/io5';
|
|
15
14
|
import { DragDropContext, Droppable, Draggable } from 'react-beautiful-dnd';
|
|
16
15
|
import { FaGripLinesVertical } from 'react-icons/fa';
|
|
16
|
+
import axios from 'axios';
|
|
17
17
|
|
|
18
18
|
const DensityToggleButton = ({ text, icon = jsx(AiOutlineColumnWidth, {}), }) => {
|
|
19
19
|
const { table } = useDataTableContext();
|
|
@@ -261,66 +261,7 @@ const DataTable = ({ columns, data, enableRowSelection = true, enableMultiRowSel
|
|
|
261
261
|
}, children: children }));
|
|
262
262
|
};
|
|
263
263
|
|
|
264
|
-
const
|
|
265
|
-
const [loading, setLoading] = useState(true);
|
|
266
|
-
const [hasError, setHasError] = useState(false);
|
|
267
|
-
const [data, setData] = useState(defaultData);
|
|
268
|
-
const refreshData = async () => {
|
|
269
|
-
await getData();
|
|
270
|
-
};
|
|
271
|
-
const getData = async () => {
|
|
272
|
-
try {
|
|
273
|
-
setHasError(false);
|
|
274
|
-
setLoading(true);
|
|
275
|
-
const { data } = await axios.get(url, { params: params });
|
|
276
|
-
console.debug("get DataFromUrl success", data);
|
|
277
|
-
onFetchSuccess(data);
|
|
278
|
-
setLoading(false);
|
|
279
|
-
setData(data);
|
|
280
|
-
}
|
|
281
|
-
catch (e) {
|
|
282
|
-
console.log("Error", e);
|
|
283
|
-
setLoading(false);
|
|
284
|
-
setHasError(true);
|
|
285
|
-
}
|
|
286
|
-
};
|
|
287
|
-
useEffect(() => {
|
|
288
|
-
if (disableFirstFetch) {
|
|
289
|
-
return;
|
|
290
|
-
}
|
|
291
|
-
getData().catch((e) => {
|
|
292
|
-
console.error(e);
|
|
293
|
-
});
|
|
294
|
-
}, [url]);
|
|
295
|
-
return { data, loading, hasError, refreshData };
|
|
296
|
-
};
|
|
297
|
-
|
|
298
|
-
const DataTableServer = ({ columns, url, enableRowSelection = true, enableMultiRowSelection = true, enableSubRowSelection = true, onRowSelect = () => { }, columnOrder, columnFilters, columnVisibility, density, globalFilter, pagination, sorting, rowSelection, setPagination, setSorting, setColumnFilters, setRowSelection, setGlobalFilter, setColumnOrder, setDensity, setColumnVisibility, onFetchSuccess, children, }) => {
|
|
299
|
-
const { data, loading, hasError, refreshData } = useDataFromUrl({
|
|
300
|
-
url: url,
|
|
301
|
-
defaultData: {
|
|
302
|
-
success: false,
|
|
303
|
-
results: [],
|
|
304
|
-
count: 0,
|
|
305
|
-
},
|
|
306
|
-
params: {
|
|
307
|
-
pagination: JSON.stringify({
|
|
308
|
-
offset: pagination.pageIndex * pagination.pageSize,
|
|
309
|
-
rows: pagination.pageSize,
|
|
310
|
-
}),
|
|
311
|
-
sorting: JSON.stringify(sorting.length > 0
|
|
312
|
-
? { field: sorting[0].id, sort: sorting[0].desc ? "desc" : "asc" }
|
|
313
|
-
: {}),
|
|
314
|
-
where: JSON.stringify(columnFilters.reduce((accumulator, filter) => {
|
|
315
|
-
const obj = {};
|
|
316
|
-
obj[filter.id] = filter.value;
|
|
317
|
-
return { ...accumulator, ...obj };
|
|
318
|
-
}, {})),
|
|
319
|
-
searching: globalFilter,
|
|
320
|
-
},
|
|
321
|
-
disableFirstFetch: true,
|
|
322
|
-
onFetchSuccess: onFetchSuccess,
|
|
323
|
-
});
|
|
264
|
+
const DataTableServer = ({ columns, enableRowSelection = true, enableMultiRowSelection = true, enableSubRowSelection = true, onRowSelect = () => { }, columnOrder, columnFilters, columnVisibility, density, globalFilter, pagination, sorting, rowSelection, setPagination, setSorting, setColumnFilters, setRowSelection, setGlobalFilter, setColumnOrder, setDensity, setColumnVisibility, data, loading, hasError, refreshData, children, }) => {
|
|
324
265
|
const table = useReactTable({
|
|
325
266
|
_features: [DensityFeature],
|
|
326
267
|
data: data.results,
|
|
@@ -368,9 +309,6 @@ const DataTableServer = ({ columns, url, enableRowSelection = true, enableMultiR
|
|
|
368
309
|
},
|
|
369
310
|
// for tanstack-table ts bug end
|
|
370
311
|
});
|
|
371
|
-
useEffect(() => {
|
|
372
|
-
refreshData();
|
|
373
|
-
}, [pagination, sorting, columnFilters, globalFilter, url]);
|
|
374
312
|
useEffect(() => {
|
|
375
313
|
setColumnOrder(table.getAllLeafColumns().map((column) => column.id));
|
|
376
314
|
}, []);
|
|
@@ -379,7 +317,7 @@ const DataTableServer = ({ columns, url, enableRowSelection = true, enableMultiR
|
|
|
379
317
|
}, [table.getState().rowSelection]);
|
|
380
318
|
useEffect(() => {
|
|
381
319
|
table.resetPagination();
|
|
382
|
-
}, [sorting, columnFilters, globalFilter
|
|
320
|
+
}, [sorting, columnFilters, globalFilter]);
|
|
383
321
|
return (jsx(TableContext.Provider, { value: {
|
|
384
322
|
table: { ...table },
|
|
385
323
|
refreshData: refreshData,
|
|
@@ -439,9 +377,9 @@ const TableRowSelector = ({ index, row, hoveredRow, pinnedBgColor = { light: "gr
|
|
|
439
377
|
onChange: row.getToggleSelectedHandler() }) }))] }));
|
|
440
378
|
};
|
|
441
379
|
|
|
442
|
-
const TableControls = ({ totalText = "Total:", showFilter = false, fitTableWidth = false, fitTableHeight = false, isMobile = false, children = jsx(Fragment, {}), showFilterName = false, showFilterTags = false, showReload = false, filterOptions = [], }) => {
|
|
380
|
+
const TableControls = ({ totalText = "Total:", showFilter = false, fitTableWidth = false, fitTableHeight = false, isMobile = false, children = jsx(Fragment, {}), showFilterName = false, showFilterTags = false, showReload = false, filterOptions = [], extraItems = jsx(Fragment, {}), }) => {
|
|
443
381
|
const { loading, hasError } = useDataTableContext();
|
|
444
|
-
return (jsxs(Grid, { templateRows: "auto auto auto 1fr auto", templateColumns: "1fr 1fr", width: fitTableWidth ? "fit-content" : "100%", height: fitTableHeight ? "fit-content" : "100%", justifySelf: "center", alignSelf: "center", gap: "0.5rem", children: [jsxs(Flex, { justifyContent: "space-between", gridColumn: "1 / span 2", children: [jsx(Box, { children: jsx(EditViewButton, { text: isMobile ? undefined : "View", icon: jsx(MdOutlineViewColumn, {}) }) }), jsxs(Flex, { gap: "0.5rem", alignItems: "center", justifySelf: "end", children: [loading && jsx(Spinner, { size: "sm" }), hasError && (jsx(Tooltip, { label: "An error occurred while fetching data", children: jsx(Box, { children: jsx(Icon, { as: BsExclamationCircleFill, color: "red.400" }) }) })), showFilter && (jsxs(Fragment, { children: [jsx(GlobalFilter, {}), jsx(EditFilterButton, { text: isMobile ? undefined : "Advanced Filter" })] })), showReload && jsx(ReloadButton, {})] })] }), jsx(Flex, { gridColumn: "1 / span 2", flexFlow: "column", gap: "0.5rem", children: filterOptions.map((column) => {
|
|
382
|
+
return (jsxs(Grid, { templateRows: "auto auto auto 1fr auto", templateColumns: "1fr 1fr", width: fitTableWidth ? "fit-content" : "100%", height: fitTableHeight ? "fit-content" : "100%", justifySelf: "center", alignSelf: "center", gap: "0.5rem", children: [jsxs(Flex, { justifyContent: "space-between", gridColumn: "1 / span 2", children: [jsx(Box, { children: jsx(EditViewButton, { text: isMobile ? undefined : "View", icon: jsx(MdOutlineViewColumn, {}) }) }), jsxs(Flex, { gap: "0.5rem", alignItems: "center", justifySelf: "end", children: [loading && jsx(Spinner, { size: "sm" }), hasError && (jsx(Tooltip, { label: "An error occurred while fetching data", children: jsx(Box, { children: jsx(Icon, { as: BsExclamationCircleFill, color: "red.400" }) }) })), showFilter && (jsxs(Fragment, { children: [jsx(GlobalFilter, {}), jsx(EditFilterButton, { text: isMobile ? undefined : "Advanced Filter" })] })), showReload && jsx(ReloadButton, {}), extraItems] })] }), jsx(Flex, { gridColumn: "1 / span 2", flexFlow: "column", gap: "0.5rem", children: filterOptions.map((column) => {
|
|
445
383
|
return (jsxs(Flex, { alignItems: "center", flexFlow: "wrap", gap: "0.5rem", children: [showFilterName && jsxs(Text, { children: [column, ":"] }), jsx(FilterOptions, { column: column })] }));
|
|
446
384
|
}) }), jsx(Flex, { gridColumn: "1 / span 2", children: showFilterTags && jsx(TableFilterTags, {}) }), jsx(Box, { overflow: "auto", gridColumn: "1 / span 2", width: "100%", height: "100%", children: children }), jsxs(Flex, { gap: "1rem", alignItems: "center", children: [jsx(PageSizeControl, {}), jsxs(Flex, { children: [jsx(Text, { paddingRight: "0.5rem", children: totalText }), jsx(RowCountText, {})] })] }), jsx(Box, { justifySelf: "end", children: jsx(TablePagination, {}) })] }));
|
|
447
385
|
};
|
|
@@ -576,8 +514,8 @@ const TableHeader = ({ canResize, pinnedBgColor = { light: "gray.50", dark: "gra
|
|
|
576
514
|
})] }, crypto.randomUUID()))) }));
|
|
577
515
|
};
|
|
578
516
|
|
|
579
|
-
const DefaultTable = ({ totalText = "Total:", showFilter = false, showFooter = false, fitTableWidth = false, fitTableHeight = false, isMobile = false, filterOptions = [], showFilterTags = false, showFilterName = false, showReload = false, }) => {
|
|
580
|
-
return (jsx(TableControls, { totalText: totalText, showFilter: showFilter, fitTableWidth: fitTableWidth, fitTableHeight: fitTableHeight, isMobile: isMobile, filterOptions: filterOptions, showFilterName: showFilterName, showFilterTags: showFilterTags, showReload: showReload, children: jsxs(Table, { variant: "striped", children: [jsx(TableHeader, { canResize: true }), jsx(TableBody, {}), showFooter && jsx(TableFooter, {})] }) }));
|
|
517
|
+
const DefaultTable = ({ totalText = "Total:", showFilter = false, showFooter = false, fitTableWidth = false, fitTableHeight = false, isMobile = false, filterOptions = [], showFilterTags = false, showFilterName = false, showReload = false, extraItems = jsx(Fragment, {}), }) => {
|
|
518
|
+
return (jsx(TableControls, { totalText: totalText, showFilter: showFilter, fitTableWidth: fitTableWidth, fitTableHeight: fitTableHeight, isMobile: isMobile, filterOptions: filterOptions, showFilterName: showFilterName, showFilterTags: showFilterTags, showReload: showReload, extraItems: extraItems, children: jsxs(Table, { variant: "striped", children: [jsx(TableHeader, { canResize: true }), jsx(TableBody, {}), showFooter && jsx(TableFooter, {})] }) }));
|
|
581
519
|
};
|
|
582
520
|
|
|
583
521
|
const Table = ({ children, showLoading = false, loadingComponent = jsx(Fragment, { children: "Loading..." }), ...props }) => {
|
|
@@ -845,6 +783,40 @@ const TextCell = ({ label, noOfLines = [1], padding = "0rem", children, tooltipP
|
|
|
845
783
|
return (jsx(Flex, { alignItems: "center", height: "100%", padding: padding, children: jsx(Text, { as: "span", overflow: "hidden", textOverflow: "ellipsis", wordBreak: "break-all", noOfLines: noOfLines, ...props, children: children }) }));
|
|
846
784
|
};
|
|
847
785
|
|
|
786
|
+
const useDataFromUrl = ({ url, params = {}, disableFirstFetch = false, onFetchSuccess = () => { }, defaultData, }) => {
|
|
787
|
+
const [loading, setLoading] = useState(true);
|
|
788
|
+
const [hasError, setHasError] = useState(false);
|
|
789
|
+
const [data, setData] = useState(defaultData);
|
|
790
|
+
const refreshData = async () => {
|
|
791
|
+
await getData();
|
|
792
|
+
};
|
|
793
|
+
const getData = async () => {
|
|
794
|
+
try {
|
|
795
|
+
setHasError(false);
|
|
796
|
+
setLoading(true);
|
|
797
|
+
const { data } = await axios.get(url, { params: params });
|
|
798
|
+
console.debug("get DataFromUrl success", data);
|
|
799
|
+
onFetchSuccess(data);
|
|
800
|
+
setLoading(false);
|
|
801
|
+
setData(data);
|
|
802
|
+
}
|
|
803
|
+
catch (e) {
|
|
804
|
+
console.log("Error", e);
|
|
805
|
+
setLoading(false);
|
|
806
|
+
setHasError(true);
|
|
807
|
+
}
|
|
808
|
+
};
|
|
809
|
+
useEffect(() => {
|
|
810
|
+
if (disableFirstFetch) {
|
|
811
|
+
return;
|
|
812
|
+
}
|
|
813
|
+
getData().catch((e) => {
|
|
814
|
+
console.error(e);
|
|
815
|
+
});
|
|
816
|
+
}, [url]);
|
|
817
|
+
return { data, loading, hasError, refreshData };
|
|
818
|
+
};
|
|
819
|
+
|
|
848
820
|
const useDataTable = ({ default: { sorting: defaultSorting = [], pagination: defaultPagination = {
|
|
849
821
|
pageIndex: 0, //initial page index
|
|
850
822
|
pageSize: 10, //default page size
|
|
@@ -903,6 +875,82 @@ const useDataTable = ({ default: { sorting: defaultSorting = [], pagination: def
|
|
|
903
875
|
};
|
|
904
876
|
};
|
|
905
877
|
|
|
878
|
+
const useDataTableServer = ({ url, onFetchSuccess = () => { }, default: { sorting: defaultSorting = [], pagination: defaultPagination = {
|
|
879
|
+
pageIndex: 0, //initial page index
|
|
880
|
+
pageSize: 10, //default page size
|
|
881
|
+
}, rowSelection: defaultRowSelection = {}, columnFilters: defaultColumnFilters = [], columnOrder: defaultColumnOrder = [], columnVisibility: defaultColumnVisibility = {}, globalFilter: defaultGlobalFilter = "", density: defaultDensity = "sm", } = {
|
|
882
|
+
sorting: [],
|
|
883
|
+
pagination: {
|
|
884
|
+
pageIndex: 0, //initial page index
|
|
885
|
+
pageSize: 10, //age size
|
|
886
|
+
},
|
|
887
|
+
rowSelection: {},
|
|
888
|
+
columnFilters: [],
|
|
889
|
+
columnOrder: [],
|
|
890
|
+
columnVisibility: {},
|
|
891
|
+
globalFilter: "",
|
|
892
|
+
density: "sm",
|
|
893
|
+
}, }) => {
|
|
894
|
+
const [sorting, setSorting] = useState(defaultSorting);
|
|
895
|
+
const [columnFilters, setColumnFilters] = useState(defaultColumnFilters); // can set initial column filter state here
|
|
896
|
+
const [pagination, setPagination] = useState(defaultPagination);
|
|
897
|
+
const [rowSelection, setRowSelection] = useState(defaultRowSelection);
|
|
898
|
+
const [columnOrder, setColumnOrder] = useState(defaultColumnOrder);
|
|
899
|
+
const [globalFilter, setGlobalFilter] = useState(defaultGlobalFilter);
|
|
900
|
+
const [density, setDensity] = useState(defaultDensity);
|
|
901
|
+
const [columnVisibility, setColumnVisibility] = useState(defaultColumnVisibility);
|
|
902
|
+
const { data, loading, hasError, refreshData } = useDataFromUrl({
|
|
903
|
+
url: url,
|
|
904
|
+
defaultData: {
|
|
905
|
+
success: false,
|
|
906
|
+
results: [],
|
|
907
|
+
count: 0,
|
|
908
|
+
},
|
|
909
|
+
params: {
|
|
910
|
+
pagination: JSON.stringify({
|
|
911
|
+
offset: pagination.pageIndex * pagination.pageSize,
|
|
912
|
+
rows: pagination.pageSize,
|
|
913
|
+
}),
|
|
914
|
+
sorting: JSON.stringify(sorting.length > 0
|
|
915
|
+
? { field: sorting[0].id, sort: sorting[0].desc ? "desc" : "asc" }
|
|
916
|
+
: {}),
|
|
917
|
+
where: JSON.stringify(columnFilters.reduce((accumulator, filter) => {
|
|
918
|
+
const obj = {};
|
|
919
|
+
obj[filter.id] = filter.value;
|
|
920
|
+
return { ...accumulator, ...obj };
|
|
921
|
+
}, {})),
|
|
922
|
+
searching: globalFilter,
|
|
923
|
+
},
|
|
924
|
+
disableFirstFetch: true,
|
|
925
|
+
onFetchSuccess: onFetchSuccess,
|
|
926
|
+
});
|
|
927
|
+
useEffect(() => {
|
|
928
|
+
refreshData();
|
|
929
|
+
}, [pagination, sorting, columnFilters, globalFilter, url]);
|
|
930
|
+
return {
|
|
931
|
+
sorting,
|
|
932
|
+
setSorting,
|
|
933
|
+
columnFilters,
|
|
934
|
+
setColumnFilters,
|
|
935
|
+
pagination,
|
|
936
|
+
setPagination,
|
|
937
|
+
rowSelection,
|
|
938
|
+
setRowSelection,
|
|
939
|
+
columnOrder,
|
|
940
|
+
setColumnOrder,
|
|
941
|
+
globalFilter,
|
|
942
|
+
setGlobalFilter,
|
|
943
|
+
density,
|
|
944
|
+
setDensity,
|
|
945
|
+
columnVisibility,
|
|
946
|
+
setColumnVisibility,
|
|
947
|
+
data,
|
|
948
|
+
loading,
|
|
949
|
+
hasError,
|
|
950
|
+
refreshData,
|
|
951
|
+
};
|
|
952
|
+
};
|
|
953
|
+
|
|
906
954
|
const FilterOptions = ({ column }) => {
|
|
907
955
|
const { table } = useDataTableContext();
|
|
908
956
|
const tableColumn = table.getColumn(column);
|
|
@@ -933,4 +981,4 @@ const GlobalFilter = ({ icon = MdSearch }) => {
|
|
|
933
981
|
} })] }) }) }));
|
|
934
982
|
};
|
|
935
983
|
|
|
936
|
-
export { DataTable, DataTableServer, DefaultTable, DensityToggleButton, EditFilterButton, EditOrderButton, EditSortingButton, EditViewButton, FilterOptions, GlobalFilter, PageSizeControl, ReloadButton, ResetFilteringButton, ResetSelectionButton, ResetSortingButton, RowCountText, Table, TableBody, TableCardContainer, TableCards, TableComponent, TableControls, TableFilter, TableFilterTags, TableFooter, TableHeader, TableLoadingComponent, TableOrderer, TablePagination, TableSelector, TableSorter, TableViewer, TextCell, useDataFromUrl, useDataTable, useDataTableContext };
|
|
984
|
+
export { DataTable, DataTableServer, DefaultTable, DensityToggleButton, EditFilterButton, EditOrderButton, EditSortingButton, EditViewButton, FilterOptions, GlobalFilter, PageSizeControl, ReloadButton, ResetFilteringButton, ResetSelectionButton, ResetSortingButton, RowCountText, Table, TableBody, TableCardContainer, TableCards, TableComponent, TableControls, TableFilter, TableFilterTags, TableFooter, TableHeader, TableLoadingComponent, TableOrderer, TablePagination, TableSelector, TableSorter, TableViewer, TextCell, useDataFromUrl, useDataTable, useDataTableContext, useDataTableServer };
|
|
@@ -1,9 +1,10 @@
|
|
|
1
1
|
/// <reference types="react" />
|
|
2
2
|
import { ColumnDef, ColumnFiltersState, ColumnOrderState, OnChangeFn, PaginationState, RowSelectionState, SortingState, VisibilityState } from "@tanstack/react-table";
|
|
3
3
|
import { DensityState } from "../Controls/DensityFeature";
|
|
4
|
-
|
|
4
|
+
import { UseDataFromUrlReturn } from "./useDataFromUrl";
|
|
5
|
+
import { DataResponse } from "./useDataTableServer";
|
|
6
|
+
export interface DataTableServerProps<TData> extends UseDataFromUrlReturn<DataResponse<TData>> {
|
|
5
7
|
children: JSX.Element | JSX.Element[];
|
|
6
|
-
url: string;
|
|
7
8
|
columns: ColumnDef<TData, any>[];
|
|
8
9
|
enableRowSelection?: boolean;
|
|
9
10
|
enableMultiRowSelection?: boolean;
|
|
@@ -25,13 +26,5 @@ export interface DataTableServerProps<TData> {
|
|
|
25
26
|
setColumnOrder: OnChangeFn<ColumnOrderState>;
|
|
26
27
|
setDensity: OnChangeFn<DensityState>;
|
|
27
28
|
setColumnVisibility: OnChangeFn<VisibilityState>;
|
|
28
|
-
onFetchSuccess?: (response: DataResponse<TData>) => void;
|
|
29
29
|
}
|
|
30
|
-
export
|
|
31
|
-
results: T[];
|
|
32
|
-
}
|
|
33
|
-
export interface DataResponse<T> extends Result<T> {
|
|
34
|
-
success: boolean;
|
|
35
|
-
count: number;
|
|
36
|
-
}
|
|
37
|
-
export declare const DataTableServer: <TData>({ columns, url, enableRowSelection, enableMultiRowSelection, enableSubRowSelection, onRowSelect, columnOrder, columnFilters, columnVisibility, density, globalFilter, pagination, sorting, rowSelection, setPagination, setSorting, setColumnFilters, setRowSelection, setGlobalFilter, setColumnOrder, setDensity, setColumnVisibility, onFetchSuccess, children, }: DataTableServerProps<TData>) => import("react/jsx-runtime").JSX.Element;
|
|
30
|
+
export declare const DataTableServer: <TData>({ columns, enableRowSelection, enableMultiRowSelection, enableSubRowSelection, onRowSelect, columnOrder, columnFilters, columnVisibility, density, globalFilter, pagination, sorting, rowSelection, setPagination, setSorting, setColumnFilters, setRowSelection, setGlobalFilter, setColumnOrder, setDensity, setColumnVisibility, data, loading, hasError, refreshData, children, }: DataTableServerProps<TData>) => import("react/jsx-runtime").JSX.Element;
|
|
@@ -2,4 +2,4 @@ import { TableControlsProps } from "./TableControls";
|
|
|
2
2
|
export interface DefaultTableProps extends TableControlsProps {
|
|
3
3
|
showFooter?: boolean;
|
|
4
4
|
}
|
|
5
|
-
export declare const DefaultTable: ({ totalText, showFilter, showFooter, fitTableWidth, fitTableHeight, isMobile, filterOptions, showFilterTags, showFilterName, showReload, }: DefaultTableProps) => import("react/jsx-runtime").JSX.Element;
|
|
5
|
+
export declare const DefaultTable: ({ totalText, showFilter, showFooter, fitTableWidth, fitTableHeight, isMobile, filterOptions, showFilterTags, showFilterName, showReload, extraItems, }: DefaultTableProps) => import("react/jsx-runtime").JSX.Element;
|
|
@@ -10,5 +10,6 @@ export interface TableControlsProps {
|
|
|
10
10
|
showFilterTags?: boolean;
|
|
11
11
|
showReload?: boolean;
|
|
12
12
|
filterOptions?: string[];
|
|
13
|
+
extraItems?: JSX.Element;
|
|
13
14
|
}
|
|
14
|
-
export declare const TableControls: ({ totalText, showFilter, fitTableWidth, fitTableHeight, isMobile, children, showFilterName, showFilterTags, showReload, filterOptions, }: TableControlsProps) => import("react/jsx-runtime").JSX.Element;
|
|
15
|
+
export declare const TableControls: ({ totalText, showFilter, fitTableWidth, fitTableHeight, isMobile, children, showFilterName, showFilterTags, showReload, filterOptions, extraItems, }: TableControlsProps) => import("react/jsx-runtime").JSX.Element;
|
|
@@ -1,14 +1,14 @@
|
|
|
1
|
-
export interface
|
|
1
|
+
export interface UseDataFromUrlReturn<T> {
|
|
2
2
|
data: T;
|
|
3
3
|
loading: boolean;
|
|
4
4
|
hasError: boolean;
|
|
5
5
|
refreshData: () => void;
|
|
6
6
|
}
|
|
7
|
-
export interface
|
|
7
|
+
export interface UseDataFromUrlProps<T> {
|
|
8
8
|
url: string;
|
|
9
9
|
params?: object;
|
|
10
10
|
defaultData: T;
|
|
11
11
|
disableFirstFetch?: boolean;
|
|
12
12
|
onFetchSuccess?: (data: T) => void;
|
|
13
13
|
}
|
|
14
|
-
export declare const useDataFromUrl: <T>({ url, params, disableFirstFetch, onFetchSuccess, defaultData, }:
|
|
14
|
+
export declare const useDataFromUrl: <T>({ url, params, disableFirstFetch, onFetchSuccess, defaultData, }: UseDataFromUrlProps<T>) => UseDataFromUrlReturn<T>;
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import { ColumnFiltersState, ColumnOrderState, OnChangeFn, PaginationState, RowSelectionState, SortingState, VisibilityState } from "@tanstack/react-table";
|
|
2
2
|
import { DensityState } from "../Controls/DensityFeature";
|
|
3
3
|
export interface UseDataTableProps {
|
|
4
|
-
default
|
|
4
|
+
default?: {
|
|
5
5
|
sorting?: SortingState;
|
|
6
6
|
columnFilters?: ColumnFiltersState;
|
|
7
7
|
pagination?: PaginationState;
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
import { UseDataFromUrlProps, UseDataFromUrlReturn } from "./useDataFromUrl";
|
|
2
|
+
import { UseDataTableProps, UseDataTableReturn } from "./useDataTable";
|
|
3
|
+
export interface UseDataTableServerProps<TData> extends Omit<UseDataFromUrlProps<DataResponse<TData>>, keyof {
|
|
4
|
+
defaultData: any;
|
|
5
|
+
}>, UseDataTableProps {
|
|
6
|
+
}
|
|
7
|
+
export interface UseDataTableServerReturn<TData> extends UseDataFromUrlReturn<DataResponse<TData>>, UseDataTableReturn {
|
|
8
|
+
}
|
|
9
|
+
export interface Result<T> {
|
|
10
|
+
results: T[];
|
|
11
|
+
}
|
|
12
|
+
export interface DataResponse<T> extends Result<T> {
|
|
13
|
+
success: boolean;
|
|
14
|
+
count: number;
|
|
15
|
+
}
|
|
16
|
+
export declare const useDataTableServer: <TData>({ url, onFetchSuccess, default: { sorting: defaultSorting, pagination: defaultPagination, rowSelection: defaultRowSelection, columnFilters: defaultColumnFilters, columnOrder: defaultColumnOrder, columnVisibility: defaultColumnVisibility, globalFilter: defaultGlobalFilter, density: defaultDensity, }, }: UseDataTableServerProps<TData>) => UseDataTableServerReturn<TData>;
|
package/dist/types/index.d.ts
CHANGED
|
@@ -84,6 +84,7 @@ export * from "./components/DataTable/TableViewer";
|
|
|
84
84
|
export * from "./components/DataTable/TextCell";
|
|
85
85
|
export * from "./components/DataTable/useDataFromUrl";
|
|
86
86
|
export * from "./components/DataTable/useDataTable";
|
|
87
|
+
export * from "./components/DataTable/useDataTableServer";
|
|
87
88
|
export * from "./components/DataTable/useDataTableContext";
|
|
88
89
|
export * from "./components/Filter/FilterOptions";
|
|
89
90
|
export * from "./components/Filter/GlobalFilter";
|