import React, { useState, useEffect } from 'react'; import { StyleSheet, View, Text, StyleProp, TextStyle } from 'react-native'; import PropTypes from 'prop-types'; interface PercentageCircleProps { color?: string; bgcolor?: string; innerColor?: string; radius: number; percent: number; borderWidth?: number; textStyle?: StyleProp; disabled?: boolean; disabledText?: string; children?: React.ReactNode; } 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', }, }); const PercentageCircle: React.FC = ({ color, bgcolor = '#e3e3e3', innerColor = '#fff', radius, percent, borderWidth = 2, textStyle, disabled = false, disabledText, children, }) => { const [leftTransformerDegree, setLeftTransformerDegree] = useState('0deg'); const [rightTransformerDegree, setRightTransformerDegree] = useState('0deg'); useEffect(() => { let leftDegree = '0deg'; let rightDegree = '0deg'; if (percent >= 50) { rightDegree = '180deg'; leftDegree = `${(percent - 50) * 3.6}deg`; } else { rightDegree = `${percent * 3.6}deg`; } setLeftTransformerDegree(leftDegree); setRightTransformerDegree(rightDegree); }, [percent]); if (disabled) { return ( {disabledText} ); } return ( {children ? ( children ) : ( {percent}% )} ); }; PercentageCircle.propTypes = { color: PropTypes.string, bgcolor: PropTypes.string, innerColor: PropTypes.string, radius: PropTypes.number.isRequired, percent: PropTypes.number.isRequired, borderWidth: PropTypes.number, textStyle: PropTypes.object, disabled: PropTypes.bool, disabledText: PropTypes.string, children: PropTypes.node, }; export default PercentageCircle;