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.

AbstractToolboxItem.js 5.0KB

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