radix-ui / primitives

Radix Primitives is an open-source UI component library for building high-quality, accessible design systems and web apps. Maintained by @workos.
https://radix-ui.com/primitives
MIT License
15.93k stars 833 forks source link

[Select] add form prop (#2975) #3134

Closed cmd-johnson closed 1 month ago

cmd-johnson commented 2 months ago

Description

This PR adds a new form prop to the Select.Root primitive which serves the same purpose as the form attribute on a normal HTML <select> element. This allows us to use radix Select elements outside of a form's markup and still have its value included in that form's submissions.

Apart from adding the form prop to the Select.Root primitive, this PR also changes how isFormControl is determined. Instead of using element.closest('form'), it now uses element.form. This shouldn't have an impact to controls that are located inside a <form> already, because the form attribute is automatically set to the parent form element. For elements outside a form, this attribute is unset, so it should be functionally the same as the previous behavior, with the added benefit of supporting overriding the associated form using the form attribute passed to the element.

There's more details in my original feature request #2975.

chaance commented 1 month ago

See https://github.com/radix-ui/primitives/pull/2484#issuecomment-2379643220. We'll need to revert to the closet check.

chaance commented 1 month ago

I'm actually going to close this for now. Per https://github.com/radix-ui/primitives/issues/2530#issuecomment-2380105452 I'd like to introduce a more wholistic solution to cover all affected primitives, and I still need to do some manual testing for edge cases.

Tracking in https://github.com/radix-ui/primitives/issues/2530