MCPcopy Create free account
hub / github.com/adobe/react-spectrum / useDateSegment

Function useDateSegment

packages/react-aria/src/datepicker/useDateSegment.ts:45–427  ·  view source on GitHub ↗
(
  segment: DateSegment,
  state: DateFieldState,
  ref: RefObject<HTMLElement | null>
)

Source from the content-addressed store, hash-verified

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

Callers 4

DateField.tsxFile · 0.85
EditableSegmentFunction · 0.85
DateSegmentFunction · 0.85
DateSegmentFunction · 0.85

Calls 15

useLocaleFunction · 0.90
useDisplayNamesFunction · 0.90
useDateFormatterFunction · 0.90
useSpinButtonFunction · 0.90
useKeyboardFunction · 0.90
useFilterFunction · 0.90
toCalendarFunction · 0.90
useEventFunction · 0.90
nodeContainsFunction · 0.90
getActiveElementFunction · 0.90
useIdFunction · 0.90
useLabelsFunction · 0.90

Tested by

no test coverage detected