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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  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 icon to render for this {@code ToolboxItem}.
  39. */
  40. icon: Object,
  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. * True if the item is toggled, false otherwise.
  64. */
  65. toggled: boolean,
  66. /**
  67. * The text to display in the tooltip. Used only on web.
  68. */
  69. tooltip: ?string,
  70. /**
  71. * From which direction the tooltip should appear, relative to the
  72. * item. Used only on web.
  73. */
  74. tooltipPosition: string,
  75. /**
  76. * Whether this item is visible or not.
  77. */
  78. visible: boolean
  79. };
  80. /**
  81. * Abstract (base) class for an item in {@link Toolbox}. The item can be located
  82. * anywhere in the {@link Toolbox}, it will morph its shape to accommodate it.
  83. *
  84. * @abstract
  85. */
  86. export default class AbstractToolboxItem<P : Props> extends Component<P> {
  87. /**
  88. * Default values for {@code AbstractToolboxItem} component's properties.
  89. *
  90. * @static
  91. */
  92. static defaultProps = {
  93. disabled: false,
  94. label: '',
  95. showLabel: false,
  96. t: undefined,
  97. tooltip: '',
  98. tooltipPosition: 'top',
  99. visible: true
  100. };
  101. /**
  102. * Initializes a new {@code AbstractToolboxItem} instance.
  103. *
  104. * @param {Object} props - The React {@code Component} props to initialize
  105. * the new {@code AbstractToolboxItem} instance with.
  106. */
  107. constructor(props: P) {
  108. super(props);
  109. // Bind event handlers so they are only bound once per instance.
  110. this._onClick = this._onClick.bind(this);
  111. }
  112. /**
  113. * Helper property to get the item label. If a translation function was
  114. * provided then it will be translated using it.
  115. *
  116. * @protected
  117. * @returns {?string}
  118. */
  119. get label(): ?string {
  120. return this._maybeTranslateAttribute(this.props.label);
  121. }
  122. /**
  123. * Helper property to get the item tooltip. If a translation function was
  124. * provided then it will be translated using it.
  125. *
  126. * @protected
  127. * @returns {?string}
  128. */
  129. get tooltip(): ?string {
  130. return this._maybeTranslateAttribute(this.props.tooltip);
  131. }
  132. /**
  133. * Helper property to get the item accessibilityLabel. If a translation
  134. * function was provided then it will be translated using it.
  135. *
  136. * @protected
  137. * @returns {?string}
  138. */
  139. get accessibilityLabel(): ?string {
  140. return this._maybeTranslateAttribute(this.props.accessibilityLabel);
  141. }
  142. /**
  143. * Utility function to translate the given string, if a translation
  144. * function is available.
  145. *
  146. * @param {string} text - What needs translating.
  147. * @private
  148. * @returns {string}
  149. */
  150. _maybeTranslateAttribute(text) {
  151. const { t } = this.props;
  152. return typeof t === 'function' ? t(text) : text;
  153. }
  154. _onClick: (*) => void;
  155. /**
  156. * Handles clicking/pressing this {@code AbstractToolboxItem} by
  157. * forwarding the event to the {@code onClick} prop of this instance if any.
  158. *
  159. * @protected
  160. * @returns {void}
  161. */
  162. _onClick(...args) {
  163. const { disabled, onClick } = this.props;
  164. disabled || (onClick && onClick(...args));
  165. }
  166. /**
  167. * Renders this {@code AbstractToolboxItem} (if it is {@code visible}). To
  168. * be implemented/overridden by extenders. The default implementation of
  169. * {@code AbstractToolboxItem} does nothing.
  170. *
  171. * @protected
  172. * @returns {ReactElement}
  173. */
  174. _renderItem() {
  175. // To be implemented by a subclass.
  176. return null;
  177. }
  178. /**
  179. * Implements React's {@link Component#render()}.
  180. *
  181. * @inheritdoc
  182. * @returns {ReactElement}
  183. */
  184. render() {
  185. return this.props.visible ? this._renderItem() : null;
  186. }
  187. }