Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow Enter for form submit in RadioGroup, Switch and Combobox improvements #1285

Merged
merged 7 commits into from
Mar 31, 2022

Conversation

RobinMalfait
Copy link
Member

@RobinMalfait RobinMalfait commented Mar 30, 2022

When the Combobox is closed, then the Enter keydown event will be ignored and thus not use event.preventDefault().

With recent changes where we always have an active option, it means that you will always be able to select an option.

If we have no option at all (some edge case) or when the combobox is closed, then the Enter keydown event will just bubble, allowing you to submit a form.

Fixes: #1282

This is a continuation of a PR (#1176) provided by Alexander, so wanted to include them as a co-author because of their initial work.


This also applies a few more form related improvements.

  • Adding display: none to the VisuallyHidden component, otherwise even with the hidden and readOnly attributes it is still focusable...
  • Rendering the input fields in the same nested Fragment, otherwise the body gets focus once the input is rendered... it's either a React bug (because the DOM is the same with or without the Fragment in the end) or it's an obscure edge case.
  • Ensuring that you can use Enter on other form related elements.

@vercel
Copy link

vercel bot commented Mar 30, 2022

This pull request is being automatically deployed with Vercel (learn more).
To see the status of your deployments, click below or on the icon next to each commit.

headlessui-react – ./packages/playground-react

🔍 Inspect: https://vercel.com/tailwindlabs/headlessui-react/FqYF7CFaSYmAsqJ9qDmPXNGPD1K5
✅ Preview: https://headlessui-react-git-allow-submits-in-combo-89d9c1-tailwindlabs.vercel.app

headlessui-vue – ./packages/playground-vue

🔍 Inspect: https://vercel.com/tailwindlabs/headlessui-vue/4QpY5VKqRrXAxBoGT2R9CR2daoPo
✅ Preview: https://headlessui-vue-git-allow-submits-in-combobo-29bd55-tailwindlabs.vercel.app

@RobinMalfait RobinMalfait force-pushed the allow-submits-in-combobox-input-field branch from 31bf3ff to 96f14b6 Compare March 30, 2022 12:01
@RobinMalfait RobinMalfait force-pushed the allow-submits-in-combobox-input-field branch from 96f14b6 to cd879d3 Compare March 30, 2022 12:02
@RobinMalfait RobinMalfait changed the title Ensure we can submit a form from a closes Combobox Ensure we can submit a form from a closes Combobox Mar 30, 2022
@RobinMalfait RobinMalfait changed the title Ensure we can submit a form from a closes Combobox Ensure we can submit a form from a closed Combobox Mar 30, 2022
@RobinMalfait RobinMalfait changed the title Ensure we can submit a form from a closed Combobox Form improvements Mar 30, 2022
@RobinMalfait RobinMalfait force-pushed the allow-submits-in-combobox-input-field branch from cd879d3 to a813b2e Compare March 31, 2022 19:37
RobinMalfait and others added 7 commits March 31, 2022 21:38
This will allow us to _try_ and submit a form based on any element you
pass it. It will try and lookup the current form and if it is
submittable it will attempt to submit it.

Instead of submitting the form directly, we try to follow the native
browser support where it looks for the first `input[type=submit]`,
`input[type=image]`, `button` or `button[type=submit]`, then it clicks
it.

This allows you to disable your submit button, or have an `onClick` that
does an `event.preventDefault()` just like the native form in a browser
would do.
When the Combobox is closed, then the `Enter` keydown event will be
ignored and thus not use `event.preventDefault()`.

With recent changes where we always have an active option, it means that
you will always be able to select an option.

If we have no option at all (some edge case) or when the combobox is
closed, then the `Enter` keydown event will just bubble, allowing you to
submit a form.

Fixes: #1282

This is a continuation of a PR ([#1176](#1176)) provided by Alexander, so wanted to include
them as a co-author because of their initial work.

Co-authored-by: Alexander Lyon <arlyon@me.com>
@RobinMalfait RobinMalfait force-pushed the allow-submits-in-combobox-input-field branch from a813b2e to f6368a8 Compare March 31, 2022 19:40
@RobinMalfait RobinMalfait changed the title Form improvements Allow Enter for form submit in RadioGroup, Switch and Combobox improvements Mar 31, 2022
@RobinMalfait RobinMalfait merged commit c475cab into main Mar 31, 2022
@RobinMalfait RobinMalfait deleted the allow-submits-in-combobox-input-field branch March 31, 2022 19:42
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[BUG] Combobox in a <form> tag issue
1 participant