( segment: DateSegment, state: DateFieldState, ref: RefObject<HTMLElement | null> )
| 42 | * the value by typing or using the arrow keys to increment and decrement. |
| 43 | */ |
| 44 | export function useDateSegment( |
| 45 | segment: DateSegment, |
| 46 | state: DateFieldState, |
| 47 | ref: RefObject<HTMLElement | null> |
| 48 | ): DateSegmentAria { |
| 49 | let enteredKeys = useRef(''); |
| 50 | let {locale, direction} = useLocale(); |
| 51 | let displayNames = useDisplayNames(); |
| 52 | let {ariaLabel, ariaLabelledBy, ariaDescribedBy, focusManager} = hookData.get(state)!; |
| 53 | |
| 54 | let textValue = segment.isPlaceholder ? '' : segment.text; |
| 55 | let options = useMemo(() => state.dateFormatter.resolvedOptions(), [state.dateFormatter]); |
| 56 | let monthDateFormatter = useDateFormatter({month: 'long', timeZone: options.timeZone}); |
| 57 | let hourDateFormatter = useDateFormatter({ |
| 58 | hour: 'numeric', |
| 59 | hour12: options.hour12, |
| 60 | timeZone: options.timeZone |
| 61 | }); |
| 62 | |
| 63 | if (segment.type === 'month' && !segment.isPlaceholder) { |
| 64 | let monthTextValue = monthDateFormatter.format(state.dateValue); |
| 65 | textValue = monthTextValue !== textValue ? `${textValue} – ${monthTextValue}` : monthTextValue; |
| 66 | } else if (segment.type === 'hour' && !segment.isPlaceholder) { |
| 67 | textValue = hourDateFormatter.format(state.dateValue); |
| 68 | } |
| 69 | |
| 70 | let {spinButtonProps} = useSpinButton({ |
| 71 | // The ARIA spec says aria-valuenow is optional if there's no value, but aXe seems to require it. |
| 72 | // This doesn't seem to have any negative effects with real AT since we also use aria-valuetext. |
| 73 | // https://github.com/dequelabs/axe-core/issues/3505 |
| 74 | value: segment.value ?? undefined, |
| 75 | textValue, |
| 76 | minValue: segment.minValue, |
| 77 | maxValue: segment.maxValue, |
| 78 | isDisabled: state.isDisabled, |
| 79 | isReadOnly: state.isReadOnly || !segment.isEditable, |
| 80 | isRequired: state.isRequired, |
| 81 | onIncrement: () => { |
| 82 | enteredKeys.current = ''; |
| 83 | state.increment(segment.type); |
| 84 | }, |
| 85 | onDecrement: () => { |
| 86 | enteredKeys.current = ''; |
| 87 | state.decrement(segment.type); |
| 88 | }, |
| 89 | onIncrementPage: () => { |
| 90 | enteredKeys.current = ''; |
| 91 | state.incrementPage(segment.type); |
| 92 | }, |
| 93 | onDecrementPage: () => { |
| 94 | enteredKeys.current = ''; |
| 95 | state.decrementPage(segment.type); |
| 96 | }, |
| 97 | onIncrementToMax: () => { |
| 98 | enteredKeys.current = ''; |
| 99 | state.incrementToMax(segment.type); |
| 100 | }, |
| 101 | onDecrementToMin: () => { |
no test coverage detected