Skip to content

Commit 0638e32

Browse files
authored
revert: revert initialValues protect (#376)
* Revert "fix: initValues should not be modified if preserve is false (#370)" This reverts commit 23f4f5f. * chore: update gitignore
1 parent 395da2c commit 0638e32

File tree

8 files changed

+47
-192
lines changed

8 files changed

+47
-192
lines changed

.gitignore

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
# misc
1919
.DS_Store
2020
.vscode
21-
.idea
21+
.idea/
2222

2323
# umi
2424
.umi

docs/demo/initialValues.md

Lines changed: 0 additions & 4 deletions
This file was deleted.

docs/examples/initialValues.tsx

Lines changed: 0 additions & 67 deletions
This file was deleted.

src/useForm.ts

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,6 @@ import {
3535
setValue,
3636
setValues,
3737
} from './utils/valueUtil';
38-
import cloneDeep from './utils/cloneDeep';
3938

4039
type InvalidateFieldEntity = { INVALIDATE_NAME_PATH: InternalNamePath };
4140

@@ -134,9 +133,7 @@ export class FormStore {
134133
}
135134
};
136135

137-
private getInitialValue = (namePath: InternalNamePath) => {
138-
return cloneDeep(getValue(this.initialValues, namePath));
139-
};
136+
private getInitialValue = (namePath: InternalNamePath) => getValue(this.initialValues, namePath);
140137

