Select
A component for collecting long-form data Select.
Enter your full name
Usage
import { Select, SelectOption, SelectWrapper } from "#/ui/components/Select"
<SelectWrapper label="Name">
<Select options={employees.map((employee) => employee.name)} />
</SelectWrapper>
How it works
The Select component provides some base styling to the radix Select element.
The SelectWrapper goes around your Select to provide additional associated elements easily like labels & hints, but it’s totally optional if you want to use a Select separate from your labels and roll-your-own.
<SelectWrapper label="Name">
<Select options={employees.map((employee) => employee.name)} client:load />
</SelectWrapper><div className="w-full flex items-center">
<label htmlFor="example-Select" className="w-1/3">
Custom Label
</label>
<Select
id="example-Select"
options={employees.map((employee) => employee.name)}
client:load
/>
</div>Hint
You can pass a hint to the SelectWrapper to provide additional context to the user.
What do you call yourself?
<SelectWrapper label="Name" hint="required">
<Select options={employees.map((employee) => employee.name)} client:load />
</SelectWrapper>Error
Passing an error to the SelectWrapper and Select will display error styles. If a hint exists, it will be replaced with the error.
Your name is required
<SelectWrapper label="Name" hint="required" error="Your name is required">
<Select options={employees.map((employee) => employee.name)} error={true} client:load />
</SelectWrapper>Customization
Custom Option Elements
If you need more fine grain control over your option elements and how they look, you can use the SelectOption element and pass it as a child.
The child elements of SelectOption will be displayed however you create them. In order to ensure the Select works properly, you must pass a value string to the SelectOption element.
<SelectWrapper label="Name">
<Select>
{employees.map((employee) => (
<SelectOption key={employee.name} value={employee.name}>
<div className="flex items-center gap-2 text-left">
<img src={employee.avatar} alt={employee.name} className="size-8 rounded-full" />
<div>
<div className="font-bold">{employee.name}</div>
<div className="text-xs text-gray-500">{employee.role}</div>
</div>
</div>
</SelectOption>
))}
</Select>
</SelectWrapper>