{
  "$schema": "https://ui.shadcn.com/schema/registry-item.json",
  "name": "tw-combobox",
  "type": "registry:ui",
  "title": "react-tw/combobox.tsx",
  "description": "Carbon `ComboBox` as a React + Tailwind component: the look of `<ui-combobox>` on the headless Copied from @purpurabog/ui 2.3.0; the package is the maintained source, this copy is yours to change. Icons from the Carbon Design System (Apache-2.0, © IBM Corp.; see THIRD-PARTY-NOTICES.md in the package).",
  "dependencies": [
    "@purpurabog/ui@^2.3.0"
  ],
  "registryDependencies": [
    "@kanok-ui/core-field",
    "@kanok-ui/core-listbox",
    "@kanok-ui/tw-field",
    "@kanok-ui/tw-i18n",
    "@kanok-ui/tw-list",
    "@kanok-ui/tw-theme",
    "@kanok-ui/tw-utils"
  ],
  "files": [
    {
      "path": "src/react-tw/combobox.tsx",
      "type": "registry:ui",
      "target": "components/ui/react-tw/combobox.tsx",
      "content": "import { useId, type CSSProperties, type ReactNode, type ComponentProps } from 'react';\nimport type { TextFilter } from '../core/listbox.ts';\nimport { Combobox as ComboboxPrimitive } from '@purpurabog/ui/headless';\nimport { FieldLabel, FieldMessageLine, StatusIcon, fluidBoxClasses, fluidControlClasses, useFluid, useFieldDisabled, fieldMessage, fieldBoxClasses, fieldStateEdges, useRequiredError, fieldInvalidRing, fieldTextSize } from './field.tsx';\nimport { useTranslation } from './lib/i18n.ts';\nimport type { FieldMessage } from '../core/field.ts';\nimport { cn } from './lib/utils.ts';\nimport { controlsPadding, EmptyRow, FieldControls, ListRows, ListStatus, listMenuClasses, type ListItem } from './list.tsx';\n\n/**\n * Carbon `ComboBox` as a React + Tailwind component: the look of `<ui-combobox>` on the headless\n * `Combobox` (the rules of core/combobox, traced key by key on Carbon React): an editable text box with `role=\"combobox\"`;\n * typing highlights the first option containing the text (and, with `filter`, narrows the list);\n * Enter picks the highlight (or the first option containing the text); leaving the field commits\n * an exact match, keeps free text with `allowCustomValue`, else snaps back; Esc closes, then clears.\n * × clears and keeps focus; neither × nor the chevron is a Tab stop. `loading` shows \"Searching…\".\n */\nexport interface ComboboxProps extends Omit<ComponentProps<'input'>, 'label' | 'hideLabel' | 'items' | 'value' | 'defaultValue' | 'onChange' | 'onInputChange' | 'inputValue' | 'placeholder' | 'size' | 'direction' | 'filter' | 'allowCustomValue' | 'hideClear' | 'loading' | 'helperText' | 'invalid' | 'invalidText' | 'warn' | 'warnText' | 'disabled' | 'readOnly' | 'required' | 'name' | 'id' | 'lang' | 'fluid' | 'onMenuChange' | 'className'> {\n    label: ReactNode;\n    hideLabel?: boolean;\n    items: ListItem[];\n    value?: string;\n    defaultValue?: string;\n    onChange?: (value: string) => void;\n    /** Every keystroke (e.g. to search a server; with `filter=\"none\"` the list is yours to fill); with `inputValue`, also a pick, a clear and the snap-back on leaving. */\n    onInputChange?: (text: string) => void;\n    /** What the text box shows, controlled (Lit `inputValue`). */\n    inputValue?: string;\n    placeholder?: string;\n    size?: 'sm' | 'md' | 'lg';\n    direction?: 'bottom' | 'top';\n    /** How typed text narrows the list; `none` (default) only highlights, as Carbon. */\n    filter?: TextFilter;\n    /** Keep text that matches no option as the value. */\n    allowCustomValue?: boolean;\n    hideClear?: boolean;\n    loading?: boolean;\n    /** After the list opens or closes (as Carbon React MultiSelect's `onMenuChange`). */\n    onMenuChange?: (open: boolean) => void;\n    helperText?: ReactNode;\n    invalid?: boolean;\n    invalidText?: ReactNode;\n    warn?: boolean;\n    warnText?: ReactNode;\n    disabled?: boolean;\n    readOnly?: boolean;\n    required?: boolean;\n    name?: string;\n    id?: string;\n    lang?: string;\n    /** Carbon `FluidComboBox`: the label inside a 64px box (also set by `FluidForm`); the open list keeps its row height. */\n    fluid?: boolean;\n    className?: string;\n}\n\nconst ROW = { sm: 'var(--ui-field-height-sm)', md: 'var(--ui-field-height-md)', lg: 'var(--ui-field-height-lg)' } as const;\n\n/** What the open list says to screen readers: still searching, or nothing found. */\nfunction listStatus(loading: boolean, count: number, t: { searching: string; noResults: string }) {\n    if (loading) return t.searching;\n    return count === 0 ? t.noResults : '';\n}\n\n/** The text box; fluid: 64px with the text under the label and room for × and the chevron. */\nfunction inputClasses({ showClear, statusKind, open, showError, fluid }: { showClear: boolean; statusKind: FieldMessage['kind'] | undefined; open: boolean; showError: boolean; fluid: boolean }) {\n    return cn(\n        'm-0 box-border h-(--list-row) w-full ps-4',\n        controlsPadding(showClear, !!statusKind),\n        fieldBoxClasses,\n        'text-ink type-body-sm',\n        fieldTextSize,\n        'outline-[length:var(--ui-field-focus-width,2px)] [outline-offset:calc(-1*var(--ui-field-focus-width,2px))] outline-transparent placeholder:text-ink-placeholder placeholder:opacity-100 focus:outline-focus focus:[box-shadow:var(--ui-field-focus-halo,none)]',\n        // Carbon: the combo box's box is a text-input, background-color and outline at fast-01\n        'transition-[background-color,outline-color] duration-(--ui-duration-fast) ease-carbon',\n        open && 'outline-focus [box-shadow:var(--ui-field-focus-halo,none)] border-[color:var(--ui-field-focus-border-color,inherit)]',\n        showError && fieldInvalidRing,\n        'disabled:cursor-not-allowed disabled:text-ink-disabled',\n        'read-only:not-disabled:bg-[var(--ui-field-readonly-background,transparent)]',\n        fieldStateEdges,\n        'forced-colors:border forced-colors:border-solid forced-colors:border-[FieldText] forced-colors:focus:outline-[Highlight]',\n        fluid && cn('h-16 pt-[33px] pb-[13px]', showClear ? 'pe-24' : 'pe-16', fluidControlClasses(statusKind)),\n    );\n}\n\nexport function Combobox({ label, hideLabel, items, value, defaultValue = '', onChange, onInputChange, inputValue, placeholder = '', size = 'md', direction = 'bottom', filter = 'none', allowCustomValue = false, hideClear, loading, onMenuChange, helperText, invalid, invalidText, warn, warnText, disabled: ownDisabled, readOnly, required, name, id, lang, fluid: fluidProp, className, ref, ...rest }: Readonly<ComboboxProps>) {\n    const disabled = useFieldDisabled(ownDisabled);\n    const t = useTranslation(lang).list;\n    const autoId = useId();\n    const fieldId = id ?? autoId;\n    const interactive = !disabled && !readOnly;\n    // nothing chosen on a submit attempt: the required message, until a choice is made\n    const requiredError = useRequiredError(onChange);\n    const { message, content } = fieldMessage({ interactive, invalid: invalid || requiredError.missing, invalidText: invalid ? invalidText : undefined, fallbackError: requiredError.missing ? t.required : '', warn, warnText, helperText });\n    const showError = message?.kind === 'error';\n    const hasStatus = !!message && message.kind !== 'helper';\n    const fluid = useFluid(fluidProp);\n    const statusKind = hasStatus ? message.kind : undefined;\n    return (\n        <ComboboxPrimitive.Root items={items} value={value} defaultValue={defaultValue} onValueChange={requiredError.onValueChange} onInvalid={requiredError.onInvalid} onInputValueChange={onInputChange} inputValue={inputValue} filter={filter} allowCustomValue={allowCustomValue} loading={!!loading} onOpenChange={onMenuChange} disabled={disabled} readOnly={readOnly} required={required} invalid={showError} name={name} id={fieldId}>\n            <div style={{ '--list-row': ROW[size] } as CSSProperties} className={cn('block font-sans', fluid && fluidBoxClasses(statusKind, readOnly), className)}>\n                <FieldLabel htmlFor={fieldId} hidden={hideLabel} disabled={disabled} fluid={fluid}>\n                    <span id={`${fieldId}-label`}>{label}</span>\n                </FieldLabel>\n                <ComboboxPrimitive.Anchor className=\"relative\">\n                    <ComboboxField {...rest} ref={ref} placeholder={placeholder} describedBy={message ? `${fieldId}-message` : undefined} statusKind={statusKind} showError={showError} hideClear={!!hideClear} fluid={fluid} labels={t} />\n                </ComboboxPrimitive.Anchor>\n                <FieldMessageLine id={`${fieldId}-message`} message={message} content={content} disabled={disabled} fluid={fluid} />\n                <ComboboxPrimitive.Popup side={direction} className={listMenuClasses}>\n                    {(shown) => <ComboboxRows items={shown} labels={t} />}\n                </ComboboxPrimitive.Popup>\n                <ComboboxStatus labels={t} />\n            </div>\n        </ComboboxPrimitive.Root>\n    );\n}\n\ntype FieldProps = Omit<ComponentProps<'input'>, 'value' | 'defaultValue' | 'onChange'> & {\n    describedBy?: string;\n    statusKind: FieldMessage['kind'] | undefined;\n    showError: boolean;\n    hideClear: boolean;\n    fluid: boolean;\n    labels: { clear: string; closeList: string; openList: string };\n};\n\n/** The text box with its status icon, × (while there's text) and chevron. */\nfunction ComboboxField({ describedBy, statusKind, showError, hideClear, fluid, labels, ...rest }: Readonly<FieldProps>) {\n    const { open, text, interactive, disabled, readOnly } = ComboboxPrimitive.useComboboxContext();\n    const showClear = interactive && text !== '' && !hideClear;\n    return (\n        <>\n            <ComboboxPrimitive.Input {...rest} aria-describedby={describedBy} className={inputClasses({ showClear, statusKind, open, showError, fluid })} />\n            <FieldControls\n                status={statusKind ? <StatusIcon kind={statusKind} className={fluid ? 'absolute end-4 top-[50px]' : undefined} /> : null}\n                clearLabel={labels.clear}\n                Clear={showClear ? ComboboxPrimitive.Clear : undefined}\n                toggleLabel={open ? labels.closeList : labels.openList}\n                Toggle={ComboboxPrimitive.Toggle}\n                open={open}\n                disabled={disabled}\n                readOnly={readOnly}\n                fluid={fluid}\n            />\n        </>\n    );\n}\n\n/** What the open list shows: \"Searching…\", \"No results\", or the option rows. */\nfunction ComboboxRows({ items, labels }: Readonly<{ items: ListItem[]; labels: { searching: string; noResults: string } }>) {\n    const { loading, listed, value } = ComboboxPrimitive.useComboboxContext();\n    if (loading) return <EmptyRow>{labels.searching}</EmptyRow>;\n    if (listed.length === 0) return <EmptyRow>{labels.noResults}</EmptyRow>;\n    return <ListRows items={items} isSelected={(o) => o.value === value} />;\n}\n\n/** \"Searching…\" / \"No results\" for screen readers while the list is open. */\nfunction ComboboxStatus({ labels }: Readonly<{ labels: { searching: string; noResults: string } }>) {\n    const { open, loading, listed } = ComboboxPrimitive.useComboboxContext();\n    return <ListStatus>{open ? listStatus(loading, listed.length, labels) : ''}</ListStatus>;\n}\n"
    }
  ]
}
