Dropdown Component
The <Dropdown> component is a versatile React component that provides a customizable dropdown menu with search functionality, keyboard navigation, and various styling options.
Basic Usage
All of the React.CSSProperties are valid props with the benefit of passing just the number without unit (px are used by default). The library extends the CSS properties and adds all of them with Hover suffix that will take effect only when the component is hovered by the device pointer.
- Basic Dropdown
- With Hover
- Multiselect
import { Dropdown } from "react-better-html";
function App() {
return (
<Dropdown
label="Select an option"
options={[
{ value: 1, label: "Option 1" },
{ value: 2, label: "Option 2" },
{ value: 3, label: "Option 3" },
]}
onChange={(value) => {
console.log("Selected value:", value);
}}
/>
);
}
import { Dropdown } from "react-better-html";
function App() {
return (
<Dropdown
label="Select an option"
options={[
{ value: 1, label: "Option 1" },
{ value: 2, label: "Option 2" },
{ value: 3, label: "Option 3" },
]}
onChange={(value) => {
console.log("Selected value:", value);
}}
borderColorHover="#ff0000"
/>
);
}
When hovering the component the border will change to red.
import { Dropdown } from "react-better-html";
function App() {
return (
<Dropdown
label="Tags"
options={[
{ value: "react", label: "React" },
{ value: "vue", label: "Vue" },
{ value: "svelte", label: "Svelte" },
]}
withMultiselect
onChange={(values) => {
console.log("Selected values:", values); // ["react", "vue"]
}}
/>
);
}
The selected options are rendered as chips inside the field and the value, defaultValue and onChange work with arrays.
Common Props
All standard <div> attributes are valid props for this component. The component can be controlled with value or uncontrolled with defaultValue.
import { Dropdown } from "react-better-html";
function App() {
return (
<Dropdown
label="Select an option"
options={[
{ value: 1, label: "Option 1" },
{ value: 2, label: "Option 2" },
{ value: 3, label: "Option 3" }
]}
value={1}
placeholder="Choose an option"
leftIcon="magnifyingGlass"
required
disabled
errorText="Please select an option"
infoText="Select one of the available options"
onChange={(value) => {
console.log("Selected value:", value);
}}
/>
);
}
Options
The options prop accepts an array of objects with the following structure:
type DropdownOption<Value, Data = unknown> = {
label: string;
value: Value;
disabled?: boolean;
searchValues?: string[];
data?: Data;
renderType?: "default" | "chip" | "chip.colored";
chipProps?: ChipProps;
};
label- The text to display.value- The value to be selected.disabled- Whether the option is disabled.searchValues- Additional search terms for the option.data- Any data that need to be passed down and used in therenderOptionprop.renderType- Renders the option as a<Chip>or<Chip.colored>both in the list and when selected.chipProps- Props passed to the chip whenrenderTypeischiporchip.colored.
import { Dropdown } from "react-better-html";
function App() {
return (
<Dropdown
label="Status"
options={[
{ value: "active", label: "Active", renderType: "chip.colored", chipProps: { color: "#28a745" } },
{ value: "pending", label: "Pending", renderType: "chip.colored", chipProps: { color: "#ffc107" } },
{ value: "blocked", label: "Blocked", renderType: "chip.colored", chipProps: { color: "#dc3545" } },
]}
/>
);
}
Search Functionality
The component supports search functionality through the withSearch prop. The component also has the event onChangeSearch that returns the typed value inside the input field. This event only fires if withSearch prop is true.
import { Dropdown } from "react-better-html";
function App() {
return (
<Dropdown
label="Select a person"
options={[
{
value: "john",
label: "John Doe",
searchValues: ["johny", "johnny"]
},
{
value: "jane",
label: "Jane Smith",
searchValues: ["jenny"]
}
]}
withSearch
onChange={(value) => {
console.log("Selected value:", value);
}}
onChangeSearch={(value) => {
console.log("Input field value:", value); // What the user has typed in the field
}}
/>
);
}
When withSearch is enabled:
- The input field becomes editable.
- Options are filtered based on the search query.
- Search matches both the label and searchValues (if exists).
Custom Option Rendering
You can customize how each option is rendered using the renderOption prop:
import { Div, Text, Dropdown, useTheme } from "react-better-html";
function App() {
const theme = useTheme();
return (
<Dropdown
label="Select an option"
options={[
{ value: 1, label: "Option 1" },
{ value: 2, label: "Option 2" },
{ value: 3, label: "Option 3" }
]}
renderOption={(option) => (
<Div.row alignItems="center" gap={theme.styles.gap}>
<Text as="span">🎯</Text>
<Text as="span">{option.label}</Text>
</Div.row>
)}
onChange={(value) => {
console.log("Selected value:", value);
}}
/>
);
}
Option Dividers
The renderOptionDivider prop renders an element between every two options (and before the first one). It receives the previous and the next option with their indexes, so it is useful for grouping.
import { Dropdown, Text } from "react-better-html";
function App() {
return (
<Dropdown
label="City"
options={[
{ value: "sofia", label: "Sofia", data: { country: "Bulgaria" } },
{ value: "plovdiv", label: "Plovdiv", data: { country: "Bulgaria" } },
{ value: "berlin", label: "Berlin", data: { country: "Germany" } },
]}
renderOptionDivider={(previousOption, nextOption) =>
nextOption && previousOption?.data?.country !== nextOption.data?.country ? (
<Text fontSize={12} fontWeight={700} paddingInline={16} paddingTop={8}>
{nextOption.data?.country}
</Text>
) : undefined
}
/>
);
}
With Debounce
The prop withDebounce is required to turn on that feature. When true, the onChangeSearch event will be called with a delay (debounce). You can change the delay by passing the debounceDelay prop. The default value for it is 0.5s and it works for the most part.
import { Dropdown } from "react-better-html";
function App() {
return (
<Dropdown
label="Address"
placeholder="Enter your address"
options={[
{ value: 1, label: "Address 1" },
{ value: 2, label: "Address 2" },
{ value: 3, label: "Address 3" }
]}
withSearch
withDebounce
onChange={(value) => {
console.log("Searched address:", value); // Will fire immediately when the user selects an option
}}
onChangeSearch={(value) => {
console.log("Input field value:", value); // Will fire ones after the user stops typing
}}
/>
);
}
The component can also be configured to show loading of the components when the debounce is active ond/or debounceIsLoading prop is passed. This is especially useful when the content of the options is rendered dynamically from an API, for example. A debounceMinimumSymbolsRequired can also be passed for a required minimum length of the search query.
Other props
withoutClearButton- removes theXbutton inside the dropdown and prevents the value to get back toundefinedones an option is selected.withoutArrow- removes the arrow on the right side of the field.withoutRenderingOptionsWhenClosed- the options are not rendered in the DOM while the dropdown is closed. Useful for long lists.inputFieldClassName- a class name passed to the inner input field.labelColor,labelFontSize,labelFontFamily,labelLetterSpacing,labelTextTransform,labelGap- customize the label.infoTextColor- the color of theinfoText.
Keyboard Navigation
The component supports keyboard navigation:
- Up/Down arrows to navigate through options
- Enter to select the focused option
- Escape to close the dropdown
- Enter and/or Space to toggle the dropdown (when not in search mode)
Subcomponents
A number of components in the library have a subcomponent feature witch is like a preset of the same component that is frequently used.
Dropdown.countries
This component renders a dropdown with a list of all countries.
import { Dropdown } from "react-better-html";
function App() {
return (
<Dropdown.countries label="Countries" />
);
}
Dropdown.language
This component renders a dropdown with the languages defined with generateI18n and changes the current language when an option is selected. The selected language is saved in the localStorage.
- Default
- Small
import { Dropdown } from "react-better-html";
function App() {
return <Dropdown.language />;
}
import { Dropdown } from "react-better-html";
function App() {
return <Dropdown.language isSmall />;
}
Renders only the flags - suitable for headers and navigation bars.