forked from Expensify/App
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.tsx
More file actions
184 lines (165 loc) · 6.97 KB
/
index.tsx
File metadata and controls
184 lines (165 loc) · 6.97 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
import {format, setYear} from 'date-fns';
import React, {useCallback, useEffect, useMemo, useRef, useState} from 'react';
// eslint-disable-next-line no-restricted-imports
import {InteractionManager, View} from 'react-native';
import TextInput from '@components/TextInput';
import type {BaseTextInputRef} from '@components/TextInput/BaseTextInput/types';
import useAccessibilityAnnouncement from '@hooks/useAccessibilityAnnouncement';
import useAutoFocusInput from '@hooks/useAutoFocusInput';
import {useMemoizedLazyExpensifyIcons} from '@hooks/useLazyAsset';
import useLocalize from '@hooks/useLocalize';
import useThemeStyles from '@hooks/useThemeStyles';
import useWindowDimensions from '@hooks/useWindowDimensions';
import {setDraftValues} from '@userActions/FormActions';
import CONST from '@src/CONST';
import DatePickerModal from './DatePickerModal';
import type {DateInputWithPickerProps} from './types';
const PADDING_MODAL_DATE_PICKER = 8;
function DatePicker({
defaultValue,
disabled,
errorText,
inputID,
label,
minDate = setYear(new Date(), CONST.CALENDAR_PICKER.MIN_YEAR),
maxDate = setYear(new Date(), CONST.CALENDAR_PICKER.MAX_YEAR),
onInputChange,
onTouched = () => {},
placeholder,
value,
shouldSaveDraft = false,
formID,
autoFocus = false,
shouldHideClearButton = false,
autoComplete = 'off',
forwardedFSClass,
}: DateInputWithPickerProps) {
const icons = useMemoizedLazyExpensifyIcons(['Calendar']);
const styles = useThemeStyles();
const {windowHeight, windowWidth} = useWindowDimensions();
const {translate} = useLocalize();
const [isModalVisible, setIsModalVisible] = useState(false);
const announcementMessage = label ? `${label}, ${translate('common.calendarOpened')}` : translate('common.calendarOpened');
useAccessibilityAnnouncement(announcementMessage, isModalVisible, {shouldAnnounceOnNative: true, shouldAnnounceOnWeb: true});
const [selectedDate, setSelectedDate] = useState(() => value ?? defaultValue ?? '');
const [popoverPosition, setPopoverPosition] = useState({horizontal: 0, vertical: 0});
const textInputRef = useRef<BaseTextInputRef | null>(null);
const anchorRef = useRef<View>(null);
const [isInverted, setIsInverted] = useState(false);
const {inputCallbackRef: autoFocusCallbackRef, cancelAutoFocus} = useAutoFocusInput();
const autoFocusCallbackRefRef = useRef(autoFocusCallbackRef);
autoFocusCallbackRefRef.current = autoFocusCallbackRef;
useEffect(() => {
if (shouldSaveDraft && formID) {
setDraftValues(formID, {[inputID]: selectedDate});
}
if (selectedDate === value) {
return;
}
if (value === undefined) {
return;
}
setSelectedDate(value);
}, [formID, inputID, selectedDate, shouldSaveDraft, value]);
const calculatePopoverPosition = useCallback(() => {
anchorRef.current?.measureInWindow((x, y, width, height) => {
const wouldExceedBottom = y + CONST.POPOVER_DATE_MAX_HEIGHT + PADDING_MODAL_DATE_PICKER > windowHeight;
setIsInverted(wouldExceedBottom);
setPopoverPosition({
horizontal: x + width,
vertical: y + (wouldExceedBottom ? 0 : height + PADDING_MODAL_DATE_PICKER),
});
});
}, [windowHeight]);
const showDatePickerModal = useCallback(() => {
cancelAutoFocus();
// Blur the input before showing the modal, so the focus won't be returned after the modal is closed
textInputRef.current?.blur();
calculatePopoverPosition();
setIsModalVisible(true);
}, [calculatePopoverPosition, cancelAutoFocus]);
const closeDatePicker = useCallback(() => {
setIsModalVisible(false);
}, []);
const handleDateSelected = (newDate: string) => {
onTouched?.();
onInputChange?.(newDate);
setSelectedDate(newDate);
closeDatePicker();
};
const handleClear = () => {
onTouched?.();
onInputChange?.('');
setSelectedDate('');
};
useEffect(() => {
InteractionManager.runAfterInteractions(() => {
calculatePopoverPosition();
});
}, [calculatePopoverPosition, windowWidth]);
// Combined ref: updates textInputRef (needed for blur() in showDatePickerModal) and connects
// autoFocusCallbackRef only when autoFocus=true so useAutoFocusInput's useFocusEffect cleanup
// can cancel any pending focus task when the screen starts closing.
const combinedTextInputRef = useCallback(
(ref: BaseTextInputRef | null) => {
textInputRef.current = ref;
if (autoFocus) {
(autoFocusCallbackRefRef.current as unknown as (ref: BaseTextInputRef | null) => void)(ref);
}
},
// autoFocusCallbackRefRef is a stable ref — its identity never changes, so it's not a dep
[autoFocus],
);
const getValidDateForCalendar = useMemo(() => {
if (!selectedDate) {
// eslint-disable-next-line @typescript-eslint/prefer-nullish-coalescing
return defaultValue || format(new Date(), CONST.DATE.FNS_FORMAT_STRING);
}
return selectedDate;
}, [selectedDate, defaultValue]);
return (
<>
<View
ref={anchorRef}
style={styles.mv2}
>
<TextInput
ref={combinedTextInputRef}
inputID={inputID}
forceActiveLabel
icon={selectedDate ? null : icons.Calendar}
iconContainerStyle={styles.pr0}
label={label}
accessibilityLabel={label}
role={CONST.ROLE.PRESENTATION}
value={selectedDate}
placeholder={placeholder ?? translate('common.dateFormat')}
errorText={errorText}
inputStyle={styles.pointerEventsNone}
disabled={disabled}
onFocus={showDatePickerModal}
textInputContainerStyles={isModalVisible ? styles.borderColorFocus : {}}
shouldHideClearButton={shouldHideClearButton}
onClearInput={handleClear}
forwardedFSClass={forwardedFSClass}
autoComplete={autoComplete}
disableKeyboard
/>
</View>
<DatePickerModal
inputID={inputID}
minDate={minDate}
maxDate={maxDate}
value={getValidDateForCalendar}
onSelected={handleDateSelected}
isVisible={isModalVisible}
onClose={closeDatePicker}
anchorPosition={popoverPosition}
shouldPositionFromTop={!isInverted}
forwardedFSClass={forwardedFSClass}
shouldCloseWhenBrowserNavigationChanged
/>
</>
);
}
export default DatePicker;