-
Notifications
You must be signed in to change notification settings - Fork 2.9k
/
index.js
342 lines (293 loc) · 12.2 KB
/
index.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
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
import React, {Component} from 'react';
import {ActivityIndicator, View} from 'react-native';
import PropTypes from 'prop-types';
import styles from '../../styles/styles';
import themeColors from '../../styles/themes/default';
import Text from '../Text';
import KeyboardShortcut from '../../libs/KeyboardShortcut';
import Icon from '../Icon';
import CONST from '../../CONST';
import * as StyleUtils from '../../styles/StyleUtils';
import HapticFeedback from '../../libs/HapticFeedback';
import withNavigationFallback from '../withNavigationFallback';
import compose from '../../libs/compose';
import * as Expensicons from '../Icon/Expensicons';
import withNavigationFocus from '../withNavigationFocus';
import validateSubmitShortcut from './validateSubmitShortcut';
import PressableWithFeedback from '../Pressable/PressableWithFeedback';
import refPropTypes from '../refPropTypes';
const propTypes = {
/** Should the press event bubble across multiple instances when Enter key triggers it. */
allowBubble: PropTypes.bool,
/** The text for the button label */
text: PropTypes.string,
/** Boolean whether to display the right icon */
shouldShowRightIcon: PropTypes.bool,
/** The icon asset to display to the left of the text */
icon: PropTypes.func,
/** The icon asset to display to the right of the text */
iconRight: PropTypes.func,
/** The fill color to pass into the icon. */
iconFill: PropTypes.string,
/** Any additional styles to pass to the left icon container. */
// eslint-disable-next-line react/forbid-prop-types
iconStyles: PropTypes.arrayOf(PropTypes.object),
/** Any additional styles to pass to the right icon container. */
// eslint-disable-next-line react/forbid-prop-types
iconRightStyles: PropTypes.arrayOf(PropTypes.object),
/** Small sized button */
small: PropTypes.bool,
/** Large sized button */
large: PropTypes.bool,
/** medium sized button */
medium: 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,
/** Callback that is called when mousedown is triggered. */
onMouseDown: PropTypes.func,
/** Call the onPress function when Enter key is pressed */
pressOnEnter: PropTypes.bool,
/** The priority to assign the enter key event listener. 0 is the highest priority. */
enterKeyEventListenerPriority: PropTypes.number,
/** 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 */
// eslint-disable-next-line react/forbid-prop-types
innerStyles: PropTypes.arrayOf(PropTypes.object),
/** Additional text styles */
// eslint-disable-next-line react/forbid-prop-types
textStyles: PropTypes.arrayOf(PropTypes.object),
/** Whether we should use the default hover style */
shouldUseDefaultHover: PropTypes.bool,
/** Whether we should use the success theme color */
success: PropTypes.bool,
/** Whether we should use the danger theme color */
danger: PropTypes.bool,
/** Children to replace all inner contents of button */
children: PropTypes.node,
/** Should we remove the right border radius top + bottom? */
shouldRemoveRightBorderRadius: PropTypes.bool,
/** Should we remove the left border radius top + bottom? */
shouldRemoveLeftBorderRadius: PropTypes.bool,
/** Should enable the haptic feedback? */
shouldEnableHapticFeedback: PropTypes.bool,
/** Whether Button is on active screen */
isFocused: PropTypes.bool.isRequired,
/** Id to use for this button */
nativeID: PropTypes.string,
/** Accessibility label for the component */
accessibilityLabel: PropTypes.string,
/** A ref to forward the button */
forwardedRef: refPropTypes,
};
const defaultProps = {
allowBubble: false,
text: '',
shouldShowRightIcon: false,
icon: null,
iconRight: Expensicons.ArrowRight,
iconFill: themeColors.textLight,
iconStyles: [],
iconRightStyles: [],
isLoading: false,
isDisabled: false,
small: false,
large: false,
medium: false,
onPress: () => {},
onLongPress: () => {},
onPressIn: () => {},
onPressOut: () => {},
onMouseDown: undefined,
pressOnEnter: false,
enterKeyEventListenerPriority: 0,
style: [],
innerStyles: [],
textStyles: [],
shouldUseDefaultHover: true,
success: false,
danger: false,
children: null,
shouldRemoveRightBorderRadius: false,
shouldRemoveLeftBorderRadius: false,
shouldEnableHapticFeedback: false,
nativeID: '',
accessibilityLabel: '',
forwardedRef: undefined,
};
class Button extends Component {
constructor(props) {
super(props);
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,
(e) => {
if (!validateSubmitShortcut(this.props.isFocused, this.props.isDisabled, this.props.isLoading, e)) {
return;
}
this.props.onPress();
},
shortcutConfig.descriptionKey,
shortcutConfig.modifiers,
true,
this.props.allowBubble,
this.props.enterKeyEventListenerPriority,
false,
);
}
componentWillUnmount() {
// Cleanup event listeners
if (!this.unsubscribe) {
return;
}
this.unsubscribe();
}
renderContent() {
if (this.props.children) {
return this.props.children;
}
const textComponent = (
<Text
numberOfLines={1}
selectable={false}
style={[
this.props.isLoading && styles.opacity0,
styles.pointerEventsNone,
styles.buttonText,
this.props.small && styles.buttonSmallText,
this.props.medium && styles.buttonMediumText,
this.props.large && styles.buttonLargeText,
this.props.success && styles.buttonSuccessText,
this.props.danger && styles.buttonDangerText,
this.props.icon && styles.textAlignLeft,
...this.props.textStyles,
]}
dataSet={{[CONST.SELECTION_SCRAPER_HIDDEN_ELEMENT]: true}}
>
{this.props.text}
</Text>
);
if (this.props.icon || this.props.shouldShowRightIcon) {
return (
<View style={[styles.justifyContentBetween, styles.flexRow]}>
<View style={[styles.alignItemsCenter, styles.flexRow, styles.flexShrink1]}>
{this.props.icon && (
<View style={[styles.mr1, ...this.props.iconStyles]}>
<Icon
src={this.props.icon}
fill={this.props.iconFill}
small={this.props.small}
/>
</View>
)}
{textComponent}
</View>
{this.props.shouldShowRightIcon && (
<View style={[styles.justifyContentCenter, styles.ml1, ...this.props.iconRightStyles]}>
<Icon
src={this.props.iconRight}
fill={this.props.iconFill}
small={this.props.small}
/>
</View>
)}
</View>
);
}
return textComponent;
}
render() {
return (
<PressableWithFeedback
ref={this.props.forwardedRef}
onPress={(e) => {
if (e && e.type === 'click') {
e.currentTarget.blur();
}
if (this.props.shouldEnableHapticFeedback) {
HapticFeedback.press();
}
return this.props.onPress(e);
}}
onLongPress={(e) => {
if (this.props.shouldEnableHapticFeedback) {
HapticFeedback.longPress();
}
this.props.onLongPress(e);
}}
onPressIn={this.props.onPressIn}
onPressOut={this.props.onPressOut}
onMouseDown={this.props.onMouseDown}
disabled={this.props.isLoading || this.props.isDisabled}
wrapperStyle={[
this.props.isDisabled ? {...styles.cursorDisabled, ...styles.noSelect} : {},
styles.buttonContainer,
this.props.shouldRemoveRightBorderRadius ? styles.noRightBorderRadius : undefined,
this.props.shouldRemoveLeftBorderRadius ? styles.noLeftBorderRadius : undefined,
...StyleUtils.parseStyleAsArray(this.props.style),
]}
style={[
styles.button,
this.props.small ? styles.buttonSmall : undefined,
this.props.medium ? styles.buttonMedium : 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 || this.props.danger) ? styles.buttonOpacityDisabled : undefined,
this.props.isDisabled && !this.props.danger && !this.props.success ? styles.buttonDisabled : undefined,
this.props.shouldRemoveRightBorderRadius ? styles.noRightBorderRadius : undefined,
this.props.shouldRemoveLeftBorderRadius ? styles.noLeftBorderRadius : undefined,
...this.props.innerStyles,
]}
hoverStyle={[
this.props.shouldUseDefaultHover && !this.props.isDisabled ? styles.buttonDefaultHovered : undefined,
this.props.success && !this.props.isDisabled ? styles.buttonSuccessHovered : undefined,
this.props.danger && !this.props.isDisabled ? styles.buttonDangerHovered : undefined,
]}
nativeID={this.props.nativeID}
accessibilityLabel={this.props.accessibilityLabel}
accessibilityRole={CONST.ACCESSIBILITY_ROLE.BUTTON}
hoverDimmingValue={1}
>
{this.renderContent()}
{this.props.isLoading && (
<ActivityIndicator
color={this.props.success || this.props.danger ? themeColors.textLight : themeColors.text}
style={[styles.pAbsolute, styles.l0, styles.r0]}
/>
)}
</PressableWithFeedback>
);
}
}
Button.propTypes = propTypes;
Button.defaultProps = defaultProps;
export default compose(
withNavigationFallback,
withNavigationFocus,
)(
React.forwardRef((props, ref) => (
<Button
// eslint-disable-next-line react/jsx-props-no-spreading
{...props}
forwardedRef={ref}
/>
)),
);