import React, { useMemo, useState } from 'react'; import { lucidClassNames } from '../../util/style-helpers'; import ClockIcon from '../Icon/ClockIcon/ClockIcon'; import TimeSelectHour from './TimeSelectHour'; import TimeSelectMeridiem from './TimeSelectMeridiem'; import TimeSelectMinute from './TimeSelectMinute'; const cx = lucidClassNames.bind('&-TimeSelect'); export interface ITimeSelect { /** JS Date for the time to display and update */ time: Date; /** Set to true to display the TimeSelect as a 24 hour clock. Set to false to display the TimeSelect as a 12 hour clock */ is24HourClock?: boolean; /** The callback that will take a new Date object */ onChange(time: Date): void; /** Set to true to disable the TimeSelect */ isDisabled?: boolean; } enum MeridiemType { AM = 'AM', PM = 'PM', } const TimeSelect = ({ time, is24HourClock, onChange, isDisabled, }: ITimeSelect) => { const [inputFocus, setInputFocus] = useState(false); const minute = useMemo(() => time.getMinutes(), [time]); const { hour, meridiem = MeridiemType.AM } = useMemo(() => { const hour = time.getHours(); if (is24HourClock) { return { hour }; } const cleanedHour = hour === 0 || hour === 12 ? 12 : hour < 12 ? hour : hour - 12; const cleanedMeridiem = hour < 12 ? MeridiemType.AM : MeridiemType.PM; return { hour: cleanedHour, meridiem: cleanedMeridiem }; }, [time, is24HourClock]); const isAM = useMemo(() => meridiem === MeridiemType.AM, [meridiem]); const isDisabledClass = isDisabled ? '&-time-disabled' : ''; const timeSelectorClass = isDisabled ? '&-isDisabled' : ''; const toggleInputFocus = () => setInputFocus(!inputFocus); return (