The RhSelect is a component for creating dropdown menus. And used to create searchable and customizable dropdown menus where users can select one or more options from a list of available options. And this component is highly customizable and can be used for various types of dropdown menus and with different data sources.
Once installed, you can import the RhSelect component into your React application:
import {RhSelect} from "@rhythm-ui/react"
Then, you can use the RhSelect component in your JSX code:
<div className="h-[200px] w-full flex justify-center"> <div className="h-full w-80 p-4"> <RhSelect onChange={() => {}} className="text-sm" options={[ { label: "Blues", value: "blues" }, { label: "Rock", value: "rock" }, { label: "Jazz", value: "jazz" }, ]} /> </div> </div>
Name | Type | Default | Description |
---|---|---|---|
className | string | Sets a className attribute on the outer component | |
inputSize | "sm" | "lg" | "md" | Handle select input size | |
error | boolean | Make this true if there is an error (handy for validation such as formik) | |
name | string | Name of the HTML Input (optional - without this, no input will be rendered) | |
placeholder | ReactNode | Placeholder for the select value | |
aria-errormessage | string | HTML ID of an element containing an error message related to the input* | |
aria-invalid | boolean | "false" | "true" | "grammar" | "spelling" | Indicate if the value entered in the field is invalid * | |
aria-label | string | Aria label (for assistive tech) | |
aria-labelledby | string | HTML ID of an element that should be used as the label (for assistive tech) | |
aria-live | "off" | "assertive" | "polite" | Used to set the priority with which screen reader should treat updates to live regions. The possible settings are: off, polite (default) or assertive |
The options prop accepts an array of objects, where each object represents a group of options in the dropdown menu. The options in the dropdown menu can be organized into logical categories or sections, which can help users navigate and find the options they need more easily.
<div className="h-[200px] w-full flex justify-center"> <div className="h-full w-80 p-4"> <RhSelect onChange={() => {}} className="text-sm" options={[ { label: "Group 1", options: [{ label: "Group one option A", value: "value1" }], }, { label: "Group 2", options: [{ label: "Group two option B", value: "value2" }], }, ]} /> </div> </div>
The isMulti prop is used to specify whether to select multiple options or not. If isMulti is set to true, the user can select multiple options by clicking on them, and the selected options will be displayed as separate tags in the input field. If isMulti is set to false or omitted, the user can only select one option at a time.
<div className="h-[200px] w-full flex justify-center"> <div className="h-full w-80 p-4"> <RhSelect isMulti onChange={() => {}} className="text-sm" options={[ { label: "Blues", value: "blues" }, { label: "Rock", value: "rock" }, { label: "Jazz", value: "jazz" }, ]} /> </div> </div>
The defaultValue prop is used to specify the options when the component is first rendered. It works similarly to the value prop, but it is only used to set the initial value, and it does not update the selected value in response to user interactions.
<div className="h-[200px] w-full flex justify-center"> <div className="h-full w-80 p-4"> <RhSelect defaultValue={{ label: "Rock", value: "rock" }} className="text-sm" onChange={() => {}} options={[ { label: "Blues", value: "blues" }, { label: "Rock", value: "rock" }, { label: "Jazz", value: "jazz" }, ]} /> </div> </div>
The autoFocus prop is used to specify whether a component should automatically focus on the input field when it is mounted. If autoFocus is set to true, the input field will be focused automatically, allowing the user to begin typing right away. If it is set to false or omitted, the input field will not be automatically focused.
<div className="h-[200px] w-full flex justify-center"> <div className="h-full w-80 p-4"> <RhSelect autoFocus onChange={() => {}} className="text-sm" options={[ { label: "Blues", value: "blues" }, { label: "Rock", value: "rock" }, { label: "Jazz", value: "jazz" }, ]} /> </div> </div>
The isClearable prop is used to specify whether the component should display a clear button that allows the user to clear the currently selected option or options.
<div className="h-[200px] w-full flex justify-center"> <div className="h-full w-80 p-4"> <RhSelect isClearable onChange={() => {}} className="text-sm" options={[ { label: "Blues", value: "blues" }, { label: "Rock", value: "rock" }, { label: "Jazz", value: "jazz" }, ]} /> </div> </div>
The backspaceRemovesValue prop is used to specify whether the component should remove the last selected option when the user presses the backspace key.
<div className="h-[200px] w-full flex justify-center"> <div className="h-full w-80 p-4"> <RhSelect backspaceRemovesValue isMulti className="text-sm" onChange={() => {}} options={[ { label: "Blues", value: "blues" }, { label: "Rock", value: "rock" }, { label: "Jazz", value: "jazz" }, ]} /> </div> </div>
The hideSelectedOptions prop is used to specify whether the component should hide the selected options from the dropdown menu or not.
<div className="h-[200px] w-full flex justify-center"> <div className="h-full w-80 p-4"> <RhSelect hideSelectedOptions className="text-sm" onChange={() => {}} options={[ { label: "Blues", value: "blues" }, { label: "Rock", value: "rock" }, { label: "Jazz", value: "jazz" }, ]} /> </div> </div>
The isDisabled prop is used to specify whether the component should be disabled or not.
<div className="h-[200px] w-full flex justify-center"> <div className="h-full w-80 p-4"> <RhSelect isDisabled className="textsm" onChange={() => {}} options={[ { label: "Blues", value: "blues" }, { label: "Rock", value: "rock" }, { label: "Jazz", value: "jazz" }, ]} /> </div> </div>
The selectStyle prop is a configuration option that allows you to customize the appearance of the component. This prop accepts three possible values: checkbox, tick, and radio. Depending on the value you choose for this prop, the appearance of the RhSelect component options will vary.
<div className="h-[200px] w-full flex justify-center"> <div className="h-full w-80 p-4"> <RhSelect isMulti className="text-sm" onChange={() => {}} options={[ { label: "Blues", value: "blues" }, { label: "Rock", value: "rock" }, { label: "Jazz", value: "jazz" }, ]} selectStyle="checkbox" hideSelectedOptions={false} /> </div> </div>
If you set selectStyle to tick, the component will appear as a group of checkboxes with a tick mark inside them. This is similar to the checkbox style, but the tick mark provides a visual indicator to the user that the option has been selected.
<div className="h-[200px] w-full flex justify-center"> <div className="h-full w-80 p-4"> <RhSelect isMulti onChange={() => {}} className="text-sm" options={[ { label: "Blues", value: "blues" }, { label: "Rock", value: "rock" }, { label: "Jazz", value: "jazz" }, ]} selectStyle="tick" hideSelectedOptions={false} /> </div> </div>
If you set selectStyle to radio, the component will appear as a group of radio buttons. Each option will be represented by a radio button that the user can select.
<div className="h-[200px] w-full flex justify-center"> <div className="h-full w-80 p-4"> <RhSelect isMulti className="text-sm" onChange={() => {}} options={[ { label: "Blues", value: "blues" }, { label: "Rock", value: "rock" }, { label: "Jazz", value: "jazz" }, ]} selectStyle="radio" hideSelectedOptions={false} /> </div> </div>
The customOption prop is a configuration option that allows you to define a custom rendering function for the options in the component. When set, this prop overrides the default rendering behavior of the options and allows you to create a custom user interface for each option.
function CustomOptions() { const CustomOptionComp = ({ label }) => ( <div className="flex gap-4"> <RhIcon icon={`emojione:flag-for-${label.toLowerCase()}`} className="text-2xl" /> {label} </div> ); return ( <div className="h-[200px] w-full flex justify-center"> <div className="h-full w-80 p-4"> <RhSelect className="text-sm" onChange={() => {}} options={[ { label: "Canada", value: "Canada" }, { label: "India", value: "India" }, { label: "China", value: "China" }, ]} customOption={CustomOptionComp} creatable /> </div> </div> ); }