{
  "$schema": "https://ui.shadcn.com/schema/registry-item.json",
  "name": "opentui/tree-select",
  "title": "Tree Select",
  "description": "Hierarchical tree selection",
  "dependencies": [
    "@opentui/react"
  ],
  "registryDependencies": [
    "https://termcn.dev/r/opentui/use-theme.json"
  ],
  "files": [
    {
      "path": "registry/opentui/ui/tree-select.tsx",
      "content": "/* @jsxImportSource @opentui/react */\nimport { useKeyboard } from \"@opentui/react\";\nimport { useState } from \"react\";\n\nimport { useTheme } from \"@/hooks/use-theme\";\n\nexport interface TreeSelectNode<T = string> {\n  value: T;\n  label: string;\n  children?: TreeSelectNode<T>[];\n  disabled?: boolean;\n}\n\nexport interface TreeSelectProps<T = string> {\n  nodes: TreeSelectNode<T>[];\n  value?: T;\n  onChange?: (value: T) => void;\n  onSubmit?: (value: T) => void;\n  label?: string;\n  expandedByDefault?: boolean;\n}\n\ninterface FlatNode<T> {\n  node: TreeSelectNode<T>;\n  depth: number;\n  path: string;\n  hasChildren: boolean;\n}\n\nconst flatten = <T,>(\n  nodes: TreeSelectNode<T>[],\n  depth: number,\n  expanded: Set<string>,\n  pathPrefix: string,\n  expandedByDefault: boolean\n): FlatNode<T>[] => {\n  const result: FlatNode<T>[] = [];\n\n  for (let i = 0; i < nodes.length; i += 1) {\n    const node = nodes[i];\n    const path = `${pathPrefix}/${i}`;\n    const hasChildren = !!(node.children && node.children.length > 0);\n    result.push({ depth, hasChildren, node, path });\n    if (hasChildren) {\n      const isExpanded =\n        expanded.has(path) ||\n        (expandedByDefault && !expanded.has(`${path}:collapsed`));\n      if (isExpanded) {\n        result.push(\n          ...flatten(\n            node.children ?? [],\n            depth + 1,\n            expanded,\n            path,\n            expandedByDefault\n          )\n        );\n      }\n    }\n  }\n\n  return result;\n};\n\nconst getNodeColor = (\n  disabled: boolean | undefined,\n  isCursor: boolean,\n  isSelected: boolean,\n  theme: ReturnType<typeof useTheme>\n): string => {\n  if (disabled) {\n    return theme.colors.mutedForeground;\n  }\n  if (isCursor) {\n    return theme.colors.primary;\n  }\n  if (isSelected) {\n    return theme.colors.accent;\n  }\n  return theme.colors.foreground;\n};\n\nexport const TreeSelect = <T = string,>({\n  nodes,\n  value: controlledValue,\n  onChange,\n  onSubmit,\n  label,\n  expandedByDefault = false,\n}: TreeSelectProps<T>) => {\n  const theme = useTheme();\n  const [expanded, setExpanded] = useState<Set<string>>(new Set());\n  const [cursor, setCursor] = useState(0);\n  const [internalValue, setInternalValue] = useState<T | undefined>();\n\n  const selected = controlledValue ?? internalValue;\n  const flat = flatten(nodes, 0, expanded, \"\", expandedByDefault);\n\n  useKeyboard((key) => {\n    if (key.name === \"up\") {\n      setCursor((c) => Math.max(0, c - 1));\n    } else if (key.name === \"down\") {\n      setCursor((c) => Math.min(flat.length - 1, c + 1));\n    } else if (key.name === \"left\") {\n      const item = flat[cursor];\n      if (item && item.hasChildren) {\n        const isExpanded =\n          expanded.has(item.path) ||\n          (expandedByDefault && !expanded.has(`${item.path}:collapsed`));\n        if (isExpanded) {\n          setExpanded((prev) => {\n            const next = new Set(prev);\n            next.delete(item.path);\n            if (expandedByDefault) {\n              next.add(`${item.path}:collapsed`);\n            }\n            return next;\n          });\n        }\n      }\n    } else if (key.name === \"right\" || key.name === \" \") {\n      const item = flat[cursor];\n      if (item && item.hasChildren) {\n        const isExpanded =\n          expanded.has(item.path) ||\n          (expandedByDefault && !expanded.has(`${item.path}:collapsed`));\n        setExpanded((prev) => {\n          const next = new Set(prev);\n          if (isExpanded) {\n            next.delete(item.path);\n            if (expandedByDefault) {\n              next.add(`${item.path}:collapsed`);\n            }\n          } else {\n            next.add(item.path);\n            next.delete(`${item.path}:collapsed`);\n          }\n          return next;\n        });\n      }\n    } else if (key.name === \"return\") {\n      const item = flat[cursor];\n      if (item && !item.node.disabled) {\n        onChange?.(item.node.value);\n        onSubmit?.(item.node.value);\n        setInternalValue(item.node.value);\n      }\n    }\n  });\n\n  return (\n    <box flexDirection=\"column\">\n      {label && (\n        <text>\n          <b>{label}</b>\n        </text>\n      )}\n      {flat.map((item, idx) => {\n        const isCursor = idx === cursor;\n        const isSelected =\n          selected !== undefined && item.node.value === selected;\n        const isExpanded =\n          item.hasChildren &&\n          (expanded.has(item.path) ||\n            (expandedByDefault && !expanded.has(`${item.path}:collapsed`)));\n        const prefix = \"  \".repeat(item.depth);\n        let icon = \"\";\n        if (item.hasChildren) {\n          icon = isExpanded ? \"▼ \" : \"▶ \";\n        } else {\n          icon = \"· \";\n        }\n        const color = getNodeColor(\n          item.node.disabled,\n          isCursor,\n          isSelected,\n          theme\n        );\n        const content = `${prefix}${icon}${item.node.label}`;\n        return (\n          <box key={`${item.path}-${idx}`}>\n            <text\n              fg={item.node.disabled ? \"#666\" : color}\n              bg={isCursor ? theme.colors.selection : undefined}\n            >\n              {isCursor || isSelected ? <b>{content}</b> : content}\n            </text>\n          </box>\n        );\n      })}\n    </box>\n  );\n};\n",
      "type": "registry:ui",
      "target": "components/ui/tree-select.tsx"
    }
  ],
  "categories": [
    "selection"
  ],
  "type": "registry:ui"
}