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 2.4KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. // @flow
  2. import React, { Component } from 'react';
  3. import { Animated, Text, View } from 'react-native';
  4. import styles, { DEFAULT_COLOR } 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. /**
  19. * A react {@code Component} that implements an expanded label as tooltip-like
  20. * component to explain the meaning of the {@code Label}.
  21. */
  22. export default class ExpandedLabel<P: Props> extends Component<P, State> {
  23. /**
  24. * Instantiates a new {@code ExpandedLabel} instance.
  25. *
  26. * @inheritdoc
  27. */
  28. constructor(props: P) {
  29. super(props);
  30. this.state = {
  31. opacityAnimation: new Animated.Value(0)
  32. };
  33. }
  34. /**
  35. * Implements React {@code Component}'s componentDidMount.
  36. *
  37. * @inheritdoc
  38. */
  39. componentDidMount() {
  40. Animated.decay(this.state.opacityAnimation, {
  41. toValue: 1,
  42. velocity: 1,
  43. useNativeDriver: true
  44. }).start();
  45. }
  46. /**
  47. * Implements React {@code Component}'s render.
  48. *
  49. * @inheritdoc
  50. */
  51. render() {
  52. return (
  53. <Animated.View
  54. style = { [ styles.expandedLabelContainer, { opacity: this.state.opacityAnimation } ] }>
  55. <View
  56. style = { [ styles.expandedLabelTextContainer,
  57. { backgroundColor: this._getColor() || DEFAULT_COLOR } ] }>
  58. <Text style = { styles.expandedLabelText }>
  59. { this._getLabel() }
  60. </Text>
  61. </View>
  62. </Animated.View>
  63. );
  64. }
  65. /**
  66. * Returns the label that needs to be rendered in the box. To be implemented
  67. * by its overriding classes.
  68. *
  69. * @returns {string}
  70. */
  71. _getLabel: () => string
  72. _getColor: () => string
  73. /**
  74. * Defines the color of the expanded label. This function returns a default
  75. * value if implementing classes don't override it, but the goal is to have
  76. * expanded labels matching to circular labels in color.
  77. * If implementing classes return a falsy value, it also uses the default
  78. * color.
  79. *
  80. * @returns {string}
  81. */
  82. _getColor() {
  83. return DEFAULT_COLOR;
  84. }
  85. }