Skip to content

Commit

Permalink
fix(core): fix initialValue=null and reset null invalid bug (#2951)
Browse files Browse the repository at this point in the history
  • Loading branch information
frehaiku authored Mar 20, 2022
1 parent 3bdfe2f commit 221d39f
Show file tree
Hide file tree
Showing 6 changed files with 109 additions and 8 deletions.
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { createForm } from '../'
import { createForm } from '..'
import {
isArrayField,
isArrayFieldState,
Expand Down
53 changes: 52 additions & 1 deletion packages/core/src/__tests__/field.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -683,6 +683,7 @@ test('reset', async () => {
},
initialValues: {
aa: 123,
cc: null,
},
})
)
Expand All @@ -698,12 +699,28 @@ test('reset', async () => {
required: true,
})
)
const cc = attach(
form.createField({
name: 'cc',
required: true,
})
)
const dd = attach(
form.createField({
name: 'dd',
required: true,
})
)
expect(aa.value).toEqual(123)
expect(bb.value).toEqual(123)
expect(cc.value).toEqual(null)
expect(form.values.aa).toEqual(123)
expect(form.values.bb).toEqual(123)
expect(form.values.cc).toEqual(null)
aa.onInput('xxxxx')
expect(form.values.aa).toEqual('xxxxx')
dd.onInput(null)
expect(form.values.dd).toEqual(null)
aa.reset()
expect(aa.value).toEqual(123)
expect(form.values.aa).toEqual(123)
Expand All @@ -712,17 +729,40 @@ test('reset', async () => {
bb.reset()
expect(bb.value).toBeUndefined()
expect(form.values.bb).toBeUndefined()

cc.onInput('xxxxx')
expect(form.values.cc).toEqual('xxxxx')
cc.reset()
expect(cc.value).toBeNull()
expect(form.values.cc).toBeNull()
dd.reset()
expect(dd.value).toBeUndefined()
expect(form.values.dd).toBeUndefined()

aa.reset({
forceClear: true,
})
expect(aa.value).toBeUndefined()
expect(form.values.aa).toBeUndefined()
cc.reset({
forceClear: true,
})
expect(cc.value).toBeUndefined()
expect(form.values.cc).toBeUndefined()

expect(aa.valid).toBeTruthy()
await aa.reset({
forceClear: true,
validate: true,
})
expect(aa.valid).toBeFalsy()

expect(cc.valid).toBeTruthy()
await cc.reset({
forceClear: true,
validate: true,
})
expect(cc.valid).toBeFalsy()
})

test('match', () => {
Expand Down Expand Up @@ -1550,6 +1590,9 @@ test('initial value with empty', () => {
const form = attach(createForm())
const array = attach(form.createField({ name: 'array', initialValue: '' }))
expect(array.value).toEqual('')

const beNull = attach(form.createField({ name: 'null', initialValue: null }))
expect(beNull.value).toEqual(null)
})

test('field submit', async () => {
Expand Down Expand Up @@ -2003,12 +2046,13 @@ test('relative query with void field', () => {
expect(bb.query('.aa').take()).toBe(aa)
})

test('empty string or number value need rewrite default value', () => {
test('empty string or number or null value need rewrite default value', () => {
const form = attach(
createForm<any>({
values: {
aa: '',
bb: 0,
ee: null,
},
})
)
Expand Down Expand Up @@ -2036,8 +2080,15 @@ test('empty string or number value need rewrite default value', () => {
initialValue: 123,
})
)
attach(
form.createField({
name: 'ee',
initialValue: 'test',
})
)
expect(form.values.aa).toEqual('')
expect(form.values.bb).toEqual(0)
expect(form.values.cc).toEqual('test')
expect(form.values.dd).toEqual(123)
expect(form.values.ee).toEqual(null)
})
17 changes: 11 additions & 6 deletions packages/core/src/shared/internals.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import {
pascalCase,
isFn,
isValid,
isUndef,
isEmpty,
isPlainObj,
isNumberLike,
Expand Down Expand Up @@ -974,11 +975,15 @@ export const resetSelf = batch.bound(
target.inputValue = typedDefaultValue
target.inputValues = []
target.caches = {}
if (isValid(target.value)) {
if (!isUndef(target.value)) {
if (options?.forceClear) {
target.value = typedDefaultValue
} else {
target.value = toJS(target.initialValue ?? typedDefaultValue)
target.value = toJS(
!isUndef(target.initialValue)
? target.initialValue
: typedDefaultValue
)
}
}
if (!noEmit) {
Expand Down Expand Up @@ -1016,10 +1021,10 @@ export const getValidFieldDefaultValue = (value: any, initialValue: any) => {
}

export const allowAssignDefaultValue = (target: any, source: any) => {
const isEmptyTarget = isEmpty(target)
const isEmptySource = isEmpty(source)
const isValidTarget = isValid(target)
const isValidSource = isValid(source)
const isEmptyTarget = target !== null && isEmpty(target)
const isEmptySource = source !== null && isEmpty(source)
const isValidTarget = !isUndef(target)
const isValidSource = !isUndef(source)
if (!isValidTarget) {
if (isValidSource) {
return true
Expand Down
42 changes: 42 additions & 0 deletions packages/path/src/__tests__/accessor.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -311,3 +311,45 @@ test('complex destructing', () => {
},
})
})

test('test getIn with invalid value', () => {
const value = {
array: [null, undefined, { nil: null, undef: undefined }],
nil: null,
undef: undefined,
}
expect(getIn(value, 'array.0')).toBeNull()
expect(getIn(value, 'array.1')).toBeUndefined()
expect(getIn(value, 'array.2.nil')).toBeNull()
expect(getIn(value, 'array.2.undef')).toBeUndefined()
expect(getIn(value, 'nil')).toBeNull()
expect(getIn(value, 'undef')).toBeUndefined()
})

test('test setIn with invalid value', () => {
const value = {
a: 1,
b: 2,
array: [null, undefined, { nil: null, undef: undefined }],
nil: null,
undef: undefined,
}
setIn(value, 'a', null)
setIn(value, 'b', undefined)
// undefined 与 null 互转
setIn(value, 'array.0', undefined)
setIn(value, 'array.1', null)
setIn(value, 'array.2.nil', undefined)
setIn(value, 'array.2.undef', null)
setIn(value, 'nil', undefined)
setIn(value, 'undef', null)

expect(getIn(value, 'a')).toBeNull()
expect(getIn(value, 'b')).toBeUndefined()
expect(getIn(value, 'array.0')).toBeUndefined()
expect(getIn(value, 'array.1')).toBeNull()
expect(getIn(value, 'array.2.nil')).toBeUndefined()
expect(getIn(value, 'array.2.undef')).toBeNull()
expect(getIn(value, 'nil')).toBeUndefined()
expect(getIn(value, 'undef')).toBeNull()
})
1 change: 1 addition & 0 deletions packages/path/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ const setIn = (segments: Segments, source: any, value: any) => {
}
if (!isValid(source[index])) {
if (value === undefined) {
if (source[index] === null) source[index] = value
return
}
if (i < segments.length - 1) {
Expand Down
2 changes: 2 additions & 0 deletions packages/shared/src/isEmpty.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ const has = Object.prototype.hasOwnProperty

const toString = Object.prototype.toString

export const isUndef = (val: any) => val === undefined

export const isValid = (val: any) => val !== undefined && val !== null

export function isEmpty(val: any, strict = false): boolean {
Expand Down

0 comments on commit 221d39f

Please sign in to comment.