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

Function useDateField

packages/react-aria/src/datepicker/useDateField.ts:89–229  ·  view source on GitHub ↗
(
  props: AriaDateFieldOptions<T>,
  state: DateFieldState,
  ref: RefObject<Element | null>
)

Source from the content-addressed store, hash-verified

87 * Each part of a date value is displayed in an individually editable segment.
88 */
89export function useDateField<T extends DateValue>(
90 props: AriaDateFieldOptions<T>,
91 state: DateFieldState,
92 ref: RefObject<Element | null>
93): DateFieldAria {
94 let {isInvalid, validationErrors, validationDetails} = state.displayValidation;
95 let {labelProps, fieldProps, descriptionProps, errorMessageProps} = useField({
96 ...props,
97 labelElementType: 'span',
98 isInvalid,
99 errorMessage: props.errorMessage || validationErrors
100 });
101
102 let valueOnFocus = useRef<DateValue | null>(null);
103 let {focusWithinProps} = useFocusWithin({
104 ...props,
105 onFocusWithin(e) {
106 valueOnFocus.current = state.value;
107 props.onFocus?.(e);
108 },
109 onBlurWithin: e => {
110 state.confirmPlaceholder();
111 if (state.value !== valueOnFocus.current) {
112 state.commitValidation();
113 }
114 props.onBlur?.(e);
115 },
116 onFocusWithinChange: props.onFocusChange
117 });
118
119 let stringFormatter = useLocalizedStringFormatter(intlMessages, '@react-aria/datepicker');
120 let message =
121 state.maxGranularity === 'hour' ? 'selectedTimeDescription' : 'selectedDateDescription';
122 let field = state.maxGranularity === 'hour' ? 'time' : 'date';
123 let description = state.value
124 ? stringFormatter.format(message, {[field]: state.formatValue({month: 'long'})})
125 : '';
126 let descProps = useDescription(description);
127
128 // If within a date picker or date range picker, the date field will have role="presentation" and an aria-describedby
129 // will be passed in that references the value (e.g. entire range). Otherwise, add the field's value description.
130 let describedBy =
131 props[roleSymbol] === 'presentation'
132 ? fieldProps['aria-describedby']
133 : [descProps['aria-describedby'], fieldProps['aria-describedby']].filter(Boolean).join(' ') ||
134 undefined;
135 let propsFocusManager = props[focusManagerSymbol];
136 let focusManager = useMemo(
137 () => propsFocusManager || createFocusManager(ref),
138 [propsFocusManager, ref]
139 );
140 let groupProps = useDatePickerGroup(state, ref, props[roleSymbol] === 'presentation');
141
142 // Pass labels and other information to segments.
143 hookData.set(state, {
144 ariaLabel: props['aria-label'],
145 ariaLabelledBy:
146 [labelProps.id, props['aria-labelledby']].filter(Boolean).join(' ') || undefined,

Callers 6

DateField.tsxFile · 0.90
DatePickerFieldFunction · 0.90
DateField.tsxFile · 0.90
DateFieldFunction · 0.90
useTimeFieldFunction · 0.85

Calls 15

useFieldFunction · 0.90
useFocusWithinFunction · 0.90
useDescriptionFunction · 0.90
createFocusManagerFunction · 0.90
useDatePickerGroupFunction · 0.90
mergePropsFunction · 0.90
useFormResetFunction · 0.90
useFormValidationFunction · 0.90
filterDOMPropsFunction · 0.90
confirmPlaceholderMethod · 0.80
commitValidationMethod · 0.80

Tested by

no test coverage detected