diff --git a/packages/core/client/src/locale/en_US.ts b/packages/core/client/src/locale/en_US.ts index 2999d925a..1bf78d5ad 100644 --- a/packages/core/client/src/locale/en_US.ts +++ b/packages/core/client/src/locale/en_US.ts @@ -706,5 +706,5 @@ export default { "Create":"Create", "Current form": "Current form", "Current object":"Current object", - "Linkage form form data":"Linkage form form data", + "Linkage with form fields":"Linkage with form fields", }; diff --git a/packages/core/client/src/locale/ja_JP.ts b/packages/core/client/src/locale/ja_JP.ts index dcbf53e0e..52913291f 100644 --- a/packages/core/client/src/locale/ja_JP.ts +++ b/packages/core/client/src/locale/ja_JP.ts @@ -617,5 +617,5 @@ export default { "Create":"新規のみ" , "Current form":"現在のフォーム", "Current object":"現在のオブジェクト", - "Linkage form form data":"フォームデータから連動", + "Linkage with form fields":"フォームデータから連動", } diff --git a/packages/core/client/src/locale/zh_CN.ts b/packages/core/client/src/locale/zh_CN.ts index 7d32b8794..8b8f80055 100644 --- a/packages/core/client/src/locale/zh_CN.ts +++ b/packages/core/client/src/locale/zh_CN.ts @@ -790,5 +790,5 @@ export default { "File manager": "文件管理器", "Direct duplicate": "直接复制", "Copy into the form and continue to fill in": "复制到表单并继续填写", - "Linkage form form data":"从表单数据里联动", + "Linkage with form fields":"从表单字段联动", } diff --git a/packages/core/client/src/schema-initializer/components/CreateRecordAction.tsx b/packages/core/client/src/schema-initializer/components/CreateRecordAction.tsx index 2b60a215b..3694fd278 100644 --- a/packages/core/client/src/schema-initializer/components/CreateRecordAction.tsx +++ b/packages/core/client/src/schema-initializer/components/CreateRecordAction.tsx @@ -121,9 +121,18 @@ export const CreateRecordAction = observer( { displayName: 'CreateRecordAction' }, ); -function getLinkageCollection(str, form) { - const data = parseVariables(str, { $form: form.values }); - return data; +function getLinkageCollection(str, form, field) { + const variablesCtx = { $form: form.values, $iteration: form.values }; + if (str.includes('$iteration')) { + const path = field.path.segments.concat([]); + path.splice(-2); + str = str.replace('$iteration.', `$iteration.${path.join('.')}.`); + const data = parseVariables(str, variablesCtx); + return data; + } else { + const data = parseVariables(str, { $form: form.values }); + return data; + } } export const CreateAction = observer( (props: any) => { @@ -222,7 +231,7 @@ export const CreateAction = observer( danger={componentType === 'danger'} icon={icon} onClick={(info) => { - const collectionName = getLinkageCollection(linkageFromForm, form); + const collectionName = getLinkageCollection(linkageFromForm, form, field); const targetCollection = inheritsCollections.find((v) => v.name === collectionName) ? collectionName : collection.name; diff --git a/packages/core/client/src/schema-settings/EnableChildCollections/DynamicComponent.tsx b/packages/core/client/src/schema-settings/EnableChildCollections/DynamicComponent.tsx index 6c1f4a753..f705a5e95 100644 --- a/packages/core/client/src/schema-settings/EnableChildCollections/DynamicComponent.tsx +++ b/packages/core/client/src/schema-settings/EnableChildCollections/DynamicComponent.tsx @@ -4,15 +4,22 @@ import React, { useEffect, useMemo } from 'react'; import { useCompile } from '../../schema-component'; import { Variable } from '.././../schema-component'; import { useFormVariable } from '../VariableInput/hooks/useFormVariable'; +import { useIterationVariable } from '../VariableInput/hooks/useIterationVariable'; export const ChildDynamicComponent = observer( - (props: { collectionName: string; form: any; onChange; value; default }) => { - const { form, collectionName, onChange, value } = props; - const formVariabele = useFormVariable({ blockForm: form, rootCollection: collectionName }); - const compile = useCompile(); - const result = useMemo(() => [formVariabele].filter(Boolean), [formVariabele]); - const scope = compile(result); + (props: { rootCollection: string; form: any; onChange; value; default; collectionField }) => { + const { form, rootCollection, onChange, value, collectionField } = props; const fieldSchema = useFieldSchema(); + const formVariabele = useFormVariable({ blockForm: form, rootCollection }); + const iterationVariabele = useIterationVariable({ + blockForm: form, + collectionField, + rootCollection, + }); + + const compile = useCompile(); + const result = useMemo(() => [formVariabele, iterationVariabele].filter(Boolean), [formVariabele]); + const scope = compile(result); useEffect(() => { onChange(fieldSchema.default); }, []); diff --git a/packages/core/client/src/schema-settings/SchemaSettings.tsx b/packages/core/client/src/schema-settings/SchemaSettings.tsx index 7dc46e9b6..d1b8f61cc 100644 --- a/packages/core/client/src/schema-settings/SchemaSettings.tsx +++ b/packages/core/client/src/schema-settings/SchemaSettings.tsx @@ -47,6 +47,7 @@ import { SchemaComponentContext, SchemaComponentOptions, useAPIClient, + useBlockRequestContext, useCollection, useCollectionManager, useCompile, @@ -1180,6 +1181,7 @@ SchemaSettings.EnableChildCollections = function EnableChildCollectionsItem(prop const allowAddToCurrent = fieldSchema?.['x-allow-add-to-current']; const form = useForm(); const { getCollectionJoinField } = useCollectionManager(); + const ctx = useBlockRequestContext(); const collectionField = getCollectionJoinField(fieldSchema?.parent?.['x-collection-field']) || {}; const isAssocationAdd = fieldSchema?.parent?.['x-component'] === 'CollectionField'; return ( @@ -1212,13 +1214,14 @@ SchemaSettings.EnableChildCollections = function EnableChildCollectionsItem(prop }, linkageFromForm: { type: 'string', - title: "{{t('Linkage form form')}}", + title: "{{t('Linkage with form fields')}}", 'x-visible': '{{isAssocationAdd}}', 'x-decorator': 'FormItem', 'x-component': ChildDynamicComponent, 'x-component-props': { - collectionName: collectionField?.collectionName || collectionName, + rootCollection: ctx.props.collection || ctx.props.resource, form, + collectionField, }, default: fieldSchema?.['x-component-props']?.['linkageFromForm'], }, diff --git a/packages/core/client/src/schema-settings/VariableInput/hooks/useIterationVariable.ts b/packages/core/client/src/schema-settings/VariableInput/hooks/useIterationVariable.ts index ea0fae822..763a16924 100644 --- a/packages/core/client/src/schema-settings/VariableInput/hooks/useIterationVariable.ts +++ b/packages/core/client/src/schema-settings/VariableInput/hooks/useIterationVariable.ts @@ -59,7 +59,7 @@ export const useIterationVariable = ({ blockForm?: any; collectionField: any; operator?: any; - schema: any; + schema?: any; level?: number; rootCollection?: string; }) => {