You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

ExpandedLabel.native.js 3.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. // @flow
  2. import React, { Component } from 'react';
  3. import { Animated, Text, View } from 'react-native';
  4. import styles, { DEFAULT_COLOR, LABEL_MARGIN, LABEL_SIZE } from './styles';
  5. export type Props = {
  6. /**
  7. * The position of the parent element (from right to left) to display the
  8. * arrow.
  9. */
  10. parentPosition: number
  11. };
  12. type State = {
  13. /**
  14. * The opacity animation Object.
  15. */
  16. opacityAnimation: Object,
  17. /**
  18. * A boolean to descide to show or not show the arrow. This is required as
  19. * we can't easily animate this transformed Component so we render it once
  20. * the animation is done.
  21. */
  22. showArrow: boolean
  23. };
  24. /**
  25. * Offset to the arrow to be rendered in the right position.
  26. */
  27. const ARROW_OFFSET = 0;
  28. /**
  29. * A react {@code Component} that implements an expanded label as tooltip-like
  30. * component to explain the meaning of the {@code Label}.
  31. */
  32. export default class ExpandedLabel<P: Props> extends Component<P, State> {
  33. /**
  34. * Instantiates a new {@code ExpandedLabel} instance.
  35. *
  36. * @inheritdoc
  37. */
  38. constructor(props: P) {
  39. super(props);
  40. this.state = {
  41. opacityAnimation: new Animated.Value(0),
  42. showArrow: false
  43. };
  44. }
  45. /**
  46. * Implements React {@code Component}'s componentDidMount.
  47. *
  48. * @inheritdoc
  49. */
  50. componentDidMount() {
  51. Animated.decay(this.state.opacityAnimation, {
  52. toValue: 1,
  53. velocity: 1,
  54. useNativeDriver: true
  55. }).start(({ finished }) => {
  56. finished && this.setState({
  57. showArrow: true
  58. });
  59. });
  60. }
  61. /**
  62. * Implements React {@code Component}'s render.
  63. *
  64. * @inheritdoc
  65. */
  66. render() {
  67. const arrowPosition
  68. = this.props.parentPosition - LABEL_MARGIN - (LABEL_SIZE / 2);
  69. return (
  70. <Animated.View
  71. style = { [
  72. styles.expandedLabelWrapper,
  73. {
  74. opacity: this.state.opacityAnimation
  75. }
  76. ] } >
  77. <View
  78. style = { [
  79. styles.expandedLabelArrow,
  80. {
  81. backgroundColor: this._getColor() || DEFAULT_COLOR,
  82. marginRight: arrowPosition + ARROW_OFFSET
  83. }
  84. ] } />
  85. <View
  86. style = { [
  87. styles.expandedLabelContainer,
  88. {
  89. backgroundColor: this._getColor() || DEFAULT_COLOR
  90. }
  91. ] }>
  92. <Text style = { styles.expandedLabelText }>
  93. { this._getLabel() }
  94. </Text>
  95. </View>
  96. </Animated.View>
  97. );
  98. }
  99. /**
  100. * Returns the label that needs to be rendered in the box. To be implemented
  101. * by its overriding classes.
  102. *
  103. * @returns {string}
  104. */
  105. _getLabel: () => string
  106. _getColor: () => string
  107. /**
  108. * Defines the color of the expanded label. This function returns a default
  109. * value if implementing classes don't override it, but the goal is to have
  110. * expanded labels matching to circular labels in color.
  111. * If implementing classes return a falsy value, it also uses the default
  112. * color.
  113. *
  114. * @returns {string}
  115. */
  116. _getColor() {
  117. return DEFAULT_COLOR;
  118. }
  119. }