Components

BadgeSelector

Badge-based single select compound component with built-in search and CRUD

Overview

BadgeSelector is a compound component for badge-based single selection. It has built-in search, create, edit, and delete functionality.

Features

  • Item search and filtering
  • Inline item creation
  • Item edit/delete
  • Keyboard navigation
  • Close on outside click
  • Customizable trigger
  • TypeScript support

Anatomy

Root holds the data and the handlers. Inside it go Trigger (what opens the selector) and Content, which usually holds Search and List. See the full example under Basic Usage.

API Reference

Root

Root component that manages state and provides context.

PropTypeDefaultRequiredDescription
childrenReactNode-YesTrigger·Content(Search·List)
itemsBadgeItem[]-YesBadges to choose from
selectedItemBadgeItem | null-YesThe selected badge
maxNameLengthnumber100NoMaximum length of a new badge name
onSelectItem(itemId: string) => Promise<void>-YesCalled when a badge is chosen
onClearItem() => Promise<void>-YesCalled when the choice is cleared
onCreateItem(data: Partial<OmittedBadgeItem>) => Promise<string>-YesCalled to create a badge from the search text. Returns the new id
onDeleteItem(itemId: string) => Promise<void>-YesCalled to delete a badge
onUpdateItem(itemId: string, data: Partial<OmittedBadgeItem>) => Promise<void>-YesCalled to change a badge's name or color

Type Definitions:

export type BadgeItem = {
  id: string
  name: string
  color?: BadgeColor
  createdAt?: Date | string
  updatedAt?: Date | string | null
  deletedAt?: Date | string | null
}
export type OmittedBadgeItem = Omit<BadgeItem, 'id' | 'createdAt' | 'updatedAt' | 'deletedAt'>

Trigger

Trigger element that opens and closes the dropdown.

PropTypeDefaultRequiredDescription
childrenReactNode-YesWhat opens the selector, usually a Badge
asChildboolean-NoPuts the trigger behavior (role="button", tabIndex=0, Enter and Space) on the child instead of a wrapper
styleCSSProperties-NoInline style

Data Attributes: data-state: "open" | "closed"

Content

Container that renders dropdown content via a portal.

PropTypeDefaultRequiredDescription
childrenReactNode-NoUsually Search and List
classNamestring-NoExtra CSS class

Input field for filtering and creating items.

PropTypeDefaultRequiredDescription
placeholderstring'Search...'NoPlaceholder of the search input
maxLengthnumber'100'NoMaximum length of the search text

List

Renders the filtered item list.

No props.

Examples

Complete Example

Specification

base

상태요소속성값
enabledcontentwidth296px토큰 아님 — no sizing token scale
enabledcontentmaxHeight400px토큰 아님 — no sizing token scale
enabledcontentpadding$size.space.200
enabledcontentgap$size.space.200
enabledcontentbackgroundColor$color.background.default.default
enabledcontentborderWidth$size.stroke.border
enabledcontentborderColor$color.border.default.default
enabledcontentborderRadius$size.radius.200
enabledsearchpadding$size.space.200
enabledsearchgap$size.space.200
enabledsearchborderRadius$size.radius.100
enabledsearchbackgroundColor$color.background.default.secondary
enabledsearchInputtypography$text.bodySmall
enabledsearchInputcolor$color.text.default.default
enableditemgap$size.space.100
enableditempaddingRight$size.space.200
enableditemborderRadius$size.radius.200
enabledselectButtonpaddingX$size.space.200
enabledselectButtonpaddingY$size.space.100
enabledselectButtongap$size.space.200
enabledselectButtonborderRadius$size.radius.200
enablededitButtonopacity0토큰 아님 — opacity keeps it reachable with Tab while hidden
hoveritembackgroundColor$color.background.default.defaultHover
hovereditButtonopacity1토큰 아님 — show
selectedcheckcolor$color.icon.default.default
focussearchstrokeWidth$size.stroke.focus
focussearchstrokeColor$color.border.brand.default
focuseditButtonopacity1토큰 아님 — show

Accessibility

Keyboard Interactions

KeyAction
TabMoves search → each option row → that row's edit button
Enter / Space (row)Chooses the option and closes
Enter (search)Creates a new option from the search text
Backspace (search)Clears the selection when the search is empty
Escape (search)Clears the search text and closes

ARIA

  • Trigger: aria-expanded, aria-haspopup="dialog"
  • List: ul labelled "Options". Each row is a button; the chosen one has aria-current="true" and a check icon
  • Edit button: named Edit <option name>, visible on row hover or keyboard focus

Custom API

useBadgeSelectorContext

Access internal state and handlers in custom components:

What the hook returns:

export type BadgeSelectorContextValue = {
  isOpen: boolean
  searchTerm: string
  selectedItem: BadgeItem | null
  filteredItems: BadgeItem[]
  canCreateNew: boolean
  previewColor: BadgeColor

  triggerRef: RefObject<HTMLElement | null>
  contentRef: RefObject<HTMLDivElement | null>
  inputRef: RefObject<HTMLInputElement | null>

  setIsOpen: (open: boolean) => void
  setSearchTerm: (term: string) => void
  selectItem: (item: BadgeItem) => Promise<void>
  clearItem: () => Promise<void>
  createItem: () => Promise<void>
  deleteItem: (itemId: string) => Promise<void>
  updateItem: (itemId: string, data: Partial<OmittedBadgeItem>) => Promise<void>
  handleKeyDown: (e: React.KeyboardEvent<HTMLInputElement>) => void
}