Skip to content

Commit

Permalink
fix(core): onInput ignore HTMLInputEvent propagation (#3856)
Browse files Browse the repository at this point in the history
  • Loading branch information
frehaiku committed Jun 14, 2023
1 parent 77d7e58 commit b3edf2d
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 0 deletions.
16 changes: 16 additions & 0 deletions packages/core/src/__tests__/field.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<any>())
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')
})
7 changes: 7 additions & 0 deletions packages/core/src/models/Field.ts
Original file line number Diff line number Diff line change
Expand Up @@ -459,12 +459,19 @@ export class Field<
getState: IModelGetter<IFieldState> = 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
Expand Down

0 comments on commit b3edf2d

Please sign in to comment.