Skip to content

Commit

Permalink
Submit form on Enter even if no submit-like button was found (#2613)
Browse files Browse the repository at this point in the history
* `requestSubmit` when a submit-like button cannot be found

* add tests

* update changelog
  • Loading branch information
RobinMalfait committed Jul 25, 2023
1 parent 2eabdd4 commit ac859fe
Show file tree
Hide file tree
Showing 10 changed files with 247 additions and 0 deletions.
1 change: 1 addition & 0 deletions packages/@headlessui-react/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- Improve "outside click" behaviour in combination with 3rd party libraries ([#2572](https://github.com/tailwindlabs/headlessui/pull/2572))
- Ensure IME works on Android devices ([#2580](https://github.com/tailwindlabs/headlessui/pull/2580))
- Calculate `aria-expanded` purely based on the open/closed state ([#2610](https://github.com/tailwindlabs/headlessui/pull/2610))
- Submit form on `Enter` even if no submit-like button was found ([#2613](https://github.com/tailwindlabs/headlessui/pull/2613))

## [1.7.15] - 2023-06-01

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2820,6 +2820,54 @@ describe('Keyboard interactions', () => {
expect(submits).toHaveBeenCalledWith([['option', 'b']])
})
)

it(
'should submit the form on `Enter` (when no submit button was found)',
suppressConsoleLogs(async () => {
let submits = jest.fn()

function Example() {
let [value, setValue] = useState<string>('b')

return (
<form
onKeyUp={(event) => {
// JSDom doesn't automatically submit the form but if we can
// catch an `Enter` event, we can assume it was a submit.
if (event.key === 'Enter') event.currentTarget.submit()
}}
onSubmit={(event) => {
event.preventDefault()
submits([...new FormData(event.currentTarget).entries()])
}}
>
<Combobox value={value} onChange={setValue} name="option">
<Combobox.Input onChange={NOOP} />
<Combobox.Button>Trigger</Combobox.Button>
<Combobox.Options>
<Combobox.Option value="a">Option A</Combobox.Option>
<Combobox.Option value="b">Option B</Combobox.Option>
<Combobox.Option value="c">Option C</Combobox.Option>
</Combobox.Options>
</Combobox>
</form>
)
}

render(<Example />)

// Focus the input field
await focus(getComboboxInput())
assertActiveElement(getComboboxInput())

// Press enter (which should submit the form)
await press(Keys.Enter)

// Verify the form was submitted
expect(submits).toHaveBeenCalledTimes(1)
expect(submits).toHaveBeenCalledWith([['option', 'b']])
})
)
})

describe('`Tab` key', () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1286,6 +1286,44 @@ describe('Keyboard interactions', () => {
expect(submits).toHaveBeenCalledWith([['option', 'bob']])
})
)

it(
'should submit the form on `Enter` (when no submit button was found)',
suppressConsoleLogs(async () => {
let submits = jest.fn()

function Example() {
let [value, setValue] = useState('bob')

return (
<form
onSubmit={(event) => {
event.preventDefault()
submits([...new FormData(event.currentTarget).entries()])
}}
>
<RadioGroup value={value} onChange={setValue} name="option">
<RadioGroup.Option value="alice">Alice</RadioGroup.Option>
<RadioGroup.Option value="bob">Bob</RadioGroup.Option>
<RadioGroup.Option value="charlie">Charlie</RadioGroup.Option>
</RadioGroup>
</form>
)
}

render(<Example />)

// Focus the RadioGroup
await press(Keys.Tab)

// Press enter (which should submit the form)
await press(Keys.Enter)

// Verify the form was submitted
expect(submits).toHaveBeenCalledTimes(1)
expect(submits).toHaveBeenCalledWith([['option', 'bob']])
})
)
})
})

Expand Down
32 changes: 32 additions & 0 deletions packages/@headlessui-react/src/components/switch/switch.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -461,6 +461,38 @@ describe('Keyboard interactions', () => {
expect(submits).toHaveBeenCalledTimes(1)
expect(submits).toHaveBeenCalledWith([['option', 'on']])
})

it('should submit the form on `Enter` (when no submit button was found)', async () => {
let submits = jest.fn()

function Example() {
let [value, setValue] = useState(true)

return (
<form
onSubmit={(event) => {
event.preventDefault()
submits([...new FormData(event.currentTarget).entries()])
}}
>
<Switch checked={value} onChange={setValue} name="option" />
</form>
)
}

render(<Example />)

// Focus the input field
await focus(getSwitch())
assertActiveElement(getSwitch())

// Press enter (which should submit the form)
await press(Keys.Enter)

// Verify the form was submitted
expect(submits).toHaveBeenCalledTimes(1)
expect(submits).toHaveBeenCalledWith([['option', 'on']])
})
})

