-
Notifications
You must be signed in to change notification settings - Fork 3k
/
Copy pathButtonWithMenu.js
104 lines (92 loc) · 3.56 KB
/
ButtonWithMenu.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
import React, {PureComponent} from 'react';
import PropTypes from 'prop-types';
import {View} from 'react-native';
import _ from 'underscore';
import styles from '../styles/styles';
import Button from './Button';
import ButtonWithDropdown from './ButtonWithDropdown';
import PopoverMenu from './PopoverMenu';
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,
/** Menu options to display */
/** e.g. [{text: 'Pay with Expensify', icon: Wallet}, {text: 'PayPal', icon: PayPal}, {text: 'Venmo', icon: Venmo}] */
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: '',
};
class ButtonWithMenu extends PureComponent {
constructor(props) {
super(props);
this.state = {
selectedItem: props.options[0],
isMenuVisible: false,
};
}
setMenuVisibility(isMenuVisible) {
this.setState({isMenuVisible});
}
render() {
const selectedItemText = this.state.selectedItem.text;
return (
<View>
{this.props.options.length > 1 ? (
<ButtonWithDropdown
buttonText={selectedItemText}
isLoading={this.props.isLoading}
onButtonPress={event => this.props.onPress(event, this.state.selectedItem.value)}
onDropdownPress={() => {
this.setMenuVisibility(true);
}}
/>
) : (
<Button
success
isDisabled={this.props.isDisabled}
style={[styles.w100]}
isLoading={this.props.isLoading}
text={selectedItemText}
onPress={event => this.props.onPress(event, this.props.options[0].value)}
pressOnEnter
/>
)}
{this.props.options.length > 1 && (
<PopoverMenu
isVisible={this.state.isMenuVisible}
onClose={() => this.setMenuVisibility(false)}
onItemSelected={() => this.setMenuVisibility(false)}
anchorPosition={styles.createMenuPositionRightSidepane}
animationIn="fadeInUp"
animationOut="fadeOutDown"
headerText={this.props.menuHeaderText}
menuItems={_.map(this.props.options, item => ({
...item,
onSelected: () => {
this.setState({selectedItem: item});
},
}))}
/>
)}
</View>
);
}
}
ButtonWithMenu.propTypes = propTypes;
ButtonWithMenu.defaultProps = defaultProps;
export default ButtonWithMenu;