Skip to content

Commit

Permalink
fix(core): fix immer autoFreeze and reset Native Object (#816)
Browse files Browse the repository at this point in the history
* fix(core): fix immer autoFreeze and reset Native Object

* fix(core): fix ci
  • Loading branch information
janryWang authored Apr 24, 2020
1 parent 3704873 commit aff2318
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 7 deletions.
25 changes: 25 additions & 0 deletions packages/core/src/__tests__/index.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -413,6 +413,31 @@ describe('reset', () => {
expect(form.getFormState(state => state.values)).toEqual({ aa: { bb: [] } })
expect(form.getFormState(state => state.initialValues)).toEqual({})
})

test('date reset', () => {
const form = createForm({
values: {},
initialValues: {},
onChange: values => {
console.log(values)
}
})

const aa = form.registerField({
path: 'aa',
initialValue: ''
})

aa.setState(state => {
state.value = new Date()
})

form.reset()
expect(
form.getFormState(state => state.values.aa)
).toBe('')
})

})

describe('clearErrors', () => {
Expand Down
7 changes: 4 additions & 3 deletions packages/core/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@ import {
FormPathPattern,
BigData,
each,
isObj
isObj,
isPlainObj
} from '@formily/shared'
import {
FormValidator,
Expand Down Expand Up @@ -1034,8 +1035,8 @@ export function createForm(options: IFormCreatorOptions = {}) {
} else {
state.value = []
}
} else if (isObj(state.value)) {
if (isObj(value)) {
} else if (isPlainObj(state.value)) {
if (isPlainObj(value)) {
state.value = value
} else {
state.value = {}
Expand Down
6 changes: 4 additions & 2 deletions packages/core/src/shared/model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import {
defaults,
shallowClone
} from '@formily/shared'
import { Draft, Immer, enablePatches } from 'immer'
import { Draft, Immer, enablePatches, setAutoFreeze } from 'immer'
import {
IStateModelProvider,
IStateModelFactory,
Expand All @@ -23,6 +23,8 @@ const hasProxy = !!globalThisPolyfill.Proxy

enablePatches()

setAutoFreeze(false)

const { produce } = new Immer({
autoFreeze: false
})
Expand Down Expand Up @@ -184,7 +186,7 @@ export const createStateModel = <State = {}, Props = {}>(
patches => {
patches.forEach(({ path, op, value }) => {
if (op === 'replace') {
if (path.length > 1 ||!isEqual(this.state[path[0]], value)) {
if (path.length > 1 || !isEqual(this.state[path[0]], value)) {
this.dirtys[path[0]] = true
this.dirtyNum++
}
Expand Down
4 changes: 2 additions & 2 deletions packages/core/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -238,8 +238,8 @@ export interface IFormState<FormProps = any> {
editable: boolean | ((name: string) => boolean)
errors: string[]
warnings: string[]
values: {}
initialValues: {}
values: any
initialValues: any
mounted: boolean
unmounted: boolean
props: FormProps
Expand Down

0 comments on commit aff2318

Please sign in to comment.