From 80779c3a980ace528049347621bd57310dc11506 Mon Sep 17 00:00:00 2001 From: Junyi Date: Wed, 26 Jul 2023 14:38:51 +0700 Subject: [PATCH] fix(plugin-workflow): fix expression field in sub-form (#2324) --- .../src/client/components/DynamicExpression.tsx | 14 +++++++++----- 1 file changed, 9 insertions(+), 5 deletions(-) diff --git a/packages/plugins/workflow/src/client/components/DynamicExpression.tsx b/packages/plugins/workflow/src/client/components/DynamicExpression.tsx index 385b998f7..777173bd2 100644 --- a/packages/plugins/workflow/src/client/components/DynamicExpression.tsx +++ b/packages/plugins/workflow/src/client/components/DynamicExpression.tsx @@ -1,5 +1,5 @@ import { onFieldInputValueChange, onFormInitialValuesChange } from '@formily/core'; -import { connect, mapReadPretty, observer, useForm, useFormEffects } from '@formily/react'; +import { connect, mapReadPretty, observer, useField, useForm, useFormEffects } from '@formily/react'; import { Tag } from 'antd'; import React, { useMemo, useState } from 'react'; import { useTranslation } from 'react-i18next'; @@ -12,16 +12,20 @@ import { getCollectionFieldOptions } from '../variable'; const InternalExpression = observer( (props: any) => { const { onChange } = props; - const { values } = useForm(); - const [collection, setCollection] = useState(values?.sourceCollection); + const field = useField(); + // TODO(refactor): better to provide another context like useFieldset() + const form = useForm(); + const basePath = field.path.segments.slice(0, -1); + const collectionPath = [...basePath, 'sourceCollection'].join('.'); + const [collection, setCollection] = useState(form.getValuesIn(collectionPath)); const compile = useCompile(); const { getCollectionFields } = useCollectionManager(); useFormEffects(() => { onFormInitialValuesChange((form) => { - setCollection(form.values.sourceCollection); + setCollection(form.getValuesIn(collectionPath)); }); - onFieldInputValueChange('sourceCollection', (f) => { + onFieldInputValueChange(collectionPath, (f) => { setCollection(f.value); onChange(null); });