"use client"; import * as React from "react"; import { cn } from "./lib/utils"; export interface TimeInputProps extends Omit, "onChange"> { value?: string; // "19:30" or "07:30 PM" defaultValue?: string; onValueChange?: (val: string) => void; format24h?: boolean; className?: string; } export function TimeInput({ value: controlledValue, defaultValue = "19:30", onValueChange, format24h = true, className, ...props }: TimeInputProps) { const [internalVal, setInternalVal] = React.useState(defaultValue); const isControlled = controlledValue !== undefined; const currentVal = isControlled ? controlledValue : internalVal; const [hours, minutes] = React.useMemo(() => { const parts = currentVal.split(":"); return [ parseInt(parts[0] || "19", 10), parseInt(parts[1] || "30", 10), ]; }, [currentVal]); const updateTime = (newH: number, newM: number) => { const clampedH = (newH + 24) % 24; const clampedM = (newM + 60) % 60; const pad = (n: number) => n.toString().padStart(2, "0"); const formatted = `${pad(clampedH)}:${pad(clampedM)}`; if (!isControlled) { setInternalVal(formatted); } onValueChange?.(formatted); }; const handleHourStep = (delta: number) => updateTime(hours + delta, minutes); const handleMinuteStep = (delta: number) => updateTime(hours, minutes + delta); return (
{/* Hours Box */}
{hours.toString().padStart(2, "0")}
: {/* Minutes Box */}
{minutes.toString().padStart(2, "0")}
{!format24h && ( )}
SHOWTIME
); }