ui icon indicating copy to clipboard operation
ui copied to clipboard

Add `required` support for Combobox

Open gkorland opened this issue 1 year ago • 3 comments

The Combobox doesn't support a built it option to mark it as required when included in a form.

gkorland avatar Jan 31 '24 11:01 gkorland

+1

I have been searching for it in all docs, cant find. Any workaround available?

This is attributing to bad UX, where user does not know what fields are mandatory until he clicks on submit

therealravitej avatar Feb 01 '24 07:02 therealravitej

I couldn't find a workaround.

gkorland avatar Feb 05 '24 09:02 gkorland

Since the combobox component is actually composed of the popover and the command component, it will probably be difficult to add a required field to the combobox component, since neither the popover nor the command component are typical form elements.

However, if you use react-hook-form and (more importantly) zod, as suggested in the documentation, you can mark a field as required as follows:

const FormSchema = z.object({
  language: z.string({
    required_error: "Please select a language.",
  }),
})

Here is the implemented example.

However, if you are more concerned with the UX, a common option is to mark required fields with a specific symbol (*) and indicate at a specific point that marked fields are required. Hope that helps!

chris23lngr avatar Feb 09 '24 17:02 chris23lngr

This issue has been automatically closed because it received no activity for a while. If you think it was closed by accident, please leave a comment. Thank you.

shadcn avatar Mar 02 '24 23:03 shadcn

I don't think it was solved....

gkorland avatar Mar 03 '24 12:03 gkorland

You can always add a hidden input and forward the required to it together with all other form related props

<input type="hidden" required={required} value={value} name={name} onChange={onChange} .... />

I use it like that for any SSR for example remix-run

stevica-canadi avatar Mar 28 '24 17:03 stevica-canadi