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

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