-
Notifications
You must be signed in to change notification settings - Fork 3k
/
Copy pathYearPickerModal.js
96 lines (85 loc) · 3.4 KB
/
YearPickerModal.js
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
import PropTypes from 'prop-types';
import React, {useEffect, useMemo, useState} from 'react';
import _ from 'underscore';
import HeaderWithBackButton from '@components/HeaderWithBackButton';
import Modal from '@components/Modal';
import ScreenWrapper from '@components/ScreenWrapper';
import SelectionList from '@components/SelectionList';
import {radioListItemPropTypes} from '@components/SelectionList/selectionListPropTypes';
import useLocalize from '@hooks/useLocalize';
import styles from '@styles/styles';
import CONST from '@src/CONST';
const propTypes = {
/** Whether the modal is visible */
isVisible: PropTypes.bool.isRequired,
/** The list of years to render */
years: PropTypes.arrayOf(PropTypes.shape(radioListItemPropTypes.item)).isRequired,
/** Currently selected year */
currentYear: PropTypes.number,
/** Function to call when the user selects a year */
onYearChange: PropTypes.func,
/** Function to call when the user closes the year picker */
onClose: PropTypes.func,
};
const defaultProps = {
currentYear: new Date().getFullYear(),
onYearChange: () => {},
onClose: () => {},
};
function YearPickerModal(props) {
const {translate} = useLocalize();
const [searchText, setSearchText] = useState('');
const {sections, headerMessage} = useMemo(() => {
const yearsList = searchText === '' ? props.years : _.filter(props.years, (year) => year.text.includes(searchText));
return {
headerMessage: !yearsList.length ? translate('common.noResultsFound') : '',
sections: [{data: yearsList, indexOffset: 0}],
};
}, [props.years, searchText, translate]);
useEffect(() => {
if (props.isVisible) {
return;
}
setSearchText('');
}, [props.isVisible]);
return (
<Modal
type={CONST.MODAL.MODAL_TYPE.RIGHT_DOCKED}
isVisible={props.isVisible}
onClose={props.onClose}
onModalHide={props.onClose}
hideModalContentWhileAnimating
useNativeDriver
>
<ScreenWrapper
style={[styles.pb0]}
includePaddingTop={false}
includeSafeAreaPaddingBottom={false}
testID={YearPickerModal.displayName}
>
<HeaderWithBackButton
title={translate('yearPickerPage.year')}
onBackButtonPress={props.onClose}
/>
<SelectionList
shouldDelayFocus
textInputLabel={translate('yearPickerPage.selectYear')}
textInputValue={searchText}
textInputMaxLength={4}
onChangeText={(text) => setSearchText(text.replace(CONST.REGEX.NON_NUMERIC, '').trim())}
keyboardType={CONST.KEYBOARD_TYPE.NUMBER_PAD}
headerMessage={headerMessage}
sections={sections}
onSelectRow={(option) => props.onYearChange(option.value)}
initiallyFocusedOptionKey={props.currentYear.toString()}
showScrollIndicator
shouldStopPropagation
/>
</ScreenWrapper>
</Modal>
);
}
YearPickerModal.propTypes = propTypes;
YearPickerModal.defaultProps = defaultProps;
YearPickerModal.displayName = 'YearPickerModal';
export default YearPickerModal;