Skip to content

Typeahead #80

Closed
Closed

Description

@cixzhang

Spec

Search-as-you-type component for selecting an item from a search source. Two-layer architecture: XDSBaseTypeahead (unstyled) and XDSTypeahead (styled with XDSField).

Shared Types

/** Minimal item interface for search results */
interface XDSSearchableItem<TAuxData = unknown> {
  /** Unique identifier */
  id: string;
  /** Display label */
  label: string;
  /** Pre-rendered element for SSR compatibility */
  element?: ReactNode;
  /** Arbitrary extra data */
  auxiliaryData?: TAuxData;
}

/** Search source interface */
interface XDSSearchSource<T extends XDSSearchableItem = XDSSearchableItem> {
  /** Called on query change. Returns matching items. */
  search(query: string): Promise<T[]> | T[];
  /** Called on init. Returns initial/default items. */
  bootstrap(): Promise<T[]> | T[];
}

XDSBaseTypeahead (unstyled)

Handles search, keyboard navigation, selection, dropdown positioning. No field wrapper, no styling. Used by XDSTypeahead and XDSTokenizer.

interface XDSBaseTypeaheadProps<T extends XDSSearchableItem> {
  /** Search source providing items */
  searchSource: XDSSearchSource<T>;
  /** Currently selected item (null = nothing selected) */
  value: T | null;
  /** Callback when selection changes */
  onChange: (item: T | null) => void;
  /** Render function for dropdown items. Default: XDSTypeaheadItem */
  renderItem?: (item: T) => ReactNode;
  /** Placeholder text */
  placeholder?: string;
  /** Show results on focus before typing */
  hasEntriesOnFocus?: boolean;
  /** Max dropdown items */
  maxMenuItems?: number;
  /** Text shown when no results found */
  emptySearchResultsText?: string;
  /** Whether the input is disabled */
  isDisabled?: boolean;
  /** Show clear button */
  hasClear?: boolean;
  /** Auto-focus on mount */
  hasAutoFocus?: boolean;
  /** Query change callback (for logging/external use) */
  onChangeQuery?: (query: string) => void;
  /** Callback when dropdown opens/closes */
  onOpenChange?: (isOpen: boolean) => void;
  /** Test ID */
  "data-testid"?: string;
  children?: ReactNode;
}

XDSTypeahead (styled)

Wraps XDSBaseTypeahead with XDSField for label, description, status.

interface XDSTypeaheadProps<T extends XDSSearchableItem> extends XDSBaseTypeaheadProps<T> {
  /** Accessible label (required) */
  label: string;
  /** Visually hide the label */
  isLabelHidden?: boolean;
  /** Helper text */
  description?: string;
  /** Required field */
  isRequired?: boolean;
  /** Optional field */
  isOptional?: boolean;
  /** Validation status */
  status?: { type: "error" | "warning" | "success"; message?: string };
  /** Label tooltip */
  labelTooltip?: string;
  /** Input size */
  size?: "sm" | "md";
  /** Start icon */
  startIcon?: XDSIconType;
  /** StyleX overrides */
  xstyle?: StyleXStyles;
}

XDSTypeaheadItem

Default item component for the dropdown. Exported for use in renderItem.

interface XDSTypeaheadItemProps<T extends XDSSearchableItem> {
  /** The search result item */
  item: T;
  /** Icon/avatar before the label */
  icon?: ReactNode;
  /** Description text below the label */
  description?: string;
  /** Whether this item is disabled */
  isDisabled?: boolean;
  /** Group label for grouping items */
  group?: string;
}

Usage

// Basic
<XDSTypeahead
  label="Assignee"
  searchSource={userSource}
  value={assignee}
  onChange={setAssignee}
  placeholder="Search users..."
/>

// With custom items
<XDSTypeahead
  label="Assignee"
  searchSource={userSource}
  value={assignee}
  onChange={setAssignee}
  renderItem={(item) => (
    <XDSTypeaheadItem
      item={item}
      icon={<XDSAvatar src={item.auxiliaryData.avatar} size="sm" />}
      description={item.auxiliaryData.role}
    />
  )}
/>

// SSR items with pre-rendered elements
const serverSource: XDSSearchSource<UserItem> = {
  search: async (query) => fetchUsers(query), // returns items with element
  bootstrap: async () => fetchRecentUsers(),
};

Accessibility

  • role="combobox" on input, aria-expanded, aria-controls
  • role="listbox" on dropdown, role="option" on items
  • aria-activedescendant for keyboard highlight
  • Arrow keys navigate, Enter selects, Escape closes
  • aria-selected on selected item

Design decisions

Decision Choice Rationale
Two-layer (Base + Styled) Follows Table pattern Base reused by Tokenizer
XDSSearchSource interface {search, bootstrap} Decouples data from component, supports sync + async
XDSSearchableItem {id, label, element?, auxiliaryData?} Minimal, SSR-compatible, extensible via generic
renderItem prop Function returning ReactNode Flexible, defaults to XDSTypeaheadItem
XDSTypeaheadItem exported Recommended item component Grouping, disabled, icon, description as props
Render priority element > renderItem > label SSR first, custom second, default third

Research

  • Cross-system comparison available in Navi workspace
  • Internal API analyzed: XDSTypeahead + XDSBaseTypeahead

v1 scope

Include: search, selection, keyboard nav, renderItem, XDSTypeaheadItem, grouping, disabled items, clear, empty state
Defer: onCreateNewItem, tokenizeQueryString, onEnterWithoutSelection

Activity

  1. added this to the Component set 2 milestone on Feb 6, 2026
  2. added
    enhancement
    on Feb 18, 2026
  3. cixzhang commented on Feb 20, 2026

    @cixzhang
    ContributorAuthor

    Vibe test evidence for this gap: in Daily Vibe Test #226, the autocomplete prompt (io-2) produced a yellow-tier result because the LLM had to build a custom dropdown with <div onClick> for suggestion items — no XDS typeahead/combobox exists. The result has an a11y_click_handler anti-pattern since clickable items should be interactive elements.

    View the generated code →

  4. added
    component
    on Mar 1, 2026
  5. cixzhang commented on Mar 7, 2026

    @cixzhang
    ContributorAuthor

    XDSTypeahead shipped in PR #299 and landed on main. Closing the spec tracking issue.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    componentenhancement

    Type

    No type

    Projects

    No projects

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions