"use client" import * as React from "react" import { ClockIcon } from "lucide-react" import { cn } from "@/shared/lib/utils" import { Button } from "@/shared/components/ui/button" import { Input } from "@/shared/components/ui/input" import { Popover, PopoverContent, PopoverTrigger, } from "@/shared/components/ui/popover" import type { BaseFieldControlProps } from "../types" export type TimePickerFieldProps = BaseFieldControlProps & { placeholder?: string withSeconds?: boolean } function pad(n: number) { return String(Math.max(0, n)).padStart(2, "0") } function parseTime(value: string) { const parts = (value ?? "").split(":") return { hours: Math.min(23, Math.max(0, Number(parts[0]) || 0)), minutes: Math.min(59, Math.max(0, Number(parts[1]) || 0)), seconds: Math.min(59, Math.max(0, Number(parts[2]) || 0)), } } export function TimePickerField({ value, onChange, onBlur, disabled, invalid, placeholder = "Pick a time", withSeconds = false, }: TimePickerFieldProps) { const { hours, minutes, seconds } = parseTime(value ?? "") const hasValue = !!value function update(h: number, m: number, s: number) { const hh = Math.min(23, Math.max(0, h)) const mm = Math.min(59, Math.max(0, m)) const ss = Math.min(59, Math.max(0, s)) onChange(withSeconds ? `${pad(hh)}:${pad(mm)}:${pad(ss)}` : `${pad(hh)}:${pad(mm)}`) } const display = hasValue ? withSeconds ? `${pad(hours)}:${pad(minutes)}:${pad(seconds)}` : `${pad(hours)}:${pad(minutes)}` : null return (
HH update(Number(e.target.value), minutes, seconds)} className="w-16 text-center" />
:
MM update(hours, Number(e.target.value), seconds)} className="w-16 text-center" />
{withSeconds && ( <> :
SS update(hours, minutes, Number(e.target.value))} className="w-16 text-center" />
)}
) }