index.js 6.58 KB
/** React Native Percentage Circle
 ** @github  https://github.com/JackPu/react-native-percentage-circle
 ** React Native Version >=0.25
 ** to fixed react native version
 **/

import React, { Component } from 'react';
import { StyleSheet, View, Text, Platform } from 'react-native';

const styles = StyleSheet.create({
    circle: {
        overflow: 'hidden',
        position: 'relative',
        justifyContent: 'center',
        alignItems: 'center',
        backgroundColor: '#e3e3e3',
    },
    leftWrap: {
        overflow: 'hidden',
        position: 'absolute',
        top: 0,
    },
    rightWrap: {
        position: 'absolute',
    },

    loader: {
        position: 'absolute',
        left: 0,
        top: 0,
        borderRadius: 1000,
    },

    innerCircle: {
        overflow: 'hidden',
        position: 'relative',
        justifyContent: 'center',
        alignItems: 'center',
    },
    text: {
        fontSize: 11,
        color: '#888',
    },
});

class PercentageCircle extends Component {
    constructor(props) {
        super(props);
        let percent = this.props.percent;
        let leftTransformerDegree = '0deg';
        let rightTransformerDegree = '0deg';
        if (percent >= 50) {
            rightTransformerDegree = Platform.OS == 'android' ? '180deg' : '180deg';
            leftTransformerDegree =
                Platform.OS == 'android' ? (percent - 50) * 3.6 + 'deg' : (percent - 50) * 3.6 + 'deg';
        } else {
            rightTransformerDegree = Platform.OS == 'android' ? (percent - 50) * 3.6 + 'deg' : percent * 3.6 + 'deg';
            leftTransformerDegree = Platform.OS == 'android' ? '0deg' : '0deg';
        }
        this.state = {
            percent: this.props.percent,
            borderWidth: this.props.borderWidth < 2 || !this.props.borderWidth ? 2 : this.props.borderWidth,
            leftTransformerDegree: leftTransformerDegree,
            rightTransformerDegree: rightTransformerDegree,
            textStyle: this.props.textStyle ? this.props.textStyle : null,
        };
    }

    render() {
        if (this.props.disabled) {
            return (
                <View
                    style={[
                        styles.circle,
                        {
                            width: this.props.radius * 2,
                            height: this.props.radius * 2,
                            borderRadius: this.props.radius,
                        },
                    ]}>
                    <Text style={styles.text}>{this.props.disabledText}</Text>
                </View>
            );
        }
        return (
            <View
                style={[
                    styles.circle,
                    {
                        width: this.props.radius * 2,
                        height: this.props.radius * 2,
                        borderRadius: this.props.radius,
                        backgroundColor: Platform.OS == 'android' ? this.props.bgcolor : null,
                    },
                ]}>
                <View
                    style={[
                        styles.leftWrap,
                        {
                            width: this.props.radius,
                            height: this.props.radius * 2,
                            left: 0,
                        },
                    ]}>
                    <View
                        style={[
                            styles.loader,
                            {
                                left: this.props.radius,
                                width: this.props.radius,
                                height: this.props.radius * 2,
                                borderTopLeftRadius: 0,
                                borderBottomLeftRadius: 0,
                                backgroundColor: this.props.color,
                                transform: [
                                    { translateX: -this.props.radius / 2 },
                                    { rotate: this.state.leftTransformerDegree },
                                    { translateX: this.props.radius / 2 },
                                ],
                            },
                        ]}></View>
                </View>
                <View
                    style={[
                        styles.leftWrap,
                        {
                            left: this.props.radius,
                            width: this.props.radius,
                            height: this.props.radius * 2,
                        },
                    ]}>
                    <View
                        style={[
                            styles.loader,
                            {
                                left: -this.props.radius,
                                width: this.props.radius,
                                height: this.props.radius * 2,
                                borderTopRightRadius: 0,
                                borderBottomRightRadius: 0,
                                backgroundColor:
                                    Platform.OS == 'android'
                                        ? this.props.percent < 50
                                            ? this.props.bgcolor
                                            : this.props.color
                                        : this.props.color,
                                transform: [
                                    { translateX: this.props.radius / 2 },
                                    { rotate: this.state.rightTransformerDegree },
                                    { translateX: -this.props.radius / 2 },
                                ],
                            },
                        ]}></View>
                </View>
                <View
                    style={[
                        styles.innerCircle,
                        {
                            width: (this.props.radius - this.state.borderWidth) * 2,
                            height: (this.props.radius - this.state.borderWidth) * 2,
                            borderRadius: this.props.radius - this.state.borderWidth,
                            backgroundColor: this.props.innerColor,
                        },
                    ]}>
                    {this.props.children ? (
                        this.props.children
                    ) : (
                        <Text style={[styles.text, this.state.textStyle]}>{this.props.percent}%</Text>
                    )}
                </View>
            </View>
        );
    }
}

// set some attributes default value
PercentageCircle.defaultProps = {
    bgcolor: '#e3e3e3',
    innerColor: '#fff',
};

module.exports = PercentageCircle;