MCPcopy Index your code
hub / github.com/adobe/react-spectrum / useDateSegment

Function useDateSegment

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

Source from the content-addressed store, hash-verified

42 * the value by typing or using the arrow keys to increment and decrement.
43 */
44export 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: () => {

Callers 2

DateField.tsxFile · 0.85
EditableSegmentFunction · 0.85

Calls 15

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

Tested by

no test coverage detected