Skip to content

Commit f0b4d6d

Browse files
author
alvaromb
committed
Added disabled state and refactored code. Extended README.
Updated example
1 parent 441eabe commit f0b4d6d

File tree

4 files changed

+34
-39
lines changed

4 files changed

+34
-39
lines changed

Button.js

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

5-
class Button extends Component {
6-
constructor(props) {
7-
super(props)
8-
9-
this.state = {
10-
isLoading: (this.props.isLoading === true ? true : false),
11-
isDisabled: (this.props.isDisabled === true ? true : false),
12-
}
13-
}
14-
5+
class Button extends React.Component {
156
_renderInnerText () {
16-
let children = this.props.children
17-
if (this.state.isLoading) {
7+
if (this.props.isLoading) {
188
if (Platform.OS !== 'android') {
199
return (
2010
<ActivityIndicatorIOS
2111
animating={true}
22-
size="small"
12+
size='small'
2313
style={styles.spinner}
24-
color='black'
14+
color={this.props.activityIndicatorColor || 'black'}
2515
/>
2616
)
2717
} else {
2818
return (
29-
<ProgressBarAndroid
19+
<ProgressBarAndroid
3020
style={[{
3121
height: 20,
3222
}, styles.spinner]}
33-
styleAttr="Inverse"
23+
styleAttr='Inverse'
3424
/>
3525
)
3626
}
3727
}
3828
return (
3929
<Text style={[styles.textButton, this.props.textStyle]}>
40-
{children}
30+
{this.props.children}
4131
</Text>
4232
)
4333
}
@@ -50,8 +40,8 @@ class Button extends Component {
5040
onPressOut: this.props.onPressOut,
5141
onLongPress: this.props.onLongPress
5242
}
53-
54-
if (this.state.isDisabled === true || this.state.isLoading === true) {
43+
44+
if (this.props.isDisabled === true || this.props.isLoading === true) {
5545
return (
5646
<View style={[styles.button, this.props.style, styles.opacity]}>
5747
{this._renderInnerText()}
@@ -63,21 +53,9 @@ class Button extends Component {
6353
style={[styles.button, this.props.style]}>
6454
{this._renderInnerText()}
6555
</TouchableOpacity>
66-
)
56+
)
6757
}
6858
}
69-
70-
setIsLoading(val = false) {
71-
this.setState({
72-
isLoading: Boolean(val)
73-
})
74-
}
75-
76-
setIsDisabled(val = false) {
77-
this.setState({
78-
isDisabled: Boolean(val)
79-
})
80-
}
8159
}
8260

8361
Button.propTypes = {
@@ -86,9 +64,10 @@ Button.propTypes = {
8664
children: PropTypes.string.isRequired,
8765
isLoading: PropTypes.bool,
8866
isDisabled: PropTypes.bool,
67+
activityIndicatorColor: PropTypes.string,
8968
}
9069

91-
let styles = StyleSheet.create({
70+
const styles = StyleSheet.create({
9271
button: {
9372
height: 44,
9473
flexDirection: 'row',
@@ -106,7 +85,7 @@ let styles = StyleSheet.create({
10685
alignSelf: 'center',
10786
},
10887
opacity: {
109-
opacity: 0.3,
88+
opacity: 0.5,
11089
},
11190
})
11291

Example/button/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
"start": "node_modules/react-native/packager/packager.sh"
77
},
88
"dependencies": {
9-
"apsl-react-native-button": "^2.1.0",
9+
"apsl-react-native-button": "^2.2.0",
1010
"react-native": "^0.12.0"
1111
}
1212
}

README.md

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,15 +25,30 @@ import Button from 'apsl-react-native-button'
2525
Provide ``TouchableOpacity``' props to the component (including ``style``),
2626
``textStyle``'s ``StyleSheet`` to customize the inner text and a children node
2727
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.
28+
and disable it to prevent accidental taps.
3029

3130
```javascript
3231
<Button style={{backgroundColor: 'red'}} textStyle={{fontSize: 18}}>
3332
Hello!
3433
</Button>
3534
```
3635

36+
## API
37+
38+
| Prop | Type | Description |
39+
-----------------------------
40+
| ``onPress`` | ``func`` | Function to execute when the ``onPress`` event is triggered. |
41+
| ``onPressIn`` | ``func`` | Function to execute when the ``onPressIn`` event is triggered. |
42+
| ``onPressOut`` | ``func`` | Function to execute when the ``onPressOut`` event is triggered. |
43+
| ``onLongPress`` | ``func`` | Function to execute when the ``onLongPress`` event is triggered. |
44+
| ``textStyle`` | ``StyleSheetPropType(TextStylePropTypes)`` | The StyleSheet to apply to the inner button text. |
45+
| ``children`` | ``string`` | The ``string`` to render as the text button. |
46+
| ``isLoading`` | ``bool`` | Renders an inactive state dimmed button with a spinner if ``true``. |
47+
| ``isDisabled`` | ``bool`` | Renders an inactive state dimmed button if ``true``. |
48+
| ``activityIndicatorColor`` | ``string`` | **iOS only**. Sets the button of the ``ActivityIndicatorIOS`` in the loading state. |
49+
50+
Check the included example for more options.
51+
3752
## License
3853

3954
MIT.

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "apsl-react-native-button",
3-
"version": "2.1.0",
3+
"version": "2.2.0",
44
"description": "React Native button component with rounded corners.",
55
"main": "Button.js",
66
"scripts": {
@@ -13,6 +13,7 @@
1313
"keywords": [
1414
"react-native",
1515
"ios",
16+
"android",
1617
"react-component",
1718
"react",
1819
"button"

0 commit comments

Comments
 (0)