ComboBox

PreviousNext
Docs
react-ariaui

Preview

Loading preview…
components/ui/ComboBox.tsx
'use client';
import {
  ComboBox as AriaComboBox,
  ComboBoxProps as AriaComboBoxProps,
  Input,
  ListBoxItemProps,
  ListBoxProps,
  ValidationResult
} from 'react-aria-components';
import {Label, FieldError, FieldButton, Description} from '@/registry/react-aria/ui/Form';
import {DropdownItem, DropdownListBox} from '@/registry/react-aria/ui/ListBox';
import {Popover} from '@/registry/react-aria/ui/Popover';
import {ChevronDown} from 'lucide-react';

import './ComboBox.css';

export interface ComboBoxProps<T extends object>
  extends Omit<AriaComboBoxProps<T>, 'children'> {
  label?: string;
  description?: string | null;
  errorMessage?: string | ((validation: ValidationResult) => string);
  children: React.ReactNode | ((item: T) => React.ReactNode);
  placeholder?: string;
}

export function ComboBox<T extends object>(
  { label, description, errorMessage, children, placeholder, ...props }: ComboBoxProps<T>
) {
  return (
    <AriaComboBox {...props}>
      <Label>{label}</Label>
      <div className="combobox-field">
        <Input className="react-aria-Input inset" placeholder={placeholder} />
        <FieldButton><ChevronDown /></FieldButton>
      </div>
      {description && <Description>{description}</Description>}
      <FieldError>{errorMessage}</FieldError>
      <Popover hideArrow className="combobox-popover">
        <ComboBoxListBox>
          {children}
        </ComboBoxListBox>
      </Popover>
    </AriaComboBox>
  );
}

export function ComboBoxListBox<T extends object>(props: ListBoxProps<T>) {
  return <DropdownListBox {...props} />;
}

export function ComboBoxItem(props: ListBoxItemProps) {
  return <DropdownItem {...props} />;
}

Installation

npx shadcn@latest add @react-aria/css-combobox

Usage

import { CssCombobox } from "@/components/ui/css-combobox"
<CssCombobox />