|
| 1 | +/* |
| 2 | + * Copyright 2025 Adobe. All rights reserved. |
| 3 | + * This file is licensed to you under the Apache License, Version 2.0 (the "License"); |
| 4 | + * you may not use this file except in compliance with the License. You may obtain a copy |
| 5 | + * of the License at http://www.apache.org/licenses/LICENSE-2.0 |
| 6 | + * |
| 7 | + * Unless required by applicable law or agreed to in writing, software distributed under |
| 8 | + * the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR REPRESENTATIONS |
| 9 | + * OF ANY KIND, either express or implied. See the License for the specific language |
| 10 | + * governing permissions and limitations under the License. |
| 11 | + */ |
| 12 | + |
| 13 | + |
| 14 | +import {CalendarDate, CalendarDateTime, parseDate, parseDateTime} from '@internationalized/date'; |
| 15 | +import {DateFieldState, DatePickerState, DateSegmentType} from 'react-stately'; |
| 16 | +import React, {ReactNode} from 'react'; |
| 17 | +import {useVisuallyHidden} from 'react-aria'; |
| 18 | + |
| 19 | +interface AriaHiddenDateInputProps { |
| 20 | + /** |
| 21 | + * Describes the type of autocomplete functionality the input should provide if any. See [MDN](https://developer.mozilla.org/en-US/docs/Web/HTML/Element/input#htmlattrdefautocomplete). |
| 22 | + */ |
| 23 | + autoComplete?: string, |
| 24 | + /** HTML form input name. */ |
| 25 | + name?: string, |
| 26 | + /** Sets the disabled state of the input. */ |
| 27 | + isDisabled?: boolean |
| 28 | +} |
| 29 | + |
| 30 | +interface HiddenDateInputProps extends AriaHiddenDateInputProps { |
| 31 | + /** |
| 32 | + * State for the input. |
| 33 | + */ |
| 34 | + state: DateFieldState | DatePickerState |
| 35 | +} |
| 36 | + |
| 37 | +export interface HiddenDateAria { |
| 38 | + /** Props for the container element. */ |
| 39 | + containerProps: React.HTMLAttributes<HTMLDivElement>, |
| 40 | + /** Props for the hidden input element. */ |
| 41 | + inputProps: React.InputHTMLAttributes<HTMLInputElement> |
| 42 | +} |
| 43 | + |
| 44 | +const dateSegments = ['day', 'month', 'year']; |
| 45 | +const granularityMap = {'hour': 1, 'minute': 2, 'second': 3}; |
| 46 | + |
| 47 | +export function useHiddenDateInput(props: HiddenDateInputProps, state: DateFieldState | DatePickerState) : HiddenDateAria { |
| 48 | + let { |
| 49 | + autoComplete, |
| 50 | + isDisabled, |
| 51 | + name |
| 52 | + } = props; |
| 53 | + let {visuallyHiddenProps} = useVisuallyHidden(); |
| 54 | + |
| 55 | + let inputStep = 60; |
| 56 | + if (state.granularity === 'second') { |
| 57 | + inputStep = 1; |
| 58 | + } else if (state.granularity === 'hour') { |
| 59 | + inputStep = 3600; |
| 60 | + } |
| 61 | + |
| 62 | + let dateValue = state.value == null ? '' : state.value.toString(); |
| 63 | + |
| 64 | + let inputType = state.granularity === 'day' ? 'date' : 'datetime-local'; |
| 65 | + |
| 66 | + let timeSegments = ['hour', 'minute', 'second']; |
| 67 | + // Depending on the granularity, we only want to validate certain time segments |
| 68 | + let end = 0; |
| 69 | + if (timeSegments.includes(state.granularity)) { |
| 70 | + end = granularityMap[state.granularity]; |
| 71 | + timeSegments = timeSegments.slice(0, end); |
| 72 | + } |
| 73 | + |
| 74 | + return { |
| 75 | + containerProps: { |
| 76 | + ...visuallyHiddenProps, |
| 77 | + 'aria-hidden': true, |
| 78 | + // @ts-ignore |
| 79 | + ['data-react-aria-prevent-focus']: true, |
| 80 | + // @ts-ignore |
| 81 | + ['data-a11y-ignore']: 'aria-hidden-focus' |
| 82 | + }, |
| 83 | + inputProps: { |
| 84 | + tabIndex: -1, |
| 85 | + autoComplete, |
| 86 | + disabled: isDisabled, |
| 87 | + type: inputType, |
| 88 | + // We set the form prop to an empty string to prevent the hidden date input's value from being submitted |
| 89 | + form: '', |
| 90 | + name, |
| 91 | + step: inputStep, |
| 92 | + value: dateValue, |
| 93 | + onChange: (e) => { |
| 94 | + let targetString = e.target.value.toString(); |
| 95 | + if (targetString) { |
| 96 | + try { |
| 97 | + let targetValue: CalendarDateTime | CalendarDate = parseDateTime(targetString); |
| 98 | + if (state.granularity === 'day') { |
| 99 | + targetValue = parseDate(targetString); |
| 100 | + } |
| 101 | + // We check to to see if setSegment exists in the state since it only exists in DateFieldState and not DatePickerState. |
| 102 | + // The setValue method has different behavior depending on if it's coming from DateFieldState or DatePickerState. |
| 103 | + // In DateFieldState, setValue firsts checks to make sure that each segment is filled before committing the newValue |
| 104 | + // which is why in the code below we first set each segment to validate it before committing the new value. |
| 105 | + // However, in DatePickerState, since we have to be able to commit values from the Calendar popover, we are also able to |
| 106 | + // set a new value when the field itself is empty. |
| 107 | + if ('setSegment' in state) { |
| 108 | + for (let type in targetValue) { |
| 109 | + if (dateSegments.includes(type)) { |
| 110 | + state.setSegment(type as DateSegmentType, targetValue[type]); |
| 111 | + } |
| 112 | + if (timeSegments.includes(type)) { |
| 113 | + state.setSegment(type as DateSegmentType, targetValue[type]); |
| 114 | + } |
| 115 | + } |
| 116 | + } |
| 117 | + state.setValue(targetValue); |
| 118 | + } catch { |
| 119 | + // ignore |
| 120 | + } |
| 121 | + } |
| 122 | + } |
| 123 | + } |
| 124 | + }; |
| 125 | +} |
| 126 | + |
| 127 | +export function HiddenDateInput(props: HiddenDateInputProps): ReactNode | null { |
| 128 | + let {state} = props; |
| 129 | + let {containerProps, inputProps} = useHiddenDateInput({...props}, state); |
| 130 | + return ( |
| 131 | + <div {...containerProps} data-testid="hidden-dateinput-container"> |
| 132 | + <input {...inputProps} /> |
| 133 | + </div> |
| 134 | + ); |
| 135 | +} |
0 commit comments