You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Native <select>s (and all other input elements) have a form prop that lets us override which form the input belongs to. As far as I can tell, the @radix-ui/react-select primitives don't support this property right now.
Overview
When using native <select> elements, you can render a select element outside of a <form> and later set the element's form using the form's id:
Clicking the submit button now doesn't include the selectInsideOfForm element in the form.
Who does this impact? Who is this for?
Supporting the form attribute would enable use-cases such as
rendering a native <select> element using SSR and then replace it with a radix <Select> element after hydration, allowing users to use the app before it is hydrated (or even with JS disabled)
allowing more ways to structure forms by rendering <Select> elements outside of a <form> element
From looking through radix's code, I'd expect the implementation to work somewhat along these lines:
adding a new form prop to the Select.Root component
adding a new form property to the SelectContextValue type
passing the form prop value to the <SelectProvider>
set the form attribute of the Primitive.button element inside the Select.Trigger to the form value from useSelectContext
set the form attribute of the BubbleSelect component to the form value from useSelectContext
change the logic of determining isFormControl inside of the Select.Root component to check for the trigger's form (which will the browser sets to the actual form element reference at runtime) instead of using trigger.closest("form").
This might be a breaking change, but should only impact users that are passing a form prop directly to a Select.Trigger component. This is currently valid because the trigger element re-uses the native HTMLButtonProps type. Changing the trigger's form prop directly would lead to issues with the isFormControl logic, so an implementation would ideally omit the form prop from the SelectTriggerProps type, so the form prop can only be set through Select.Root.
I think that's it, though. I'd be happy to submit a PR for this if requested.
The text was updated successfully, but these errors were encountered:
Feature request
Native
<select>
s (and all other input elements) have aform
prop that lets us override which form the input belongs to. As far as I can tell, the@radix-ui/react-select
primitives don't support this property right now.Overview
When using native
<select>
elements, you can render a select element outside of a<form>
and later set the element's form using the form's id:Clicking the submit button includes the
selectOutsideOfForm
value in the resulting submission.Similarly you can exclude a
<select>
from a form by setting it'sform
attribute to an invalid form id:Clicking the submit button now doesn't include the
selectInsideOfForm
element in the form.Who does this impact? Who is this for?
Supporting the
form
attribute would enable use-cases such as<select>
element using SSR and then replace it with a radix<Select>
element after hydration, allowing users to use the app before it is hydrated (or even with JS disabled)<Select>
elements outside of a<form>
elementAdditional context
MDN reference for the
<select>
form
attributeFrom looking through radix's code, I'd expect the implementation to work somewhat along these lines:
form
prop to theSelect.Root
componentform
property to theSelectContextValue
typeform
prop value to the<SelectProvider>
form
attribute of thePrimitive.button
element inside theSelect.Trigger
to theform
value fromuseSelectContext
form
attribute of theBubbleSelect
component to theform
value fromuseSelectContext
isFormControl
inside of theSelect.Root
component to check for the trigger'sform
(which will the browser sets to the actual form element reference at runtime) instead of usingtrigger.closest("form")
.form
prop directly to aSelect.Trigger
component. This is currently valid because the trigger element re-uses the nativeHTMLButtonProps
type. Changing the trigger'sform
prop directly would lead to issues with theisFormControl
logic, so an implementation would ideally omit theform
prop from theSelectTriggerProps
type, so the form prop can only be set throughSelect.Root
.I think that's it, though. I'd be happy to submit a PR for this if requested.
The text was updated successfully, but these errors were encountered: