您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

ExpandedLabel.tsx 2.6KB

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