From 34e9420bdccf449c48a8fc26f0e05a7142e4bac5 Mon Sep 17 00:00:00 2001 From: Janry Date: Thu, 21 Apr 2022 22:23:24 +0800 Subject: [PATCH] fix(core): fix array child reactions invalid with remove (#3063) --- packages/core/src/__tests__/array.spec.ts | 13 ++++++++++++- packages/core/src/shared/internals.ts | 10 +++++----- 2 files changed, 17 insertions(+), 6 deletions(-) diff --git a/packages/core/src/__tests__/array.spec.ts b/packages/core/src/__tests__/array.spec.ts index c152460c5f6..9f23e7b53a8 100644 --- a/packages/core/src/__tests__/array.spec.ts +++ b/packages/core/src/__tests__/array.spec.ts @@ -698,9 +698,20 @@ test('array field remove can not memory leak', async () => { basePath: 'array.1', }) ) + const bb = attach( + form.createField({ + name: 'bb', + basePath: 'array.1', + reactions: (field) => { + field.visible = field.query('.aa').value() === '123' + }, + }) + ) + expect(bb.visible).toBeFalsy() await array.remove(0) form.query('array.0.aa').take((field) => { ;(field as DataField).value = '123' }) - expect(handler).toBeCalledTimes(2) + expect(bb.visible).toBeTruthy() + expect(handler).toBeCalledTimes(1) }) diff --git a/packages/core/src/shared/internals.ts b/packages/core/src/shared/internals.ts index 7c7153299af..22019670587 100644 --- a/packages/core/src/shared/internals.ts +++ b/packages/core/src/shared/internals.ts @@ -163,7 +163,6 @@ export const patchFieldStates = ( if (payload) { target[address] = payload if (target[oldAddress] === payload) { - target[oldAddress]?.dispose() delete target[oldAddress] } } @@ -387,10 +386,11 @@ export const spliceArrayState = ( if (number === undefined) return false const index = Number(number) return ( - index >= startIndex && - !fields[ - `${preStr}${afterStr.replace(/^\.\d+/, `.${index + deleteCount}`)}` - ] + (index > startIndex && + !fields[ + `${preStr}${afterStr.replace(/^\.\d+/, `.${index + deleteCount}`)}` + ]) || + index === startIndex ) } const moveIndex = (identifier: string) => {