Skip to content

Commit 8bc948f

Browse files
authored
refactor: Use rc-util for merge (#590)
* refactor: use rc-util * chore: bump rc-util * chore: update deps
1 parent ffd4b68 commit 8bc948f

File tree

6 files changed

+10
-94
lines changed

6 files changed

+10
-94
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@
5151
"dependencies": {
5252
"@babel/runtime": "^7.18.0",
5353
"async-validator": "^4.1.0",
54-
"rc-util": "^5.8.0"
54+
"rc-util": "^5.32.2"
5555
},
5656
"devDependencies": {
5757
"@testing-library/jest-dom": "^5.16.4",

src/useForm.ts

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ import type {
2525
WatchCallBack,
2626
} from './interface';
2727
import { allPromiseFinish } from './utils/asyncUtil';
28-
import cloneDeep from './utils/cloneDeep';
28+
import { merge } from 'rc-util/lib/utils/set';
2929
import { defaultValidateMessages } from './utils/messages';
3030
import NameMap from './utils/NameMap';
3131
import {
@@ -35,7 +35,6 @@ import {
3535
getValue,
3636
matchNamePath,
3737
setValue,
38-
setValues,
3938
} from './utils/valueUtil';
4039

4140
type InvalidateFieldEntity = { INVALIDATE_NAME_PATH: InternalNamePath };
@@ -141,7 +140,7 @@ export class FormStore {
141140
private setInitialValues = (initialValues: Store, init: boolean) => {
142141
this.initialValues = initialValues || {};
143142
if (init) {
144-
let nextStore = setValues({}, initialValues, this.store);
143+
let nextStore = merge(initialValues, this.store);
145144

146145
// We will take consider prev form unmount fields.
147146
// When the field is not `preserve`, we need fill this with initialValues instead of store.
@@ -170,7 +169,7 @@ export class FormStore {
170169
const initValue = getValue(this.initialValues, namePath);
171170

172171
// Not cloneDeep when without `namePath`
173-
return namePath.length ? cloneDeep(initValue) : initValue;
172+
return namePath.length ? merge(initValue) : initValue;
174173
};
175174

176175
private setCallbacks = (callbacks: Callbacks) => {
@@ -523,7 +522,7 @@ export class FormStore {
523522

524523
const prevStore = this.store;
525524
if (!nameList) {
526-
this.updateStore(setValues({}, this.initialValues));
525+
this.updateStore(merge(this.initialValues));
527526
this.resetWithFieldInitialValue();
528527
this.notifyObservers(prevStore, null, { type: 'reset' });
529528
this.notifyWatch();
@@ -743,7 +742,7 @@ export class FormStore {
743742
const prevStore = this.store;
744743

745744
if (store) {
746-
const nextStore = setValues(this.store, store);
745+
const nextStore = merge(this.store, store);
747746
this.updateStore(nextStore);
748747
}
749748

src/utils/cloneDeep.ts

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

src/utils/validateUtil.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import type {
99
RuleError,
1010
} from '../interface';
1111
import { defaultValidateMessages } from './messages';
12-
import { setValues } from './valueUtil';
12+
import { merge } from 'rc-util/lib/utils/set';
1313

1414
// Remove incorrect original ts define
1515
const AsyncValidator: any = RawAsyncValidator;
@@ -67,7 +67,7 @@ async function validateRule(
6767
[name]: [cloneRule],
6868
});
6969

70-
const messages = setValues({}, defaultValidateMessages, options.validateMessages);
70+
const messages = merge(defaultValidateMessages, options.validateMessages);
7171
validator.messages(messages);
7272

7373
let result = [];

src/utils/valueUtil.ts

Lines changed: 1 addition & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
11
import getValue from 'rc-util/lib/utils/get';
22
import setValue from 'rc-util/lib/utils/set';
3-
import type { InternalNamePath, NamePath, Store, StoreValue, EventArgs } from '../interface';
3+
import type { InternalNamePath, NamePath, Store, EventArgs } from '../interface';
44
import { toArray } from './typeUtil';
5-
import cloneDeep from '../utils/cloneDeep';
65

76
export { getValue, setValue };
87

@@ -31,41 +30,6 @@ export function containsNamePath(namePathList: InternalNamePath[], namePath: Int
3130
return namePathList && namePathList.some(path => matchNamePath(path, namePath));
3231
}
3332

34-
function isObject(obj: StoreValue) {
35-
return typeof obj === 'object' && obj !== null && Object.getPrototypeOf(obj) === Object.prototype;
36-
}
37-
38-
/**
39-
* Copy values into store and return a new values object
40-
* ({ a: 1, b: { c: 2 } }, { a: 4, b: { d: 5 } }) => { a: 4, b: { c: 2, d: 5 } }
41-
*/
42-
function internalSetValues<T>(store: T, values: T): T {
43-
const newStore: T = (Array.isArray(store) ? [...store] : { ...store }) as T;
44-
45-
if (!values) {
46-
return newStore;
47-
}
48-
49-
Object.keys(values).forEach(key => {
50-
const prevValue = newStore[key];
51-
const value = values[key];
52-
53-
// If both are object (but target is not array), we use recursion to set deep value
54-
const recursive = isObject(prevValue) && isObject(value);
55-
56-
newStore[key] = recursive ? internalSetValues(prevValue, value || {}) : cloneDeep(value); // Clone deep for arrays
57-
});
58-
59-
return newStore;
60-
}
61-
62-
export function setValues<T>(store: T, ...restValues: T[]): T {
63-
return restValues.reduce(
64-
(current: T, newStore: T): T => internalSetValues<T>(current, newStore),
65-
store,
66-
);
67-
}
68-
6933
export function matchNamePath(
7034
namePath: InternalNamePath,
7135
changedNamePath: InternalNamePath | null,

tests/utils.test.ts

Lines changed: 1 addition & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
1-
import { move, isSimilar, setValues } from '../src/utils/valueUtil';
1+
import { move, isSimilar } 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', () => {
@@ -31,19 +30,6 @@ describe('utils', () => {
3130
expect(isSimilar({}, null)).toBeFalsy();
3231
expect(isSimilar(null, {})).toBeFalsy();
3332
});
34-
35-
describe('setValues', () => {
36-
it('basic', () => {
37-
expect(setValues({}, { a: 1 }, { b: 2 })).toEqual({ a: 1, b: 2 });
38-
expect(setValues([], [123])).toEqual([123]);
39-
});
40-
41-
it('Correct handle class instance', () => {
42-
const out = setValues({}, { a: 1, b: { c: new Date() } });
43-
expect(out.a).toEqual(1);
44-
expect(out.b.c instanceof Date).toBeTruthy();
45-
});
46-
});
4733
});
4834

4935
describe('NameMap', () => {
@@ -72,12 +58,4 @@ describe('utils', () => {
7258
});
7359
});
7460
});
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-
});
8361
});

0 commit comments

Comments
 (0)