@antscorp/antsomi-ui 1.3.5-beta.426 → 1.3.5-beta.427
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/es/components/organism/DataTable/components/Table/index.js +13 -7
- package/es/components/organism/DataTable/constants/theme.js +1 -0
- package/es/components/organism/DataTable/hooks/useDataTableListing/types.d.ts +4 -0
- package/es/components/organism/DataTable/hooks/useDataTableListing/useDataTableListing.js +18 -8
- package/es/services/DataTable/index.d.ts +1 -0
- package/package.json +1 -1
|
@@ -16,7 +16,7 @@ import { cloneDeep } from 'lodash';
|
|
|
16
16
|
import { useShallow } from 'zustand/react/shallow';
|
|
17
17
|
// Components
|
|
18
18
|
import { ResizableCell } from './ResizableCell';
|
|
19
|
-
import { Spin } from '@antscorp/antsomi-ui/es/components/atoms';
|
|
19
|
+
import { Spin, Icon } from '@antscorp/antsomi-ui/es/components/atoms';
|
|
20
20
|
// Hooks
|
|
21
21
|
import { useDeepCompareEffect } from '@antscorp/antsomi-ui/es/hooks';
|
|
22
22
|
// Styled
|
|
@@ -25,12 +25,13 @@ import { StyledTable } from './styled';
|
|
|
25
25
|
import { useDataTableContext } from '../../contexts/DataTableContext';
|
|
26
26
|
// Constants
|
|
27
27
|
import { DEFAULT_COLUMN_WIDTH, DEFAULT_COLUMN_WIDTHS, DEFAULT_ROW_SELECTION_WIDTH, } from '../../constants';
|
|
28
|
+
import { globalToken } from '@antscorp/antsomi-ui/es/constants';
|
|
28
29
|
export const Table = memo(() => {
|
|
29
|
-
var _a
|
|
30
|
+
var _a;
|
|
30
31
|
// Store
|
|
31
32
|
const tableProps = useDataTableContext(useShallow(store => store.table));
|
|
32
33
|
const setTable = useDataTableContext(useShallow(store => store.setTable));
|
|
33
|
-
const
|
|
34
|
+
const _b = tableProps || {}, { rowSelection } = _b, restOfTable = __rest(_b, ["rowSelection"]);
|
|
34
35
|
// State
|
|
35
36
|
const [state, setState] = useState({
|
|
36
37
|
columns: [],
|
|
@@ -63,12 +64,12 @@ export const Table = memo(() => {
|
|
|
63
64
|
}) }));
|
|
64
65
|
});
|
|
65
66
|
}
|
|
66
|
-
}, [
|
|
67
|
+
}, [tableProps.columns, columnWidths]);
|
|
67
68
|
return (React.createElement(ConfigProvider, { theme: { token: { motion: false } } },
|
|
68
|
-
React.createElement(StyledTable, Object.assign({}, restOfTable, {
|
|
69
|
+
React.createElement(StyledTable, Object.assign({}, restOfTable, { ref: ref => {
|
|
69
70
|
tableRef.current = ref === null || ref === void 0 ? void 0 : ref.nativeElement;
|
|
70
71
|
}, scroll: Object.assign(Object.assign({}, scroll), { y: (scroll === null || scroll === void 0 ? void 0 : scroll.y) || '500px', x: (scroll === null || scroll === void 0 ? void 0 : scroll.x) || '100%' }), components: {
|
|
71
|
-
header: Object.assign({ cell: ResizableCell }, (
|
|
72
|
+
header: Object.assign({ cell: ResizableCell }, (_a = tableProps === null || tableProps === void 0 ? void 0 : tableProps.components) === null || _a === void 0 ? void 0 : _a.header),
|
|
72
73
|
} }, (Array.isArray(tableProps === null || tableProps === void 0 ? void 0 : tableProps.dataSource) &&
|
|
73
74
|
(tableProps === null || tableProps === void 0 ? void 0 : tableProps.dataSource.length) &&
|
|
74
75
|
!!rowSelection && {
|
|
@@ -79,7 +80,12 @@ export const Table = memo(() => {
|
|
|
79
80
|
});
|
|
80
81
|
(_a = rowSelection === null || rowSelection === void 0 ? void 0 : rowSelection.onChange) === null || _a === void 0 ? void 0 : _a.call(rowSelection, selectedRowKeys, selectedRows, info);
|
|
81
82
|
} }),
|
|
82
|
-
}), { columns: columns === null || columns === void 0 ? void 0 : columns.map((col, index) => (Object.assign(Object.assign({}, col), {
|
|
83
|
+
}), { columns: columns === null || columns === void 0 ? void 0 : columns.map((col, index) => (Object.assign(Object.assign({}, col), { sortIcon(props) {
|
|
84
|
+
const { sortOrder } = props;
|
|
85
|
+
if (!sortOrder)
|
|
86
|
+
return null;
|
|
87
|
+
return (React.createElement(Icon, { type: `icon-ants-${sortOrder === 'ascend' ? 'arrow-up-square' : 'arrow-down-square'}`, size: 16, color: globalToken === null || globalToken === void 0 ? void 0 : globalToken.bw8 }));
|
|
88
|
+
}, onHeaderCell: (column) => ({
|
|
83
89
|
tableRef,
|
|
84
90
|
disableResize: index === ((columns === null || columns === void 0 ? void 0 : columns.length) || 1) - 1,
|
|
85
91
|
fixed: column.fixed,
|
|
@@ -5,6 +5,7 @@ import { FilterItem, TSearchActionButton, TSearchItem, TableProps } from '../../
|
|
|
5
5
|
import { PaginationProps } from '../../components';
|
|
6
6
|
import { ColumnType } from 'antd/es/table';
|
|
7
7
|
import { ReactNode } from 'react';
|
|
8
|
+
import { SorterResult } from 'antd/es/table/interface';
|
|
8
9
|
export type TApiGlobal = AxiosRequestConfig<any> & {
|
|
9
10
|
enabled?: boolean;
|
|
10
11
|
};
|
|
@@ -28,6 +29,7 @@ type TGeneralTableColumnType<TTableType> = Omit<ColumnType<TTableType>, 'render'
|
|
|
28
29
|
};
|
|
29
30
|
type TToggleColumnType<TTableType> = TGeneralTableColumnType<TTableType> & {
|
|
30
31
|
value?: ((record: TTableType) => boolean) | boolean;
|
|
32
|
+
disabled?: ((record: TTableType) => boolean) | boolean;
|
|
31
33
|
valueKey?: keyof TTableType;
|
|
32
34
|
onChange?: (checked: boolean, record: TTableType) => void;
|
|
33
35
|
};
|
|
@@ -40,6 +42,7 @@ export interface UseDataTableListingProps<TTableType = any, TSearchType = any> {
|
|
|
40
42
|
columns?: {
|
|
41
43
|
[K in keyof TTableType | 'toggle']?: TTableColumnType<TTableType, K>;
|
|
42
44
|
};
|
|
45
|
+
sortDirectionKey?: string;
|
|
43
46
|
};
|
|
44
47
|
search?: Partial<Pick<TSearchActionButton, 'addFilterInfo' | 'isClientSearch' | 'isAddFilterFromSearchItem' | 'onClickSearchItem'>> & {
|
|
45
48
|
link?: ((record: TSearchType) => string) | string;
|
|
@@ -59,5 +62,6 @@ export interface DataTableLocalStorage<TTableType = any> {
|
|
|
59
62
|
};
|
|
60
63
|
table?: TableProps<TTableType>;
|
|
61
64
|
pagination?: PaginationProps;
|
|
65
|
+
sorter?: SorterResult<TTableType>;
|
|
62
66
|
}
|
|
63
67
|
export {};
|
|
@@ -75,6 +75,7 @@ export function useDataTableListing(props) {
|
|
|
75
75
|
const _f = apiSearch || {}, { url: searchUrl = '', enabled: enabledApiSearch = true } = _f, restOfApiSearchRequest = __rest(_f, ["url", "enabled"]);
|
|
76
76
|
const { filters, selectedFilterId, pagination, selectedRowKeys, searchValue, table, sorter, selectedRow, } = state;
|
|
77
77
|
const { externalFilters, includeDataType = false } = filterProps || {};
|
|
78
|
+
const { sortDirectionKey = 'az' } = tableProps || {};
|
|
78
79
|
const modifyColumnAuth = Object.assign(Object.assign({}, auth), { url: columnUrl });
|
|
79
80
|
const filterAuth = Object.assign(Object.assign({}, auth), { url: filterUrl });
|
|
80
81
|
const listingAuth = Object.assign(Object.assign({}, auth), { url: listingUrl });
|
|
@@ -140,7 +141,7 @@ export function useDataTableListing(props) {
|
|
|
140
141
|
params: Object.assign({ limit: pagination.pageSize, page: pagination.page, filter: JSON.stringify(mapFiltersToApiFilters([...(filters || []), ...(externalFilters || [])], includeDataType)), columns: JSON.stringify(((_b = (_a = modifyColumnsData === null || modifyColumnsData === void 0 ? void 0 : modifyColumnsData.find(modifyColumnItem => modifyColumnItem.modifyColId === modifyColumnSelected)) === null || _a === void 0 ? void 0 : _a.columns) === null || _b === void 0 ? void 0 : _b.map(col => { var _a; return (_a = col.metricCode) === null || _a === void 0 ? void 0 : _a.toUpperCase(); })) || []) }, ((sorter === null || sorter === void 0 ? void 0 : sorter.order) &&
|
|
141
142
|
(sorter === null || sorter === void 0 ? void 0 : sorter.columnKey) && {
|
|
142
143
|
sort: sorter.columnKey.toString(),
|
|
143
|
-
|
|
144
|
+
[sortDirectionKey]: SORT_MAP[sorter.order],
|
|
144
145
|
})),
|
|
145
146
|
request: restOfApiListingRequest,
|
|
146
147
|
},
|
|
@@ -239,15 +240,16 @@ export function useDataTableListing(props) {
|
|
|
239
240
|
const { columns } = tableProps || {};
|
|
240
241
|
// Check if has column toggle then add toggle switch column
|
|
241
242
|
if (columns === null || columns === void 0 ? void 0 : columns.toggle) {
|
|
242
|
-
const { key, dataIndex, valueKey, title, value, onChange } = columns.toggle || {};
|
|
243
|
+
const { key, dataIndex, valueKey, title, value, disabled, onChange } = columns.toggle || {};
|
|
243
244
|
data.push(Object.assign(Object.assign({ key: 'toggle' || key, fixed: 'left', align: 'center', dataIndex: 'toggle' || dataIndex, title: 'Toggle' || title, width: DEFAULT_TOGGLE_WIDTH }, columns.toggle), { render: (_, record) => {
|
|
244
245
|
const checked = value
|
|
245
246
|
? typeof value === 'function'
|
|
246
247
|
? value(record)
|
|
247
248
|
: value
|
|
248
249
|
: !!record[valueKey];
|
|
250
|
+
const switchDisabled = typeof disabled === 'function' ? disabled(record) : disabled;
|
|
249
251
|
return (React.createElement(Flex, { align: "center", justify: "center" },
|
|
250
|
-
React.createElement(Switch, { checked: checked, onChange: checked => {
|
|
252
|
+
React.createElement(Switch, { checked: checked, disabled: switchDisabled, onChange: checked => {
|
|
251
253
|
if (onChange) {
|
|
252
254
|
onChange(checked, record);
|
|
253
255
|
}
|
|
@@ -256,7 +258,7 @@ export function useDataTableListing(props) {
|
|
|
256
258
|
}
|
|
257
259
|
data = data.concat((tableHeader === null || tableHeader === void 0 ? void 0 : tableHeader.map(headerCol => {
|
|
258
260
|
const { name, label, fixColumn, sortable } = headerCol;
|
|
259
|
-
return Object.assign(Object.assign({ key: name, dataIndex: name, title: label, fixed: fixColumn ? 'left' : undefined, sorter: !!sortable }, columns === null || columns === void 0 ? void 0 : columns[name]), { render(value, record, index) {
|
|
261
|
+
return Object.assign(Object.assign({ key: name, dataIndex: name, title: label, fixed: fixColumn ? 'left' : undefined, sorter: !!sortable, sortOrder: (sorter === null || sorter === void 0 ? void 0 : sorter.columnKey) === name ? sorter === null || sorter === void 0 ? void 0 : sorter.order : undefined }, columns === null || columns === void 0 ? void 0 : columns[name]), { render(value, record, index) {
|
|
260
262
|
var _a;
|
|
261
263
|
const { link } = (columns === null || columns === void 0 ? void 0 : columns[name]) || {};
|
|
262
264
|
const RenderComponent = (React.createElement(Text, { ellipsis: { tooltip: true }, style: Object.assign({}, (link
|
|
@@ -275,7 +277,7 @@ export function useDataTableListing(props) {
|
|
|
275
277
|
} });
|
|
276
278
|
})) || []);
|
|
277
279
|
return data;
|
|
278
|
-
}, [tableHeader, tableProps]);
|
|
280
|
+
}, [tableHeader, tableProps, sorter]);
|
|
279
281
|
const tableData = useDeepCompareMemo(() => (tableBody === null || tableBody === void 0 ? void 0 : tableBody.map((row) => (Object.assign(Object.assign({}, row), { key: row === null || row === void 0 ? void 0 : row[tableProps === null || tableProps === void 0 ? void 0 : tableProps.mainColumnKey] })))) || [], [tableBody]);
|
|
280
282
|
const searchList = useDeepCompareMemo(() => {
|
|
281
283
|
const { itemMapKeys, icon, link } = searchProps || {};
|
|
@@ -286,7 +288,7 @@ export function useDataTableListing(props) {
|
|
|
286
288
|
* Update values from local storage to state
|
|
287
289
|
*/
|
|
288
290
|
useDeepCompareEffect(() => {
|
|
289
|
-
const { filter, pagination, table } = localStorageValues || {};
|
|
291
|
+
const { filter, pagination, table, sorter } = localStorageValues || {};
|
|
290
292
|
if (!isEmpty(filter)) {
|
|
291
293
|
const { filters, selectedFilterId } = filter || {};
|
|
292
294
|
setState(prev => (Object.assign(Object.assign({}, prev), { selectedFilterId,
|
|
@@ -298,6 +300,9 @@ export function useDataTableListing(props) {
|
|
|
298
300
|
if (!isEmpty(table)) {
|
|
299
301
|
setState(prev => (Object.assign(Object.assign({}, prev), { table })));
|
|
300
302
|
}
|
|
303
|
+
if (!isEmpty(sorter)) {
|
|
304
|
+
setState(prev => (Object.assign(Object.assign({}, prev), { sorter })));
|
|
305
|
+
}
|
|
301
306
|
}, [localStorageValues]);
|
|
302
307
|
// /** Update filters from external filters */
|
|
303
308
|
// useDeepCompareEffect(() => {
|
|
@@ -394,6 +399,11 @@ export function useDataTableListing(props) {
|
|
|
394
399
|
// Set Local Storage
|
|
395
400
|
localStorage.setItem(localStorageKey, JSON.stringify(Object.assign(Object.assign({}, localStorageValues), { table: Object.assign(Object.assign({}, localStorageValues === null || localStorageValues === void 0 ? void 0 : localStorageValues.table), table) })));
|
|
396
401
|
}, [localStorageKey, localStorageValues]);
|
|
402
|
+
const handleUpdateSorter = useCallback((sorter) => {
|
|
403
|
+
setState(prev => (Object.assign(Object.assign({}, prev), { sorter })));
|
|
404
|
+
// Set Local Storage
|
|
405
|
+
localStorage.setItem(localStorageKey, JSON.stringify(Object.assign(Object.assign({}, localStorageValues), { sorter })));
|
|
406
|
+
}, [localStorageKey, localStorageValues]);
|
|
397
407
|
const onChangeFilters = useCallback((filters) => {
|
|
398
408
|
handleUpdateFilter({
|
|
399
409
|
filters,
|
|
@@ -451,9 +461,9 @@ export function useDataTableListing(props) {
|
|
|
451
461
|
/* Table */
|
|
452
462
|
const onChangeTable = useCallback((_pagination, _filters, sorter, _extra) => {
|
|
453
463
|
if (sorter) {
|
|
454
|
-
|
|
464
|
+
handleUpdateSorter(sorter);
|
|
455
465
|
}
|
|
456
|
-
}, []);
|
|
466
|
+
}, [handleUpdateSorter]);
|
|
457
467
|
/* Pagination */
|
|
458
468
|
const onChangePagination = useCallback(({ page, pageSize }) => {
|
|
459
469
|
handleUpdatePagination({
|