fix: should lazy load association data in subform (#3012)

This commit is contained in:
被雨水过滤的空气-Rain 2023-11-10 11:00:14 +08:00 committed by GitHub
parent 3d0b42e72e
commit d72a64976e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -4,12 +4,13 @@ import { useEffect } from 'react';
import { useCollection, useCollectionManager } from '../../../../collection-manager';
import { useVariables } from '../../../../variables';
import { transformVariableValue } from '../../../../variables/utils/transformVariableValue';
import { useSubFormValue } from '../../association-field/hooks';
/**
* Form
*
* - Display association fields
* -
* -
*/
const useLazyLoadDisplayAssociationFieldsOfForm = () => {
const { name } = useCollection();
@ -18,10 +19,13 @@ const useLazyLoadDisplayAssociationFieldsOfForm = () => {
const fieldSchema = useFieldSchema();
const variables = useVariables();
const field = useField<Field>();
const { formValue: subFormValue } = useSubFormValue();
const schemaName = fieldSchema.name.toString();
const formValue = subFormValue || form.values;
useEffect(() => {
// 如果 schemaName 中是以 `.` 分割的,说明是一个关联字段,需要去获取关联字段的值;
// 在数据表管理页面,也存在 `a.b` 之类的 schema name其 collectionName 为 fields所以在这里排除一下 `name === 'fields'` 的情况
if (!schemaName.includes('.') || !variables || name === 'fields') {
return;
@ -29,7 +33,7 @@ const useLazyLoadDisplayAssociationFieldsOfForm = () => {
const formVariable = {
name: '$nForm',
ctx: form.values,
ctx: formValue,
collectionName: name,
};
const variableString = `{{ $nForm.${schemaName} }}`;
@ -47,7 +51,7 @@ const useLazyLoadDisplayAssociationFieldsOfForm = () => {
.catch((err) => {
console.error(err);
});
}, [schemaName.includes('.') ? form.values[schemaName.split('.')[0]] : null]);
}, [schemaName.includes('.') ? formValue[schemaName.split('.')[0]] : null]);
};
export default useLazyLoadDisplayAssociationFieldsOfForm;