import classNames from 'classnames' import { DateTimePickerState, isEmptyValue } from './datePickerUtils' export type TimePickerState = Pick< DateTimePickerState, 'hour' | 'minute' | 'meridian' > interface TimePickerInputsProps { id?: string disabled?: boolean value: TimePickerState | null onChange: (state: Partial) => void showPlaceholder?: boolean } export default function TimeInput(props: TimePickerInputsProps) { const onChange = (field: keyof TimePickerState, value: string) => { if (!value) { props.onChange({ [field]: null, }) return } props.onChange({ [field]: field === 'meridian' ? value : Number(value), }) } const hasValue = !isEmptyValue(props.value?.hour) || !isEmptyValue(props.value?.minute) const showPlaceholder = props.showPlaceholder ?? true return (
onChange('hour', v)} value={props.value?.hour} disabled={props.disabled} id={props.id} > <> {isEmptyValue(props.value?.hour) && ( )} {hours.map(i => ( {(!isEmptyValue(props.value?.hour) || showPlaceholder) && ( : )} onChange('minute', v)} value={props.value?.minute} disabled={props.disabled} > <> {isEmptyValue(props.value?.minute) && ( )} {minutes.map(i => ( ))}
) } function NumberSelect({ onChange, value, disabled, children, id, }: { onChange: (value: string) => void value?: number | null disabled?: boolean children?: React.ReactChild id?: string }) { const onKeyDown = (event: React.KeyboardEvent) => { if (event.key === 'Backspace') { onChange('') } } return ( ) } const hours = Array.from(new Array(12), (_, i) => i + 1) const minutes = Array.from(new Array(60), (_, i) => i) function Option({ value }: { value: number }) { // safari doesn't support text alignment in `