-
Notifications
You must be signed in to change notification settings - Fork 3k
/
Copy pathButton.js
216 lines (182 loc) · 7.19 KB
/
Button.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
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
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
import _ from 'underscore';
import React, {Component} from 'react';
import {Pressable, ActivityIndicator, View} from 'react-native';
import PropTypes from 'prop-types';
import styles from '../styles/styles';
import themeColors from '../styles/themes/default';
import OpacityView from './OpacityView';
import Text from './Text';
import KeyboardShortcut from '../libs/KeyboardShortcut';
import Icon from './Icon';
import CONST from '../CONST';
const propTypes = {
/** The text for the button label */
text: PropTypes.string,
/** The icon asset to display to the left of the text */
icon: PropTypes.func,
/** Small sized button */
small: PropTypes.bool,
/** Large sized button */
large: PropTypes.bool,
/** Indicates whether the button should be disabled and in the loading state */
isLoading: PropTypes.bool,
/** Indicates whether the button should be disabled */
isDisabled: PropTypes.bool,
/** A function that is called when the button is clicked on */
onPress: PropTypes.func,
/** A function that is called when the button is long pressed */
onLongPress: PropTypes.func,
/** A function that is called when the button is pressed */
onPressIn: PropTypes.func,
/** A function that is called when the button is released */
onPressOut: PropTypes.func,
/** Call the onPress function when Enter key is pressed */
pressOnEnter: PropTypes.bool,
/** Additional styles to add after local styles. Applied to Pressable portion of button */
style: PropTypes.oneOfType([
PropTypes.arrayOf(PropTypes.object),
PropTypes.object,
]),
/** Additional button styles. Specific to the OpacityView of button */
innerStyles: PropTypes.arrayOf(PropTypes.object),
/** Additional text styles */
textStyles: PropTypes.arrayOf(PropTypes.object),
/** Whether we should use the success theme color */
success: PropTypes.bool,
/** Whether we should use the danger theme color */
danger: PropTypes.bool,
/** Optional content component to replace all inner contents of button */
ContentComponent: PropTypes.func,
/** Should we remove the right border radius top + bottom? */
shouldRemoveRightBorderRadius: PropTypes.bool,
/** Should we remove the left border radius top + bottom? */
shouldRemoveLeftBorderRadius: PropTypes.bool,
};
const defaultProps = {
text: '',
icon: null,
isLoading: false,
isDisabled: false,
small: false,
large: false,
onPress: () => {},
onLongPress: () => {},
onPressIn: () => {},
onPressOut: () => {},
pressOnEnter: false,
style: [],
innerStyles: [],
textStyles: [],
success: false,
danger: false,
ContentComponent: undefined,
shouldRemoveRightBorderRadius: false,
shouldRemoveLeftBorderRadius: false,
};
class Button extends Component {
constructor(props) {
super(props);
this.additionalStyles = _.isArray(this.props.style) ? this.props.style : [this.props.style];
this.renderContent = this.renderContent.bind(this);
}
componentDidMount() {
if (!this.props.pressOnEnter) {
return;
}
const shortcutConfig = CONST.KEYBOARD_SHORTCUTS.ENTER;
// Setup and attach keypress handler for pressing the button with Enter key
this.unsubscribe = KeyboardShortcut.subscribe(shortcutConfig.shortcutKey, () => {
if (this.props.isDisabled || this.props.isLoading) {
return;
}
this.props.onPress();
}, shortcutConfig.descriptionKey, shortcutConfig.modifiers, true);
}
componentWillUnmount() {
// Cleanup event listeners
if (!this.unsubscribe) {
return;
}
this.unsubscribe();
}
renderContent() {
const ContentComponent = this.props.ContentComponent;
if (ContentComponent) {
return <ContentComponent />;
}
if (this.props.isLoading) {
return <ActivityIndicator color={themeColors.textReversed} />;
}
const textComponent = (
<Text
selectable={false}
style={[
styles.buttonText,
this.props.small && styles.buttonSmallText,
this.props.large && styles.buttonLargeText,
this.props.success && styles.buttonSuccessText,
this.props.danger && styles.buttonDangerText,
...this.props.textStyles,
]}
>
{this.props.text}
</Text>
);
if (this.props.icon) {
return (
<View style={[styles.flexRow, styles.alignItemsCenter]}>
<View style={styles.mr1}>
<Icon
src={this.props.icon}
fill={themeColors.heading}
small={this.props.small}
/>
</View>
{textComponent}
</View>
);
}
return textComponent;
}
render() {
return (
<Pressable
onPress={this.props.onPress}
onLongPress={this.props.onLongPress}
onPressIn={this.props.onPressIn}
onPressOut={this.props.onPressOut}
disabled={this.props.isLoading || this.props.isDisabled}
style={[
this.props.isDisabled ? styles.cursorDisabled : {},
...this.additionalStyles,
]}
>
{({pressed, hovered}) => (
<OpacityView
shouldDim={pressed}
style={[
styles.button,
this.props.small ? styles.buttonSmall : undefined,
this.props.large ? styles.buttonLarge : undefined,
this.props.success ? styles.buttonSuccess : undefined,
this.props.danger ? styles.buttonDanger : undefined,
(this.props.isDisabled && this.props.success) ? styles.buttonSuccessDisabled : undefined,
(this.props.isDisabled && this.props.danger) ? styles.buttonDangerDisabled : undefined,
(this.props.isDisabled && !this.props.danger && !this.props.success) ? styles.buttonDisable : undefined,
(this.props.success && hovered) ? styles.buttonSuccessHovered : undefined,
(this.props.danger && hovered) ? styles.buttonDangerHovered : undefined,
this.props.shouldRemoveRightBorderRadius ? styles.noRightBorderRadius : undefined,
this.props.shouldRemoveLeftBorderRadius ? styles.noLeftBorderRadius : undefined,
...this.props.innerStyles,
]}
>
{this.renderContent()}
</OpacityView>
)}
</Pressable>
);
}
}
Button.propTypes = propTypes;
Button.defaultProps = defaultProps;
export default Button;