Skip to content

Commit

Permalink
improve attemptSubmit submission (#2625)
Browse files Browse the repository at this point in the history
When you pass in an element to the `attemptSubmit` that has a
`type="submit"`, then the `attemptSubmit` will just click this element.

We want to skip the current one and fallback to `form.requestSubmit()`
instead.
  • Loading branch information
RobinMalfait committed Jul 28, 2023
1 parent 954a3ac commit 9b42daf
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 6 deletions.
8 changes: 5 additions & 3 deletions packages/@headlessui-react/src/utils/form.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,11 +36,13 @@ function append(entries: Entries, key: string, value: any): void {
}
}

export function attemptSubmit(element: HTMLElement) {
let form = (element as any)?.form ?? element.closest('form')
export function attemptSubmit(elementInForm: HTMLElement) {
let form = (elementInForm as any)?.form ?? elementInForm.closest('form')
if (!form) return

for (let element of form.elements) {
if (element === elementInForm) continue

if (
(element.tagName === 'INPUT' && element.type === 'submit') ||
(element.tagName === 'BUTTON' && element.type === 'submit') ||
Expand All @@ -58,5 +60,5 @@ export function attemptSubmit(element: HTMLElement) {
// If we get here, then there is no submit button in the form. We can use the
// `form.requestSubmit()` function to submit the form instead. We cannot use `form.submit()`
// because then the `submit` event won't be fired and `onSubmit` listeners won't be fired.
form.requestSubmit()
form.requestSubmit?.()
}
8 changes: 5 additions & 3 deletions packages/@headlessui-vue/src/utils/form.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,11 +36,13 @@ function append(entries: Entries, key: string, value: any): void {
}
}

export function attemptSubmit(element: HTMLElement) {
let form = (element as any)?.form ?? element.closest('form')
export function attemptSubmit(elementInForm: HTMLElement) {
let form = (elementInForm as any)?.form ?? elementInForm.closest('form')
if (!form) return

for (let element of form.elements) {
if (element === elementInForm) continue

if (
(element.tagName === 'INPUT' && element.type === 'submit') ||
(element.tagName === 'BUTTON' && element.type === 'submit') ||
Expand All @@ -58,5 +60,5 @@ export function attemptSubmit(element: HTMLElement) {
// If we get here, then there is no submit button in the form. We can use the
// `form.requestSubmit()` function to submit the form instead. We cannot use `form.submit()`
// because then the `submit` event won't be fired and `onSubmit` listeners won't be fired.
form.requestSubmit()
form.requestSubmit?.()
}

2 comments on commit 9b42daf

@vercel
Copy link

@vercel vercel bot commented on 9b42daf Jul 28, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Successfully deployed to the following URLs:

headlessui-vue – ./packages/playground-vue

headlessui-vue.vercel.app
headlessui-vue-git-main-tailwindlabs.vercel.app
headlessui-vue-tailwindlabs.vercel.app

@vercel
Copy link

@vercel vercel bot commented on 9b42daf Jul 28, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Successfully deployed to the following URLs:

headlessui-react – ./packages/playground-react

headlessui-react.vercel.app
headlessui-react-tailwindlabs.vercel.app
headlessui-react-git-main-tailwindlabs.vercel.app

Please sign in to comment.