-
Notifications
You must be signed in to change notification settings - Fork 2.9k
/
ValueSelectorModal.js
84 lines (71 loc) · 2.6 KB
/
ValueSelectorModal.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
import React, {useState, useEffect} from 'react';
import PropTypes from 'prop-types';
import _ from 'lodash';
import CONST from '../../CONST';
import HeaderWithBackButton from '../HeaderWithBackButton';
import SelectionList from '../SelectionList';
import Modal from '../Modal';
import ScreenWrapper from '../ScreenWrapper';
import styles from '../../styles/styles';
const propTypes = {
/** Whether the modal is visible */
isVisible: PropTypes.bool.isRequired,
/** Current value selected */
currentValue: PropTypes.string,
/** Items to pick from */
items: PropTypes.arrayOf(PropTypes.shape({value: PropTypes.string, label: PropTypes.string})),
/** The selected item */
selectedItem: PropTypes.shape({value: PropTypes.string, label: PropTypes.string}),
/** Label for values */
label: PropTypes.string,
/** Function to call when the user selects a item */
onItemSelected: PropTypes.func,
/** Function to call when the user closes the modal */
onClose: PropTypes.func,
};
const defaultProps = {
currentValue: '',
items: [],
selectedItem: {},
label: '',
onClose: () => {},
onItemSelected: () => {},
};
function ValueSelectorModal({currentValue, items, selectedItem, label, isVisible, onClose, onItemSelected}) {
const [sectionsData, setSectionsData] = useState([]);
useEffect(() => {
const itemsData = _.map(items, (item) => ({value: item.value, keyForList: item.value, text: item.label, isSelected: item === selectedItem}));
setSectionsData(itemsData);
}, [items, selectedItem]);
return (
<Modal
type={CONST.MODAL.MODAL_TYPE.RIGHT_DOCKED}
isVisible={isVisible}
onClose={onClose}
onModalHide={onClose}
hideModalContentWhileAnimating
useNativeDriver
>
<ScreenWrapper
style={[styles.pb0]}
includePaddingTop={false}
includeSafeAreaPaddingBottom={false}
testID="ValueSelectorModal"
>
<HeaderWithBackButton
title={label}
onBackButtonPress={onClose}
/>
<SelectionList
sections={[{data: sectionsData}]}
onSelectRow={onItemSelected}
initiallyFocusedOptionKey={currentValue}
/>
</ScreenWrapper>
</Modal>
);
}
ValueSelectorModal.propTypes = propTypes;
ValueSelectorModal.defaultProps = defaultProps;
ValueSelectorModal.displayName = 'ValueSelectorModal';
export default ValueSelectorModal;