From 886144fa80de377c8250121bc698e7a5fefec06c Mon Sep 17 00:00:00 2001 From: Janry Date: Wed, 9 Nov 2022 19:29:52 +0800 Subject: [PATCH] fix(core): fix setValues/setInitialValues will change ref (#3529) --- packages/core/src/__tests__/form.spec.ts | 156 ++++++++++++++--------- packages/core/src/models/Form.ts | 10 +- 2 files changed, 99 insertions(+), 67 deletions(-) diff --git a/packages/core/src/__tests__/form.spec.ts b/packages/core/src/__tests__/form.spec.ts index 5083a65f238..ce3f47c05bc 100644 --- a/packages/core/src/__tests__/form.spec.ts +++ b/packages/core/src/__tests__/form.spec.ts @@ -1617,70 +1617,100 @@ test('form clearFormGraph not clear field values', () => { expect(form.values.aa).toEqual('123') }) -// test('form values auto clean with visible false', () => { -// const form = attach( -// createForm({ -// initialValues: { -// aa: '123', -// bb: '321', -// cc: 'cc', -// }, -// }) -// ) -// attach( -// form.createField({ -// name: 'aa', -// }) -// ) -// attach( -// form.createField({ -// name: 'bb', -// reactions: (field) => { -// field.visible = form.values.aa === '1233' -// }, -// }) -// ) -// attach( -// form.createField({ -// name: 'cc', -// }) -// ) +test('form values auto clean with visible false', () => { + const form = attach( + createForm({ + initialValues: { + aa: '123', + bb: '321', + cc: 'cc', + }, + }) + ) + attach( + form.createField({ + name: 'aa', + }) + ) + attach( + form.createField({ + name: 'bb', + reactions: (field) => { + field.visible = form.values.aa === '1233' + }, + }) + ) + attach( + form.createField({ + name: 'cc', + }) + ) -// expect(form.values).toEqual({ -// aa: '123', -// cc: 'cc', -// }) -// }) + expect(form.values).toEqual({ + aa: '123', + cc: 'cc', + }) +}) -// test('form values auto clean with visible false in async setInitialValues', () => { -// const form = attach(createForm()) -// attach( -// form.createField({ -// name: 'aa', -// }) -// ) -// attach( -// form.createField({ -// name: 'bb', -// reactions: (field) => { -// field.visible = form.values.aa === '1233' -// }, -// }) -// ) -// attach( -// form.createField({ -// name: 'cc', -// }) -// ) +test('form values auto clean with visible false in async setInitialValues', () => { + const form = attach(createForm()) + attach( + form.createField({ + name: 'aa', + }) + ) + attach( + form.createField({ + name: 'bb', + reactions: (field) => { + field.visible = form.values.aa === '1233' + }, + }) + ) + attach( + form.createField({ + name: 'cc', + }) + ) -// form.setInitialValues({ -// aa: '123', -// bb: '321', -// cc: 'cc', -// }) + form.setInitialValues({ + aa: '123', + bb: '321', + cc: 'cc', + }) + + expect(form.values).toEqual({ + aa: '123', + cc: 'cc', + }) +}) + +test('form values ref should not changed with setValues', () => { + const form = attach( + createForm({ + values: { + aa: '123', + }, + }) + ) + const values = form.values + form.setValues({ + bb: '321', + }) + expect(form.values === values).toBeTruthy() +}) -// expect(form.values).toEqual({ -// aa: '123', -// cc: 'cc', -// }) -// }) +test('form initial values ref should not changed with setInitialValues', () => { + const form = attach( + createForm({ + initialValues: { + aa: '123', + }, + }) + ) + const values = form.initialValues + form.setInitialValues({ + bb: '321', + }) + expect(form.initialValues === values).toBeTruthy() +}) diff --git a/packages/core/src/models/Form.ts b/packages/core/src/models/Form.ts index b70b9de336a..7232165756c 100644 --- a/packages/core/src/models/Form.ts +++ b/packages/core/src/models/Form.ts @@ -381,11 +381,12 @@ export class Form { setValues = (values: any, strategy: IFormMergeStrategy = 'merge') => { if (!isPlainObj(values)) return if (strategy === 'merge' || strategy === 'deepMerge') { - this.values = merge(this.values, values, { + merge(this.values, values, { arrayMerge: (target, source) => source, + assign: true, }) } else if (strategy === 'shallowMerge') { - this.values = Object.assign(this.values, values) + Object.assign(this.values, values) } else { this.values = values as any } @@ -397,11 +398,12 @@ export class Form { ) => { if (!isPlainObj(initialValues)) return if (strategy === 'merge' || strategy === 'deepMerge') { - this.initialValues = merge(this.initialValues, initialValues, { + merge(this.initialValues, initialValues, { arrayMerge: (target, source) => source, + assign: true, }) } else if (strategy === 'shallowMerge') { - this.initialValues = Object.assign(this.initialValues, initialValues) + Object.assign(this.initialValues, initialValues) } else { this.initialValues = initialValues as any }