Skip to content

Commit

Permalink
fix clicks and form submissions in Dialog component (#460)
Browse files Browse the repository at this point in the history
* fix clicks and form submissions in Dialog component

Fixes: #451

* update changelog
  • Loading branch information
RobinMalfait authored Apr 28, 2021
1 parent d412952 commit df9d439
Show file tree
Hide file tree
Showing 5 changed files with 72 additions and 4 deletions.
8 changes: 6 additions & 2 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,15 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

## [Unreleased - React]

- Nothing yet!
### Fixes

- Fix form submission within Dialog ([#460](https://github.com/tailwindlabs/headlessui/pull/460))

## [Unreleased - Vue]

- Nothing yet!
### Fixes

- Fix form submission within Dialog ([#460](https://github.com/tailwindlabs/headlessui/pull/460))

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

Expand Down
29 changes: 29 additions & 0 deletions packages/@headlessui-react/src/components/dialog/dialog.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -533,6 +533,35 @@ describe('Mouse interactions', () => {
})
)

it(
'should be possible to submit a form inside a Dialog',
suppressConsoleLogs(async () => {
let submitFn = jest.fn()
function Example() {
let [isOpen, setIsOpen] = useState(true)
return (
<Dialog open={isOpen} onClose={setIsOpen}>
<form onSubmit={submitFn}>
<input type="hidden" value="abc" />
<button type="submit">Submit</button>
</form>
<TabSentinel />
</Dialog>
)
}
render(<Example />)

// Verify it is open
assertDialog({ state: DialogState.Visible })

// Submit the form
await click(getByText('Submit'))

// Verify that the submitFn function has been called
expect(submitFn).toHaveBeenCalledTimes(1)
})
)

it(
'should stop propagating click events when clicking on an element inside the Dialog',
suppressConsoleLogs(async () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -244,7 +244,6 @@ let DialogRoot = forwardRefWithAs(function Dialog<
'aria-labelledby': state.titleId,
'aria-describedby': describedby,
onClick(event: ReactMouseEvent) {
event.preventDefault()
event.stopPropagation()
},

Expand Down
37 changes: 37 additions & 0 deletions packages/@headlessui-vue/src/components/dialog/dialog.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -652,6 +652,43 @@ describe('Mouse interactions', () => {
})
)

it(
'should be possible to submit a form inside a Dialog',
suppressConsoleLogs(async () => {
let submitFn = jest.fn()
renderTemplate({
template: `
<Dialog v-if="true" :open="isOpen" @close="setIsOpen">
<form @submit.prevent="submitFn">
<input type="hidden" value="abc" />
<button type="submit">Submit</button>
</form>
<TabSentinel />
</Dialog>
`,
setup() {
let isOpen = ref(true)
return {
isOpen,
submitFn,
setIsOpen(value: boolean) {
isOpen.value = value
},
}
},
})

// Verify it is open
assertDialog({ state: DialogState.Visible })

// Submit the form
await click(getByText('Submit'))

// Verify that the submitFn function has been called
expect(submitFn).toHaveBeenCalledTimes(1)
})
)

it(
'should stop propagating click events when clicking on an element inside the Dialog',
suppressConsoleLogs(async () => {
Expand Down
1 change: 0 additions & 1 deletion packages/@headlessui-vue/src/components/dialog/dialog.ts
Original file line number Diff line number Diff line change
Expand Up @@ -236,7 +236,6 @@ export let Dialog = defineComponent({
titleId,
describedby,
handleClick(event: MouseEvent) {
event.preventDefault()
event.stopPropagation()
},

Expand Down

0 comments on commit df9d439

Please sign in to comment.