fix: should lazy load association data in subform (#3012)
This commit is contained in:
parent
3d0b42e72e
commit
d72a64976e
@ -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;
|
||||
|
Loading…
Reference in New Issue
Block a user