select-with-search.tsx


Overview

select-with-search.tsx defines a reusable React component named SelectWithSearch that provides a searchable dropdown select input with optional grouped options. It allows users to pick an option from a list with a built-in search/filter functionality, supports clearing the selection, and visually indicates the selected item. The component is designed with accessibility and UI consistency in mind, leveraging several smaller UI primitives such as buttons, popovers, command lists, and icons.

This component is ideal for forms or interfaces where the user needs to select one option from a potentially large or grouped list, aided by search capabilities.


Components and Types

Types

SelectWithSearchFlagOptionType

Defines the shape of options passed to the component. It supports either flat options or grouped options.

SelectWithSearchFlagProps

Defines the props accepted by the SelectWithSearch component.


SelectWithSearch Component

A forward-ref React functional component that renders a searchable select dropdown.

Signature

const SelectWithSearch = forwardRef<
  React.ElementRef<typeof Button>,
  SelectWithSearchFlagProps
>((props, ref) => JSX.Element);

Props

Internal State

Key Methods

Behavior & Rendering

Usage Example

import { SelectWithSearch } from './select-with-search';

const options = [
  {
    label: "Fruits",
    options: [
      { label: "Apple", value: "apple" },
      { label: "Banana", value: "banana", disabled: true },
    ],
  },
  {
    label: "Vegetables",
    options: [
      { label: "Carrot", value: "carrot" },
      { label: "Broccoli", value: "broccoli" },
    ],
  },
];

function MyComponent() {
  const [selected, setSelected] = useState('');

  return (
    <SelectWithSearch
      options={options}
      value={selected}
      onChange={setSelected}
      allowClear
      triggerClassName="my-custom-class"
    />
  );
}

Implementation Details and Algorithms


Interaction with Other Parts

This component is designed to be plugged into forms or UI flows where users select values from possibly grouped datasets, enhancing UX with search and clear capabilities.


Mermaid Component Diagram

componentDiagram
    direction TB
    component SelectWithSearch {
        +props: SelectWithSearchFlagProps
        +state: open, value
        +handleSelect(val: string)
        +handleClear(event)
        +render()
    }
    component Button
    component Popover
    component Command
    component CommandInput
    component CommandList
    component CommandGroup
    component CommandItem
    component CheckIcon
    component ChevronDownIcon
    component XIcon
    component Separator

    SelectWithSearch --> Button : trigger button
    SelectWithSearch --> Popover : dropdown container
    Popover --> Command : dropdown content
    Command --> CommandInput : search input
    Command --> CommandList : list container
    CommandList --> CommandGroup : grouped options (optional)
    CommandGroup --> CommandItem : option item
    CommandList --> CommandItem : flat option item
    CommandItem --> CheckIcon : selected indicator
    Button --> ChevronDownIcon : dropdown arrow
    Button --> XIcon : clear icon (conditional)
    Button --> Separator : separator (conditional)

Summary

The select-with-search.tsx file implements a flexible, accessible, and user-friendly searchable select dropdown component supporting both flat and grouped options. It is highly reusable and composable with other UI elements, providing key features like searching, clearing, and visual selection cues. Its design embraces React best practices with hooks, memoization, and forward refs, making it a robust component for forms and UI applications requiring enhanced select inputs.