|
|
@@ -8,10 +8,11 @@ import {
|
|
|
LDFilterSelect,
|
|
|
LDFilterWrapper,
|
|
|
LDFilterButtonGroup,
|
|
|
+ LDFilterSelectorModal,
|
|
|
} from '@components';
|
|
|
-import {RangeValue} from '@hooks';
|
|
|
+import {RangeValue, createTableSettingContext, useContextSection} from '@hooks';
|
|
|
|
|
|
-export type FilterField<S extends Record<string, string | string[]>> =
|
|
|
+export type FilterField<S extends Record<string, unknown>> =
|
|
|
| {label: string; type: 'input'; value: keyof S}
|
|
|
| {label: string; type: 'date'; value: keyof S; enableTime?: boolean}
|
|
|
| {
|
|
|
@@ -23,21 +24,23 @@ export type FilterField<S extends Record<string, string | string[]>> =
|
|
|
hideClear?: boolean;
|
|
|
};
|
|
|
|
|
|
-export type FilterGroupMap<S extends Record<string, string | string[]>> = Map<
|
|
|
+export type FilterGroupMap<S extends Record<string, unknown>> = Map<
|
|
|
string,
|
|
|
FilterField<S>
|
|
|
>;
|
|
|
|
|
|
type Props<S extends Record<string, string | string[]>> = {
|
|
|
- filterData?: string;
|
|
|
- sourceMap?: FilterGroupMap<S>;
|
|
|
+ fieldsMap?: FilterGroupMap<S>;
|
|
|
fixedFields: FilterField<S>[];
|
|
|
fields: Record<string, string | string[]>;
|
|
|
isSearching: boolean;
|
|
|
dates?: RangeValue<Dayjs>;
|
|
|
+ modalVisible?: boolean;
|
|
|
+ settingContext: ReturnType<typeof createTableSettingContext>,
|
|
|
onReset: () => void;
|
|
|
onSearch: () => void;
|
|
|
onScreen?: () => void;
|
|
|
+ onCloseModal?: () => void;
|
|
|
onChange?: (key: any) => (value: string | number) => void;
|
|
|
onDatesChange?: (dates: RangeValue<Dayjs>) => void;
|
|
|
'date-testid'?: string;
|
|
|
@@ -53,27 +56,38 @@ function LDFilterTool<S extends Record<string, string | string[]>>(
|
|
|
onDatesChange,
|
|
|
fixedFields,
|
|
|
onSearch,
|
|
|
- filterData,
|
|
|
- sourceMap,
|
|
|
+ fieldsMap,
|
|
|
onScreen,
|
|
|
onReset,
|
|
|
isSearching,
|
|
|
+ modalVisible,
|
|
|
+ onCloseModal,
|
|
|
+ settingContext,
|
|
|
} = props;
|
|
|
|
|
|
+ const [conditions, dispatch] = useContextSection(
|
|
|
+ settingContext,
|
|
|
+ state => [state[0].conditions, state[1]],
|
|
|
+ );
|
|
|
+
|
|
|
+ function onScreenConfirm(value: string) {
|
|
|
+ dispatch({type: 'MODIFY', payload: {conditions: value}});
|
|
|
+ }
|
|
|
+
|
|
|
const prevEls = useRef<FilterField<S>[]>([]);
|
|
|
|
|
|
const list = useMemo<FilterField<S>[]>(
|
|
|
function() {
|
|
|
- if (!filterData || !sourceMap) return fixedFields;
|
|
|
+ if (!conditions || !fieldsMap) return fixedFields;
|
|
|
|
|
|
- const arr = filterData.split(',').filter(Boolean);
|
|
|
+ const arr = conditions.split(',').filter(Boolean);
|
|
|
|
|
|
const els = arr
|
|
|
.map(function(id) {
|
|
|
- if (!sourceMap.has(id))
|
|
|
+ if (!fieldsMap.has(id))
|
|
|
return;
|
|
|
|
|
|
- return {...sourceMap.get(id)!, id: Number(id)};
|
|
|
+ return {...fieldsMap.get(id)!, id: Number(id)};
|
|
|
})
|
|
|
.filter(Boolean)
|
|
|
.sort((a, b) => a.id - b.id);
|
|
|
@@ -82,7 +96,7 @@ function LDFilterTool<S extends Record<string, string | string[]>>(
|
|
|
|
|
|
return nextEls;
|
|
|
},
|
|
|
- [filterData, fixedFields, sourceMap],
|
|
|
+ [conditions, fixedFields, fieldsMap],
|
|
|
);
|
|
|
|
|
|
const onSearchFn = useLatest(onSearch);
|
|
|
@@ -125,56 +139,66 @@ function LDFilterTool<S extends Record<string, string | string[]>>(
|
|
|
);
|
|
|
|
|
|
return (
|
|
|
- <LDFilterWrapper onSearch={onSearch} data-testid={props['date-testid']}>
|
|
|
- {list.map(function(state) {
|
|
|
- switch (state.type) {
|
|
|
- default:
|
|
|
- case 'input': {
|
|
|
- return (
|
|
|
- <LDFilterInput
|
|
|
- key={state.label}
|
|
|
- name={state.value as string}
|
|
|
- label={state.label}
|
|
|
- value={fields[state.value as string] as string}
|
|
|
- onChange={onChange!(state.value)}
|
|
|
- />
|
|
|
- );
|
|
|
- }
|
|
|
- case 'date': {
|
|
|
- return (
|
|
|
- <LDFilterDate
|
|
|
- key={state.label}
|
|
|
- name={state.value as string}
|
|
|
- label={state.label}
|
|
|
- value={dates!}
|
|
|
- onChange={onDatesChange!}
|
|
|
- enableTime={state?.enableTime}
|
|
|
- />
|
|
|
- );
|
|
|
- }
|
|
|
- case 'select': {
|
|
|
- return (
|
|
|
- <LDFilterSelect
|
|
|
- key={state.label}
|
|
|
- name={state.value as string}
|
|
|
- label={state.label}
|
|
|
- value={fields[state.value as string]}
|
|
|
- onChange={onChange?.(state.value)}
|
|
|
- options={state.options}
|
|
|
- hideClear={state.hideClear}
|
|
|
- multiple={state.multiple}
|
|
|
- />
|
|
|
- );
|
|
|
+ <>
|
|
|
+ <LDFilterWrapper onSearch={onSearch} data-testid={props['date-testid']}>
|
|
|
+ {list.map(function(state) {
|
|
|
+ switch (state.type) {
|
|
|
+ default:
|
|
|
+ case 'input': {
|
|
|
+ return (
|
|
|
+ <LDFilterInput
|
|
|
+ key={state.label}
|
|
|
+ name={state.value as string}
|
|
|
+ label={state.label}
|
|
|
+ value={fields[state.value as string] as string}
|
|
|
+ onChange={onChange!(state.value)}
|
|
|
+ />
|
|
|
+ );
|
|
|
+ }
|
|
|
+ case 'date': {
|
|
|
+ return (
|
|
|
+ <LDFilterDate
|
|
|
+ key={state.label}
|
|
|
+ name={state.value as string}
|
|
|
+ label={state.label}
|
|
|
+ value={dates!}
|
|
|
+ onChange={onDatesChange!}
|
|
|
+ enableTime={state?.enableTime}
|
|
|
+ />
|
|
|
+ );
|
|
|
+ }
|
|
|
+ case 'select': {
|
|
|
+ return (
|
|
|
+ <LDFilterSelect
|
|
|
+ key={state.label}
|
|
|
+ name={state.value as string}
|
|
|
+ label={state.label}
|
|
|
+ value={fields[state.value as string]}
|
|
|
+ onChange={onChange?.(state.value)}
|
|
|
+ options={state.options}
|
|
|
+ hideClear={state.hideClear}
|
|
|
+ multiple={state.multiple}
|
|
|
+ />
|
|
|
+ );
|
|
|
+ }
|
|
|
}
|
|
|
- }
|
|
|
- })}
|
|
|
- <LDFilterButtonGroup
|
|
|
- onSearch={onSearch}
|
|
|
- isSearching={isSearching}
|
|
|
- onReset={onReset}
|
|
|
- onScreen={onScreen}
|
|
|
- />
|
|
|
- </LDFilterWrapper>
|
|
|
+ })}
|
|
|
+ <LDFilterButtonGroup
|
|
|
+ onSearch={onSearch}
|
|
|
+ isSearching={isSearching}
|
|
|
+ onReset={onReset}
|
|
|
+ onScreen={onScreen}
|
|
|
+ />
|
|
|
+ </LDFilterWrapper>
|
|
|
+
|
|
|
+ {onScreen && <LDFilterSelectorModal
|
|
|
+ visible={modalVisible!}
|
|
|
+ onClose={onCloseModal!}
|
|
|
+ filtermap={fieldsMap!}
|
|
|
+ source={conditions}
|
|
|
+ onConfirm={onScreenConfirm}
|
|
|
+ />}
|
|
|
+ </>
|
|
|
);
|
|
|
}
|
|
|
|