Skip to content

Commit

Permalink
fix(core): fix reset logic for ArrayField/ObjectField
Browse files Browse the repository at this point in the history
  • Loading branch information
janryWang committed Apr 21, 2021
1 parent 5e6be78 commit 909c590
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 1 deletion.
50 changes: 50 additions & 0 deletions packages/core/src/__tests__/form.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -854,3 +854,53 @@ test('devtools', () => {
const form = attach(createForm())
form.onUnmount()
})

test('reset array field', async () => {
const form = attach(
createForm({
values: {
array: [{ value: 123 }],
},
})
)
attach(
form.createArrayField({
name: 'array',
required: true,
})
)
expect(form.values).toEqual({
array: [{ value: 123 }],
})
await form.reset('*', {
forceClear: true,
})
expect(form.values).toEqual({
array: [],
})
})

test('reset object field', async () => {
const form = attach(
createForm({
values: {
object: { value: 123 },
},
})
)
attach(
form.createObjectField({
name: 'object',
required: true,
})
)
expect(form.values).toEqual({
object: { value: 123 },
})
await form.reset('*', {
forceClear: true,
})
expect(form.values).toEqual({
object: {},
})
})
10 changes: 9 additions & 1 deletion packages/core/src/models/Field.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,8 @@ import {
modelStateGetter,
isHTMLInputEvent,
initFieldValue,
isArrayField,
isObjectField,
} from '../shared'
import { Query } from './Query'

Expand Down Expand Up @@ -747,7 +749,13 @@ export class Field<
this.inputValue = undefined
this.inputValues = []
if (options?.forceClear) {
this.value = undefined
if (isArrayField(this)) {
this.value = [] as any
} else if (isObjectField(this)) {
this.value = {} as any
} else {
this.value = undefined
}
} else {
this.value = this.initialValue
}
Expand Down

0 comments on commit 909c590

Please sign in to comment.