From b98a63fee638b5c1a2aabb80d6b424a063df2b3e Mon Sep 17 00:00:00 2001 From: Zeke Zhang <958414905@qq.com> Date: Mon, 30 Sep 2024 20:17:58 +0800 Subject: [PATCH] fix(variable): fix the issue of inherited table fields not being able to use variables normally (#5346) --- .../form-item/hooks/useParseDefaultValue.ts | 55 +++++++++++++++---- 1 file changed, 45 insertions(+), 10 deletions(-) diff --git a/packages/core/client/src/schema-component/antd/form-item/hooks/useParseDefaultValue.ts b/packages/core/client/src/schema-component/antd/form-item/hooks/useParseDefaultValue.ts index eb170b8af4..e935e42fa9 100644 --- a/packages/core/client/src/schema-component/antd/form-item/hooks/useParseDefaultValue.ts +++ b/packages/core/client/src/schema-component/antd/form-item/hooks/useParseDefaultValue.ts @@ -16,8 +16,10 @@ import { useCallback, useEffect } from 'react'; import { useRecordIndex } from '../../../../../src/record-provider'; import { useOperators } from '../../../../block-provider/CollectOperators'; import { useFormBlockContext } from '../../../../block-provider/FormBlockProvider'; -import { useCollection_deprecated } from '../../../../collection-manager'; +import { InheritanceCollectionMixin, useCollection_deprecated } from '../../../../collection-manager'; import { useCollectionRecord } from '../../../../data-source/collection-record/CollectionRecordProvider'; +import { DataSourceManager } from '../../../../data-source/data-source/DataSourceManager'; +import { useDataSourceManager } from '../../../../data-source/data-source/DataSourceManagerProvider'; import { useFlag } from '../../../../flag-provider'; import { DEBOUNCE_WAIT, useLocalVariables, useVariables } from '../../../../variables'; import { getPath } from '../../../../variables/utils/getPath'; @@ -43,6 +45,7 @@ const useParseDefaultValue = () => { const index = useRecordIndex(); const { type, form } = useFormBlockContext(); const { getOperator } = useOperators(); + const dm = useDataSourceManager(); /** * name: 如 $user @@ -97,16 +100,25 @@ const useParseDefaultValue = () => { } } - const { value: parsedValue, collectionName: collectionNameOfVariable } = await variables.parseVariable( - fieldSchema.default, - localVariables, - { - fieldOperator: getOperator(fieldSchema.name), - }, - ); + const { + value: parsedValue, + collectionName: collectionNameOfVariable, + dataSource = 'main', + } = await variables.parseVariable(fieldSchema.default, localVariables, { + fieldOperator: getOperator(fieldSchema.name), + }); // fix https://tasks.aliyun.nocobase.com/admin/ugmnj2ycfgg/popups/1qlw5c38t3b/puid/dz42x7ffr7i/filterbytk/199 - if (collectionField.target && collectionField.target !== collectionNameOfVariable) { + if ( + collectionField.target && + collectionField.target !== collectionNameOfVariable && + !isInherit({ + collectionName: collectionField.target, + targetCollectionName: collectionNameOfVariable, + dm, + dataSource, + }) + ) { field.loading = false; return; } @@ -179,7 +191,30 @@ const useParseDefaultValue = () => { // 解决子表格(或子表单)中新增一行数据时,默认值不生效的问题 field.setValue(fieldSchema.default); } - }, [fieldSchema.default, localVariables, type, getOperator]); + }, [fieldSchema.default, localVariables, type, getOperator, dm]); }; export default useParseDefaultValue; + +/** + * Determine if there is an inheritance relationship between two data tables + * @param param0 + * @returns + */ +const isInherit = ({ + collectionName, + targetCollectionName, + dm, + dataSource, +}: { + collectionName: string; + targetCollectionName: string; + dm: DataSourceManager; + dataSource: string; +}) => { + const cm = dm?.getDataSource(dataSource)?.collectionManager; + return cm + ?.getCollection(collectionName) + ?.getAllCollectionsInheritChain() + ?.includes(targetCollectionName); +};