Skip to content

Instantly share code, notes, and snippets.

@ericnograles
Last active January 2, 2017 23:39
Show Gist options
  • Save ericnograles/abb01f7113b57200dc38f3cfef2df4d4 to your computer and use it in GitHub Desktop.
Save ericnograles/abb01f7113b57200dc38f3cfef2df4d4 to your computer and use it in GitHub Desktop.
A Generic React Native component that allows for a button with an ActivityIndicator when the attached loading prop is true
import React from 'react';
import { View, Text, ActivityIndicator, TouchableOpacity } from 'react-native';
export default class ButtonWithActivityIndicator extends React.Component {
render() {
const { buttonStyle, textStyle, label, onPress, activityIndicatorSize, activityIndicatorColor, loading } = this.props;
return (
<TouchableOpacity style={buttonStyle} onPress={() => {
if (!loading) {
return onPress();
}
}}>
<View style={{flexDirection: 'row'}}>
<View style={{flex: 1}}></View>
<Text style={textStyle}>{label}</Text>
<View style={{flex: 1, flexDirection: 'row', justifyContent: 'flex-end'}}>
<ActivityIndicator size={activityIndicatorSize} animating={loading} color={activityIndicatorColor || '#ffffff'} />
</View>
</View>
</TouchableOpacity>
)
}
}
@ericnograles
Copy link
Author

Sample use case:

 <ButtonWithActivityIndicator
                  buttonStyle={{
                    width: window.width * .8,
                    justifyContent: 'center',
                    alignItems: 'center',
                    padding: 10,
                    marginTop: 20,
                    marginBottom: window.height * 0.03,
                    backgroundColor: 'rgb(243, 138, 0)'
                  }}
                  textStyle={{
                    color: '#ffffff',
                    fontFamily: 'Corporative Sans',
                    fontSize: 16,
                    fontWeight: '700'
                  }}
                  label="save"
                  activityIndicatorSize="small"
                  onPress={this.save}
                  loading={this.state.loading} />

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment