Skip to content
Stylhero 0.8.0
Components

Core

Layout and type

Inputs

Overlays

Navigation

Data display

Charts

Transitions and utilities

Page pieces (landing pages, portfolios, docs)

Icons

Components / Inputs

Combobox

Text input with a filtered list of options (MUI calls it Autocomplete). ref points at the text box. With name, native forms submit the option values (not the labels). Give it loadOptions to search a server instead of a fixed list.

import { Combobox } from "@stylhero/ui";

Example

Code
import { useState } from "react";
import { Combobox, Stack } from "@stylhero/ui";

const sonoraCities = ["Hermosillo", "Nogales", "Ciudad Obregón", "Guaymas", "Navojoa", "Caborca", "Agua Prieta", "Puerto Peñasco"];

const searchCities = (query: string, signal: AbortSignal) =>
  new Promise<string[]>((resolve) => {
    const timer = setTimeout(() => resolve(sonoraCities.filter((c) => c.toLowerCase().includes(query.toLowerCase()))), 400);
    signal.addEventListener("abort", () => clearTimeout(timer));
  });

function ComboboxExample() {
  const [city, setCity] = useState<string | null>(null);
  const [parts, setParts] = useState<string[]>(["gpu"]);
  const [remote, setRemote] = useState<string | null>(null);
  const cities = [{ value: "her", label: "Hermosillo" }, { value: "nog", label: "Nogales" }, { value: "obr", label: "Ciudad Obregón" }, { value: "gym", label: "Guaymas" }];
  return (
    <Stack gap={4}>
      <Combobox label="City" placeholder="Pick a city" options={cities} value={city} onValueChange={setCity} />
      <Combobox multiple label="Parts to upgrade" options={[{ value: "cpu", label: "CPU" }, { value: "gpu", label: "GPU" }, { value: "ram", label: "Memory" }]}
        value={parts} onValueChange={setParts} />
      <Combobox label="City (from a server)" placeholder="Type to search" loadOptions={searchCities} value={remote} onValueChange={setRemote} />
    </Stack>
  );
}

Props

PropTypeDefaultDescription
className string -
debounceMs number 250 With loadOptions: milliseconds to wait after the last keystroke.
defaultValue string | null | string[] -
disabled boolean -
emptyText ReactNode "No matches" Shown when nothing matches.
error ReactNode -
hint ReactNode - Help text under the field. Replaced by error when set.
id string -
label ReactNode -
loadErrorText ReactNode "Couldn't load options" With loadOptions: shown when loadOptions rejects.
loadingText ReactNode "Loading…" With loadOptions: shown while options load.
loadOptions (query: string, signal: AbortSignal) => Promise<Array<ComboboxOption | string>> - Loads the options for what's typed, e.g. from your API. Runs once typing pauses (debounceMs); out-of-date requests are aborted through signal and their results ignored. The results are shown as they come: filter them on the server.
minQueryLength number 0 With loadOptions: don't load until this many characters are typed.
multiple boolean - Pick several; they show as chips in the field (MUI's Autocomplete multiple / Select multiple).
name string -
onBlur FocusEventHandler<HTMLInputElement> - Runs when the text box loses focus (react-hook-form's field.onBlur).
onValueChange ((value: string | null) => void) | ((value: string[]) => void) -
options Array<ComboboxOption | string> [] The options to pick from. With loadOptions, optional: pass the already selected option(s) here so their labels show before anything has loaded.
placeholder string -
value string | null | string[] -

Takes a ref.

Types

type ComboboxOption = { value: string; label: string; hint?: string };

Keyboard

KeyDoes
TypingFilters the options
ArrowDown / ArrowUpOpen the list and move through the options
EnterPicks the highlighted option
EscCloses the list
BackspaceWith multiple and an empty text box, removes the last chip

Accessibility

  • Follows the ARIA combobox pattern: the text box has aria-autocomplete="list", and the highlighted option is announced while focus stays in the text box.
  • With loadOptions, a status message announces "Loading…", the number of options found, or the error; the list has aria-busy while it loads.
  • The label, hint and error are linked to the control (aria-describedby), and error sets aria-invalid.

Generated from src/components/Combobox.tsx. Something wrong or missing? Open an issue.