Skip to content

Commit

Permalink
fix(core): fix form initialValues not work after array field removed …
Browse files Browse the repository at this point in the history
…elements (#3324)
  • Loading branch information
janryWang committed Aug 11, 2022
1 parent aa8ed99 commit f7e1b7d
Show file tree
Hide file tree
Showing 3 changed files with 71 additions and 10 deletions.
68 changes: 67 additions & 1 deletion packages/core/src/__tests__/array.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -533,7 +533,9 @@ test('nest array remove', async () => {
expect(form.fields['metrics.0.content.0.attr']).not.toBeUndefined()
await metrics.remove(1)
expect(form.fields['metrics.0.content.0.attr']).not.toBeUndefined()
expect(form.initialValues.metrics?.[1]?.content?.[0]?.attr).toBeUndefined()
expect(
form.initialValues.metrics?.[1]?.content?.[0]?.attr
).not.toBeUndefined()
})

test('incomplete insertion of array elements', async () => {
Expand Down Expand Up @@ -769,3 +771,67 @@ test('array field patch values', async () => {
)
expect(form.values).toEqual({ a: [{ c: 'A' }, { c: 'A' }] })
})

test('array remove with initialValues', async () => {
const form = attach(
createForm({
initialValues: {
array: [{ a: 1 }, { a: 2 }],
},
})
)
const array = attach(
form.createArrayField({
name: 'array',
})
)
attach(
form.createObjectField({
name: '0',
basePath: 'array',
})
)
attach(
form.createObjectField({
name: '1',
basePath: 'array',
})
)
attach(
form.createField({
name: 'a',
basePath: 'array.0',
})
)
attach(
form.createField({
name: 'a',
basePath: 'array.1',
})
)
expect(form.values).toEqual({ array: [{ a: 1 }, { a: 2 }] })
await array.remove(1)
expect(form.values).toEqual({ array: [{ a: 1 }] })
expect(form.initialValues).toEqual({ array: [{ a: 1 }, { a: 2 }] })
await array.reset()
attach(
form.createObjectField({
name: '1',
basePath: 'array',
})
)
attach(
form.createField({
name: 'a',
basePath: 'array.0',
})
)
attach(
form.createField({
name: 'a',
basePath: 'array.1',
})
)
expect(form.values).toEqual({ array: [{ a: 1 }, { a: 2 }] })
expect(form.initialValues).toEqual({ array: [{ a: 1 }, { a: 2 }] })
})
6 changes: 2 additions & 4 deletions packages/core/src/models/Field.ts
Original file line number Diff line number Diff line change
Expand Up @@ -125,11 +125,8 @@ export class Field<
this.validator = this.props.validator
this.required = this.props.required
this.content = this.props.content
this.value = getValidFieldDefaultValue(
this.props.value,
this.props.initialValue
)
this.initialValue = this.props.initialValue
this.value = this.props.value
this.data = this.props.data
this.decorator = toArr(this.props.decorator)
this.component = toArr(this.props.component)
Expand Down Expand Up @@ -357,6 +354,7 @@ export class Field<
this.caches.value = value
return
}
value = getValidFieldDefaultValue(value, this.initialValue)
if (!allowAssignDefaultValue(this.value, value) && !this.designable) {
return
}
Expand Down
7 changes: 2 additions & 5 deletions packages/core/src/shared/internals.ts
Original file line number Diff line number Diff line change
Expand Up @@ -170,11 +170,11 @@ export const patchFieldStates = (
export const destroy = (
target: Record<string, GeneralField>,
address: string,
removeValue = true
forceClear = true
) => {
const field = target[address]
field?.dispose()
if (isDataField(field) && removeValue) {
if (isDataField(field) && forceClear) {
const form = field.form
const path = field.path
form.deleteValuesIn(path)
Expand Down Expand Up @@ -417,9 +417,6 @@ export const spliceArrayState = (
})
}
if (isInsertNode(identifier) || isDeleteNode(identifier)) {
if (isDataField(field)) {
form.deleteInitialValuesIn(field.path)
}
fieldPatches.push({ type: 'remove', address: identifier })
}
}
Expand Down

0 comments on commit f7e1b7d

Please sign in to comment.