describe('`Tab` key', () => {
Expand Down
5 changes: 5 additions & 0 deletions packages/@headlessui-react/src/utils/form.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,4 +54,9 @@ export function attemptSubmit(element: HTMLElement) {
return
}
}

// 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()
}
1 change: 1 addition & 0 deletions packages/@headlessui-vue/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- Improve performance of `Combobox` component ([#2574](https://github.com/tailwindlabs/headlessui/pull/2574))
- Ensure IME works on Android devices ([#2580](https://github.com/tailwindlabs/headlessui/pull/2580))
- Calculate `aria-expanded` purely based on the open/closed state ([#2610](https://github.com/tailwindlabs/headlessui/pull/2610))
- Submit form on `Enter` even if no submit-like button was found ([#2613](https://github.com/tailwindlabs/headlessui/pull/2613))

## [1.7.14] - 2023-06-01

Expand Down
49 changes: 49 additions & 0 deletions packages/@headlessui-vue/src/components/combobox/combobox.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2941,6 +2941,55 @@ describe('Keyboard interactions', () => {
expect(submits).toHaveBeenCalledWith([['option', 'b']])
})
)

it(
'should submit the form on `Enter` (when no submit button was found)',
suppressConsoleLogs(async () => {
let submits = jest.fn()

renderTemplate({
template: html`
<form @submit="handleSubmit" @keyup="handleKeyUp">
<Combobox v-model="value" name="option">
<ComboboxInput />
<ComboboxButton>Trigger</ComboboxButton>
<ComboboxOptions>
<ComboboxOption value="a">Option A</ComboboxOption>
<ComboboxOption value="b">Option B</ComboboxOption>
<ComboboxOption value="c">Option C</ComboboxOption>
</ComboboxOptions>
</Combobox>
</form>
`,
setup() {
let value = ref('b')
return {
value,
handleKeyUp(event: KeyboardEvent) {
// JSDom doesn't automatically submit the form but if we can
// catch an `Enter` event, we can assume it was a submit.
if (event.key === 'Enter') (event.currentTarget as HTMLFormElement).submit()
},
handleSubmit(event: SubmitEvent) {
event.preventDefault()
submits([...new FormData(event.currentTarget as HTMLFormElement).entries()])
},
}
},
})

// Focus the input field
getComboboxInput()?.focus()
assertActiveElement(getComboboxInput())

// Press enter (which should submit the form)
await press(Keys.Enter)

// Verify the form was submitted
expect(submits).toHaveBeenCalledTimes(1)
expect(submits).toHaveBeenCalledWith([['option', 'b']])
})
)
})

describe('`Tab` key', () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1488,6 +1488,42 @@ describe('Keyboard interactions', () => {
expect(submits).toHaveBeenCalledTimes(1)
expect(submits).toHaveBeenCalledWith([['option', 'bob']])
})

it('should submit the form on `Enter` (when no submit button was found)', async () => {
let submits = jest.fn()

renderTemplate({
template: html`
<form @submit="handleSubmit">
<RadioGroup v-model="value" name="option">
<RadioGroupOption value="alice">Alice</RadioGroupOption>
<RadioGroupOption value="bob">Bob</RadioGroupOption>
<RadioGroupOption value="charlie">Charlie</RadioGroupOption>
</RadioGroup>
</form>
`,
setup() {
let value = ref('bob')
return {
value,
handleSubmit(event: KeyboardEvent) {
event.preventDefault()
submits([...new FormData(event.currentTarget as HTMLFormElement).entries()])
},
}
},
})

// Focus the RadioGroup
await press(Keys.Tab)

// Press enter (which should submit the form)
await press(Keys.Enter)

// Verify the form was submitted
expect(submits).toHaveBeenCalledTimes(1)
expect(submits).toHaveBeenCalledWith([['option', 'bob']])
})
})
})

Expand Down
32 changes: 32 additions & 0 deletions packages/@headlessui-vue/src/components/switch/switch.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -589,6 +589,38 @@ describe('Keyboard interactions', () => {
expect(submits).toHaveBeenCalledTimes(1)
expect(submits).toHaveBeenCalledWith([['option', 'on']])
})

it('should submit the form on `Enter` (when no submit button was found)', async () => {
let submits = jest.fn()
renderTemplate({
template: html`
<form @submit="handleSubmit">
<Switch v-model="checked" name="option" />
</form>
`,
setup() {
let checked = ref(true)
return {
checked,
handleSubmit(event: KeyboardEvent) {
event.preventDefault()
submits([...new FormData(event.currentTarget as HTMLFormElement).entries()])
},
}
},
})

// Focus the input field
getSwitch()?.focus()
assertActiveElement(getSwitch())

// Press enter (which should submit the form)
await press(Keys.Enter)

// Verify the form was submitted
expect(submits).toHaveBeenCalledTimes(1)
expect(submits).toHaveBeenCalledWith([['option', 'on']])
})
})

describe('`Tab` key', () => {
Expand Down
5 changes: 5 additions & 0 deletions packages/@headlessui-vue/src/utils/form.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,4 +54,9 @@ export function attemptSubmit(element: HTMLElement) {
return
}
}

// 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()
}

2 comments on commit ac859fe

@vercel
Copy link

@vercel vercel bot commented on ac859fe Jul 25, 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 ac859fe Jul 25, 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.