(store: T, values: T): T {
// If both are object (but target is not array), we use recursion to set deep value
const recursive = isObject(prevValue) && isObject(value);
- newStore[key] = recursive ? internalSetValues(prevValue, value || {}) : value;
+
+ newStore[key] = recursive ? internalSetValues(prevValue, value || {}) : cloneDeep(value); // Clone deep for arrays
});
return newStore;
diff --git a/tests/index.test.js b/tests/index.test.js
index 3168dc39..5870598a 100644
--- a/tests/index.test.js
+++ b/tests/index.test.js
@@ -726,4 +726,81 @@ describe('Form.Basic', () => {
wrapper.find('Input').simulate('change', { event: { target: { value: 'Light' } } });
}).not.toThrowError();
});
+
+ it('setFieldsValue for List should work', () => {
+ const Demo = () => {
+ const [form] = useForm();
+
+ const handelReset = () => {
+ form.setFieldsValue({
+ users: [],
+ });
+ };
+
+ const initialValues = {
+ users: [{ name: '11' }, { name: '22' }],
+ };
+
+ return (
+
+ {(fields, { add, remove }) => (
+ <>
+ {fields.map(({ key, name, ...restField }) => (
+
+
+
+ ))}
+ >
+ )}
+
+
+
+
+
+ );
+ };
+
+ const wrapper = mount();
+ expect(wrapper.find('input').first().getDOMNode().value).toBe('11');
+ wrapper.find('.reset-btn').first().simulate('click');
+ expect(wrapper.find('input').length).toBe(0);
+ });
+
+ it('setFieldsValue should work for multiple Select', () => {
+ const Select = ({ value, defaultValue }) => {
+ return {(value || defaultValue || []).toString()}
;
+ };
+
+ const Demo = () => {
+ const [formInstance] = Form.useForm();
+
+ React.useEffect(() => {
+ formInstance.setFieldsValue({ selector: ['K1', 'K2'] });
+ }, [formInstance]);
+
+ return (
+
+ );
+ };
+
+ const wrapper = mount();
+ expect(wrapper.find('.select-div').text()).toBe('K1,K2');
+ });
});
diff --git a/tests/initialValue.test.js b/tests/initialValue.test.js
index f6ced1eb..80a8162f 100644
--- a/tests/initialValue.test.js
+++ b/tests/initialValue.test.js
@@ -1,7 +1,7 @@
-import React from 'react';
+import React, { useState } from 'react';
import { mount } from 'enzyme';
import { resetWarned } from 'rc-util/lib/warning';
-import Form, { Field, useForm } from '../src';
+import Form, { Field, useForm, List } from '../src';
import { Input } from './common/InfoField';
import { changeValue, getField } from './common';
@@ -47,16 +47,8 @@ describe('Form.InitialValues', () => {
path2: 'Bamboo',
},
});
- expect(
- getField(wrapper, 'username')
- .find('input')
- .props().value,
- ).toEqual('Light');
- expect(
- getField(wrapper, ['path1', 'path2'])
- .find('input')
- .props().value,
- ).toEqual('Bamboo');
+ expect(getField(wrapper, 'username').find('input').props().value).toEqual('Light');
+ expect(getField(wrapper, ['path1', 'path2']).find('input').props().value).toEqual('Bamboo');
});
it('update and reset should use new initialValues', () => {
@@ -91,11 +83,7 @@ describe('Form.InitialValues', () => {
expect(form.getFieldsValue()).toEqual({
username: 'Bamboo',
});
- expect(
- getField(wrapper, 'username')
- .find('input')
- .props().value,
- ).toEqual('Bamboo');
+ expect(getField(wrapper, 'username').find('input').props().value).toEqual('Bamboo');
// Should not change it
wrapper.setProps({ initialValues: { username: 'Light' } });
@@ -103,11 +91,7 @@ describe('Form.InitialValues', () => {
expect(form.getFieldsValue()).toEqual({
username: 'Bamboo',
});
- expect(
- getField(wrapper, 'username')
- .find('input')
- .props().value,
- ).toEqual('Bamboo');
+ expect(getField(wrapper, 'username').find('input').props().value).toEqual('Bamboo');
// Should change it
form.resetFields();
@@ -116,11 +100,68 @@ describe('Form.InitialValues', () => {
expect(form.getFieldsValue()).toEqual({
username: 'Light',
});
- expect(
- getField(wrapper, 'username')
- .find('input')
- .props().value,
- ).toEqual('Light');
+ expect(getField(wrapper, 'username').find('input').props().value).toEqual('Light');
+ });
+
+ it(`initialValues shouldn't be modified if preserve is false`, () => {
+ const formValue = {
+ test: 'test',
+ users: [{ first: 'aaa', last: 'bbb' }],
+ };
+
+ const Demo = () => {
+ const [form] = Form.useForm();
+ const [show, setShow] = useState(false);
+
+ return (
+ <>
+
+ {show && (
+
+ )}
+ >
+ );
+ };
+
+ const wrapper = mount();
+ wrapper.find('button').simulate('click');
+ expect(formValue.users[0].last).toEqual('bbb');
+ wrapper.find('button').simulate('click');
+ expect(formValue.users[0].last).toEqual('bbb');
+ wrapper.find('button').simulate('click');
+ expect(wrapper.find('.first-name-input').first().find('input').instance().value).toEqual('aaa');
});
describe('Field with initialValue', () => {
@@ -237,21 +278,12 @@ describe('Form.InitialValues', () => {
expect(wrapper.find('input').props().value).toEqual('story');
// First reset will get nothing
- wrapper
- .find('button')
- .first()
- .simulate('click');
+ wrapper.find('button').first().simulate('click');
expect(wrapper.find('input').props().value).toEqual('');
// Change field initialValue and reset
- wrapper
- .find('button')
- .last()
- .simulate('click');
- wrapper
- .find('button')
- .first()
- .simulate('click');
+ wrapper.find('button').last().simulate('click');
+ wrapper.find('button').first().simulate('click');
expect(wrapper.find('input').props().value).toEqual('light');
});
diff --git a/tests/utils.test.js b/tests/utils.test.js
index 6d727a8b..7bc47894 100644
--- a/tests/utils.test.js
+++ b/tests/utils.test.js
@@ -1,5 +1,6 @@
import { move, isSimilar, setValues } from '../src/utils/valueUtil';
import NameMap from '../src/utils/NameMap';
+import cloneDeep from '../src/utils/cloneDeep';
describe('utils', () => {
describe('arrayMove', () => {
@@ -71,4 +72,12 @@ describe('utils', () => {
});
});
});
+
+ describe('clone deep', () => {
+ it('should not deep clone Class', () => {
+ const data = { a: new Date() };
+ const clonedData = cloneDeep(data);
+ expect(data.a === clonedData.a).toBeTruthy();
+ });
+ });
});