|
| 1 | +import React, {Component} from 'react'; |
| 2 | +import {View,Text} from 'react-native'; |
| 3 | +import PropTypes from 'prop-types'; |
| 4 | + |
| 5 | +export default class RBadge extends Component { |
| 6 | + |
| 7 | + constructor(props) { |
| 8 | + super(props); |
| 9 | + } |
| 10 | + |
| 11 | + |
| 12 | + static defaultProps = { |
| 13 | + size:10, //只当dot为true时生效 |
| 14 | + text: '', |
| 15 | + dot:false, |
| 16 | + backgroundColor:'#ff0000', |
| 17 | + textColor:'#ffffff', |
| 18 | + textSize:12, |
| 19 | + overflowCount:99, |
| 20 | + textStyle:{}, |
| 21 | + style:{}, |
| 22 | + }; |
| 23 | + |
| 24 | + static propTypes = { |
| 25 | + size:PropTypes.number, |
| 26 | + text: PropTypes.oneOfType([PropTypes.string,PropTypes.number,]), |
| 27 | + dot:PropTypes.bool, |
| 28 | + backgroundColor:PropTypes.string, |
| 29 | + textColor:PropTypes.string, |
| 30 | + textSize:PropTypes.number, |
| 31 | + overflowCount:PropTypes.number, |
| 32 | + textStyle:Text.propTypes.style, |
| 33 | + style:View.propTypes.style |
| 34 | + }; |
| 35 | + |
| 36 | + isInteger=(obj)=> { |
| 37 | + return typeof obj === 'number' && obj%1 === 0; |
| 38 | + } |
| 39 | + |
| 40 | + render() { |
| 41 | + const {size,text,dot,overflowCount,backgroundColor,textColor,textSize,textStyle,style}=this.props; |
| 42 | + |
| 43 | + if(dot){ |
| 44 | + return ( |
| 45 | + <View style={[style,{width:size,height:size,backgroundColor:backgroundColor, |
| 46 | + borderRadius:50}]}> |
| 47 | + </View> |
| 48 | + ); |
| 49 | + }else{ |
| 50 | + if(this.isInteger(text)){ |
| 51 | + if(text<=0){ |
| 52 | + return; |
| 53 | + }else{ |
| 54 | + return (<View style={[style,{backgroundColor:backgroundColor, |
| 55 | + borderRadius:text<10?50:10,alignItems:'center',justifyContent:'center',padding:2}]}> |
| 56 | + <Text style={[textStyle,{color:textColor,fontSize:textSize}]}> |
| 57 | + {text<=overflowCount?text:overflowCount+'+'} |
| 58 | + </Text> |
| 59 | + </View>); |
| 60 | + } |
| 61 | + }else{ |
| 62 | + return (<View style={[style,{backgroundColor:backgroundColor, |
| 63 | + borderRadius:10,alignItems:'center',justifyContent:'center',padding:2},]}> |
| 64 | + <Text style={[textStyle,{color:textColor,fontSize:textSize}]}> |
| 65 | + {text<=overflowCount?text:overflowCount+'+'} |
| 66 | + </Text> |
| 67 | + </View>); |
| 68 | + } |
| 69 | + } |
| 70 | + } |
| 71 | + |
| 72 | +} |
0 commit comments