|
| 1 | +import { Input } from "~/components/primitives/Input"; |
| 2 | +import { cn } from "~/utils/cn"; |
| 3 | +import React, { useRef, useState, useEffect } from "react"; |
| 4 | +import { Button } from "./Buttons"; |
| 5 | + |
| 6 | +export interface DurationPickerProps { |
| 7 | + id?: string; // used for the hidden input for form submission |
| 8 | + name?: string; // used for the hidden input for form submission |
| 9 | + defaultValueSeconds?: number; |
| 10 | + value?: number; |
| 11 | + onChange?: (totalSeconds: number) => void; |
| 12 | + variant?: "small" | "medium"; |
| 13 | + showClearButton?: boolean; |
| 14 | +} |
| 15 | + |
| 16 | +export function DurationPicker({ |
| 17 | + name, |
| 18 | + defaultValueSeconds: defaultValue = 0, |
| 19 | + value: controlledValue, |
| 20 | + onChange, |
| 21 | + variant = "small", |
| 22 | + showClearButton = true, |
| 23 | +}: DurationPickerProps) { |
| 24 | + // Use controlled value if provided, otherwise use default |
| 25 | + const initialValue = controlledValue ?? defaultValue; |
| 26 | + |
| 27 | + const defaultHours = Math.floor(initialValue / 3600); |
| 28 | + const defaultMinutes = Math.floor((initialValue % 3600) / 60); |
| 29 | + const defaultSeconds = initialValue % 60; |
| 30 | + |
| 31 | + const [hours, setHours] = useState<number>(defaultHours); |
| 32 | + const [minutes, setMinutes] = useState<number>(defaultMinutes); |
| 33 | + const [seconds, setSeconds] = useState<number>(defaultSeconds); |
| 34 | + |
| 35 | + const minuteRef = useRef<HTMLInputElement>(null); |
| 36 | + const hourRef = useRef<HTMLInputElement>(null); |
| 37 | + const secondRef = useRef<HTMLInputElement>(null); |
| 38 | + |
| 39 | + const totalSeconds = hours * 3600 + minutes * 60 + seconds; |
| 40 | + |
| 41 | + const isEmpty = hours === 0 && minutes === 0 && seconds === 0; |
| 42 | + |
| 43 | + // Sync internal state with external value changes |
| 44 | + useEffect(() => { |
| 45 | + if (controlledValue !== undefined && controlledValue !== totalSeconds) { |
| 46 | + const newHours = Math.floor(controlledValue / 3600); |
| 47 | + const newMinutes = Math.floor((controlledValue % 3600) / 60); |
| 48 | + const newSeconds = controlledValue % 60; |
| 49 | + |
| 50 | + setHours(newHours); |
| 51 | + setMinutes(newMinutes); |
| 52 | + setSeconds(newSeconds); |
| 53 | + } |
| 54 | + }, [controlledValue]); |
| 55 | + |
| 56 | + useEffect(() => { |
| 57 | + onChange?.(totalSeconds); |
| 58 | + }, [totalSeconds, onChange]); |
| 59 | + |
| 60 | + const handleHoursChange = (e: React.ChangeEvent<HTMLInputElement>) => { |
| 61 | + const value = parseInt(e.target.value) || 0; |
| 62 | + setHours(Math.max(0, value)); |
| 63 | + }; |
| 64 | + |
| 65 | + const handleMinutesChange = (e: React.ChangeEvent<HTMLInputElement>) => { |
| 66 | + const value = parseInt(e.target.value) || 0; |
| 67 | + if (value >= 60) { |
| 68 | + setHours((prev) => prev + Math.floor(value / 60)); |
| 69 | + setMinutes(value % 60); |
| 70 | + return; |
| 71 | + } |
| 72 | + |
| 73 | + setMinutes(Math.max(0, Math.min(59, value))); |
| 74 | + }; |
| 75 | + |
| 76 | + const handleSecondsChange = (e: React.ChangeEvent<HTMLInputElement>) => { |
| 77 | + const value = parseInt(e.target.value) || 0; |
| 78 | + if (value >= 60) { |
| 79 | + setMinutes((prev) => { |
| 80 | + const newMinutes = prev + Math.floor(value / 60); |
| 81 | + if (newMinutes >= 60) { |
| 82 | + setHours((prevHours) => prevHours + Math.floor(newMinutes / 60)); |
| 83 | + return newMinutes % 60; |
| 84 | + } |
| 85 | + return newMinutes; |
| 86 | + }); |
| 87 | + setSeconds(value % 60); |
| 88 | + return; |
| 89 | + } |
| 90 | + |
| 91 | + setSeconds(Math.max(0, Math.min(59, value))); |
| 92 | + }; |
| 93 | + |
| 94 | + const handleKeyDown = ( |
| 95 | + e: React.KeyboardEvent<HTMLInputElement>, |
| 96 | + nextRef?: React.RefObject<HTMLInputElement>, |
| 97 | + prevRef?: React.RefObject<HTMLInputElement> |
| 98 | + ) => { |
| 99 | + if (e.key === "Tab") { |
| 100 | + return; |
| 101 | + } |
| 102 | + |
| 103 | + if (e.key === "ArrowRight" && nextRef) { |
| 104 | + e.preventDefault(); |
| 105 | + nextRef.current?.focus(); |
| 106 | + nextRef.current?.select(); |
| 107 | + return; |
| 108 | + } |
| 109 | + |
| 110 | + if (e.key === "ArrowLeft" && prevRef) { |
| 111 | + e.preventDefault(); |
| 112 | + prevRef.current?.focus(); |
| 113 | + prevRef.current?.select(); |
| 114 | + return; |
| 115 | + } |
| 116 | + }; |
| 117 | + |
| 118 | + const clearDuration = () => { |
| 119 | + setHours(0); |
| 120 | + setMinutes(0); |
| 121 | + setSeconds(0); |
| 122 | + hourRef.current?.focus(); |
| 123 | + }; |
| 124 | + |
| 125 | + return ( |
| 126 | + <div className="flex items-center gap-3"> |
| 127 | + <input type="hidden" name={name} value={totalSeconds} /> |
| 128 | + |
| 129 | + <div className="flex items-center gap-1"> |
| 130 | + <div className="group flex items-center gap-1"> |
| 131 | + <Input |
| 132 | + variant={variant} |
| 133 | + ref={hourRef} |
| 134 | + className={cn( |
| 135 | + "w-10 text-center font-mono tabular-nums caret-transparent [&::-webkit-inner-spin-button]:appearance-none", |
| 136 | + isEmpty && "text-text-dimmed" |
| 137 | + )} |
| 138 | + value={hours.toString()} |
| 139 | + onChange={handleHoursChange} |
| 140 | + onKeyDown={(e) => handleKeyDown(e, minuteRef)} |
| 141 | + onFocus={(e) => e.target.select()} |
| 142 | + type="number" |
| 143 | + min={0} |
| 144 | + inputMode="numeric" |
| 145 | + /> |
| 146 | + <span className="text-sm text-text-dimmed transition-colors duration-200 group-focus-within:text-text-bright/80"> |
| 147 | + h |
| 148 | + </span> |
| 149 | + </div> |
| 150 | + <div className="group flex items-center gap-1"> |
| 151 | + <Input |
| 152 | + variant={variant} |
| 153 | + ref={minuteRef} |
| 154 | + className={cn( |
| 155 | + "w-10 text-center font-mono tabular-nums caret-transparent [&::-webkit-inner-spin-button]:appearance-none", |
| 156 | + isEmpty && "text-text-dimmed" |
| 157 | + )} |
| 158 | + value={minutes.toString()} |
| 159 | + onChange={handleMinutesChange} |
| 160 | + onKeyDown={(e) => handleKeyDown(e, secondRef, hourRef)} |
| 161 | + onFocus={(e) => e.target.select()} |
| 162 | + type="number" |
| 163 | + min={0} |
| 164 | + max={59} |
| 165 | + inputMode="numeric" |
| 166 | + /> |
| 167 | + <span className="text-sm text-text-dimmed transition-colors duration-200 group-focus-within:text-text-bright/80"> |
| 168 | + m |
| 169 | + </span> |
| 170 | + </div> |
| 171 | + <div className="group flex items-center gap-1"> |
| 172 | + <Input |
| 173 | + variant={variant} |
| 174 | + ref={secondRef} |
| 175 | + className={cn( |
| 176 | + "w-10 text-center font-mono tabular-nums caret-transparent [&::-webkit-inner-spin-button]:appearance-none", |
| 177 | + isEmpty && "text-text-dimmed" |
| 178 | + )} |
| 179 | + value={seconds.toString()} |
| 180 | + onChange={handleSecondsChange} |
| 181 | + onKeyDown={(e) => handleKeyDown(e, undefined, minuteRef)} |
| 182 | + onFocus={(e) => e.target.select()} |
| 183 | + type="number" |
| 184 | + min={0} |
| 185 | + max={59} |
| 186 | + inputMode="numeric" |
| 187 | + /> |
| 188 | + <span className="text-sm text-text-dimmed transition-colors duration-200 group-focus-within:text-text-bright/80"> |
| 189 | + s |
| 190 | + </span> |
| 191 | + </div> |
| 192 | + </div> |
| 193 | + |
| 194 | + {showClearButton && ( |
| 195 | + <Button type="button" variant={`tertiary/${variant}`} onClick={clearDuration}> |
| 196 | + Clear |
| 197 | + </Button> |
| 198 | + )} |
| 199 | + </div> |
| 200 | + ); |
| 201 | +} |
0 commit comments