From 6abe97291ebc06da401d48ef80bdaccfc907bc04 Mon Sep 17 00:00:00 2001 From: Rairn <958414905@qq.com> Date: Sun, 16 Apr 2023 17:24:43 +0800 Subject: [PATCH] fix(data-templates): fix subform --- .../src/block-provider/FormFieldProvider.tsx | 25 ++++++++++++------- .../antd/form-v2/FormField.tsx | 4 +-- 2 files changed, 18 insertions(+), 11 deletions(-) diff --git a/packages/core/client/src/block-provider/FormFieldProvider.tsx b/packages/core/client/src/block-provider/FormFieldProvider.tsx index bdb1fec57..989363513 100644 --- a/packages/core/client/src/block-provider/FormFieldProvider.tsx +++ b/packages/core/client/src/block-provider/FormFieldProvider.tsx @@ -1,6 +1,6 @@ -import { createForm, onFieldReact, onFormInputChange, onFormValuesChange } from '@formily/core'; -import { ArrayField, Field, ObjectField } from '@formily/core'; -import { useField, useFieldSchema } from '@formily/react'; +import { createForm, onFormValuesChange } from '@formily/core'; +import { useField } from '@formily/react'; +import { autorun } from '@formily/reactive'; import { Spin } from 'antd'; import React, { createContext, useContext, useEffect, useMemo } from 'react'; 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(); if (service.loading) { return ; @@ -52,9 +61,8 @@ const InternalFormFieldProvider = (props) => { {props.children} - ); -} +}; export const WithoutFormFieldResource = createContext(null); @@ -65,8 +73,8 @@ export const FormFieldProvider = (props) => { - ) -} + ); +}; export const useFormFieldContext = () => { return useContext(FormFieldContext); @@ -80,5 +88,4 @@ export const useFormFieldProps = () => { return { form: ctx.form, }; - -} +}; diff --git a/packages/core/client/src/schema-component/antd/form-v2/FormField.tsx b/packages/core/client/src/schema-component/antd/form-v2/FormField.tsx index 11f913bb0..547e39881 100644 --- a/packages/core/client/src/schema-component/antd/form-v2/FormField.tsx +++ b/packages/core/client/src/schema-component/antd/form-v2/FormField.tsx @@ -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 { useFormBlockContext } from '../../../block-provider'; import { useCollection } from '../../../collection-manager'; @@ -21,4 +21,4 @@ export const FormField: any = observer((props) => { } }, []); return
{props.children}
; -}); \ No newline at end of file +});