import {TableOptionSpace} from "../AutoTable/utils/TableOption.space"; import {computed, reactive, RenderNode} from "@peryl/react-compose"; import {iFilterQueryParam} from "../FilterService/utils/filter.service.utils"; export function useTableFilter( { modelValue, option, defaultValue, display, handleClickTip, handleClear, getQueryParam, noClear, }: { option: TableOptionSpace.iTableOption, display: (val: T) => RenderNode, getQueryParam: (val: T) => iFilterQueryParam, modelValue?: { set: (newVal: T | null) => void, get: () => T | null }, defaultValue?: T, handleClickTip?: (val: T) => void, handleClear?: (val: T) => void, noClear?: boolean, }) { const model = (() => { const _modelValue = modelValue || (() => { const state = reactive({ value: defaultValue }); return { get: (): T | null => state.value as any, set: (newVal: T | null) => {state.value = newVal as any;} }; })(); return { get: () => _modelValue.get(), set: (newVal: T | null) => {_modelValue.set(newVal);} }; })(); const clear = (val: T) => { if (noClear) {return;} !!handleClear ? handleClear(val) : model.set(null); }; const tips = computed((): TableOptionSpace.iTableOptionTipMeta | null => { const val = model.get(); if (val == null) {return null;} return { noClear, display: () => display(val), onClear: () => { clear(val); reload(); }, onClick: () => {handleClickTip?.(val);}, }; }); option.hooks.onClearFilter.use(() => { const val = model.get(); if (val == null) {return;} clear(val); }); option.hooks.onTips.use((list) => {!!tips.value && list.unshift(tips.value);}); option.hooks.onQueryParam.use(async () => { const val = model.get(); if (val == null) {return;} return getQueryParam(val); }); const reload = () => option.methods.pageMethods.reload(); return { model, reload }; }