{
  "$schema": "https://ui.shadcn.com/schema/registry-item.json",
  "name": "opentui/time-picker",
  "title": "Time Picker",
  "description": "Time picker with hour/minute/second spinners",
  "dependencies": [
    "@opentui/react"
  ],
  "registryDependencies": [
    "https://termcn.dev/r/opentui/use-theme.json"
  ],
  "files": [
    {
      "path": "registry/opentui/ui/time-picker.tsx",
      "content": "/* @jsxImportSource @opentui/react */\nimport { useKeyboard } from \"@opentui/react\";\nimport React, { useState } from \"react\";\n\nimport { useTheme } from \"@/hooks/use-theme\";\n\nexport interface TimePickerProps {\n  value?: {\n    hours: number;\n    minutes: number;\n  };\n  onChange?: (time: { hours: number; minutes: number }) => void;\n  onSubmit?: (time: { hours: number; minutes: number }) => void;\n  label?: string;\n  format?: 12 | 24;\n  autoFocus?: boolean;\n  id?: string;\n}\n\nexport const TimePicker = ({\n  value: controlledValue,\n  onChange,\n  onSubmit,\n  label,\n  format = 24,\n  autoFocus: _autoFocus = false,\n  id: _id,\n}: TimePickerProps) => {\n  const theme = useTheme();\n  const [isFocused] = useState(true);\n\n  const now = new Date();\n  const initialHours = controlledValue?.hours ?? now.getHours();\n  const initialMinutes = controlledValue?.minutes ?? now.getMinutes();\n\n  const [hours, setHours] = useState(initialHours);\n  const [minutes, setMinutes] = useState(initialMinutes);\n  const [ampm, setAmPm] = useState<\"AM\" | \"PM\">(\n    initialHours < 12 ? \"AM\" : \"PM\"\n  );\n  const [field, setField] = useState<\"hours\" | \"minutes\" | \"ampm\">(\"hours\");\n\n  const maxHours = format === 12 ? 12 : 23;\n  const minHours = format === 12 ? 1 : 0;\n\n  const notify = (h: number, m: number, ap: \"AM\" | \"PM\") => {\n    let actualHours = h;\n    if (format === 12) {\n      if (ap === \"AM\" && h === 12) {\n        actualHours = 0;\n      } else if (ap === \"PM\" && h !== 12) {\n        actualHours = h + 12;\n      }\n    }\n    onChange?.({ hours: actualHours, minutes: m });\n  };\n\n  useKeyboard((key) => {\n    if (!isFocused) {\n      return;\n    }\n    if (key.name === \"tab\") {\n      if (format === 12) {\n        setField((f) => {\n          if (f === \"hours\") {\n            return \"minutes\";\n          }\n          if (f === \"minutes\") {\n            return \"ampm\";\n          }\n          return \"hours\";\n        });\n      } else {\n        setField((f) => (f === \"hours\" ? \"minutes\" : \"hours\"));\n      }\n      return;\n    }\n    if (key.name === \"return\") {\n      let actualHours = hours;\n      if (format === 12) {\n        if (ampm === \"AM\" && hours === 12) {\n          actualHours = 0;\n        } else if (ampm === \"PM\" && hours !== 12) {\n          actualHours = hours + 12;\n        }\n      }\n      onSubmit?.({ hours: actualHours, minutes });\n      return;\n    }\n    if (key.name === \"up\") {\n      if (field === \"hours\") {\n        const newH = hours >= maxHours ? minHours : hours + 1;\n        setHours(newH);\n        notify(newH, minutes, ampm);\n      } else if (field === \"minutes\") {\n        const newM = minutes >= 59 ? 0 : minutes + 1;\n        setMinutes(newM);\n        notify(hours, newM, ampm);\n      } else if (field === \"ampm\") {\n        const newAp: \"AM\" | \"PM\" = ampm === \"AM\" ? \"PM\" : \"AM\";\n        setAmPm(newAp);\n        notify(hours, minutes, newAp);\n      }\n    } else if (key.name === \"down\") {\n      if (field === \"hours\") {\n        const newH = hours <= minHours ? maxHours : hours - 1;\n        setHours(newH);\n        notify(newH, minutes, ampm);\n      } else if (field === \"minutes\") {\n        const newM = minutes <= 0 ? 59 : minutes - 1;\n        setMinutes(newM);\n        notify(hours, newM, ampm);\n      } else if (field === \"ampm\") {\n        const newAp: \"AM\" | \"PM\" = ampm === \"AM\" ? \"PM\" : \"AM\";\n        setAmPm(newAp);\n        notify(hours, minutes, newAp);\n      }\n    }\n  });\n\n  const fieldColor = (f: typeof field): string =>\n    field === f && isFocused ? theme.colors.primary : theme.colors.foreground;\n\n  const fieldBg = (f: typeof field): string | undefined =>\n    field === f && isFocused ? theme.colors.selection : undefined;\n\n  let displayHours: number;\n  if (format !== 12) {\n    displayHours = hours;\n  } else if (hours === 0) {\n    displayHours = 12;\n  } else if (hours > 12) {\n    displayHours = hours - 12;\n  } else {\n    displayHours = hours;\n  }\n\n  return (\n    <box flexDirection=\"column\">\n      {label && (\n        <text>\n          <b>{label}</b>\n        </text>\n      )}\n      <box\n        gap={0}\n        borderStyle=\"rounded\"\n        borderColor={isFocused ? theme.colors.focusRing : theme.colors.border}\n        paddingLeft={1}\n        paddingRight={1}\n      >\n        <box flexDirection=\"column\" alignItems=\"center\">\n          <text fg={fieldColor(\"hours\")}>▲</text>\n          <text fg={fieldColor(\"hours\")} bg={fieldBg(\"hours\")}>\n            {field === \"hours\" ? (\n              <b>{String(displayHours).padStart(2, \"0\")}</b>\n            ) : (\n              String(displayHours).padStart(2, \"0\")\n            )}\n          </text>\n          <text fg={fieldColor(\"hours\")}>▼</text>\n        </box>\n\n        <box flexDirection=\"column\" alignItems=\"center\" justifyContent=\"center\">\n          <text fg={theme.colors.border}> </text>\n          <text fg={theme.colors.foreground}>\n            <b>:</b>\n          </text>\n          <text fg={theme.colors.border}> </text>\n        </box>\n\n        <box flexDirection=\"column\" alignItems=\"center\">\n          <text fg={fieldColor(\"minutes\")}>▲</text>\n          <text fg={fieldColor(\"minutes\")} bg={fieldBg(\"minutes\")}>\n            {field === \"minutes\" ? (\n              <b>{String(minutes).padStart(2, \"0\")}</b>\n            ) : (\n              String(minutes).padStart(2, \"0\")\n            )}\n          </text>\n          <text fg={fieldColor(\"minutes\")}>▼</text>\n        </box>\n\n        {format === 12 && (\n          <>\n            <box\n              flexDirection=\"column\"\n              alignItems=\"center\"\n              justifyContent=\"center\"\n            >\n              <text fg={theme.colors.border}> </text>\n              <text fg={theme.colors.border}> </text>\n              <text fg={theme.colors.border}> </text>\n            </box>\n            <box flexDirection=\"column\" alignItems=\"center\">\n              <text fg={fieldColor(\"ampm\")}>▲</text>\n              <text fg={fieldColor(\"ampm\")} bg={fieldBg(\"ampm\")}>\n                {field === \"ampm\" ? <b>{ampm}</b> : ampm}\n              </text>\n              <text fg={fieldColor(\"ampm\")}>▼</text>\n            </box>\n          </>\n        )}\n      </box>\n      {isFocused && (\n        <text fg=\"#666\">↑↓: change · Tab: next field · Enter: confirm</text>\n      )}\n    </box>\n  );\n};\n",
      "type": "registry:ui",
      "target": "components/ui/time-picker.tsx"
    }
  ],
  "categories": [
    "forms"
  ],
  "type": "registry:ui"
}