import {eAutoTableFilterMode} from "./useTableOption.filter.state"; import {FilterFormSingle, iFilterFormSingleData} from "../../FilterFormSingle"; import {computed, reactive, useRefs} from "@peryl/react-compose"; import {deepcopy} from "@peryl/utils/deepcopy"; import {$message} from "../../$message"; import {TableOptionSpace} from "../utils/TableOption.space"; import '../utils/TableOption.space'; import {TableOptionUtils} from "../utils/TableOption.utils"; import i18n from "../../i18n"; import {AutoModule} from "../utils/AutoModule"; declare module '../utils/TableOption.space' { namespace TableOptionSpace { interface iTableOption { filterSearch: ReturnType; } } } export const useTableOptionFilterSearch = AutoModule.createRegistration(( { hooks, methods, filterState, config, } ) => { const { refs, onRef } = useRefs({ filter: FilterFormSingle }); /** * 绑定的筛选参数 * @author 韦胜健 * @date 2023.1.8 19:05 */ const state = reactive({ data: undefined as undefined | iFilterFormSingleData, }); /** * 搜索的字段信息 * @author 韦胜健 * @date 2023.1.8 19:06 */ const filterOptions = filterState.createComputedFilterOptions(eAutoTableFilterMode.filterSearch); const handler = { /** * 当外部的搜索时 * 只所以直接调用reload,而不采取这里获取值的原因是因为,可能点击其他的查询按钮触发的reload,此时一样要获取最新的查询参数 * 取搜索值的时候固定取外部的filter form single的值 * @author 韦胜健 * @date 2023.1.8 19:06 */ onNormalSearch: async () => { await methods.pageMethods.reload(); }, /** * 当设置面板中的搜索栏值变化时取出来变化的值,更新到外部的filter form single组件实例中 * @author 韦胜健 * @date 2023.1.8 19:07 */ onSettingSearch: (val: iFilterFormSingleData) => { refs.filter?.update(val); methods.pageMethods.reload(); }, }; const filterSearch = { refs, onRef, filterOptions, handler, state, }; if (config.hideFilterSearch) {return filterSearch;} /** * 申请缓存 * @author 韦胜健 * @date 2023.1.8 19:08 */ filterState.useState({ seq: 1, key: 'search-filter', title: i18n.$it('table.searchBar').d('搜索栏'), getActiveFilterCount: () => { const length = state.data?.filterQueryParam.queries?.length; return length == null ? 0 : length; }, applyCache({ cacheData }) { state.data = cacheData; refs.filter?.update(cacheData); }, getCache: () => { return state.data; }, getDisplay: () => { return () => ( !!filterOptions.value && ( ) ); }, clear: () => { if (!state.data) {return;} state.data = undefined; refs.filter?.update(undefined); }, }); /** * 获取查询参数时返回当前绑定的参数 * @author 韦胜健 * @date 2023.1.8 19:09 */ hooks.onQueryParam.use(async () => { /*每次查询之前获取组件实例中最新的筛选参数*/ !!refs.filter && (state.data = deepcopy(await refs.filter!.getData())); if (!!state.data && state.data.filterQueryParam && Object.keys(state.data.filterQueryParam).length > 0) { return state.data.filterQueryParam; } }); /** * 渲染左侧操作栏时渲染搜索表单 * @author 韦胜健 * @date 2023.1.8 19:09 */ hooks.onRenderLeftOperation.use({ processor: ({ list, render }) => {list.push({ seq: 0, key: 'search', render, });}, render: () => ( !!filterOptions.value && filterOptions.value.length > 0 && ( ) ) }); const tips = computed((): TableOptionSpace.iTableOptionTipMeta | void | null => { const data = state.data; if (!data || !data.filterQueryParam.queries?.length || !filterOptions.value) {return;} let filterOption = filterOptions.value.find(i => i.field === data.field); if (!filterOption) {return;} filterOption = { ...filterOption, filterHandler: data.filterHandler }; return TableOptionUtils.createTableOptionTipMeta({ filterOption, formData: data.formData, onClick: () => {$message(i18n.$it('table.pleaseEditSearchParam').d('请直接编辑搜索栏筛选参数'));}, onClear: () => { data.filterQueryParam = {}; refs.filter?.update({ ...data, }); methods.pageMethods.reload(); }, }); }); hooks.onTips.use((list) => {!!tips.value && list.push(tips.value);}); return filterSearch; });