refactor: enable child collection support current object (#2188)

* refactor: enable child collection support current object

* refactor: locale improve
This commit is contained in:
katherinehhh 2023-07-06 11:30:45 +08:00 committed by GitHub
parent c9b726916c
commit 566668daa1
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
7 changed files with 35 additions and 16 deletions

View File

@ -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",
};

View File

@ -617,5 +617,5 @@ export default {
"Create":"新規のみ" ,
"Current form":"現在のフォーム",
"Current object":"現在のオブジェクト",
"Linkage form form data":"フォームデータから連動",
"Linkage with form fields":"フォームデータから連動",
}

View File

@ -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":"从表单字段联动",
}

View File

@ -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;

View File

@ -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);
}, []);

View File

@ -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'],
},

View File

@ -59,7 +59,7 @@ export const useIterationVariable = ({
blockForm?: any;
collectionField: any;
operator?: any;
schema: any;
schema?: any;
level?: number;
rootCollection?: string;
}) => {