Skip to content

Commit cddcd29

Browse files
committed
fix clicks and form submissions in Dialog component (#460)
* fix clicks and form submissions in Dialog component Fixes: #451 * update changelog
1 parent 0534b2e commit cddcd29

File tree

5 files changed

+72
-4
lines changed

5 files changed

+72
-4
lines changed

CHANGELOG.md

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,15 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
77

88
## [Unreleased - React]
99

10-
- Nothing yet!
10+
### Fixes
11+
12+
- Fix form submission within Dialog ([#460](https://github.com/tailwindlabs/headlessui/pull/460))
1113

1214
## [Unreleased - Vue]
1315

14-
- Nothing yet!
16+
### Fixes
17+
18+
- Fix form submission within Dialog ([#460](https://github.com/tailwindlabs/headlessui/pull/460))
1519

1620
## [@headlessui/react@v1.1.0] - 2021-04-26
1721

packages/@headlessui-react/src/components/dialog/dialog.test.tsx

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -533,6 +533,35 @@ describe('Mouse interactions', () => {
533533
})
534534
)
535535

536+
it(
537+
'should be possible to submit a form inside a Dialog',
538+
suppressConsoleLogs(async () => {
539+
let submitFn = jest.fn()
540+
function Example() {
541+
let [isOpen, setIsOpen] = useState(true)
542+
return (
543+
<Dialog open={isOpen} onClose={setIsOpen}>
544+
<form onSubmit={submitFn}>
545+
<input type="hidden" value="abc" />
546+
<button type="submit">Submit</button>
547+
</form>
548+
<TabSentinel />
549+
</Dialog>
550+
)
551+
}
552+
render(<Example />)
553+
554+
// Verify it is open
555+
assertDialog({ state: DialogState.Visible })
556+
557+
// Submit the form
558+
await click(getByText('Submit'))
559+
560+
// Verify that the submitFn function has been called
561+
expect(submitFn).toHaveBeenCalledTimes(1)
562+
})
563+
)
564+
536565
it(
537566
'should stop propagating click events when clicking on an element inside the Dialog',
538567
suppressConsoleLogs(async () => {

packages/@headlessui-react/src/components/dialog/dialog.tsx

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -244,7 +244,6 @@ let DialogRoot = forwardRefWithAs(function Dialog<
244244
'aria-labelledby': state.titleId,
245245
'aria-describedby': describedby,
246246
onClick(event: ReactMouseEvent) {
247-
event.preventDefault()
248247
event.stopPropagation()
249248
},
250249

packages/@headlessui-vue/src/components/dialog/dialog.test.ts

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -652,6 +652,43 @@ describe('Mouse interactions', () => {
652652
})
653653
)
654654

655+
it(
656+
'should be possible to submit a form inside a Dialog',
657+
suppressConsoleLogs(async () => {
658+
let submitFn = jest.fn()
659+
renderTemplate({
660+
template: `
661+
<Dialog v-if="true" :open="isOpen" @close="setIsOpen">
662+
<form @submit.prevent="submitFn">
663+
<input type="hidden" value="abc" />
664+
<button type="submit">Submit</button>
665+
</form>
666+
<TabSentinel />
667+
</Dialog>
668+
`,
669+
setup() {
670+
let isOpen = ref(true)
671+
return {
672+
isOpen,
673+
submitFn,
674+
setIsOpen(value: boolean) {
675+
isOpen.value = value
676+
},
677+
}
678+
},
679+
})
680+
681+
// Verify it is open
682+
assertDialog({ state: DialogState.Visible })
683+
684+
// Submit the form
685+
await click(getByText('Submit'))
686+
687+
// Verify that the submitFn function has been called
688+
expect(submitFn).toHaveBeenCalledTimes(1)
689+
})
690+
)
691+
655692
it(
656693
'should stop propagating click events when clicking on an element inside the Dialog',
657694
suppressConsoleLogs(async () => {

packages/@headlessui-vue/src/components/dialog/dialog.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -236,7 +236,6 @@ export let Dialog = defineComponent({
236236
titleId,
237237
describedby,
238238
handleClick(event: MouseEvent) {
239-
event.preventDefault()
240239
event.stopPropagation()
241240
},
242241

0 commit comments

Comments
 (0)