fix(variable): fix the issue of inherited table fields not being able to use variables normally (#5346)
Some checks failed
auto-merge / push-commit (push) Has been cancelled
Build Docker Image / build-and-push (push) Has been cancelled
Build Pro Image / build-and-push (push) Has been cancelled
deploy client docs / Build (push) Has been cancelled
E2E / Build (push) Has been cancelled
NocoBase FrontEnd Test / frontend-test (18) (push) Has been cancelled
E2E / Core and plugins (push) Has been cancelled
E2E / plugin-workflow (push) Has been cancelled
E2E / plugin-workflow-approval (push) Has been cancelled
E2E / plugin-data-source-main (push) Has been cancelled
E2E / Comment on PR (push) Has been cancelled

This commit is contained in:
Zeke Zhang 2024-09-30 20:17:58 +08:00 committed by GitHub
parent c273f33081
commit b98a63fee6
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -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<InheritanceCollectionMixin>(collectionName)
?.getAllCollectionsInheritChain()
?.includes(targetCollectionName);
};