From 8a0167941a147158d0b79fd10039ab0b67744f43 Mon Sep 17 00:00:00 2001 From: Janry Date: Tue, 2 Nov 2021 15:50:39 +0800 Subject: [PATCH] refactor(core): revert field unmount to skip validate (#2379) --- packages/core/src/__tests__/field.spec.ts | 5 +++++ packages/core/src/__tests__/form.spec.ts | 20 ++++++++++++++------ packages/core/src/models/Field.ts | 6 +++--- packages/core/src/shared/internals.ts | 14 ++------------ 4 files changed, 24 insertions(+), 21 deletions(-) diff --git a/packages/core/src/__tests__/field.spec.ts b/packages/core/src/__tests__/field.spec.ts index bd1d38cf6b1..81ca74aa842 100644 --- a/packages/core/src/__tests__/field.spec.ts +++ b/packages/core/src/__tests__/field.spec.ts @@ -1301,6 +1301,11 @@ test('fields unmount and selfValidate', async () => { } catch {} expect(form.invalid).toBeTruthy() field.onUnmount() + try { + await form.validate() + } catch {} + expect(form.invalid).toBeTruthy() + form.clearFormGraph('parent') await form.validate() expect(form.invalid).toBeFalsy() }) diff --git a/packages/core/src/__tests__/form.spec.ts b/packages/core/src/__tests__/form.spec.ts index 7d45a056232..ebc4e3665a2 100644 --- a/packages/core/src/__tests__/form.spec.ts +++ b/packages/core/src/__tests__/form.spec.ts @@ -1413,6 +1413,14 @@ test('validate will skip unmounted', async () => { await form.validate() } catch (e) { expect(e).toEqual([ + { + triggerType: 'onInput', + type: 'error', + code: 'ValidateError', + messages: ['error'], + address: 'aa', + path: 'aa', + }, { triggerType: 'onInput', type: 'error', @@ -1423,18 +1431,18 @@ test('validate will skip unmounted', async () => { }, ]) } - expect(validateA).toBeCalledTimes(1) + expect(validateA).toBeCalledTimes(2) expect(validateB).toBeCalledTimes(2) - expect(aa.invalid).toBeFalsy() + expect(aa.invalid).toBeTruthy() expect(bb.invalid).toBeTruthy() - expect(validator).toBeCalledTimes(3) - bb.onUnmount() + expect(validator).toBeCalledTimes(4) + form.clearFormGraph('*(aa,bb)') await form.validate() - expect(validateA).toBeCalledTimes(1) + expect(validateA).toBeCalledTimes(2) expect(validateB).toBeCalledTimes(2) expect(aa.invalid).toBeFalsy() expect(bb.invalid).toBeFalsy() - expect(validator).toBeCalledTimes(3) + expect(validator).toBeCalledTimes(4) }) test('validate will skip uneditable', async () => { diff --git a/packages/core/src/models/Field.ts b/packages/core/src/models/Field.ts index 6e7bb92e8ae..0dea099e47b 100644 --- a/packages/core/src/models/Field.ts +++ b/packages/core/src/models/Field.ts @@ -253,9 +253,9 @@ export class Field< } ), reaction( - () => [this.pattern, this.unmounted], - ([pattern, unmounted]) => { - if (pattern !== 'editable' || unmounted) { + () => this.pattern, + (pattern) => { + if (pattern !== 'editable') { this.setFeedback({ type: 'error', messages: [], diff --git a/packages/core/src/shared/internals.ts b/packages/core/src/shared/internals.ts index 2cbec1b7d7d..4e77abd90c5 100644 --- a/packages/core/src/shared/internals.ts +++ b/packages/core/src/shared/internals.ts @@ -886,12 +886,7 @@ export const batchValidate = async ( ) => { if (isForm(target)) target.setValidating(true) else { - if ( - target.pattern !== 'editable' || - target.display !== 'visible' || - target.unmounted - ) - return + if (target.pattern !== 'editable' || target.display !== 'visible') return } const tasks = [] target.query(pattern).forEach((field) => { @@ -946,12 +941,7 @@ export const validateSelf = batch.bound( } } - if ( - target.pattern !== 'editable' || - target.display !== 'visible' || - target.unmounted - ) - return {} + if (target.pattern !== 'editable' || target.display !== 'visible') return {} start() if (!triggerType) { const allTriggerTypes = parseValidatorDescriptions(target.validator).map(