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.5KB

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