Skip to content

Commit

Permalink
fix(core): fix initialValues patch values with wrong type (#2214)
Browse files Browse the repository at this point in the history
  • Loading branch information
janryWang authored Sep 21, 2021
1 parent 07739bb commit 334ff1b
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 6 deletions.
21 changes: 20 additions & 1 deletion packages/core/src/__tests__/form.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -315,7 +315,7 @@ test('query', () => {
expect(form.query('object.void').get('initialValue')).toBeUndefined()
expect(form.query('object.void').get('inputValue')).toBeUndefined()
expect(form.query('array').get('value')).toEqual([])
expect(form.query('array').get('initialValue')).toEqual([])
expect(form.query('array').get('initialValue')).toBeUndefined()
expect(form.query('array').get('inputValue')).toBeNull()
form.setFieldState('array', (state) => {
state.value = [111]
Expand Down Expand Up @@ -987,6 +987,25 @@ test('initialValues merge values before create field', () => {
expect(arr_0_aa.value).toEqual('321')
})

test('no patch with empty initialValues', () => {
const form = attach(
createForm({
values: {
array: [1, 2, 3],
},
})
)
attach(
form.createObjectField({
name: 'array.0.1',
})
)

expect(form.values).toEqual({
array: [1, 2, 3],
})
})

test('initialValues merge values after create field', () => {
const form = attach(createForm())
const aa = attach(
Expand Down
2 changes: 0 additions & 2 deletions packages/core/src/models/Form.ts
Original file line number Diff line number Diff line change
Expand Up @@ -323,7 +323,6 @@ export class Form<ValueType extends object = any> {
{
...props,
value: isArr(props.value) ? props.value : [],
initialValue: isObj(props.initialValue) ? props.initialValue : [],
},
this,
this.props.designable
Expand All @@ -350,7 +349,6 @@ export class Form<ValueType extends object = any> {
{
...props,
value: isObj(props.value) ? props.value : {},
initialValue: isObj(props.initialValue) ? props.initialValue : {},
},
this,
this.props.designable
Expand Down
6 changes: 3 additions & 3 deletions packages/core/src/shared/internals.ts
Original file line number Diff line number Diff line change
Expand Up @@ -670,7 +670,7 @@ export const applyValuesPatch = (
each(source, (value, key) => {
patch(value, path.concat(key))
})
} else {
} else if (!isEmpty(source)) {
if (targetField) {
if (
!isVoidField(targetField) &&
Expand Down Expand Up @@ -960,9 +960,9 @@ export const selfReset = batch.bound(
target.inputValues = []
if (options?.forceClear) {
if (isArrayField(target)) {
target.value = [] as any
target.value = []
} else if (isObjectField(target)) {
target.value = {} as any
target.value = {}
} else {
target.value = undefined
}
Expand Down

0 comments on commit 334ff1b

Please sign in to comment.