import {iUrlResponseData} from "../utils/TableOption.url"; import {eTableOptionStatus} from "./useTableOption.confirm"; import {createEffects} from "@peryl/utils/createEffects"; import {onBeforeUnmount} from "@peryl/react-compose"; import {TableOptionSpace} from "../utils/TableOption.space"; import '../utils/TableOption.space'; import {AutoModule} from "../utils/AutoModule"; import {delay} from "@peryl/utils/delay"; declare module '../utils/TableOption.space' { namespace TableOptionSpace { interface iTableOption { parent: ReturnType; } } } export const useTableOptionParent = AutoModule.createRegistration(( { config, hooks, state, methods, pagination, } ) => { const { effects } = createEffects(); const parentMap = config.parentMap; const parentOption = config.parentOption as TableOptionSpace.iTableOption; if (!parentOption || !parentMap) { return; } const isDestroy = () => !state.getAutoTable(); /*自己不加载数据,等父表选中数据之后再加载*/ config.loadOnStart = false; /*父表切换选中行之后,重新加载数据*/ effects.push( parentOption.hooks.onCurrentChange.use(async (selectData) => { if (isDestroy()) {return;} const noParentData = !selectData || (() => { return Object.values(parentMap).every(parentKey => selectData[parentKey] == null); })(); if (noParentData) { /*父表没有值,则清空行数据*/ let responseData: iUrlResponseData['query'] = { success: true, code: "200", list: [], hasNext: false, }; responseData = await hooks.onAfterLoad.exec(responseData); responseData = await hooks.onLoaded.exec(responseData); if (!(state.list.length === 0 && responseData.list.length === 0)) { state.list = responseData.list; } hooks.onPageDataChange.exec({ ...pagination.methods.createInitialData(), size: pagination.state.size, hasNext: false, total: null, }); await hooks.onLoaded.exec(responseData); } else { await methods.pageMethods.reload(); } }) ); /*子表查询之前,带上父表的查询参数*/ effects.push( hooks.onQueryParam.use(async () => { if (isDestroy()) {return;} /*这里等待下一个宏任务执行,等 option.state.currentNode 刷新*/ await delay(0); const currentNode = await parentOption.methods.utils.getCurrentNode(); return { queries: Object.entries(parentMap).map(([childKey, parentKey], index) => ({ id: `parent_filter_${index}`, field: childKey, value: currentNode.data[parentKey], operator: '=' })) }; }) ); /*子表新建数据的时候,从父表拿到关联字段的值*/ effects.push( hooks.onHandleNewRow.use(async row => { if (isDestroy()) {return;} const parentNode = await parentOption.methods.utils.getCurrentNode(); if (!parentNode) { console.log(parentOption); throw new Error('TableOption: parent option has no current node when create new record in child option!'); } const { data } = parentNode; Object.entries(parentMap).forEach(([childKey, parentKey]) => { row[childKey] = data[parentKey]; }); }) ); effects.push( hooks.onPermitConfig.use((permit) => { if (isDestroy()) {return;} const parents: TableOptionSpace.iTableOption[] = (() => { let list: TableOptionSpace.iTableOption[] = []; let option = parentOption; while (!!option) { list.push(option); option = option.config.parentOption as any; } return list; })(); if (parents.some(i => i.confirm.state.status !== eTableOptionStatus.normal)) { if (!permit.disabled) {permit.disabled = {};} permit.disabled.insert = true; permit.disabled.update = true; permit.disabled.delete = true; } else { return; } }) ); onBeforeUnmount(() => {effects.clear();}); return {}; });