Skip to content

Commit

Permalink
feat(core): support_disable_unmount_clear_states (#938)
Browse files Browse the repository at this point in the history
  • Loading branch information
janryWang committed Jul 3, 2020
1 parent 0d3c381 commit 236c781
Show file tree
Hide file tree
Showing 8 changed files with 56 additions and 143 deletions.
117 changes: 0 additions & 117 deletions packages/core/src/__tests__/__snapshots__/index.spec.ts.snap

Large diffs are not rendered by default.

27 changes: 15 additions & 12 deletions packages/core/src/externals.ts
Expand Up @@ -64,6 +64,9 @@ export const createFormExternals = (
setFormValuesIn,
setFormInitialValuesIn,
updateRecoverableShownState,
supportUnmountClearStates,
disableUnmountClearStates,
enableUnmountClearStates,
resetFormMessages,
syncFormMessages,
batchRunTaskQueue,
Expand Down Expand Up @@ -254,14 +257,15 @@ export const createFormExternals = (
syncFormMessages('warnings', published)
}

if (
dirtys.unmounted ||
dirtys.visible ||
dirtys.display ||
dirtys.editable
) {
if (dirtys.visible || dirtys.display || dirtys.editable) {
//fix #682
resetFormMessages(published)
if (dirtys.unmounted) {
if (supportUnmountClearStates(published.path)) {
resetFormMessages(published)
}
} else {
resetFormMessages(published)
}
}

heart.publish(LifeCycleTypes.ON_FIELD_CHANGE, field)
Expand Down Expand Up @@ -330,7 +334,6 @@ export const createFormExternals = (
display,
computeState,
dataType,
unmountRemoveValue,
props
}: IFieldRegistryProps<FormilyCore.FieldProps>) {
let field: IField
Expand All @@ -345,7 +348,8 @@ export const createFormExternals = (
getValue(name) {
return getFormValuesIn(name)
},
needRemoveValue(path) {
supportUnmountClearStates(path) {
if (!supportUnmountClearStates(path)) return false
if (!env.realRemoveTags?.length) return true
return env.realRemoveTags.every(tag => {
return !FormPath.parse(calculateMathTag(tag)).match(path)
Expand Down Expand Up @@ -387,9 +391,6 @@ export const createFormExternals = (
const formInitialValue = getFormInitialValuesIn(state.name)
const syncValue = pickNotEmpty(value, formValue)
const syncInitialValue = pickNotEmpty(initialValue, formInitialValue)
if (isValid(unmountRemoveValue)) {
state.unmountRemoveValue = unmountRemoveValue
}

if (isValid(syncInitialValue)) {
state.initialValue = syncInitialValue
Expand Down Expand Up @@ -1051,6 +1052,8 @@ export const createFormExternals = (
getFieldValue,
setFieldInitialValue,
getFieldInitialValue,
disableUnmountClearStates,
enableUnmountClearStates,
isHostRendering,
hostUpdate,
subscribe,
Expand Down
24 changes: 24 additions & 0 deletions packages/core/src/internals.ts
Expand Up @@ -339,6 +339,26 @@ export const createFormInternals = (options: IFormCreatorOptions = {}) => {
return env.hostRendering
}

function disableUnmountClearStates(pattern: FormPathPattern = '*') {
const path = FormPath.parse(pattern)
env.clearStatesPatterns[path.toString()] = true
}

function enableUnmountClearStates(pattern: FormPathPattern = '*') {
const path = FormPath.parse(pattern)
env.clearStatesPatterns[path.toString()] = false
}

function supportUnmountClearStates(path: FormPathPattern) {
for (const pattern in env.clearStatesPatterns) {
const enable = env.clearStatesPatterns[pattern]
if (matchStrategy(pattern, path)) {
return enable
}
}
return true
}

const graph = new FormGraph({
matchStrategy
})
Expand Down Expand Up @@ -368,6 +388,7 @@ export const createFormInternals = (options: IFormCreatorOptions = {}) => {
taskIndexes: {},
realRemoveTags: [],
lastShownStates: {},
clearStatesPatterns: {},
submittingTask: undefined
}
form.subscription = {
Expand All @@ -392,6 +413,9 @@ export const createFormInternals = (options: IFormCreatorOptions = {}) => {
existFormValuesIn,
deleteFormValuesIn,
updateRecoverableShownState,
disableUnmountClearStates,
enableUnmountClearStates,
supportUnmountClearStates,
resetFormMessages,
syncFormMessages,
batchRunTaskQueue,
Expand Down
16 changes: 10 additions & 6 deletions packages/core/src/models/field.ts
Expand Up @@ -102,7 +102,6 @@ export const Field = createModel<IFieldState, IFieldStateProps>(
required: false,
mounted: false,
unmounted: false,
unmountRemoveValue: true,
props: {}
}

Expand Down Expand Up @@ -205,7 +204,15 @@ export const Field = createModel<IFieldState, IFieldStateProps>(
)
}

supportUnmountClearStates() {
if (isFn(this.props?.supportUnmountClearStates)) {
return this.props?.supportUnmountClearStates(this.state.path)
}
return true
}

produceSideEffects(draft: Draft<IFieldState>, dirtys: FieldStateDirtyMap) {
const supportClearStates = this.supportUnmountClearStates()
if (dirtys.validating) {
if (draft.validating === true) {
draft.loading = true
Expand All @@ -217,7 +224,7 @@ export const Field = createModel<IFieldState, IFieldStateProps>(
dirtys.editable ||
dirtys.selfEditable ||
draft.visible === false ||
draft.unmounted === true
(dirtys.unmounted && draft.unmounted === true && supportClearStates)
) {
draft.errors = []
draft.effectErrors = []
Expand All @@ -240,10 +247,7 @@ export const Field = createModel<IFieldState, IFieldStateProps>(
draft.mounted = true
}
if (dirtys.visible || dirtys.mounted || dirtys.unmounted) {
if (
draft.unmountRemoveValue &&
this.props?.needRemoveValue?.(this.state.path)
) {
if (supportClearStates) {
if (draft.display) {
if (draft.visible === false || draft.unmounted === true) {
if (!dirtys.visibleCacheValue) {
Expand Down
2 changes: 1 addition & 1 deletion packages/core/src/types.ts
Expand Up @@ -198,7 +198,7 @@ export interface IFieldStateProps {
setValue?: (name: FormPathPattern, value: any) => void
removeValue?: (name: FormPathPattern) => void
setInitialValue?: (name: FormPathPattern, initialValue: any) => void
needRemoveValue?: (path: FormPathPattern) => boolean
supportUnmountClearStates?: (path: FormPathPattern) => boolean
computeState?: (draft: IFieldState, prevState: IFieldState) => void
unControlledValueChanged?: () => void
}
Expand Down
Expand Up @@ -57,7 +57,6 @@ Object {
"rules": Array [],
"selfEditable": undefined,
"touched": false,
"unmountRemoveValue": true,
"unmounted": false,
"valid": true,
"validating": false,
Expand Down Expand Up @@ -151,7 +150,6 @@ Object {
"rules": Array [],
"selfEditable": undefined,
"touched": false,
"unmountRemoveValue": true,
"unmounted": false,
"valid": true,
"validating": false,
Expand Down
Expand Up @@ -56,7 +56,6 @@ Object {
"rules": Array [],
"selfEditable": undefined,
"touched": false,
"unmountRemoveValue": true,
"unmounted": false,
"valid": true,
"validating": false,
Expand Down Expand Up @@ -128,7 +127,6 @@ Object {
"rules": Array [],
"selfEditable": undefined,
"touched": false,
"unmountRemoveValue": true,
"unmounted": false,
"valid": true,
"validating": false,
Expand Down Expand Up @@ -215,7 +213,6 @@ Object {
"rules": Array [],
"selfEditable": undefined,
"touched": false,
"unmountRemoveValue": true,
"unmounted": false,
"valid": true,
"validating": false,
Expand Down
8 changes: 6 additions & 2 deletions packages/react/src/shared.ts
Expand Up @@ -53,7 +53,9 @@ export const createFormActions = (): IFormActions => {
'setFieldValue',
'getFieldValue',
'setFieldInitialValue',
'getFieldInitialValue'
'getFieldInitialValue',
'disableUnmountClearStates',
'enableUnmountClearStates'
) as IFormActions
}

Expand All @@ -80,7 +82,9 @@ export const createAsyncFormActions = (): IFormAsyncActions =>
'setFieldValue',
'getFieldValue',
'setFieldInitialValue',
'getFieldInitialValue'
'getFieldInitialValue',
'disableUnmountClearStates',
'enableUnmountClearStates'
) as IFormAsyncActions

export interface IEventTargetOption {
Expand Down

0 comments on commit 236c781

Please sign in to comment.