fix(data-templates): fix subform

This commit is contained in:
Rairn 2023-04-16 17:24:43 +08:00
parent 2a05e60504
commit 6abe97291e
2 changed files with 18 additions and 11 deletions

View File

@ -1,6 +1,6 @@
import { createForm, onFieldReact, onFormInputChange, onFormValuesChange } from '@formily/core'; import { createForm, onFormValuesChange } from '@formily/core';
import { ArrayField, Field, ObjectField } from '@formily/core'; import { useField } from '@formily/react';
import { useField, useFieldSchema } from '@formily/react'; import { autorun } from '@formily/reactive';
import { Spin } from 'antd'; import { Spin } from 'antd';
import React, { createContext, useContext, useEffect, useMemo } from 'react'; import React, { createContext, useContext, useEffect, useMemo } from 'react';
import { RecordProvider } from '../record-provider'; import { RecordProvider } from '../record-provider';
@ -32,6 +32,15 @@ const InternalFormFieldProvider = (props) => {
[], [],
); );
// 当使用数据模板时,会 formBlockCtx.form.values 会被整体赋值,这时候需要同步到 form.values
useEffect(() => {
const dispose = autorun(() => {
form.values = formBlockCtx?.form?.values[fieldName] || form.values;
});
return dispose;
}, []);
const { resource, service } = useBlockRequestContext(); const { resource, service } = useBlockRequestContext();
if (service.loading) { if (service.loading) {
return <Spin />; return <Spin />;
@ -52,9 +61,8 @@ const InternalFormFieldProvider = (props) => {
{props.children} {props.children}
</FormFieldContext.Provider> </FormFieldContext.Provider>
</RecordProvider> </RecordProvider>
); );
} };
export const WithoutFormFieldResource = createContext(null); export const WithoutFormFieldResource = createContext(null);
@ -65,8 +73,8 @@ export const FormFieldProvider = (props) => {
<InternalFormFieldProvider {...props} /> <InternalFormFieldProvider {...props} />
</BlockProvider> </BlockProvider>
</WithoutFormFieldResource.Provider> </WithoutFormFieldResource.Provider>
) );
} };
export const useFormFieldContext = () => { export const useFormFieldContext = () => {
return useContext(FormFieldContext); return useContext(FormFieldContext);
@ -80,5 +88,4 @@ export const useFormFieldProps = () => {
return { return {
form: ctx.form, form: ctx.form,
}; };
};
}

View File

@ -1,4 +1,4 @@
import { observer, useField, useFieldSchema, useForm } from '@formily/react'; import { observer, useField, useFieldSchema } from '@formily/react';
import React, { useEffect } from 'react'; import React, { useEffect } from 'react';
import { useFormBlockContext } from '../../../block-provider'; import { useFormBlockContext } from '../../../block-provider';
import { useCollection } from '../../../collection-manager'; import { useCollection } from '../../../collection-manager';