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

Default Switch to type="button" #178

Closed
ulken opened this issue Dec 19, 2020 · 3 comments
Closed

Default Switch to type="button" #178

ulken opened this issue Dec 19, 2020 · 3 comments

Comments

@ulken
Copy link
Contributor

ulken commented Dec 19, 2020

Problem

Ran into an interesting behavior where hitting Enter on/in another form field toggled the switch state. To be clear: the switch was not focused.

Demo

Cause

After some digging, I found the reason to be: a <button> inside a <form> is implicitly assigned type="submit" if nothing else is specified. Hitting Enter inside a <form> triggers a click on the first submit (implicit or not) button in the form. Since, in my case, the <Switch> is the first such button of the form and there's an onClick handler assigned, the <Switch> is toggled.

Workaround

Explicitly set <Switch type="button">.

Solution

When <Switch> is rendered as <button> (as per default), explicitly set type="button" by default.

Rationale

I can't think of a valid reason the current behavior is ever desired.

@ulken ulken changed the title Default Switch to type=button Default Switch to type="button" Dec 20, 2020
@ulken
Copy link
Contributor Author

ulken commented Dec 21, 2020

I'd be happy to supply a PR if you're onboard.

RobinMalfait added a commit that referenced this issue Jan 8, 2021
Closes: #178

Co-authored-by: =?UTF-8?q?Oskar=20L=C3=B6fgren?= <[email protected]>
@RobinMalfait
Copy link
Member

Hey! Thank you for your suggestion!
Much appreciated! 🙏

I implemented it myself here #192 however I marked you as a co-author!

@Kalissaac
Copy link

This issue also seems to occur if the as type is not specified to be button explicitly: https://codesandbox.io/s/headlessuireact-switch-example-forked-yjn8i (on the latest 0.3.1 version of headlessui)

If the as prop is not passed it defaults to button, however in that case the type prop is not passed to the element. Is there a way for the type prop to be passed if as and type are not explicitly specified?

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

No branches or pull requests

3 participants