fix(association-field): form.get & set valuesIn field.path (#1972)

* fix: useCreateActionProps

* refactor: code improve

* fix: form.getValuesIn(field.path)

---------

Co-authored-by: katherinehhh <katherine_15995@163.com>
This commit is contained in:
chenos 2023-06-02 00:55:12 +08:00 committed by GitHub
parent 7e30b046d9
commit 295faf569d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -1,3 +1,4 @@
import { Field } from '@formily/core';
import { observer, useField, useFieldSchema, useForm } from '@formily/react';
import React from 'react';
import { SchemaComponentOptions } from '../../';
@ -13,7 +14,7 @@ import { useAssociationFieldContext } from './hooks';
const EditableAssociationField = observer((props: any) => {
const { multiple } = props;
const field: any = useField();
const field: Field = useField();
const form = useForm();
const fieldSchema = useFieldSchema();
const { options: collectionField, currentMode } = useAssociationFieldContext();
@ -27,22 +28,13 @@ const EditableAssociationField = observer((props: any) => {
const { data } = actionField.data?.data?.data || {};
if (data) {
if (['m2m', 'o2m'].includes(collectionField?.interface) && multiple !== false) {
const values = JSON.parse(JSON.stringify(form.values[fieldSchema.name] || []));
values.push({
...data,
});
setTimeout(() => {
form.setValuesIn(field.props.name, values);
field.onInput(values)
}, 100);
const values = form.getValuesIn(field.path) || [];
values.push(data);
form.setValuesIn(field.path, values);
field.onInput(values);
} else {
const value = {
...data,
};
setTimeout(() => {
form.setValuesIn(field.props.name, value);
field.onInput(value)
}, 100);
form.setValuesIn(field.path, data);
field.onInput(data);
}
}
},