import { ChangeEvent, useRef } from "react"; import { Box, Label, Input, Button, Flex, Text } from "theme-ui"; import Modal from "../components/Modal"; import { getHMSDuration, getDurationHMS } from "../helpers/timer"; import useSetting from "../hooks/useSetting"; type StartTimerProps = { isOpen: boolean, onRequestClose: () => void, onTimerStart: any, onTimerStop: any, timer: any, } function StartTimerModal({ isOpen, onRequestClose, onTimerStart, onTimerStop, timer, }: StartTimerProps) { const inputRef = useRef(); function focusInput() { inputRef.current && inputRef.current.focus(); } const [hour, setHour] = useSetting("timer.hour"); const [minute, setMinute] = useSetting("timer.minute"); const [second, setSecond] = useSetting("timer.second"); function handleSubmit(event: ChangeEvent) { event.preventDefault(); if (timer) { onTimerStop(); } else { const duration = getHMSDuration({ hour, minute, second }); onTimerStart({ current: duration, max: duration }); } } const inputStyle = { width: "70px", border: "none", ":focus": { outline: "none", }, fontSize: "32px", padding: 2, paddingLeft: 0, }; function parseValue(value: string, max: number) { const num = parseInt(value); if (isNaN(num)) { return 0; } return Math.min(num, max); } const timerHMS = timer && getDurationHMS(timer.current); return ( H: setHour(parseValue(e.target.value, 24))} type="number" disabled={timer} min={0} max={24} /> M: setMinute(parseValue(e.target.value, 59))} type="number" ref={inputRef} disabled={timer} min={0} max={59} /> S: setSecond(parseValue(e.target.value, 59))} type="number" disabled={timer} min={0} max={59} /> ); } export default StartTimerModal;