Skip to content

Commit

Permalink
fix(core): fix setValues/setInitialValues will change ref (#3529)
Browse files Browse the repository at this point in the history
  • Loading branch information
janryWang authored Nov 9, 2022
1 parent c658cb9 commit 886144f
Show file tree
Hide file tree
Showing 2 changed files with 99 additions and 67 deletions.
156 changes: 93 additions & 63 deletions packages/core/src/__tests__/form.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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()
})
10 changes: 6 additions & 4 deletions packages/core/src/models/Form.ts
Original file line number Diff line number Diff line change
Expand Up @@ -381,11 +381,12 @@ export class Form<ValueType extends object = any> {
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
}
Expand All @@ -397,11 +398,12 @@ export class Form<ValueType extends object = any> {
) => {
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
}
Expand Down

0 comments on commit 886144f

Please sign in to comment.