From 8f99e5b32ae0a1e342945dfd4ab8ff393776c684 Mon Sep 17 00:00:00 2001 From: janrywang Date: Sat, 17 Oct 2020 15:36:10 +0800 Subject: [PATCH] feat(core): support enableUnmountRemoveNode/disableUnmountRemoveNode API --- packages/core/src/externals.ts | 10 ++++++++++ packages/core/src/internals.ts | 11 +++++++++++ packages/react/src/shared.ts | 8 ++++++-- 3 files changed, 27 insertions(+), 2 deletions(-) diff --git a/packages/core/src/externals.ts b/packages/core/src/externals.ts index d5cab2efca1..727b1680497 100644 --- a/packages/core/src/externals.ts +++ b/packages/core/src/externals.ts @@ -68,6 +68,8 @@ export const createFormExternals = ( supportUnmountClearStates, disableUnmountClearStates, enableUnmountClearStates, + enableUnmountRemoveNode, + disableUnmountRemoveNode, resetFormMessages, syncFormMessages, batchRunTaskQueue, @@ -295,6 +297,9 @@ export const createFormExternals = ( env.realRemoveTags = [] }) heart.publish(LifeCycleTypes.ON_FIELD_UNMOUNT, field) + if (env.unmountRemoveNode) { + graph.remove(field.state.path) + } } if (dirtys.mounted && published.mounted) { @@ -363,6 +368,9 @@ export const createFormExternals = ( env.realRemoveTags = [] }) heart.publish(LifeCycleTypes.ON_FIELD_UNMOUNT, field) + if (env.unmountRemoveNode) { + graph.remove(field.state.path) + } } if (dirtys.mounted && published.mounted) { heart.publish(LifeCycleTypes.ON_FIELD_MOUNT, field) @@ -1143,6 +1151,8 @@ export const createFormExternals = ( getFieldInitialValue, disableUnmountClearStates, enableUnmountClearStates, + enableUnmountRemoveNode, + disableUnmountRemoveNode, isHostRendering, hostUpdate, subscribe, diff --git a/packages/core/src/internals.ts b/packages/core/src/internals.ts index 53dff6bcb5b..0502f53e149 100644 --- a/packages/core/src/internals.ts +++ b/packages/core/src/internals.ts @@ -362,6 +362,14 @@ export const createFormInternals = (options: IFormCreatorOptions = {}) => { env.clearStatesPatterns[path.toString()] = true } + function disableUnmountRemoveNode() { + env.unmountRemoveNode = false + } + + function enableUnmountRemoveNode() { + env.unmountRemoveNode = true + } + function supportUnmountClearStates(path: FormPathPattern) { for (const pattern in env.clearStatesPatterns) { const enable = env.clearStatesPatterns[pattern] @@ -409,6 +417,7 @@ export const createFormInternals = (options: IFormCreatorOptions = {}) => { realRemoveTags: [], lastShownStates: {}, clearStatesPatterns: {}, + unmountRemoveNode: false, submittingTask: undefined } form.subscription = { @@ -437,6 +446,8 @@ export const createFormInternals = (options: IFormCreatorOptions = {}) => { disableUnmountClearStates, enableUnmountClearStates, supportUnmountClearStates, + enableUnmountRemoveNode, + disableUnmountRemoveNode, resetFormMessages, syncFormMessages, batchRunTaskQueue, diff --git a/packages/react/src/shared.ts b/packages/react/src/shared.ts index f93c4b39903..08c584998dd 100644 --- a/packages/react/src/shared.ts +++ b/packages/react/src/shared.ts @@ -55,7 +55,9 @@ export const createFormActions = (): IFormActions => { 'setFieldInitialValue', 'getFieldInitialValue', 'disableUnmountClearStates', - 'enableUnmountClearStates' + 'enableUnmountClearStates', + 'enableUnmountRemoveNode', + 'disableUnmountRemoveNode' ) as IFormActions } @@ -84,7 +86,9 @@ export const createAsyncFormActions = (): IFormAsyncActions => 'setFieldInitialValue', 'getFieldInitialValue', 'disableUnmountClearStates', - 'enableUnmountClearStates' + 'enableUnmountClearStates', + 'enableUnmountRemoveNode', + 'disableUnmountRemoveNode' ) as IFormAsyncActions export interface IEventTargetOption {