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.
| Prop | Type | Default | Required | Description |
|---|---|---|---|---|
children | ReactNode | - | Yes | Trigger·Content(Search·List) |
items | BadgeItem[] | - | Yes | Badges to choose from |
selectedItem | BadgeItem | null | - | Yes | The selected badge |
maxNameLength | number | 100 | No | Maximum length of a new badge name |
onSelectItem | (itemId: string) => Promise<void> | - | Yes | Called when a badge is chosen |
onClearItem | () => Promise<void> | - | Yes | Called when the choice is cleared |
onCreateItem | (data: Partial<OmittedBadgeItem>) => Promise<string> | - | Yes | Called to create a badge from the search text. Returns the new id |
onDeleteItem | (itemId: string) => Promise<void> | - | Yes | Called to delete a badge |
onUpdateItem | (itemId: string, data: Partial<OmittedBadgeItem>) => Promise<void> | - | Yes | Called 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.
| Prop | Type | Default | Required | Description |
|---|---|---|---|---|
children | ReactNode | - | Yes | What opens the selector, usually a Badge |
asChild | boolean | - | No | Puts the trigger behavior (role="button", tabIndex=0, Enter and Space) on the child instead of a wrapper |
style | CSSProperties | - | No | Inline style |
Data Attributes: data-state: "open" | "closed"
Content
Container that renders dropdown content via a portal.
| Prop | Type | Default | Required | Description |
|---|---|---|---|---|
children | ReactNode | - | No | Usually Search and List |
className | string | - | No | Extra CSS class |
Search
Input field for filtering and creating items.
| Prop | Type | Default | Required | Description |
|---|---|---|---|---|
placeholder | string | 'Search...' | No | Placeholder of the search input |
maxLength | number | '100' | No | Maximum length of the search text |
List
Renders the filtered item list.
No props.
Examples
Complete Example
Specification
base
| 상태 | 요소 | 속성 | 값 |
|---|---|---|---|
| enabled | content | width | 296px토큰 아님 — no sizing token scale |
| enabled | content | maxHeight | 400px토큰 아님 — no sizing token scale |
| enabled | content | padding | $size.space.200 |
| enabled | content | gap | $size.space.200 |
| enabled | content | backgroundColor | $color.background.default.default |
| enabled | content | borderWidth | $size.stroke.border |
| enabled | content | borderColor | $color.border.default.default |
| enabled | content | borderRadius | $size.radius.200 |
| enabled | search | padding | $size.space.200 |
| enabled | search | gap | $size.space.200 |
| enabled | search | borderRadius | $size.radius.100 |
| enabled | search | backgroundColor | $color.background.default.secondary |
| enabled | searchInput | typography | $text.bodySmall |
| enabled | searchInput | color | $color.text.default.default |
| enabled | item | gap | $size.space.100 |
| enabled | item | paddingRight | $size.space.200 |
| enabled | item | borderRadius | $size.radius.200 |
| enabled | selectButton | paddingX | $size.space.200 |
| enabled | selectButton | paddingY | $size.space.100 |
| enabled | selectButton | gap | $size.space.200 |
| enabled | selectButton | borderRadius | $size.radius.200 |
| enabled | editButton | opacity | 0토큰 아님 — opacity keeps it reachable with Tab while hidden |
| hover | item | backgroundColor | $color.background.default.defaultHover |
| hover | editButton | opacity | 1토큰 아님 — show |
| selected | check | color | $color.icon.default.default |
| focus | search | strokeWidth | $size.stroke.focus |
| focus | search | strokeColor | $color.border.brand.default |
| focus | editButton | opacity | 1토큰 아님 — show |
Accessibility
Keyboard Interactions
| Key | Action |
|---|---|
Tab | Moves 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:
ullabelled "Options". Each row is a button; the chosen one hasaria-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
}