-
Notifications
You must be signed in to change notification settings - Fork 3k
/
Copy pathButtonWithDropdownMenu.js
141 lines (129 loc) · 5.17 KB
/
ButtonWithDropdownMenu.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
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
import React, {useState, useRef, useEffect} from 'react';
import PropTypes from 'prop-types';
import {View} from 'react-native';
import _ from 'underscore';
import useWindowDimensions from '../hooks/useWindowDimensions';
import styles from '../styles/styles';
import Button from './Button';
import PopoverMenu from './PopoverMenu';
import Icon from './Icon';
import * as Expensicons from './Icon/Expensicons';
import themeColors from '../styles/themes/default';
import CONST from '../CONST';
const propTypes = {
/** Text to display for the menu header */
menuHeaderText: PropTypes.string,
/** Callback to execute when the main button is pressed */
onPress: PropTypes.func.isRequired,
/** Whether we should show a loading state for the main button */
isLoading: PropTypes.bool,
/** Should the confirmation button be disabled? */
isDisabled: PropTypes.bool,
/** Additional styles to add to the component */
style: PropTypes.oneOfType([PropTypes.arrayOf(PropTypes.object), PropTypes.object]),
/** Menu options to display */
/** e.g. [{text: 'Pay with Expensify', icon: Wallet}, {text: 'PayPal', icon: PayPal}] */
options: PropTypes.arrayOf(
PropTypes.shape({
value: PropTypes.string.isRequired,
text: PropTypes.string.isRequired,
icon: PropTypes.elementType,
iconWidth: PropTypes.number,
iconHeight: PropTypes.number,
iconDescription: PropTypes.string,
}),
).isRequired,
};
const defaultProps = {
isLoading: false,
isDisabled: false,
menuHeaderText: '',
style: [],
};
function ButtonWithDropdownMenu(props) {
const [selectedItemIndex, setSelectedItemIndex] = useState(0);
const [isMenuVisible, setIsMenuVisible] = useState(false);
const [popoverAnchorPosition, setPopoverAnchorPosition] = useState(null);
const {windowWidth, windowHeight} = useWindowDimensions();
const caretButton = useRef(null);
const selectedItem = props.options[selectedItemIndex];
useEffect(() => {
if (!caretButton.current) {
return;
}
if (!isMenuVisible) {
return;
}
caretButton.current.measureInWindow((x, y, w, h) => {
setPopoverAnchorPosition({
horizontal: x + w,
vertical: y + h,
});
});
}, [windowWidth, windowHeight, isMenuVisible]);
return (
<View>
{props.options.length > 1 ? (
<View style={[styles.flexRow, styles.justifyContentBetween, styles.alignItemsCenter, ...props.style]}>
<Button
success
onPress={(event) => props.onPress(event, selectedItem.value)}
text={selectedItem.text}
isDisabled={props.isDisabled}
isLoading={props.isLoading}
shouldRemoveRightBorderRadius
style={[styles.flex1, styles.pr0]}
pressOnEnter
/>
<View style={styles.buttonDivider} />
<Button
ref={caretButton}
success
isDisabled={props.isDisabled}
style={[styles.pl0]}
onPress={() => setIsMenuVisible(true)}
shouldRemoveLeftBorderRadius
>
<Icon
src={Expensicons.DownArrow}
fill={themeColors.textLight}
/>
</Button>
</View>
) : (
<Button
success
isDisabled={props.isDisabled}
style={[styles.w100, ...props.style]}
isLoading={props.isLoading}
text={selectedItem.text}
onPress={(event) => props.onPress(event, props.options[0].value)}
pressOnEnter
/>
)}
{props.options.length > 1 && !_.isEmpty(popoverAnchorPosition) && (
<PopoverMenu
isVisible={isMenuVisible}
onClose={() => setIsMenuVisible(false)}
onItemSelected={() => setIsMenuVisible(false)}
anchorPosition={popoverAnchorPosition}
anchorAlignment={{
horizontal: CONST.MODAL.ANCHOR_ORIGIN_HORIZONTAL.RIGHT,
vertical: CONST.MODAL.ANCHOR_ORIGIN_VERTICAL.BOTTOM,
}}
headerText={props.menuHeaderText}
menuItems={_.map(props.options, (item, index) => ({
...item,
onSelected: () => {
setSelectedItemIndex(index);
},
}))}
/>
)}
</View>
);
}
ButtonWithDropdownMenu.propTypes = propTypes;
ButtonWithDropdownMenu.defaultProps = defaultProps;
ButtonWithDropdownMenu.displayName = 'ButtonWithDropdownMenu';
export default React.memo(ButtonWithDropdownMenu);