Skip to main content

Select

Listboxes are a great foundation for building custom, accessible select menus for your app, complete with robust support for keyboard navigation..

import { Select } from "rizzui";

Default

The default style of Select component.

Select
import { useState } from 'react'
import { Select } from "rizzui";

const options = [
{ label: 'Apple 🍎', value: 'apple' },
{ label: 'Banana 🍌', value: 'banana' },
{ label: 'Cherry 🍒', value: 'cherry' },
...
];

export default function App() {
const [value, setValue] = useState(null);
return (
<Select
label="Select"
options={options}
value={value}
onChange={setValue}
/>
);
}

With Clearable Button

You can clear Select value using clearable property.

Select
import { useState } from 'react'
import { Select } from "rizzui";

const options = [
{ label: 'Apple 🍎', value: 'apple' },
{ label: 'Banana 🍌', value: 'banana' },
{ label: 'Cherry 🍒', value: 'cherry' },
...
];

export default function App() {
const [value, setValue] = useState(options[0]);
return (
<Select
label="Select"
options={options}
value={value}
onChange={setValue}
clearable={value !== null}
onClear={() => setValue(null)}
/>
);
}

With Custom Option

Here is the custom option Select example.

Select
import { useState } from 'react'
import { Select, type SelectOption, Text } from "rizzui";

const options = [
{
label: "Wolverine",
value: "wolverine@rizzui.io",
avatar: "https://randomuser.me/api/portraits/men/43.jpg",
},
{
label: "MessiJr",
value: "messijr@rizzui.io",
avatar: "https://randomuser.me/api/portraits/women/65.jpg",
},
...
];

export default function App() {
const [value, setValue] = useState(null);
return (
<Select
label={label}
options={customOptions}
value={value}
onChange={setValue}
displayValue={(value) => renderDisplayValue(value)}
getOptionDisplayValue={(option) => renderOptionDisplayValue(option)}
/>
);
}

function renderDisplayValue(value: SelectOption) {
return (
<span className="flex items-center gap-2">
<img
src={value.avatar}
alt={value.label}
className="w-7 h-7 object-cover rounded-full"
/>
<Text>{value.label}</Text>
</span>
)
}

function renderOptionDisplayValue(option: SelectOption) {
return (
<div className="flex items-center gap-3">
<img
src={option.avatar}
alt={option.label}
className="w-9 h-9 object-cover rounded"
/>
<div>
<Text fontWeight="medium">{option.label}</Text>
<Text>{option.value}</Text>
</div>
</div>
)
}

With Searchable Options

You can search inside Select options.

Select
import { useState } from 'react';
import { Select } from "rizzui";

const options = [
{ label: 'Apple 🍎', value: 'apple' },
{ label: 'Banana 🍌', value: 'banana' },
{ label: 'Cherry 🍒', value: 'cherry' },
...
];

export default function App() {
const [value, setValue] = useState(null);
return (
<Select
value={value}
label="Select"
searchable={true}
options={options}
onChange={setValue}
/>
);
}

With React Hook Form and Zod Validation

In this example, we are going to use React Hook Form and Zod for validation. Open browser console to see the submitted data.

Select
import { Select, Button } from "rizzui";
import { z } from "zod";
import { Controller, useForm } from "react-hook-form";
import { zodResolver } from "@hookform/resolvers/zod";

const schema = z.object({
select: z.string(),
});

type SchemaType = z.infer<typeof schema>;

export function SelectWithForm() {
const { handleSubmit, control } = useForm({
resolver: zodResolver(schema),
});

const onSubmit = (data: SchemaType) => {
console.log("Submitted data", data);
};

return (
<form
onSubmit={handleSubmit(onSubmit)}
className="w-full max-w-md"
>
<Controller
name="select"
control={control}
render={({ field: { value, onChange }, fieldState: { error } }) => (
<Select
label="Select"
value={value}
options={options}
onChange={onChange}
getOptionValue={(option) => option.value}
displayValue={(selected) => options?.find((r) => r.value === selected)?.label ?? ""}
error={error?.message}
className="w-full max-w-md"
clearable
onClear={() => onChange("")}
/>
)}
/>

<Button
type="submit"
className="mt-4 w-full"
>
Submit
</Button>
</form>
);
}

