@antscorp/antsomi-ui 1.3.5-beta.481 → 1.3.5-beta.482
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/Filter/FilterItem.js +7 -5
- package/es/components/organism/DataTable/hooks/useDataTableListing/types.d.ts +9 -1
- package/es/components/organism/DataTable/hooks/useDataTableListing/useDataTableListing.js +2 -2
- package/es/components/organism/DataTable/utils/filter.d.ts +2 -1
- package/es/components/organism/DataTable/utils/filter.js +27 -22
- package/package.json +1 -1
|
@@ -17,15 +17,17 @@ import { Icon, Typography } from '@antscorp/antsomi-ui/es/components/atoms';
|
|
|
17
17
|
// Styled
|
|
18
18
|
import { StyledFilterItem } from './styled';
|
|
19
19
|
// Constants
|
|
20
|
-
import { DATE_TIME_FORMAT, OPERATORS_CODE, OPERATORS_OPTION, } from '@antscorp/antsomi-ui/es/constants';
|
|
21
|
-
const { MATCHES_ANY, MATCHES, NOT_MATCHES, IS } = OPERATORS_CODE;
|
|
20
|
+
import { DATE_TIME_FORMAT, METRIC_TYPE, OPERATORS_CODE, OPERATORS_OPTION, } from '@antscorp/antsomi-ui/es/constants';
|
|
21
|
+
const { MATCHES_ANY, MATCHES, NOT_MATCHES, IS, BETWEEN, AFTER_DATE, BEFORE_DATE, ON } = OPERATORS_CODE;
|
|
22
|
+
const { DATE, DATE_TIME } = METRIC_TYPE;
|
|
22
23
|
const { Text } = Typography;
|
|
23
24
|
export const FilterItem = props => {
|
|
24
|
-
const { name, operator, value, onClickRemove } = props, restProps = __rest(props, ["name", "operator", "value", "onClickRemove"]);
|
|
25
|
+
const { name, operator, datatype, value, onClickRemove } = props, restProps = __rest(props, ["name", "operator", "datatype", "value", "onClickRemove"]);
|
|
26
|
+
console.log('🚀 ~ datatype:', datatype);
|
|
25
27
|
// Memo
|
|
26
28
|
const serializeValue = useMemo(() => {
|
|
27
29
|
// Check type of value is valid date
|
|
28
|
-
if (dayjs(value).isValid()) {
|
|
30
|
+
if (dayjs(value).isValid() && [DATE, DATE_TIME].includes(datatype)) {
|
|
29
31
|
return dayjs(value).format(DATE_TIME_FORMAT.FILTER_CONDITION);
|
|
30
32
|
}
|
|
31
33
|
// Check value is an array
|
|
@@ -33,7 +35,7 @@ export const FilterItem = props => {
|
|
|
33
35
|
return value.map(item => item.title || item).join(', ');
|
|
34
36
|
}
|
|
35
37
|
return value;
|
|
36
|
-
}, [value]);
|
|
38
|
+
}, [value, datatype]);
|
|
37
39
|
const serializeOperator = useMemo(() => {
|
|
38
40
|
var _a;
|
|
39
41
|
const operatorLabel = ((_a = Object.values(OPERATORS_OPTION).find(({ value }) => value === operator)) === null || _a === void 0 ? void 0 : _a.label) || '';
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { ReactNode } from 'react';
|
|
2
2
|
import { AxiosRequestConfig } from 'axios';
|
|
3
|
-
import { TServiceAuth } from '@antscorp/antsomi-ui/es/types';
|
|
3
|
+
import { TOperatorKey, TServiceAuth } from '@antscorp/antsomi-ui/es/types';
|
|
4
4
|
import { TEnv } from '@antscorp/antsomi-ui/es/types/config';
|
|
5
5
|
import { FilterItem, TSearchActionButton, TSearchItem, TableProps } from '../../types';
|
|
6
6
|
import { PaginationProps } from '../../components';
|
|
@@ -55,6 +55,7 @@ type TSearchProps<TSearchType> = Partial<Pick<TSearchActionButton, 'addFilterInf
|
|
|
55
55
|
onClickSearchItem?: (record?: Partial<TSearchType>) => void;
|
|
56
56
|
formatData?: (response: any) => Partial<TSearchType>[];
|
|
57
57
|
};
|
|
58
|
+
export type TFormatFilterValue = Partial<Record<TOperatorKey, (value: any) => unknown>>;
|
|
58
59
|
export interface UseDataTableListingProps<TTableType = any, TSearchType = any> {
|
|
59
60
|
name?: string;
|
|
60
61
|
config: TConfig<TTableType>;
|
|
@@ -80,6 +81,13 @@ export interface UseDataTableListingProps<TTableType = any, TSearchType = any> {
|
|
|
80
81
|
matchesAny?: {
|
|
81
82
|
general: TMatchesAnyProps;
|
|
82
83
|
} & Partial<Record<keyof TTableType, TMatchesAnyProps>>;
|
|
84
|
+
/** Format filter value to call api
|
|
85
|
+
* Ex: When operator is matches_any then value auto is string array: ["1", "2", "3"]
|
|
86
|
+
* But sometimes you want to be a string join with , -> '1,2,3'
|
|
87
|
+
* Then use formatFilterValue: {matches_any(value): value.join(',')}
|
|
88
|
+
*
|
|
89
|
+
*/
|
|
90
|
+
formatFilterValue?: TFormatFilterValue;
|
|
83
91
|
};
|
|
84
92
|
}
|
|
85
93
|
export interface DataTableLocalStorage<TTableType = any> {
|
|
@@ -78,7 +78,7 @@ export function useDataTableListing(props) {
|
|
|
78
78
|
const _f = apiListing || {}, { url: listingUrl = '', enabled: enabledApiListing = true } = _f, restOfApiListingRequest = __rest(_f, ["url", "enabled"]);
|
|
79
79
|
const _g = apiSearch || {}, { url: searchUrl = '', enabled: enabledApiSearch = true } = _g, restOfApiSearchRequest = __rest(_g, ["url", "enabled"]);
|
|
80
80
|
const { filters, selectedFilterId, pagination, selectedRowKeys, searchValue, table, sorter, selectedRow, selectedFilterItem, } = state;
|
|
81
|
-
const { externalFilters, includeDataType = false, matchesAny } = filterProps || {};
|
|
81
|
+
const { externalFilters, includeDataType = false, matchesAny, formatFilterValue, } = filterProps || {};
|
|
82
82
|
const { sortDirectionKey = 'az', mainColumnKey } = tableProps || {};
|
|
83
83
|
const modifyColumnAuth = Object.assign(Object.assign({}, auth), { url: columnUrl });
|
|
84
84
|
const filterAuth = Object.assign(Object.assign({}, auth), { url: filterUrl });
|
|
@@ -151,7 +151,7 @@ export function useDataTableListing(props) {
|
|
|
151
151
|
const getTableListing = useGetTableListing({
|
|
152
152
|
args: {
|
|
153
153
|
auth: listingAuth,
|
|
154
|
-
params: Object.assign({ limit: pagination.pageSize, page: pagination.page, filter: JSON.stringify(mapFiltersToApiFilters([...(filters || []), ...(externalFilters || [])], includeDataType)), columns: JSON.stringify(((_c = (_b = modifyColumnsData === null || modifyColumnsData === void 0 ? void 0 : modifyColumnsData.find(modifyColumnItem => modifyColumnItem.modifyColId === modifyColumnSelected)) === null || _b === void 0 ? void 0 : _b.columns) === null || _c === void 0 ? void 0 : _c.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) &&
|
|
154
|
+
params: Object.assign({ limit: pagination.pageSize, page: pagination.page, filter: JSON.stringify(mapFiltersToApiFilters([...(filters || []), ...(externalFilters || [])], includeDataType, formatFilterValue)), columns: JSON.stringify(((_c = (_b = modifyColumnsData === null || modifyColumnsData === void 0 ? void 0 : modifyColumnsData.find(modifyColumnItem => modifyColumnItem.modifyColId === modifyColumnSelected)) === null || _b === void 0 ? void 0 : _b.columns) === null || _c === void 0 ? void 0 : _c.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) &&
|
|
155
155
|
(sorter === null || sorter === void 0 ? void 0 : sorter.columnKey) && {
|
|
156
156
|
sort: sorter.columnKey.toString(),
|
|
157
157
|
[sortDirectionKey]: SORT_MAP[sorter.order],
|
|
@@ -3,6 +3,7 @@ import { MenuProps } from 'antd';
|
|
|
3
3
|
import { FilterItem, FilterMetricItem, TApiFilter } from '../types';
|
|
4
4
|
import { SavedFilter } from '@antscorp/antsomi-ui/es/models/DataTable/SavedFilter';
|
|
5
5
|
import { FilterProps } from '../..';
|
|
6
|
+
import { TFormatFilterValue } from '../hooks/useDataTableListing/types';
|
|
6
7
|
type MenuItem = Required<MenuProps>['items'][number];
|
|
7
8
|
type TSerializeFilterMetricsToMenuItems = {
|
|
8
9
|
filterMetrics: FilterMetricItem[];
|
|
@@ -91,5 +92,5 @@ export declare const renderValueField: ({ filterMetric, operator, value, matches
|
|
|
91
92
|
export declare const getFilterMetricByName: ({ filterMetrics, name, }: TGetFilterMetricByName) => FilterMetricItem | undefined;
|
|
92
93
|
export declare const mapFiltersToRules: (filters: FilterItem[]) => SavedFilter['rules'];
|
|
93
94
|
export declare const mapRulesToFilters: (rules: SavedFilter['rules']) => FilterItem[];
|
|
94
|
-
export declare const mapFiltersToApiFilters: (filters: FilterItem[], includeDataType?: boolean) => TApiFilter;
|
|
95
|
+
export declare const mapFiltersToApiFilters: (filters: FilterItem[], includeDataType?: boolean, formatFilterValue?: TFormatFilterValue) => TApiFilter;
|
|
95
96
|
export {};
|
|
@@ -203,33 +203,38 @@ export const mapRulesToFilters = (rules) => rules.map(({ dataType, metricCode, o
|
|
|
203
203
|
operator: operatorValue,
|
|
204
204
|
value: filterValue,
|
|
205
205
|
}));
|
|
206
|
-
export const mapFiltersToApiFilters = (filters, includeDataType = false) => {
|
|
206
|
+
export const mapFiltersToApiFilters = (filters, includeDataType = false, formatFilterValue) => {
|
|
207
207
|
const serializedFilters = filters.map(({ column, operator, value, dataType }) => {
|
|
208
208
|
let serializeValue = '';
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
209
|
+
if (formatFilterValue && formatFilterValue[operator]) {
|
|
210
|
+
serializeValue = formatFilterValue[operator](value);
|
|
211
|
+
}
|
|
212
|
+
else {
|
|
213
|
+
switch (operator) {
|
|
214
|
+
case MATCHES:
|
|
215
|
+
case MATCHES_ANY:
|
|
216
|
+
case NOT_MATCHES: {
|
|
217
|
+
if (Array.isArray(value)) {
|
|
218
|
+
// Check if value is object the return object.key otherwise return this value
|
|
219
|
+
serializeValue = value.map(item => typeof item === 'object' ? item.key || item : item);
|
|
220
|
+
}
|
|
221
|
+
break;
|
|
216
222
|
}
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
223
|
+
/** Date time */
|
|
224
|
+
case AFTER:
|
|
225
|
+
case AFTER_DATE:
|
|
226
|
+
case BEFORE:
|
|
227
|
+
case ON:
|
|
228
|
+
case BEFORE_DATE: {
|
|
229
|
+
if (dayjs(value).isValid()) {
|
|
230
|
+
serializeValue = dayjs(value).format(DATE_TIME_FORMAT.FILTER_CONDITION);
|
|
231
|
+
}
|
|
232
|
+
break;
|
|
227
233
|
}
|
|
228
|
-
|
|
234
|
+
default:
|
|
235
|
+
serializeValue = value;
|
|
236
|
+
break;
|
|
229
237
|
}
|
|
230
|
-
default:
|
|
231
|
-
serializeValue = value;
|
|
232
|
-
break;
|
|
233
238
|
}
|
|
234
239
|
return {
|
|
235
240
|
[column]: Object.assign({ [operator]: serializeValue }, (includeDataType && { dataType: METRIC_MAP_STRING_TYPE[dataType || 'string'] })),
|