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

AbstractToolboxItem.js 4.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  1. // @flow
  2. import { Component } from 'react';
  3. export type Styles = {
  4. /**
  5. * Style for the item's icon.
  6. */
  7. iconStyle: Object,
  8. /**
  9. * Style for the item's label.
  10. */
  11. labelStyle: Object,
  12. /**
  13. * Style for the item itself.
  14. */
  15. style: Object,
  16. /**
  17. * Color for the item underlay (shows when clicked).
  18. */
  19. underlayColor: string
  20. };
  21. export type Props = {
  22. /**
  23. * A succinct description of what the item does. Used by accessibility
  24. * tools and torture tests.
  25. */
  26. accessibilityLabel: string,
  27. /**
  28. * Whether this item is disabled or not. When disabled, clicking an the item
  29. * has no effect, and it may reflect on its style.
  30. */
  31. disabled: boolean,
  32. /**
  33. * The name of the icon of this {@code ToolboxItem}.
  34. */
  35. iconName: string,
  36. /**
  37. * The text associated with this item. When `showLabel` is set to
  38. * {@code true}, it will be displayed alongside the icon.
  39. */
  40. label: string,
  41. /**
  42. * On click handler.
  43. */
  44. onClick: Function,
  45. /**
  46. * Whether to show the label or not.
  47. */
  48. showLabel: boolean,
  49. /**
  50. * Collection of styles for the item. Used only on native.
  51. */
  52. styles: ?Styles,
  53. /**
  54. * Invoked to obtain translated strings.
  55. */
  56. t: ?Function,
  57. /**
  58. * The text to display in the tooltip. Used only on web.
  59. */
  60. tooltip: string,
  61. /**
  62. * From which direction the tooltip should appear, relative to the
  63. * item. Used only on web.
  64. */
  65. tooltipPosition: string,
  66. /**
  67. * Whether this item is visible or not.
  68. */
  69. visible: boolean
  70. };
  71. /**
  72. * Abstract (base) class for an item in {@link Toolbox}. The item can be located
  73. * anywhere in the {@link Toolbox}, it will morph its shape to accommodate it.
  74. *
  75. * @abstract
  76. */
  77. export default class AbstractToolboxItem<P : Props> extends Component<P> {
  78. /**
  79. * Default values for {@code AbstractToolboxItem} component's properties.
  80. *
  81. * @static
  82. */
  83. static defaultProps = {
  84. disabled: false,
  85. label: '',
  86. showLabel: false,
  87. t: undefined,
  88. tooltip: '',
  89. tooltipPosition: 'top',
  90. visible: true
  91. };
  92. /**
  93. * Initializes a new {@code AbstractToolboxItem} instance.
  94. *
  95. * @param {Object} props - The React {@code Component} props to initialize
  96. * the new {@code AbstractToolboxItem} instance with.
  97. */
  98. constructor(props: P) {
  99. super(props);
  100. // Bind event handlers so they are only bound once per instance.
  101. this._onClick = this._onClick.bind(this);
  102. }
  103. /**
  104. * Helper property to get the item label. If a translation function was
  105. * provided then it will be translated using it.
  106. *
  107. * @protected
  108. * @returns {?string}
  109. */
  110. get label(): ?string {
  111. return this._maybeTranslateAttribute(this.props.label);
  112. }
  113. /**
  114. * Helper property to get the item tooltip. If a translation function was
  115. * provided then it will be translated using it.
  116. *
  117. * @protected
  118. * @returns {?string}
  119. */
  120. get tooltip(): ?string {
  121. return this._maybeTranslateAttribute(this.props.tooltip);
  122. }
  123. /**
  124. * Utility function to translate the given string, if a translation
  125. * function is available.
  126. *
  127. * @param {string} text - What needs translating.
  128. * @private
  129. * @returns {string}
  130. */
  131. _maybeTranslateAttribute(text) {
  132. const { t } = this.props;
  133. return typeof t === 'function' ? t(text) : text;
  134. }
  135. _onClick: (*) => void;
  136. /**
  137. * Handles clicking/pressing this {@code AbstractToolboxItem} by
  138. * forwarding the event to the {@code onClick} prop of this instance if any.
  139. *
  140. * @protected
  141. * @returns {void}
  142. */
  143. _onClick(...args) {
  144. const { disabled, onClick } = this.props;
  145. disabled || (onClick && onClick(...args));
  146. }
  147. /**
  148. * Renders this {@code AbstractToolboxItem} (if it is {@code visible}). To
  149. * be implemented/overridden by extenders. The default implementation of
  150. * {@code AbstractToolboxItem} does nothing.
  151. *
  152. * @protected
  153. * @returns {ReactElement}
  154. */
  155. _renderItem() {
  156. // To be implemented by a subclass.
  157. }
  158. /**
  159. * Implements React's {@link Component#render()}.
  160. *
  161. * @inheritdoc
  162. * @returns {ReactElement}
  163. */
  164. render() {
  165. return this.props.visible ? this._renderItem() : null;
  166. }
  167. }