API Reference


Select Props

Here is the API documentation of the Select component.

PropsTypeDescriptionDefault
optionsSelectOption[]Add options data using this prop. options prop is required.__
valueTThe selected value. value prop is required.__
onChange(value: T) => voidThe function to call when a new option is selected.. onChange prop is required.__
labelReactNodeSet field label__
labelWeightLabelWeightSet label font weight"medium"
variantSelectVariantsThe variants of the component are:"outline"
sizeSelectSizesThe size of the component. "sm" is equivalent to the dense select styling."md"
roundedSelectRoundedThe rounded variants are:"md"
inPortalbooleanWhether select options is rendered on the portal or not"true"
modalbooleanWhether the body scrollbar is hidden or not when dropdown is visible."false"
gapnumberSets the gap between trigger and dropdown if portal is true"6"
placeholderstringSet select placeholder text"Select..."
disabledbooleanWhether the select is disabled or not__
clearablebooleanAdd clearable option__
onClearSelectOnClearclear event__
prefixReactNodeThe prefix is design for adding any icon or text on the Select field's start (it's a left icon for the ltr and right icon for the rtl)__
suffixReactNodeThe suffix is design for adding any icon or text on the Select field's end (it's a right icon for the ltr and left icon for the rtl)__
helperTextReactNodeAdd helper text. It could be string or a React component__
errorstringShow error message using this prop__
displayValuefunctionA function to determine the display value of the selected item. @param value - The value of the selected item, @returns ReactNode to display for the selected item.__
getOptionDisplayValuefunctionUse this function when you want to display something other than the default displayValue. @param option - The SelectOption for which to get the display value, @returns ReactNode to display for the specified option.__
getOptionValuefunctionSelect whether the label or value should be returned in the onChange method. @param option - The SelectOption for which to get the value, @returns The selected value from the specified option.__
labelClassNamestringOverride default CSS style of label__
selectClassNamestringOverride default CSS style of select button__
dropdownClassNamestringOverride default CSS style of select dropdown__
optionClassNamestringOverride default CSS style of select option__
prefixClassNamestringOverride default CSS style of prefix__
suffixClassNamestringOverride default CSS style of suffix__
helperClassNamestringOverride default CSS style of helperText__
errorClassNamestringOverride default CSS style of error message__
classNamestringAdd custom classes to the root of the component__
searchablebooleanIs select options searchable or not"false"
searchPlaceholderstringSet search input placeholder text"Search..."
searchTypetext searchSet search input type"text"
searchReadOnlybooleanSet search input is readonly or not"false"
searchDisabledbooleanSet search input is disabled or not"false"
searchPrefixReactNodeThe prefix is design for adding any icon or text on the Search field's start (it's a left icon for the ltr and right icon for the rtl)__
searchSuffixReactNodeThe suffix is design for adding any icon or text on the Search field's end (it's a right icon for the ltr and left icon for the rtl)__
searchContainerClassNamestringOverride default CSS style of search root__
searchPrefixClassNamestringOverride default CSS style of search prefix__
searchClassNamestringOverride default CSS style of search input__
searchSuffixClassNamestringOverride default CSS style of search suffix__
searchByKeystringSet a custom key to search in options'label'
searchPropsHTMLInputElementSet's extra attributes for search input element__

Select Option type

type SelectOption = {
value: string | number;
label: string;
disabled?: boolean;
[key: string]: any;
};

Label Weight

type LabelWeight = "normal" | "medium" | "semibold" | "bold";

Select Variants

type SelectVariants = "outline" | "flat" | "text";

Select Sizes

type SelectSizes = "sm" | "md" | "lg" | "xl";

Select Rounded

type SelectRounded = "sm" | "md" | "lg" | "none" | "pill";

Select onClear

type InputOnclear = (event) => void;