Skip to content

Commit

Permalink
fix(core/reactive): fix toJS and initialValue assign value
Browse files Browse the repository at this point in the history
  • Loading branch information
janryWang committed May 12, 2021
1 parent 581db23 commit 1d6c473
Show file tree
Hide file tree
Showing 4 changed files with 70 additions and 11 deletions.
55 changes: 55 additions & 0 deletions packages/core/src/__tests__/field.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -434,6 +434,61 @@ test('empty initialValue', () => {
expect(form.values.bb).toEqual(undefined)
})

test('objectFieldWithInitialValue', async () => {
const form = attach(
createForm({
initialValues: {
obj: {
a: 'a',
}
},
})
)
attach(
form.createObjectField({
name: 'obj',
})
);
const fieldObjA = attach(
form.createField({
name: 'obj.a',
})
);

expect(fieldObjA.initialValue).toEqual('a')
fieldObjA.value = 'aa'
expect(fieldObjA.value).toEqual('aa')
expect(fieldObjA.initialValue).toEqual('a')
})

test('resetObjectFieldWithInitialValue', async () => {
const form = attach(
createForm()
)
attach(
form.createObjectField({
name: 'obj',
})
);
const fieldObjA = attach(
form.createField({
name: 'obj.a',
initialValue: 'a',
})
);

fieldObjA.value = 'aa'
expect(fieldObjA.value).toEqual('aa')
await form.reset()
expect(fieldObjA.value).toEqual('a')

fieldObjA.value = 'aa'
expect(fieldObjA.value).toEqual('aa')
await form.reset()
expect(fieldObjA.initialValue).toEqual('a')
expect(fieldObjA.value).toEqual('a')
})

test('reset', async () => {
const form = attach(
createForm({
Expand Down
4 changes: 2 additions & 2 deletions packages/core/src/models/Field.ts
Original file line number Diff line number Diff line change
Expand Up @@ -756,8 +756,8 @@ export class Field<
} else {
this.value = undefined
}
} else {
this.value = this.initialValue
} else if (isValid(this.value)) {
this.value = toJS(this.initialValue)
}
this.form.notify(LifeCycleTypes.ON_FIELD_RESET, this)

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 @@ -358,8 +358,8 @@ export const initFieldValue = (field: Field) => {
if (!isValid(field.value)) {
if (isValid(field.props.value)) {
field.value = field.props.value
} else if (isValid(field.initialValue)) {
field.value = field.initialValue
} else if (isValid(field.props.initialValue)) {
field.value = field.props.initialValue
}
}
GlobalState.initializing = false
Expand Down Expand Up @@ -537,7 +537,7 @@ export const applyValuesPatch = (
if (path.length) {
form.setValuesIn(path, toJS(source))
} else {
Object.assign(form.values, source)
Object.assign(form.values, toJS(source))
}
}

Expand Down
16 changes: 10 additions & 6 deletions packages/reactive/src/externals.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import { Annotation } from './types'

const RAW_TYPE = Symbol('RAW_TYPE')
const OBSERVABLE_TYPE = Symbol('OBSERVABLE_TYPE')
const hasOwnProperty = Object.prototype.hasOwnProperty

export const isObservable = (target: any) => {
return ProxyRaw.has(target)
Expand Down Expand Up @@ -83,10 +84,13 @@ export const toJS = <T>(values: T): T => {
const tojs: typeof toJS = (values) => {
if (visited.has(values)) {
return values
} else if (!isObservable(values)) {
return values
} else if (isArr(values)) {
visited.add(values)
}
const originValues = values
if (ProxyRaw.has(values as any)) {
values = ProxyRaw.get(values as any)
}
if (isArr(values)) {
visited.add(originValues)
const res: any = []
values.forEach((item) => {
res.push(tojs(item))
Expand All @@ -104,10 +108,10 @@ export const toJS = <T>(values: T): T => {
} else if (isFn(values['toJSON'])) {
return values['toJSON']()
} else {
visited.add(values)
visited.add(originValues)
const res: any = {}
for (const key in values) {
if (Object.hasOwnProperty.call(values, key)) {
if (hasOwnProperty.call(values, key)) {
res[key] = tojs(values[key])
}
}
Expand Down

0 comments on commit 1d6c473

Please sign in to comment.