Skip to content

Commit

Permalink
fix: submit on enter keypress for checkboxes and radio buttons
Browse files Browse the repository at this point in the history
  • Loading branch information
777PolarFox777 committed Oct 11, 2021
1 parent 2f900ef commit 09e1149
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 3 deletions.
25 changes: 25 additions & 0 deletions src/__tests__/keyboard/plugin/functional.ts
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,31 @@ test('trigger click event on [Enter] keypress on HTMLButtonElement', () => {
`)
})

test.each`
elementType | submit
${'checkbox'} | ${'input'}
${'checkbox'} | ${'button'}
${'radio'} | ${'input'}
${'radio'} | ${'button'}
`(
'trigger submit event on [Enter] keypress on HTMLInputElement type=$elementType with submit $submit',
({elementType, submit}) => {
const {element, getEvents} = setup(
`<form>
<input type="${elementType}" />
${submit === 'button' && '<button type="submit">submit</button>'}
${submit === 'input' && '<input type="submit" />'}
</form>`,
)

element.querySelector('input')?.focus()

userEvent.keyboard('[Enter]')

expect(getEvents('submit')).toHaveLength(1)
},
)

test('trigger click event on [Space] keyup on HTMLButtonElement', () => {
const {element, getEventSnapshot, getEvents} = setup(`<button/>`)
element.focus()
Expand Down
17 changes: 14 additions & 3 deletions src/keyboard/plugins/functional.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import {fireEvent} from '@testing-library/dom'
import {
calculateNewValue,
hasFormSubmit,
isClickableInput,
isCursorAtStart,
isEditable,
Expand Down Expand Up @@ -81,6 +82,18 @@ export const keydownBehavior: behaviorPlugin[] = [
]

export const keypressBehavior: behaviorPlugin[] = [
{
matches: (keyDef, element) =>
keyDef.key === 'Enter' &&
isElementType(element, 'input') &&
['checkbox', 'radio'].includes(element.type) &&
hasFormSubmit(element.form),
handle: (keyDef, element) => {
const form = (element as HTMLInputElement).form as HTMLFormElement

fireEvent.submit(form)
},
},
{
matches: (keyDef, element) =>
keyDef.key === 'Enter' &&
Expand All @@ -99,9 +112,7 @@ export const keypressBehavior: behaviorPlugin[] = [

if (
form &&
(form.querySelectorAll('input').length === 1 ||
form.querySelector('input[type="submit"]') ||
form.querySelector('button[type="submit"]'))
(form.querySelectorAll('input').length === 1 || hasFormSubmit(form))
) {
fireEvent.submit(form)
}
Expand Down
1 change: 1 addition & 0 deletions src/utils/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,3 +25,4 @@ export * from './misc/isDisabled'
export * from './misc/isDocument'
export * from './misc/wait'
export * from './misc/hasPointerEvents'
export * from './misc/hasFormSubmit'
6 changes: 6 additions & 0 deletions src/utils/misc/hasFormSubmit.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
export const hasFormSubmit = (form: HTMLFormElement | null) =>
!!(
form &&
(form.querySelector('input[type="submit"]') ||
form.querySelector('button[type="submit"]'))
)

0 comments on commit 09e1149

Please sign in to comment.