@@ -3,6 +3,7 @@ import { useLayoutEffect } from '@rc-component/util';
33import * as React from 'react' ;
44import { usePanelContext } from '../../context' ;
55import useScrollTo from './useScrollTo' ;
6+ import type { Locale } from '../../../interface' ;
67
78const SCROLL_DELAY = 300 ;
89
@@ -12,11 +13,13 @@ export type Unit<ValueType = number | string> = {
1213 disabled ?: boolean ;
1314} ;
1415
16+ type TimeUnitType = 'hour' | 'minute' | 'second' | 'millisecond' | 'meridiem' ;
17+
1518export interface TimeUnitColumnProps {
1619 units : Unit [ ] ;
1720 value : number | string ;
1821 optionalValue ?: number | string ;
19- type : 'hour' | 'minute' | 'second' | 'millisecond' | 'meridiem' ;
22+ type : TimeUnitType ;
2023 onChange : ( value : number | string ) => void ;
2124 onHover : ( value : number | string ) => void ;
2225 onDblClick ?: VoidFunction ;
@@ -28,6 +31,48 @@ function flattenUnits(units: Unit<string | number>[]) {
2831 return units . map ( ( { value, label, disabled } ) => [ value , label , disabled ] . join ( ',' ) ) . join ( ';' ) ;
2932}
3033
34+ const LIST_LABEL_MAP : Record < TimeUnitType , ( locale : Locale ) => string > = {
35+ hour : ( locale ) => locale . hourSelect ,
36+ minute : ( locale ) => locale . minuteSelect ,
37+ second : ( locale ) => locale . secondSelect ,
38+ millisecond : ( locale ) => locale . millisecondSelect ,
39+ meridiem : ( locale ) => locale . meridiemSelect ,
40+ } ;
41+
42+ // `en_US` → `en-US`, `sr_Cyrl_RS` → `sr-Cyrl-RS`
43+ const toBCP47 = ( code : string ) => code . replace ( / _ / g, '-' ) ;
44+
45+ const LIST_ITEM_LABEL_MAP : Record <
46+ TimeUnitType ,
47+ ( value : string | number , locale : Locale ) => string
48+ > = {
49+ hour : ( value , locale ) =>
50+ value . toLocaleString ( toBCP47 ( locale . locale ) , {
51+ style : 'unit' ,
52+ unit : 'hour' ,
53+ unitDisplay : 'long' ,
54+ } ) ,
55+ minute : ( value , locale ) =>
56+ value . toLocaleString ( toBCP47 ( locale . locale ) , {
57+ style : 'unit' ,
58+ unit : 'minute' ,
59+ unitDisplay : 'long' ,
60+ } ) ,
61+ second : ( value , locale ) =>
62+ value . toLocaleString ( toBCP47 ( locale . locale ) , {
63+ style : 'unit' ,
64+ unit : 'second' ,
65+ unitDisplay : 'long' ,
66+ } ) ,
67+ millisecond : ( value , locale ) =>
68+ value . toLocaleString ( toBCP47 ( locale . locale ) , {
69+ style : 'unit' ,
70+ unit : 'millisecond' ,
71+ unitDisplay : 'long' ,
72+ } ) ,
73+ meridiem : ( value ) => value . toString ( ) ,
74+ } ;
75+
3176export default function TimeColumn < DateType extends object > ( props : TimeUnitColumnProps ) {
3277 const { units, value, optionalValue, type, onChange, onHover, onDblClick, changeOnScroll } =
3378 props ;
@@ -92,20 +137,92 @@ export default function TimeColumn<DateType extends object>(props: TimeUnitColum
92137 }
93138 } ;
94139
140+ // ========================= Focus =========================
141+ const activeValue = value ?? optionalValue ;
142+
143+ // Tracks keyboard-navigation cursor separately from the committed value.
144+ const [ focusedValue , setFocusedValue ] = React . useState < number | string | null > ( null ) ;
145+
146+ // Reset cursor when the committed value changes (e.g. click or external update).
147+ React . useEffect ( ( ) => {
148+ setFocusedValue ( null ) ;
149+ } , [ value ] ) ;
150+
151+ const tabFocusValue = focusedValue ?? activeValue ;
152+
153+ // The active option registers its node via a callback ref; after keyboard
154+ // navigation we move DOM focus to it without querying the DOM.
155+ const focusedLiNodeRef = React . useRef < HTMLElement | null > ( null ) ;
156+ const pendingFocusRef = React . useRef ( false ) ;
157+
158+ const registerFocusedLi = React . useCallback ( ( node : HTMLElement | null ) => {
159+ focusedLiNodeRef . current = node ;
160+ } , [ ] ) ;
161+
162+ React . useEffect ( ( ) => {
163+ if ( pendingFocusRef . current ) {
164+ pendingFocusRef . current = false ;
165+ focusedLiNodeRef . current ?. focus ( ) ;
166+ }
167+ } , [ focusedValue ] ) ;
168+
169+ // ========================= Keyboard =========================
170+ const onCellKeyDown = ( e : React . KeyboardEvent < HTMLLIElement > ) => {
171+ const enabledUnits = units . filter ( ( u ) => ! u . disabled ) ;
172+ const currentIdx = enabledUnits . findIndex ( ( u ) => u . value === tabFocusValue ) ;
173+
174+ if ( e . key === 'ArrowDown' ) {
175+ e . preventDefault ( ) ;
176+ pendingFocusRef . current = true ;
177+ const next = currentIdx < enabledUnits . length - 1 ? currentIdx + 1 : 0 ;
178+ setFocusedValue ( enabledUnits [ next ] ?. value ) ;
179+ } else if ( e . key === 'ArrowUp' ) {
180+ e . preventDefault ( ) ;
181+ pendingFocusRef . current = true ;
182+ const prev = currentIdx > 0 ? currentIdx - 1 : enabledUnits . length - 1 ;
183+ setFocusedValue ( enabledUnits [ prev ] ?. value ) ;
184+ } else if ( e . key === 'Enter' || e . key === ' ' ) {
185+ e . preventDefault ( ) ;
186+ const target = enabledUnits . find ( ( u ) => u . value === tabFocusValue ) ;
187+ if ( target ) {
188+ onChange ( target . value ) ;
189+ }
190+ }
191+ } ;
192+
95193 // ========================= Render =========================
96194 const columnPrefixCls = `${ panelPrefixCls } -column` ;
97195
98196 return (
99- < ul className = { columnPrefixCls } ref = { ulRef } data-type = { type } onScroll = { onInternalScroll } >
197+ < ul
198+ role = "listbox"
199+ aria-label = { LIST_LABEL_MAP [ type ] ( locale ) }
200+ className = { columnPrefixCls }
201+ ref = { ulRef }
202+ data-type = { type }
203+ onScroll = { onInternalScroll }
204+ onBlur = { ( e ) => {
205+ if ( ! ulRef . current ?. contains ( e . relatedTarget as Node ) ) {
206+ setFocusedValue ( null ) ;
207+ }
208+ } }
209+ >
100210 { units . map ( ( { label, value : unitValue , disabled } ) => {
101211 const inner = < div className = { `${ cellPrefixCls } -inner` } > { label } </ div > ;
212+ const isSelected = value === unitValue ;
102213
103214 return (
104215 < li
105216 key = { unitValue }
217+ ref = { tabFocusValue === unitValue ? registerFocusedLi : undefined }
218+ aria-label = { LIST_ITEM_LABEL_MAP [ type ] ( unitValue , locale ) }
219+ tabIndex = { tabFocusValue === unitValue ? 0 : - 1 }
220+ role = "option"
221+ aria-selected = { isSelected }
222+ aria-disabled = { disabled }
106223 style = { styles . item }
107224 className = { clsx ( cellPrefixCls , classNames . item , {
108- [ `${ cellPrefixCls } -selected` ] : value === unitValue ,
225+ [ `${ cellPrefixCls } -selected` ] : isSelected ,
109226 [ `${ cellPrefixCls } -disabled` ] : disabled ,
110227 } ) }
111228 onClick = { ( ) => {
@@ -124,6 +241,7 @@ export default function TimeColumn<DateType extends object>(props: TimeUnitColum
124241 onMouseLeave = { ( ) => {
125242 onHover ( null ) ;
126243 } }
244+ onKeyDown = { onCellKeyDown }
127245 data-value = { unitValue }
128246 >
129247 { cellRender
0 commit comments