Select
Displays a list of options for the user to pick from—triggered by a button.
Usage
Select
component takes initems
andplaceholder
props.className
prop is used to style the "trigger" part of Select.placeholder
renders the placeholder value of Select. It is a required prop. It acceptsstring
value.items
prop is an array of objects. It contains:label
prop which is the label of the option.value
prop which is the value of the option.disabled
prop which is a boolean value. Iftrue
, the option will be disabled i.e. It will be visible but not clickable.
Select
also allows for grouping of options. To group options, insted of passingvalue
andlabel
passitems
andlabel
, wherelabel
is the group label anditems
is an array of options.- Refer to example below to see how to group options.
Interface for SelectProps
interface SelectItemType {
value?: string;
label: string;
items?: SelectItemType[];
disabled?: boolean;
}
interface SelectProps extends SelectPrimitive.SelectProps {
items: SelectItemType[];
placeholder: string;
className?: string;
}
Example of items
prop
const items = [
{
label: "Fruits",
items: [
{ value: "apple", label: "Apple" },
{ value: "banana", label: "Banana" },
{ value: "blueberry", label: "Blueberry" },
{ value: "grapes", label: "Grapes" },
{ value: "pineapple", label: "Pineapple" },
],
},
{
label: "Vegetables",
items: [
{ value: "aubergine", label: "Aubergine" },
{ value: "broccoli", label: "Broccoli" },
{ value: "carrot", label: "Carrot", disabled: true },
{ value: "courgette", label: "Courgette" },
{ value: "leek", label: "Leek" },
],
},
{
label: "Meat",
items: [
{ value: "beef", label: "Beef" },
{ value: "chicken", label: "Chicken" },
{ value: "lamb", label: "Lamb" },
{ value: "pork", label: "Pork" },
],
},
];
- This will render a Select component with 3 groups of options: Fruits, Vegetables, and Meat. Each group will have a list of options.
Customizable Design
More granular components provide you with more control over the design of the Select
component. You can customize the design of the Select
component by using the following sub-components:
- Style of every sub-component of the Select component can be customized using the
className
prop. - Pass in a
CSS class
or useTailwind CSS
utility classes to style the Select component.