'use client' import { forwardRef } from 'react' import { PktIcon } from '../icon/Icon' import { PktInputWrapper } from '../inputwrapper/InputWrapper' import { useTimepickerState } from './useTimepickerState' import type { IPktTimepicker } from './types' export type { IPktTimepicker } from './types' export const PktTimepicker = forwardRef((props, ref) => { const { onChange, onInput } = props const state = useTimepickerState(props, ref) const hoursLabel = state.strings.hours const minutesLabel = state.strings.minutes const hoursAriaLabel = state.label ? `${hoursLabel}, ${state.label}` : hoursLabel const minutesAriaLabel = state.label ? `${minutesLabel}, ${state.label}` : minutesLabel const renderOption = (optionValue: number, type: 'hour' | 'minute') => { const strVal = String(optionValue).padStart(2, '0') const currentVal = type === 'hour' ? state.hours !== '' ? parseInt(state.hours, 10) : NaN : state.minutes !== '' ? parseInt(state.minutes, 10) : NaN const isSelected = optionValue === currentVal return ( ) } const renderPopup = () => ( ) const renderContainer = () => (
{ const target = e.target as HTMLElement if (target.closest('button, input')) return state.hoursInputRef.current?.focus() }} > {state.stepArrows && ( )} { e.currentTarget.select() }} onChange={() => { /* value is driven by keydown handlers */ }} onPaste={(e) => e.preventDefault()} /> : { e.currentTarget.select() }} onChange={() => { /* value is driven by keydown handlers */ }} onPaste={(e) => e.preventDefault()} /> {state.hidePicker && !state.stepArrows && ( )} {!state.hidePicker && !state.stepArrows && ( )} {state.stepArrows && ( )} {/* Native constraints are reported on the hours spinbutton (setCustomValidity), not here — avoids "invalid form control is not focusable" for this hidden type="time" input. */}
) return (
{!state.hidePicker && !state.stepArrows ? (
{renderContainer()} {renderPopup()}
) : ( renderContainer() )}
) }) PktTimepicker.displayName = 'PktTimepicker'