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

add type="button" to all components using button element #150

Merged

Conversation

wysher
Copy link
Contributor

@wysher wysher commented Oct 28, 2023

Using SelectTrigger shadcn/svelte with forms causes form submit. This happens, because button by default has type="submit".

I think it's good idea to set type="button" instead, to prevent unwanted submits.
Here's implementation which allows to change it by developer, but maybe it should not be possible to changed at all?

@changeset-bot
Copy link

changeset-bot bot commented Oct 28, 2023

🦋 Changeset detected

Latest commit: 8efa3b1

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 1 package
Name Type
bits-ui Patch

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

@vercel
Copy link

vercel bot commented Oct 28, 2023

The latest updates on your projects. Learn more about Vercel for Git ↗︎

Name Status Preview Comments Updated (UTC)
primitives ✅ Ready (Inspect) Visit Preview 💬 Add feedback Oct 30, 2023 7:29am

@huntabyte
Copy link
Owner

Hey @wysher , thanks for this! I don't think it should be able to be changed at all tbh. If someone really wants to change it, they can use asChild and override the attribute.

Copy link
Owner

@huntabyte huntabyte left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for noticing this! We could also probably Omit the type prop from the TriggerProps as well!

@@ -7,6 +7,7 @@
type $$Props = TriggerProps;
type $$Events = TriggerEvents;
export let asChild = false;
export let type: $$Props["type"] = "button";
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
export let type: $$Props["type"] = "button";

@@ -22,6 +23,7 @@
use:melt={builder}
{...$$restProps}
{...attrs}
{type}
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
{type}
type="button"

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@huntabyte all done. And also more... I searched for button elements within bits components and added more type=button. I'm pretty sure all of them needs it.

Especially checkbox or radio - you don't want form inputs to trigger submit at any time. Same with tabs, popovers, etc.

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You're incredible, thank you!

@wysher wysher force-pushed the fix/SelectTrigger-with-default-button-type branch from a30b785 to e0f77b1 Compare October 29, 2023 12:44
@wysher wysher changed the title SelectTrigger with default button type add type="button" to all components using button element Oct 29, 2023
@wysher wysher force-pushed the fix/SelectTrigger-with-default-button-type branch from e0f77b1 to 10b8b52 Compare October 29, 2023 13:04
Copy link
Owner

@huntabyte huntabyte left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For the rest of the buttons, I think we should do something like this:

		type="button"
		{...$$restProps}
		{...attrs}

That way, if for some reason or another someone wants to submit the form when they close the dialog, or toggle a switch, or whatever, they are able to override the type="button" we set by passing type="submit" since the ...$$restProps follows our hardcoded type.

@@ -23,6 +23,7 @@
use:melt={builder}
{...$$restProps}
{...attrs}
type="button"
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
type="button"

I think we leave this up to the dev to add here if they wish. Imagine you wanted to add a tooltip to a button that submits a form, with the tooltip saying "Submit form" or something. We don't want to prevent the user from doing something like this.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

makes sense here. Done.

@@ -30,7 +30,7 @@ type ContentProps<
> &
HTMLDivAttributes;

type TriggerProps = AsChild & HTMLButtonAttributes;
type TriggerProps = AsChild & Omit<HTMLButtonAttributes, "type">;
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
type TriggerProps = AsChild & Omit<HTMLButtonAttributes, "type">;
type TriggerProps = AsChild & HTMLButtonAttributes;

@huntabyte
Copy link
Owner

Thanks again @wysher!

@huntabyte huntabyte merged commit 6a1e214 into huntabyte:main Oct 30, 2023
@github-actions github-actions bot mentioned this pull request Oct 30, 2023
@wysher
Copy link
Contributor Author

wysher commented Nov 6, 2023 via email

@wysher
Copy link
Contributor Author

wysher commented Nov 6, 2023

One and only reason is that changed components here uses button element under the hood - without developer knowledge. Button component which will be deliberately used by developer was not changed by this PR.

@huntabyte this one left me with one question. Maybe AlertDialogAction should not set type="button" as default, cause it looks like submit and AlertDialogContent may be wrapped with form?

@wbhob
Copy link

wbhob commented Nov 10, 2023

I realized and deleted my comments after. Great PR! :)

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.

3 participants