From b3edf2d184571c66f6743d01761eb2fd2c32f80d Mon Sep 17 00:00:00 2001 From: frehkxu Date: Thu, 15 Jun 2023 01:49:36 +0800 Subject: [PATCH] fix(core): onInput ignore HTMLInputEvent propagation (#3856) --- packages/core/src/__tests__/field.spec.ts | 16 ++++++++++++++++ packages/core/src/models/Field.ts | 7 +++++++ 2 files changed, 23 insertions(+) diff --git a/packages/core/src/__tests__/field.spec.ts b/packages/core/src/__tests__/field.spec.ts index f60bc6ce403..b7e562ed683 100644 --- a/packages/core/src/__tests__/field.spec.ts +++ b/packages/core/src/__tests__/field.spec.ts @@ -2346,3 +2346,19 @@ test('field destructor path with display none', () => { expect(form.values).toEqual({}) expect(aa.value).toEqual([]) }) + +test('onInput should ignore HTMLInputEvent propagation', async () => { + const form = attach(createForm()) + const mockHTMLInput = { value: '321' } + const mockDomEvent = { target: mockHTMLInput, currentTarget: mockHTMLInput } + const aa = attach( + form.createField({ + name: 'aa', + }) + ) + await aa.onInput(mockDomEvent) + expect(aa.value).toEqual('321') + + await aa.onInput({ target: { value: '2' }, currentTarget: { value: '4' } }) + expect(aa.value).toEqual('321') +}) diff --git a/packages/core/src/models/Field.ts b/packages/core/src/models/Field.ts index a7a03b96670..ed41c8abc22 100644 --- a/packages/core/src/models/Field.ts +++ b/packages/core/src/models/Field.ts @@ -459,12 +459,19 @@ export class Field< getState: IModelGetter = createStateGetter(this) onInput = async (...args: any[]) => { + const isHTMLInputEventFromSelf = (args: any[]) => + isHTMLInputEvent(args[0]) + ? args[0]?.target === args[0]?.currentTarget + : true const getValues = (args: any[]) => { if (args[0]?.target) { if (!isHTMLInputEvent(args[0])) return args } return getValuesFromEvent(args) } + + if (!isHTMLInputEventFromSelf(args)) return + const values = getValues(args) const value = values[0] this.caches.inputting = true