{
  "$schema": "https://ui.shadcn.com/schema/registry-item.json",
  "name": "opentui/date-picker",
  "title": "Date Picker",
  "description": "Calendar date picker with keyboard navigation",
  "dependencies": [
    "@opentui/react"
  ],
  "registryDependencies": [
    "https://termcn.dev/r/opentui/use-theme.json"
  ],
  "files": [
    {
      "path": "registry/opentui/ui/date-picker.tsx",
      "content": "/* @jsxImportSource @opentui/react */\nimport { useKeyboard } from \"@opentui/react\";\nimport { useState } from \"react\";\n\nimport { useTheme } from \"@/hooks/use-theme\";\n\nexport interface DatePickerProps {\n  value?: Date;\n  onChange?: (date: Date) => void;\n  onSubmit?: (date: Date) => void;\n  label?: string;\n  minDate?: Date;\n  maxDate?: Date;\n  autoFocus?: boolean;\n  id?: string;\n}\n\nconst MONTHS = [\n  \"January\",\n  \"February\",\n  \"March\",\n  \"April\",\n  \"May\",\n  \"June\",\n  \"July\",\n  \"August\",\n  \"September\",\n  \"October\",\n  \"November\",\n  \"December\",\n];\n\nconst daysInMonth = (month: number, year: number): number =>\n  new Date(year, month + 1, 0).getDate();\n\nconst clamp = (val: number, min: number, max: number): number =>\n  Math.min(Math.max(val, min), max);\n\nconst buildDate = (m: number, d: number, y: number): Date => new Date(y, m, d);\n\nconst getNextField = (\n  f: \"month\" | \"day\" | \"year\"\n): \"month\" | \"day\" | \"year\" => {\n  if (f === \"month\") {\n    return \"day\";\n  }\n  if (f === \"day\") {\n    return \"year\";\n  }\n  return \"month\";\n};\n\nexport const DatePicker = ({\n  value: controlledValue,\n  onChange,\n  onSubmit,\n  label,\n  minDate,\n  maxDate,\n  autoFocus: _autoFocus = false,\n  id: _id,\n}: DatePickerProps) => {\n  const theme = useTheme();\n  const [isFocused] = useState(true);\n\n  const now = new Date();\n  const initial = controlledValue ?? now;\n\n  const [month, setMonth] = useState(initial.getMonth());\n  const [day, setDay] = useState(initial.getDate());\n  const [year, setYear] = useState(initial.getFullYear());\n  const [field, setField] = useState<\"month\" | \"day\" | \"year\">(\"month\");\n\n  const notify = (m: number, d: number, y: number) => {\n    const date = buildDate(m, d, y);\n    if (minDate && date < minDate) {\n      return;\n    }\n    if (maxDate && date > maxDate) {\n      return;\n    }\n    onChange?.(date);\n  };\n\n  useKeyboard((key) => {\n    if (!isFocused) {\n      return;\n    }\n    if (key.name === \"tab\") {\n      setField((f) => getNextField(f));\n      return;\n    }\n    if (key.name === \"return\") {\n      const date = buildDate(month, day, year);\n      onSubmit?.(date);\n      return;\n    }\n    if (key.name === \"up\") {\n      if (field === \"month\") {\n        const newMonth = (month + 1) % 12;\n        const maxDay = daysInMonth(newMonth, year);\n        const newDay = clamp(day, 1, maxDay);\n        setMonth(newMonth);\n        setDay(newDay);\n        notify(newMonth, newDay, year);\n      } else if (field === \"day\") {\n        const maxDay = daysInMonth(month, year);\n        const newDay = day >= maxDay ? 1 : day + 1;\n        setDay(newDay);\n        notify(month, newDay, year);\n      } else {\n        const newYear = year + 1;\n        const maxDay = daysInMonth(month, newYear);\n        const newDay = clamp(day, 1, maxDay);\n        setYear(newYear);\n        setDay(newDay);\n        notify(month, newDay, newYear);\n      }\n    } else if (key.name === \"down\") {\n      if (field === \"month\") {\n        const newMonth = month === 0 ? 11 : month - 1;\n        const maxDay = daysInMonth(newMonth, year);\n        const newDay = clamp(day, 1, maxDay);\n        setMonth(newMonth);\n        setDay(newDay);\n        notify(newMonth, newDay, year);\n      } else if (field === \"day\") {\n        const maxDay = daysInMonth(month, year);\n        const newDay = day <= 1 ? maxDay : day - 1;\n        setDay(newDay);\n        notify(month, newDay, year);\n      } else {\n        const newYear = year - 1;\n        const maxDay = daysInMonth(month, newYear);\n        const newDay = clamp(day, 1, maxDay);\n        setYear(newYear);\n        setDay(newDay);\n        notify(month, newDay, newYear);\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  return (\n    <box flexDirection=\"column\">\n      {label && (\n        <text>\n          <b>{label}</b>\n        </text>\n      )}\n      <box\n        gap={1}\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(\"month\")}>▲</text>\n          <text fg={fieldColor(\"month\")} bg={fieldBg(\"month\")}>\n            {field === \"month\" ? (\n              <b>{(MONTHS[month] ?? \"\").slice(0, 3)}</b>\n            ) : (\n              (MONTHS[month] ?? \"\").slice(0, 3)\n            )}\n          </text>\n          <text fg={fieldColor(\"month\")}>▼</text>\n        </box>\n\n        <text fg={theme.colors.border}>/</text>\n\n        <box flexDirection=\"column\" alignItems=\"center\">\n          <text fg={fieldColor(\"day\")}>▲</text>\n          <text fg={fieldColor(\"day\")} bg={fieldBg(\"day\")}>\n            {field === \"day\" ? (\n              <b>{String(day).padStart(2, \"0\")}</b>\n            ) : (\n              String(day).padStart(2, \"0\")\n            )}\n          </text>\n          <text fg={fieldColor(\"day\")}>▼</text>\n        </box>\n\n        <text fg={theme.colors.border}>/</text>\n\n        <box flexDirection=\"column\" alignItems=\"center\">\n          <text fg={fieldColor(\"year\")}>▲</text>\n          <text fg={fieldColor(\"year\")} bg={fieldBg(\"year\")}>\n            {field === \"year\" ? <b>{String(year)}</b> : String(year)}\n          </text>\n          <text fg={fieldColor(\"year\")}>▼</text>\n        </box>\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/date-picker.tsx"
    }
  ],
  "categories": [
    "forms"
  ],
  "type": "registry:ui"
}