Bubble.js 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. import React, { Children } from 'react'
  2. import { StyleSheet, View, Text } from 'react-native'
  3. import PropTypes from 'prop-types'
  4. export default class Bubble extends React.Component {
  5. constructor(props) {
  6. super(props)
  7. this.state = {}
  8. }
  9. static propTypes = {
  10. size: PropTypes.number,
  11. bubbleColor: PropTypes.string,
  12. value: PropTypes.number,
  13. textColor: PropTypes.string,
  14. style: PropTypes.object,
  15. fontSize: PropTypes.number,
  16. }
  17. static defaultProps = {
  18. size: 14,
  19. bubbleColor: '#E14C46',
  20. textColor: '#FFF',
  21. fontSize: 12,
  22. }
  23. render() {
  24. let width,
  25. value = this.props.value
  26. if (
  27. (this.props.value && this.props.value.toString().indexOf('-') != -1) ||
  28. this.props.value >= 10
  29. ) {
  30. width = this.props.size * 1.7
  31. if (this.props.value >= 100) {
  32. value = 99 + '+'
  33. width = this.props.size * 1.9
  34. }
  35. } else {
  36. width = this.props.size
  37. }
  38. if (value != 0) {
  39. return (
  40. <View
  41. style={[
  42. this.props.style,
  43. {
  44. width: width,
  45. height: this.props.size,
  46. borderRadius: this.props.size,
  47. backgroundColor: this.props.bubbleColor,
  48. alignItems: 'center',
  49. flexDirection: 'column',
  50. justifyContent: 'center',
  51. },
  52. ]}
  53. >
  54. <Text
  55. style={{
  56. color: this.props.textColor,
  57. fontSize: this.props.fontSize,
  58. }}
  59. >
  60. {value}
  61. </Text>
  62. </View>
  63. )
  64. } else {
  65. return null
  66. }
  67. }
  68. }