Skip to content

Commit 9b42daf

Browse files
authored
improve attemptSubmit submission (#2625)
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.
1 parent 954a3ac commit 9b42daf

File tree

2 files changed

+10
-6
lines changed
  • packages

2 files changed

+10
-6
lines changed

packages/@headlessui-react/src/utils/form.ts

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -36,11 +36,13 @@ function append(entries: Entries, key: string, value: any): void {
3636
}
3737
}
3838

39-
export function attemptSubmit(element: HTMLElement) {
40-
let form = (element as any)?.form ?? element.closest('form')
39+
export function attemptSubmit(elementInForm: HTMLElement) {
40+
let form = (elementInForm as any)?.form ?? elementInForm.closest('form')
4141
if (!form) return
4242

4343
for (let element of form.elements) {
44+
if (element === elementInForm) continue
45+
4446
if (
4547
(element.tagName === 'INPUT' && element.type === 'submit') ||
4648
(element.tagName === 'BUTTON' && element.type === 'submit') ||
@@ -58,5 +60,5 @@ export function attemptSubmit(element: HTMLElement) {
5860
// If we get here, then there is no submit button in the form. We can use the
5961
// `form.requestSubmit()` function to submit the form instead. We cannot use `form.submit()`
6062
// because then the `submit` event won't be fired and `onSubmit` listeners won't be fired.
61-
form.requestSubmit()
63+
form.requestSubmit?.()
6264
}

packages/@headlessui-vue/src/utils/form.ts

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -36,11 +36,13 @@ function append(entries: Entries, key: string, value: any): void {
3636
}
3737
}
3838

39-
export function attemptSubmit(element: HTMLElement) {
40-
let form = (element as any)?.form ?? element.closest('form')
39+
export function attemptSubmit(elementInForm: HTMLElement) {
40+
let form = (elementInForm as any)?.form ?? elementInForm.closest('form')
4141
if (!form) return
4242

4343
for (let element of form.elements) {
44+
if (element === elementInForm) continue
45+
4446
if (
4547
(element.tagName === 'INPUT' && element.type === 'submit') ||
4648
(element.tagName === 'BUTTON' && element.type === 'submit') ||
@@ -58,5 +60,5 @@ export function attemptSubmit(element: HTMLElement) {
5860
// If we get here, then there is no submit button in the form. We can use the
5961
// `form.requestSubmit()` function to submit the form instead. We cannot use `form.submit()`
6062
// because then the `submit` event won't be fired and `onSubmit` listeners won't be fired.
61-
form.requestSubmit()
63+
form.requestSubmit?.()
6264
}

0 commit comments

Comments
 (0)