Skip to content

Commit 7042f4a

Browse files
author
alvaromb
committed
Added optional isLoading prop
1 parent f8b9ea4 commit 7042f4a

File tree

4 files changed

+55
-8
lines changed

4 files changed

+55
-8
lines changed

Button.js

Lines changed: 34 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,25 @@
1-
import React, { Component, TouchableOpacity, Text, StyleSheet, PropTypes } from 'react-native'
1+
import React, { Component, View, TouchableOpacity, Text, StyleSheet, PropTypes, ActivityIndicatorIOS, Platform } from 'react-native'
22
import StyleSheetPropType from 'react-native/Libraries/StyleSheet/StyleSheetPropType'
33
import TextStylePropTypes from 'react-native/Libraries/Text/TextStylePropTypes'
44

55
class Button extends Component {
6+
_renderInnerText () {
7+
let children = this.props.children
8+
if (this.props.isLoading) {
9+
if (Platform.OS === 'ios') {
10+
return <ActivityIndicatorIOS style={styles.spinner}
11+
animating={true} size='small' color='black' />
12+
} else {
13+
children = 'Loading...'
14+
}
15+
}
16+
return (
17+
<Text style={[styles.textButton, this.props.textStyle]}>
18+
{children}
19+
</Text>
20+
)
21+
}
22+
623
render () {
724
// Extract TouchableOpacity props
825
let touchableProps = {
@@ -11,12 +28,17 @@ class Button extends Component {
1128
onPressOut: this.props.onPressOut,
1229
onLongPress: this.props.onLongPress
1330
}
31+
if (this.props.isLoading) {
32+
return (
33+
<View style={[styles.button, this.props.style, styles.opacity]}>
34+
{this._renderInnerText()}
35+
</View>
36+
)
37+
}
1438
return (
1539
<TouchableOpacity {...touchableProps}
1640
style={[styles.button, this.props.style]}>
17-
<Text style={[styles.textButton, this.props.textStyle]}>
18-
{this.props.children}
19-
</Text>
41+
{this._renderInnerText()}
2042
</TouchableOpacity>
2143
)
2244
}
@@ -25,7 +47,8 @@ class Button extends Component {
2547
Button.propTypes = {
2648
...TouchableOpacity.propTypes,
2749
textStyle: StyleSheetPropType(TextStylePropTypes),
28-
children: PropTypes.string.isRequired
50+
children: PropTypes.string.isRequired,
51+
isLoading: PropTypes.bool,
2952
}
3053

3154
let styles = StyleSheet.create({
@@ -42,6 +65,12 @@ let styles = StyleSheet.create({
4265
fontSize: 18,
4366
alignSelf: 'center',
4467
},
68+
spinner: {
69+
alignSelf: 'center',
70+
},
71+
opacity: {
72+
opacity: 0.3,
73+
},
4574
})
4675

4776
export default Button

Example/button/index.ios.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,14 @@ var button = React.createClass({
7373
}}>
7474
Hello
7575
</Button>
76+
<Button
77+
isLoading={true}
78+
style={styles.buttonStyle7} textStyle={styles.textStyle6}
79+
onPress={() => {
80+
console.log('world!')
81+
}}>
82+
Hello
83+
</Button>
7684
</View>
7785
);
7886
}

README.md

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,11 @@ import Button from 'apsl-react-native-button'
2222

2323
## Usage
2424

25-
Provide ``TouchableOpacity``' props to the component (including ``style``), ``textStyle``'s ``StyleSheet`` to customize the inner text and a children node to render. Check the included example for more options.
25+
Provide ``TouchableOpacity``' props to the component (including ``style``),
26+
``textStyle``'s ``StyleSheet`` to customize the inner text and a children node
27+
to render. You can also provide the ``isLoading`` prop that will dim the button
28+
and disable it to prevent accidental taps. Check the included example for more
29+
options.
2630

2731
```javascript
2832
<Button style={{backgroundColor: 'red'}} textStyle={{fontSize: 18}}>

package.json

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "apsl-react-native-button",
3-
"version": "2.0.0",
3+
"version": "2.1.0",
44
"description": "React Native button component with rounded corners.",
55
"main": "Button.js",
66
"scripts": {
@@ -12,12 +12,18 @@
1212
},
1313
"keywords": [
1414
"react-native",
15+
"ios",
16+
"react-component",
17+
"react",
1518
"button"
1619
],
1720
"author": "Alvaro Medina Ballester",
1821
"license": "MIT",
1922
"bugs": {
2023
"url": "https://github.com/APSL/react-native-button/issues"
2124
},
22-
"homepage": "https://github.com/APSL/react-native-button#readme"
25+
"homepage": "https://github.com/APSL/react-native-button#readme",
26+
"dependencies": {
27+
"react-native": ">=0.11.0"
28+
}
2329
}

0 commit comments

Comments
 (0)