{
  "$schema": "https://ui.shadcn.com/schema/registry-item.json",
  "name": "opentui/search-input",
  "title": "Search Input",
  "description": "Fuzzy search input with autocomplete",
  "dependencies": [
    "@opentui/react"
  ],
  "registryDependencies": [
    "https://termcn.dev/r/opentui/use-theme.json"
  ],
  "files": [
    {
      "path": "registry/opentui/ui/search-input.tsx",
      "content": "/* @jsxImportSource @opentui/react */\nimport { useKeyboard } from \"@opentui/react\";\nimport { useState, useMemo, useCallback } from \"react\";\n\nimport { useTheme } from \"@/hooks/use-theme\";\nimport type { BorderStyle } from \"@/components/ui/types\";\n\nexport interface SearchInputProps<T = string> {\n  options?: T[];\n  getValue?: (item: T) => string;\n  value?: string;\n  onChange?: (query: string) => void;\n  onSelect?: (item: T) => void;\n  placeholder?: string;\n  label?: string;\n  maxResults?: number;\n  id?: string;\n  borderStyle?: BorderStyle;\n  paddingX?: number;\n  cursor?: string;\n  searchIcon?: string;\n  resultCursor?: string;\n}\n\nexport const SearchInput = <T = string,>({\n  options,\n  getValue,\n  value: controlledValue,\n  onChange,\n  onSelect,\n  placeholder = \"Search...\",\n  label,\n  maxResults = 5,\n  id: _id,\n  borderStyle = \"rounded\",\n  paddingX = 1,\n  cursor = \"█\",\n  searchIcon = \"🔍 \",\n  resultCursor = \"› \",\n}: SearchInputProps<T>) => {\n  const [internalValue, setInternalValue] = useState(\"\");\n  const [selectedIndex, setSelectedIndex] = useState(0);\n  const [showResults, setShowResults] = useState(false);\n  const theme = useTheme();\n  const [isFocused] = useState(true);\n\n  const query = controlledValue ?? internalValue;\n\n  const getItemValue = useCallback(\n    (item: T): string => {\n      if (getValue) {\n        return getValue(item);\n      }\n      return String(item);\n    },\n    [getValue]\n  );\n\n  const setQuery = (newQuery: string) => {\n    if (onChange) {\n      onChange(newQuery);\n    } else {\n      setInternalValue(newQuery);\n    }\n  };\n\n  const filteredResults = useMemo(() => {\n    if (!options || options.length === 0) {\n      return [];\n    }\n    if (!query) {\n      return options.slice(0, maxResults);\n    }\n    const lower = query.toLowerCase();\n    return options\n      .filter((item) => getItemValue(item).toLowerCase().includes(lower))\n      .slice(0, maxResults);\n  }, [options, query, maxResults, getItemValue]);\n\n  useKeyboard((key) => {\n    if (!isFocused) {\n      return;\n    }\n    if (key.name === \"escape\") {\n      setQuery(\"\");\n      setShowResults(false);\n      setSelectedIndex(0);\n      return;\n    }\n    if (key.name === \"up\") {\n      if (showResults && filteredResults.length > 0) {\n        setSelectedIndex((i) => Math.max(0, i - 1));\n      }\n      return;\n    }\n    if (key.name === \"down\") {\n      if (filteredResults.length > 0) {\n        setShowResults(true);\n        setSelectedIndex((i) => Math.min(filteredResults.length - 1, i + 1));\n      }\n      return;\n    }\n    if (key.name === \"return\") {\n      if (showResults && filteredResults.length > 0) {\n        onSelect?.(filteredResults[selectedIndex]);\n        setQuery(getItemValue(filteredResults[selectedIndex]));\n        setShowResults(false);\n        setSelectedIndex(0);\n      }\n      return;\n    }\n    if (key.name === \"backspace\" || key.name === \"delete\") {\n      const newQuery = query.slice(0, -1);\n      setQuery(newQuery);\n      setSelectedIndex(0);\n      if (newQuery.length === 0) {\n        setShowResults(false);\n      }\n      return;\n    }\n    if (key.name === \"tab\") {\n      return;\n    }\n    if (key.name && key.name.length === 1) {\n      const newQuery = query + key.name;\n      setQuery(newQuery);\n      setSelectedIndex(0);\n      if (options && options.length > 0) {\n        setShowResults(true);\n      }\n    }\n  });\n\n  const borderColor = isFocused ? theme.colors.focusRing : theme.colors.border;\n  const hasResults = showResults && filteredResults.length > 0;\n\n  return (\n    <box flexDirection=\"column\">\n      {label && (\n        <text>\n          <b>{label}</b>\n        </text>\n      )}\n      <box paddingLeft={paddingX} paddingRight={paddingX}>\n        <text fg={theme.colors.mutedForeground}>{searchIcon}</text>\n        <text\n          fg={query ? theme.colors.foreground : theme.colors.mutedForeground}\n        >\n          {query || placeholder}\n        </text>\n        {isFocused && <text fg={theme.colors.focusRing}>{cursor}</text>}\n      </box>\n      {hasResults && (\n        <box flexDirection=\"column\" paddingLeft={2}>\n          {filteredResults.map((item, idx) => {\n            const isSelected = idx === selectedIndex;\n            return (\n              <box key={idx} flexDirection=\"row\">\n                <text\n                  fg={\n                    isSelected\n                      ? theme.colors.focusRing\n                      : theme.colors.mutedForeground\n                  }\n                >\n                  {isSelected ? resultCursor : \" \".repeat(resultCursor.length)}\n                </text>\n                <text\n                  fg={\n                    isSelected\n                      ? theme.colors.foreground\n                      : theme.colors.mutedForeground\n                  }\n                >\n                  {getItemValue(item)}\n                </text>\n              </box>\n            );\n          })}\n        </box>\n      )}\n    </box>\n  );\n};\n",
      "type": "registry:ui",
      "target": "components/ui/search-input.tsx"
    }
  ],
  "categories": [
    "input"
  ],
  "type": "registry:ui"
}