-
Notifications
You must be signed in to change notification settings - Fork 11
/
Button.js
313 lines (289 loc) · 9.15 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
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
/*
* A smart button for react-native apps
* https://github.com/react-native-component/react-native-smart-button/
* Released under the MIT license
* Copyright (c) 2016 react-native-component <[email protected]>
*/
import React, {
PureComponent,
} from 'react'
import PropTypes from 'prop-types'
import {
View,
ViewPropTypes,
Text,
TouchableHighlight,
TouchableOpacity,
StyleSheet,
} from 'react-native'
import constants, {touchableTypes, SYSTEM_OPACITY, NOOP, EMPTY_OBJECT,} from './constants'
import Blur from './Blur'
const styles = StyleSheet.create({
text: {
color: '#007aff',
fontFamily: '.HelveticaNeueInterface-MediumP4',
fontSize: 17,
fontWeight: 'bold',
textAlign: 'center',
},
disabledText: {
color: '#dcdcdc',
},
touchContainer: {
flex: 1,
justifyContent: 'center',
alignItems: 'center',
},
contentContainer: {
flexDirection: 'row',
justifyContent: 'center',
alignItems: 'center',
},
})
export default class Button extends PureComponent {
static constants = constants
static defaultProps = {
touchableType: touchableTypes.fade,
activeOpacity: SYSTEM_OPACITY,
loading: false,
disabled: false,
onPress: NOOP,
onPressIn: NOOP,
onPressOut: NOOP,
onLayout: NOOP,
renderLoadingComponent: NOOP,
}
static propTypes = {
testID: PropTypes.string,
touchableType: PropTypes.oneOf([
touchableTypes.highlight,
touchableTypes.fadeContent,
touchableTypes.blur,
touchableTypes.fade,
]),
activeOpacity: PropTypes.number,
underlayColor: PropTypes.string,
style: ViewPropTypes.style,
disabledStyle: ViewPropTypes.style,
textStyle: Text.propTypes.style,
disabledTextStyle: Text.propTypes.style,
onPressIn: PropTypes.func,
onPressOut: PropTypes.func,
onPress: PropTypes.func,
onLayout: PropTypes.func,
disabled: PropTypes.bool,
loading: PropTypes.bool,
renderLoadingComponent: PropTypes.func,
shadowOpacity: PropTypes.number,
shadowColor: PropTypes.string,
}
constructor(props) {
super(props)
this.state = {
pressIn: false,
disabled: props.disabled,
loading: props.loading,
}
this._boxDimension = EMPTY_OBJECT
}
render() {
const touchableProps = EMPTY_OBJECT
const {touchableType,} = this.props
touchableProps.onPress = this.props.onPress
touchableProps.onPressIn = this.props.onPressIn
touchableProps.onPressOut = this.props.onPressOut
let renderView = null
switch (touchableType) {
case touchableTypes.highlight:
renderView = this._renderHighlightView(touchableProps)
break
case touchableTypes.fadeContent:
renderView = this._renderFadeContentView(touchableProps)
break
case touchableTypes.blur: // experimental feature
renderView = this._renderBlurView(touchableProps)
break
case touchableTypes.fade:
default:
renderView = this._renderFadeView(touchableProps)
break
}
return renderView
}
componentWillReceiveProps(nextProps) {
const { disabled: nextDisabled, loading: nextLoading, } = nextProps
const { disabled, loading, } = this.props
const newState = {}
if (nextDisabled !== disabled) {
newState.disabled = nextDisabled
} else if (nextLoading !== loading) {
newState.loading = nextLoading
}
if (Object.keys(newState).length > 0) {
this.setState(newState)
}
}
_renderHighlightView(touchableProps) {
const reassignedProps = {
...touchableProps,
activeOpacity: 1,
underlayColor: this.props.underlayColor,
}
return (
<TouchableHighlight
onLayout={this.props.onLayout}
disabled={this.state.disabled || this.state.loading}
style={[
this.props.style,
this.state.disabled ? this.props.disabledStyle : null,
]}
{...reassignedProps}
testID={this.props.testID}
>
{this._renderChildren()}
</TouchableHighlight>
)
}
_renderFadeContentView(touchableProps) {
const reassignedProps = {
...touchableProps,
activeOpacity: this.props.activeOpacity,
}
return (
<View
onLayout={this.props.onLayout}
style={[
this.props.style,
this.state.disabled ? this.props.disabledStyle : null,
]}>
<TouchableOpacity
disabled={this.state.disabled || this.state.loading}
style={[styles.touchContainer,]}
{...reassignedProps}
testID={this.props.testID}
>
{this._renderChildren()}
</TouchableOpacity>
</View>
)
}
_renderBlurView(touchableProps) {
const reassignedProps = {
...touchableProps,
activeOpacity: this.props.activeOpacity,
onPressIn: this._onBlurPressIn,
onPressOut: this._onBlurPressOut,
}
return (
<View
onLayout={this._handleButtonLayout}
style={[
{
position: 'relative',
overflow: 'hidden',
},
this.props.style,
this.state.disabled ? this.props.disabledStyle : null,
]}>
<TouchableOpacity
disabled={this.state.disabled || this.state.loading}
style={styles.touchContainer}
{...reassignedProps}
testID={this.props.testID}
>
{this._renderChildren()}
</TouchableOpacity>
{this._renderBlur()}
</View>
)
}
_renderFadeView(touchableProps) {
const reassignedProps = {
...touchableProps,
activeOpacity: this.props.activeOpacity,
}
return (
<TouchableOpacity
onLayout={this.props.onLayout}
disabled={this.state.disabled || this.state.loading}
style={[
this.props.style,
this.state.disabled ? this.props.disabledStyle : null,
]}
{...reassignedProps}
testID={this.props.testID}
>
{this._renderChildren()}
</TouchableOpacity>
)
}
_renderBlur() {
return (
<Blur
show={this.state.pressIn}
// textDimension={this.textDimension}
containerDimension={this._boxDimension}
shadowOpacity={this.props.shadowOpacity}
shadowColor={this.props.shadowColor}
/>
)
}
_renderChildren() {
let childView = null
if (this.state.loading) {
childView = (
<View style={styles.contentContainer}>
{this.props.renderLoadingComponent()}
</View>
)
} else {
const children = React.Children.map(this.props.children, (child) => {
if (!React.isValidElement(child)) {
return (
<Text
// onLayout={this._onTextLayout}
style={[
styles.text,
this.props.textStyle,
this.state.disabled ? (this.props.disabledTextStyle || styles.disabledText) : null,
]}>
{child}
</Text>
)
}
return child
})
childView = (
<View style={styles.contentContainer}>
{children}
</View>
)
}
return childView
}
_onBlurPressIn = (e) => {
this.setState({
pressIn: true,
})
this.props.onPressIn(e)
}
_onBlurPressOut = (e) => {
this.setState({
pressIn: false,
})
this.props.onPressOut(e)
}
_handleButtonLayout = (e) => {
this._boxDimension = {
width: e.nativeEvent.layout.width,
height: e.nativeEvent.layout.height,
}
this.props.onLayout(e)
}
// _onTextLayout = (e) => {
// this.textDimension = {
// width: e.nativeEvent.layout.width,
// height: e.nativeEvent.layout.height,
// }
// }
}