141138
private setCallbacks = (callbacks: Callbacks) => {
142139
this.callbacks = callbacks;
@@ -552,13 +549,14 @@ export class FormStore {
552549
// un-register field callback
553550
return (isListField?: boolean, preserve?: boolean, subNamePath: InternalNamePath = []) => {
554551
this.fieldEntities = this.fieldEntities.filter(item => item !== entity);
552+
555553
// Clean up store value if not preserve
556554
const mergedPreserve = preserve !== undefined ? preserve : this.preserve;
557555

558556
if (mergedPreserve === false && (!isListField || subNamePath.length > 1)) {
559557
const namePath = entity.getNamePath();
560558

561-
const defaultValue = isListField ? undefined : this.getInitialValue(namePath);
559+
const defaultValue = isListField ? undefined : getValue(this.initialValues, namePath);
562560

563561
if (
564562
namePath.length &&

src/utils/cloneDeep.ts

Lines changed: 0 additions & 25 deletions
This file was deleted.

src/utils/valueUtil.ts

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -44,11 +44,7 @@ export function containsNamePath(namePathList: InternalNamePath[], namePath: Int
4444
}
4545

4646
function isObject(obj: StoreValue) {
47-
return (
48-
typeof obj === 'object' &&
49-
obj !== null &&
50-
(Object.getPrototypeOf(obj) === Object.prototype || Array.isArray(obj))
51-
);
47+
return typeof obj === 'object' && obj !== null && Object.getPrototypeOf(obj) === Object.prototype;
5248
}
5349

5450
/**
@@ -66,11 +62,9 @@ function internalSetValues<T>(store: T, values: T): T {
6662
const prevValue = newStore[key];
6763
const value = values[key];
6864

69-
const recursive = isObject(value);
70-
const isArrayValue = Array.isArray(value);
71-
newStore[key] = recursive
72-
? internalSetValues(prevValue || (isArrayValue ? [] : {}), value || {})
73-
: value;
65+
// If both are object (but target is not array), we use recursion to set deep value
66+
const recursive = isObject(prevValue) && isObject(value);
67+
newStore[key] = recursive ? internalSetValues(prevValue, value || {}) : value;
7468
});
7569

7670
return newStore;

tests/initialValue.test.js

Lines changed: 39 additions & 71 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1-
import React, { useState } from 'react';
1+
import React from 'react';
22
import { mount } from 'enzyme';
33
import { resetWarned } from 'rc-util/lib/warning';
4-
import Form, { Field, useForm, List } from '../src';
4+
import Form, { Field, useForm } from '../src';
55
import { Input } from './common/InfoField';
66
import { changeValue, getField } from './common';
77

@@ -47,8 +47,16 @@ describe('Form.InitialValues', () => {
4747
path2: 'Bamboo',
4848
},
4949
});
50-
expect(getField(wrapper, 'username').find('input').props().value).toEqual('Light');
51-
expect(getField(wrapper, ['path1', 'path2']).find('input').props().value).toEqual('Bamboo');
50+
expect(
51+
getField(wrapper, 'username')
52+
.find('input')
53+
.props().value,
54+
).toEqual('Light');
55+
expect(
56+
getField(wrapper, ['path1', 'path2'])
57+
.find('input')
58+
.props().value,
59+
).toEqual('Bamboo');
5260
});
5361

5462
it('update and reset should use new initialValues', () => {
@@ -83,15 +91,23 @@ describe('Form.InitialValues', () => {
8391
expect(form.getFieldsValue()).toEqual({
8492
username: 'Bamboo',
8593
});
86-
expect(getField(wrapper, 'username').find('input').props().value).toEqual('Bamboo');
94+
expect(
95+
getField(wrapper, 'username')
96+
.find('input')
97+
.props().value,
98+
).toEqual('Bamboo');
8799

88100
// Should not change it
89101
wrapper.setProps({ initialValues: { username: 'Light' } });
90102
wrapper.update();
91103
expect(form.getFieldsValue()).toEqual({
92104
username: 'Bamboo',
93105
});
94-
expect(getField(wrapper, 'username').find('input').props().value).toEqual('Bamboo');
106+
expect(
107+
getField(wrapper, 'username')
108+
.find('input')
109+
.props().value,
110+
).toEqual('Bamboo');
95111

96112
// Should change it
97113
form.resetFields();
@@ -100,68 +116,11 @@ describe('Form.InitialValues', () => {
100116
expect(form.getFieldsValue()).toEqual({
101117
username: 'Light',
102118
});
103-
expect(getField(wrapper, 'username').find('input').props().value).toEqual('Light');
104-
});
105-
106-
it(`initialValues shouldn't be modified if preserve is false`, () => {
107-
const formValue = {
108-
test: 'test',
109-
users: [{ first: 'aaa', last: 'bbb' }],
110-
};
111-
112-
const Demo = () => {
113-
const [form] = Form.useForm();
114-
const [show, setShow] = useState(false);
115-
116-
return (
117-
<>
118-
<button onClick={() => setShow(prev => !prev)}>switch show</button>
119-
{show && (
120-
<Form form={form} initialValues={formValue} preserve={false}>
121-
<Field shouldUpdate>
122-
{() => (
123-
<Field name="test" preserve={false}>
124-
<Input />
125-
</Field>
126-
)}
127-
</Field>
128-
<List name="users">
129-
{fields => (
130-
<>
131-
{fields.map(({ key, name, ...restField }) => (
132-
<>
133-
<Field
134-
{...restField}
135-
name={[name, 'first']}
136-
rules={[{ required: true, message: 'Missing first name' }]}
137-
>
138-
<Input className="first-name-input" placeholder="First Name" />
139-
</Field>
140-
<Field
141-
{...restField}
142-
name={[name, 'last']}
143-
rules={[{ required: true, message: 'Missing last name' }]}
144-
>
145-
<Input placeholder="Last Name" />
146-
</Field>
147-
</>
148-
))}
149-
</>
150-
)}
151-
</List>
152-
</Form>
153-
)}
154-
</>
155-
);
156-
};
157-
158-
const wrapper = mount(<Demo />);
159-
wrapper.find('button').simulate('click');
160-
expect(formValue.users[0].last).toEqual('bbb');
161-
wrapper.find('button').simulate('click');
162-
expect(formValue.users[0].last).toEqual('bbb');
163-
wrapper.find('button').simulate('click');
164-
expect(wrapper.find('.first-name-input').first().find('input').instance().value).toEqual('aaa');
119+
expect(
120+
getField(wrapper, 'username')
121+
.find('input')
122+
.props().value,
123+
).toEqual('Light');
165124
});
166125

167126
describe('Field with initialValue', () => {
@@ -278,12 +237,21 @@ describe('Form.InitialValues', () => {
278237
expect(wrapper.find('input').props().value).toEqual('story');
279238

280239
// First reset will get nothing
281-
wrapper.find('button').first().simulate('click');
240+
wrapper
241+
.find('button')
242+
.first()
243+
.simulate('click');
282244
expect(wrapper.find('input').props().value).toEqual('');
283245

284246
// Change field initialValue and reset
285-
wrapper.find('button').last().simulate('click');
286-
wrapper.find('button').first().simulate('click');
247+
wrapper
248+
.find('button')
249+
.last()
250+
.simulate('click');
251+
wrapper
252+
.find('button')
253+
.first()
254+
.simulate('click');
287255
expect(wrapper.find('input').props().value).toEqual('light');
288256
});
289257

tests/utils.test.js

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
import { move, isSimilar, setValues } from '../src/utils/valueUtil';
22
import NameMap from '../src/utils/NameMap';
3-
import cloneDeep from '../src/utils/cloneDeep';
43

54
describe('utils', () => {
65
describe('arrayMove', () => {
@@ -72,12 +71,4 @@ describe('utils', () => {
7271
});
7372
});
7473
});
75-
76-
describe('clone deep', () => {
77-
it('should not deep clone Class', () => {
78-
const data = { a: new Date() };
79-
const clonedData = cloneDeep(data);
80-
expect(data.a === clonedData.a).toBeTruthy();
81-
});
82-
});
8374
});

0 commit comments

Comments
 (0)