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

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