-
Notifications
You must be signed in to change notification settings - Fork 852
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
Support ref in Select Component #2590
Comments
After working on this some more, I found a way to avoid passing the ref to the underlying <Controller
control={control}
name="installation_type"
render={({ field }) => {
return (
<SelectPrimitive.Root onValueChange={field.onChange} {...field}>
<SelectPrimitive.Trigger>
<SelectPrimitive.Value>
{field.value}
</SelectPrimitive.Value>
</SelectPrimitive.Trigger>
...
</SelectPrimitive.Root>
)
}}
></Controller> Instead of expanding |
We also use Controller for this reason (and it has been popularised by the form implementation in ShadCN). But, you do lose a benefit of autofocusing a field that's in an error state. That only works if you use So, it would be great if these could be used uncontrolled. |
This is something we need to do holistically throughout all of our primitives probably, so will need a bit more thought. |
+1 on getting access to the select element via ref, having issues currently with Remix Validated Form. :( |
I'm unsure wether the interface has changed since that, but I found that by using the ref available in SelectTrigger we can have auto-focus even when using Controller from react-hook-form. |
@dantxal Great tip to pass the ref to the Here's how this looks like: <Controller
// Makes it a bit more type safe, I prefer a typed form
name={register("foo").name}
control={control}
render={({ field }) => {
const { onChange, ref, ...rest } = field;
return (
<Select.Root onValueChange={onChange} {...rest}>
<Select.Trigger ref={ref} />
<Select.Content>
{options.map(({ label, value }) => (
// Make sure the values are unique!
<Select.Item key={value} value={value}>
{label}
</Select.Item>
))}
</Select.Content>
</Select.Root>
);
}}
/>; |
Feature request
Overview
The Select component does not allow a ref to be passed to the underlying html
select
element. This breaks react-hook-form'sregister()
function; We are using remix-forms which usesregister()
to allow any custom component to be used in a form, as long as it can pass that ref to the underlyingselect
. @jaimiz expressed the same problem in #1851 (comment)The needed change is minimal; The
Select
component must accept a ref viaReact.forwardRef
and pass the ref toBubbleSelect
.BubbleSelect
even supports the ref param, but it isn't used. PerhapsSelect
was intended to have this behavior originally?We have already tried the solution in our project with
patch-package
and saw it work. I will create a PR with the change shortly. Is there any reason this wouldn't be accepted? Is there an alternative we haven't considered?Examples in other libraries
N/A
Who does this impact? Who is this for?
The text was updated successfully, but these errors were encountered: