Skip to content

fix: submit form with external submit button when user clicks enter (#1220) #1288

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

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
11 changes: 8 additions & 3 deletions src/event/behavior/keypress.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,14 @@ behavior.keypress = (event, target, instance) => {
}
} else if (isElementType(target, 'input')) {
const form = target.form
const submit = form?.querySelector(
'input[type="submit"], button:not([type]), button[type="submit"]',
)
const submit =
form?.querySelector(
'input[type="submit"], button:not([type]), button[type="submit"]',
) ??
(form?.id &&
document.querySelector(
`input[form="${form.id}"][type="submit"], button[form="${form.id}"]:not([type]), button[form="${form?.id}"][type="submit"]`,
))
if (submit) {
return () => instance.dispatchUIEvent(submit, 'click')
} else if (
Expand Down
34 changes: 32 additions & 2 deletions tests/event/behavior/keypress.ts
Original file line number Diff line number Diff line change
Expand Up @@ -127,9 +127,9 @@ cases(
cases(
'submit form on [Enter]',
async ({html, click, submit}) => {
const {eventWasFired, xpathNode} = render(html, {focus: 'form/*[2]'})
const {eventWasFired, xpathNode} = render(html, {focus: '//form/*[2]'})

setupInstance().dispatchUIEvent(xpathNode('form/*[2]'), 'keypress', {
setupInstance().dispatchUIEvent(xpathNode('//form/*[2]'), 'keypress', {
key: 'Enter',
})

Expand All @@ -142,21 +142,51 @@ cases(
click: true,
submit: true,
},
'with `<input type="submit"/>` outside the form': {
html: `<div><form id="test-form"><input/><input/></form><input type="submit" form="test-form"/></div>`,
click: true,
submit: true,
},
'with `<input type="submit"/>` outside the form not linked by id': {
html: `<div><form><input/><input/></form><input type="submit" form="test-form"/></div>`,
click: false,
submit: false,
},
'with `<button/>`': {
html: `<form><input/><input/><button/></form>`,
click: true,
submit: true,
},
'with `<button/>` outside the form': {
html: `<div><form id="test-form"><input/><input/></form><button form="test-form"/></div>`,
click: true,
submit: true,
},
'with `<button type="submit"/>`': {
html: `<form><input/><input/><button type="submit"/></form>`,
click: true,
submit: true,
},
'with `<button type="submit"/>` outside the form': {
html: `<div><form id="test-form"><input/><input/></form><button type="submit" form="test-form"/></div>`,
click: true,
submit: true,
},
'with `<button type="button"/>`': {
html: `<form><input/><input/><button type="button"/></form>`,
click: false,
submit: false,
},
'with `<button type="button">` outside the form': {
html: `<div><form id="test-form"><input/><input/></form><button type="button" form="test-form"/></div>`,
click: false,
submit: false,
},
'with `<button/>` outside the form not linked to the form': {
html: `<div><form id="test-form"><input/><input/></form><button /></div>`,
click: false,
submit: false,
},
'on checkbox': {
html: `<form><input/><input type="checkbox"/><button type="submit"/></form>`,
click: true,
Expand Down