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 4.5KB

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