Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feat#61 custom loading indicator #63

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 24 additions & 15 deletions Button.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ const Button = React.createClass({
isLoading: PropTypes.bool,
isDisabled: PropTypes.bool,
activityIndicatorColor: PropTypes.string,
activityIndicatorWithText: PropTypes.bool,
delayLongPress: PropTypes.number,
delayPressIn: PropTypes.number,
delayPressOut: PropTypes.number,
Expand All @@ -41,8 +42,18 @@ const Button = React.createClass({
isAndroid: (Platform.OS === 'android'),
},

shouldComponentUpdate: function (nextProps, nextState) {
if (!isEqual(nextProps, this.props)) {
return true;
}
return false;
},

_renderChildren: function() {
let childElements = [];
if (this.props.isLoading && this.props.activityIndicatorWithText) {
childElements.push(this._renderActivityIndicator());
}
React.Children.forEach(this.props.children, (item) => {
if (typeof item === 'string' || typeof item === 'number') {
const element = (
Expand All @@ -61,23 +72,20 @@ const Button = React.createClass({
return (childElements);
},

shouldComponentUpdate: function (nextProps, nextState) {
if (!isEqual(nextProps, this.props)) {
return true;
}
return false;
_renderActivityIndicator: function () {
return (
<ActivityIndicator
animating={true}
size='small'
style={styles.spinner}
color={this.props.activityIndicatorColor || 'black'}
/>
)
},

_renderInnerText: function () {
if (this.props.isLoading) {
return (
<ActivityIndicator
animating={true}
size='small'
style={styles.spinner}
color={this.props.activityIndicatorColor || 'black'}
/>
);
if (this.props.isLoading && !this.props.activityIndicatorWithText) {
return this._renderActivityIndicator();
}
return this._renderChildren();
},
Expand Down Expand Up @@ -136,13 +144,14 @@ const styles = StyleSheet.create({
justifyContent: 'center',
},
textButton: {
flex: 1,
fontSize: 18,
textAlign: 'center',
backgroundColor: 'transparent',
},
spinner: {
alignSelf: 'center',
marginRight: 6,
marginLeft: 6
},
opacity: {
opacity: 0.5,
Expand Down
9 changes: 9 additions & 0 deletions Example/button/Example.js
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,15 @@ class Example extends React.Component {
}}>
Hello
</Button>
<Button
isLoading={true}
activityIndicatorWithText={true}
style={styles.buttonStyle7} textStyle={styles.textStyle6}
onPress={() => {
console.log('world!')
}}>
Hello
</Button>
<Button
disabledStyle={styles.buttonStyle8}
isDisabled={true}
Expand Down
9 changes: 9 additions & 0 deletions __tests__/Button.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,15 @@ describe('Button', () => {
const tree = component.toJSON()
expect(tree).toMatchSnapshot()
})
test('Renders loading with text', () => {
const component = renderer.create(
<Button isLoading={true} activityIndicatorWithText={true}>
Loading button with text
</Button>
)
const tree = component.toJSON()
expect(tree).toMatchSnapshot()
})
test('Renders with a inner View', () => {
const component = renderer.create(
<Button>
Expand Down
59 changes: 54 additions & 5 deletions __tests__/__snapshots__/Button.test.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@ exports[`Button Renders 1`] = `
Array [
Object {
"backgroundColor": "transparent",
"flex": 1,
"fontSize": 18,
"textAlign": "center"
},
Expand Down Expand Up @@ -74,7 +73,6 @@ exports[`Button Renders disabled 1`] = `
Array [
Object {
"backgroundColor": "transparent",
"flex": 1,
"fontSize": 18,
"textAlign": "center"
},
Expand Down Expand Up @@ -113,12 +111,65 @@ exports[`Button Renders loading 1`] = `
size="small"
style={
Object {
"alignSelf": "center"
"alignSelf": "center",
"marginLeft": 6,
"marginRight": 6
}
} />
</View>
`;

exports[`Button Renders loading with text 1`] = `
<View
style={
Array [
Object {
"alignItems": "center",
"alignSelf": "stretch",
"borderRadius": 8,
"borderWidth": 1,
"flexDirection": "row",
"height": 44,
"justifyContent": "center",
"marginBottom": 10
},
undefined,
Object {
"opacity": 0.5
}
]
}>
<ActivityIndicator
animating={true}
color="black"
hidesWhenStopped={true}
size="small"
style={
Object {
"alignSelf": "center",
"marginLeft": 6,
"marginRight": 6
}
} />
<Text
accessible={true}
allowFontScaling={true}
ellipsizeMode="tail"
style={
Array [
Object {
"backgroundColor": "transparent",
"fontSize": 18,
"textAlign": "center"
},
undefined
]
}>
Loading button with text
</Text>
</View>
`;

exports[`Button Renders with a inner View 1`] = `
<View
accessibilityComponentType={undefined}
Expand Down Expand Up @@ -187,7 +238,6 @@ exports[`Button Should contain children 1`] = `
Array [
Object {
"backgroundColor": "transparent",
"flex": 1,
"fontSize": 18,
"textAlign": "center"
},
Expand Down Expand Up @@ -235,7 +285,6 @@ exports[`Button Should react to the onPress event 1`] = `
Array [
Object {
"backgroundColor": "transparent",
"flex": 1,
"fontSize": 18,
"textAlign": "center"
},
Expand Down
6 changes: 3 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -49,9 +49,9 @@
"eslint-plugin-react-native": "^1.1.0-beta",
"jest": "^15.1.1",
"jest-react-native": "^15.0.0",
"react": "^15.3.2",
"react-native": "^0.34.0",
"react-test-renderer": "^15.3.2",
"react": "15.3.2",
"react-native": "0.34.0",
"react-test-renderer": "15.3.2",
"whatwg-fetch": "^1.0.0"
}
}