Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(keyboard): submit on enter keypress for checkboxes and radio buttons #741

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 30 additions & 0 deletions src/__tests__/keyboard/plugin/functional.ts
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,36 @@ test('trigger click event on [Enter] keypress on HTMLButtonElement', () => {
`)
})

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

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

userEvent.keyboard('[Enter]')

expect(getEvents('click')).toHaveLength(0)
expect(getEvents('submit')).toHaveLength(hasForm ? 1 : 0)
},
)

test('trigger click event on [Space] keyup on HTMLButtonElement', () => {
const {element, getEventSnapshot, getEvents} = setup(`<button/>`)
element.focus()
Expand Down
18 changes: 15 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,19 @@ export const keydownBehavior: behaviorPlugin[] = [
]

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

if (hasFormSubmit(form)) {
fireEvent.submit(form)
}
},
},
{
matches: (keyDef, element) =>
keyDef.key === 'Enter' &&
Expand All @@ -99,9 +113,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'
8 changes: 8 additions & 0 deletions src/utils/misc/hasFormSubmit.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
export const hasFormSubmit = (
form: HTMLFormElement | null,
): form is HTMLFormElement =>
!!(
form &&
(form.querySelector('input[type="submit"]') ||
form.querySelector('button[type="submit"]'))
)