Skip to content

Commit

Permalink
fix(core): sync form state (#906)
Browse files Browse the repository at this point in the history
  • Loading branch information
janryWang authored Jun 21, 2020
1 parent b51c7f4 commit de32802
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 7 deletions.
4 changes: 2 additions & 2 deletions packages/core/src/internals.ts
Original file line number Diff line number Diff line change
Expand Up @@ -206,7 +206,7 @@ export const createFormInternals = (options: IFormCreatorOptions = {}) => {
unmounted === true ||
display === false
) {
form.setSourceState(state => {
form.setState(state => {
state.errors = state.errors || []
state.warnings = state.warnings || []
state.errors = state.errors.reduce((buf: any, item: any) => {
Expand Down Expand Up @@ -237,7 +237,7 @@ export const createFormInternals = (options: IFormCreatorOptions = {}) => {
function syncFormMessages(type: string, fieldState: IFieldState) {
const { name, path } = fieldState
const messages = fieldState[type]
form.setSourceState(state => {
form.setState(state => {
let foundField = false
state[type] = state[type] || []
state[type] = state[type].reduce((buf: any, item: any) => {
Expand Down
18 changes: 13 additions & 5 deletions packages/core/src/shared/model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@ import {
FormPath,
FormPathPattern,
defaults,
shallowClone
shallowClone,
isStr
} from '@formily/shared'
import { produce, enableAllPlugins, setAutoFreeze, Draft } from 'immer'
import { StateDirtyMap, IDirtyModelFactory, NormalRecord } from '../types'
Expand Down Expand Up @@ -225,13 +226,20 @@ export const createModel = <
}
}

hasChanged = (path?: FormPathPattern) => {
return path
? !isEqual(
hasChanged = (pattern?: FormPathPattern) => {
if (!pattern) {
return this.dirtyCount > 0
} else {
const path = FormPath.parse(pattern)
if (path.length > 1 || !isStr(pattern)) {
return !isEqual(
FormPath.getIn(this.prevState, path),
FormPath.getIn(this.state, path)
)
: !isEqual(this.prevState, this.state)
} else {
return this.dirtys[pattern]
}
}
}
}
}

0 comments on commit de32802

Please sign in to comment.