Skip to content

Commit

Permalink
fix(core): fix empty string or number can not rewrite default value (#…
Browse files Browse the repository at this point in the history
…2906)

* fix(core): fix empty string/number can not rewrite default value

* chore: fix tests
  • Loading branch information
janryWang authored Mar 6, 2022
1 parent b226760 commit b6c3e31
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 0 deletions.
39 changes: 39 additions & 0 deletions packages/core/src/__tests__/field.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2002,3 +2002,42 @@ test('relative query with void field', () => {

expect(bb.query('.aa').take()).toBe(aa)
})

test('empty string or number value need rewrite default value', () => {
const form = attach(
createForm<any>({
values: {
aa: '',
bb: 0,
},
})
)
attach(
form.createField({
name: 'aa',
initialValue: 'test',
})
)
attach(
form.createField({
name: 'bb',
initialValue: 123,
})
)
attach(
form.createField({
name: 'cc',
initialValue: 'test',
})
)
attach(
form.createField({
name: 'dd',
initialValue: 123,
})
)
expect(form.values.aa).toEqual('')
expect(form.values.bb).toEqual(0)
expect(form.values.cc).toEqual('test')
expect(form.values.dd).toEqual(123)
})
5 changes: 5 additions & 0 deletions packages/core/src/shared/internals.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1027,6 +1027,11 @@ export const allowAssignDefaultValue = (target: any, source: any) => {
return false
}

if (typeof target === typeof source) {
if (target === '') return false
if (target === 0) return false
}

if (isEmptyTarget) {
if (isEmptySource) {
return false
Expand Down

0 comments on commit b6c3e31

Please sign in to comment.