|
1 | 1 | import {format, setYear} from 'date-fns'; |
2 | 2 | import React, {forwardRef, useCallback, useEffect, useMemo, useRef, useState} from 'react'; |
| 3 | +import type {ForwardedRef} from 'react'; |
3 | 4 | import {InteractionManager, View} from 'react-native'; |
4 | 5 | import * as Expensicons from '@components/Icon/Expensicons'; |
5 | 6 | import TextInput from '@components/TextInput'; |
| 7 | +import type {BaseTextInputRef} from '@components/TextInput/BaseTextInput/types'; |
6 | 8 | import useLocalize from '@hooks/useLocalize'; |
7 | 9 | import useThemeStyles from '@hooks/useThemeStyles'; |
8 | 10 | import useWindowDimensions from '@hooks/useWindowDimensions'; |
| 11 | +import mergeRefs from '@libs/mergeRefs'; |
9 | 12 | import {setDraftValues} from '@userActions/FormActions'; |
10 | 13 | import CONST from '@src/CONST'; |
11 | 14 | import DatePickerModal from './DatePickerModal'; |
12 | 15 | import type {DateInputWithPickerProps} from './types'; |
13 | 16 |
|
14 | 17 | const PADDING_MODAL_DATE_PICKER = 8; |
15 | 18 |
|
16 | | -function DatePicker({ |
17 | | - defaultValue, |
18 | | - disabled, |
19 | | - errorText, |
20 | | - inputID, |
21 | | - label, |
22 | | - minDate = setYear(new Date(), CONST.CALENDAR_PICKER.MIN_YEAR), |
23 | | - maxDate = setYear(new Date(), CONST.CALENDAR_PICKER.MAX_YEAR), |
24 | | - onInputChange, |
25 | | - onTouched = () => {}, |
26 | | - placeholder, |
27 | | - value, |
28 | | - shouldSaveDraft = false, |
29 | | - formID, |
30 | | - autoFocus = false, |
31 | | - shouldHideClearButton = false, |
32 | | -}: DateInputWithPickerProps) { |
| 19 | +function DatePicker( |
| 20 | + { |
| 21 | + defaultValue, |
| 22 | + disabled, |
| 23 | + errorText, |
| 24 | + inputID, |
| 25 | + label, |
| 26 | + minDate = setYear(new Date(), CONST.CALENDAR_PICKER.MIN_YEAR), |
| 27 | + maxDate = setYear(new Date(), CONST.CALENDAR_PICKER.MAX_YEAR), |
| 28 | + onInputChange, |
| 29 | + onTouched = () => {}, |
| 30 | + placeholder, |
| 31 | + value, |
| 32 | + shouldSaveDraft = false, |
| 33 | + formID, |
| 34 | + autoFocus = false, |
| 35 | + shouldHideClearButton = false, |
| 36 | + }: DateInputWithPickerProps, |
| 37 | + ref: ForwardedRef<BaseTextInputRef>, |
| 38 | +) { |
33 | 39 | const styles = useThemeStyles(); |
34 | 40 | const {windowHeight, windowWidth} = useWindowDimensions(); |
35 | 41 | const {translate} = useLocalize(); |
36 | 42 | const [isModalVisible, setIsModalVisible] = useState(false); |
37 | 43 | // eslint-disable-next-line @typescript-eslint/prefer-nullish-coalescing |
38 | 44 | const [selectedDate, setSelectedDate] = useState(value || defaultValue || undefined); |
39 | 45 | const [popoverPosition, setPopoverPosition] = useState({horizontal: 0, vertical: 0}); |
40 | | - const textInputRef = useRef<HTMLFormElement | null>(null); |
| 46 | + const textInputRef = useRef<BaseTextInputRef>(null); |
41 | 47 | const anchorRef = useRef<View>(null); |
42 | 48 | const [isInverted, setIsInverted] = useState(false); |
43 | 49 | const isAutoFocused = useRef(false); |
@@ -119,7 +125,7 @@ function DatePicker({ |
119 | 125 | style={styles.mv2} |
120 | 126 | > |
121 | 127 | <TextInput |
122 | | - ref={textInputRef} |
| 128 | + ref={mergeRefs(ref, textInputRef)} |
123 | 129 | inputID={inputID} |
124 | 130 | forceActiveLabel |
125 | 131 | icon={selectedDate ? null : Expensicons.Calendar} |
|
0 commit comments