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.

AbstractButton.js 8.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342
  1. // @flow
  2. import React, { Component } from 'react';
  3. import { NOTIFY_CLICK_MODE } from '../../../toolbox/constants';
  4. import { combineStyles } from '../../styles';
  5. import type { Styles } from './AbstractToolboxItem';
  6. import ToolboxItem from './ToolboxItem';
  7. export type Props = {|
  8. /**
  9. * Function to be called after the click handler has been processed.
  10. */
  11. afterClick: ?Function,
  12. /**
  13. * The button's key.
  14. */
  15. buttonKey?: string,
  16. /**
  17. * Whether or not the button is displayed in a context menu.
  18. */
  19. contextMenu?: boolean,
  20. /**
  21. * An extra class name to be added at the end of the element's class name
  22. * in order to enable custom styling.
  23. */
  24. customClass?: string,
  25. /**
  26. * Extra styles which will be applied in conjunction with `styles` or
  27. * `toggledStyles` when the button is disabled;.
  28. */
  29. disabledStyles: ?Styles,
  30. /**
  31. * External handler for click action.
  32. */
  33. handleClick?: Function,
  34. /**
  35. * Notify mode for `toolbarButtonClicked` event -
  36. * whether to only notify or to also prevent button click routine.
  37. */
  38. notifyMode?: string,
  39. /**
  40. * Whether to show the label or not.
  41. */
  42. showLabel: boolean,
  43. /**
  44. * Collection of styles for the button.
  45. */
  46. styles: ?Styles,
  47. /**
  48. * Collection of styles for the button, when in toggled state.
  49. */
  50. toggledStyles: ?Styles,
  51. /**
  52. * From which direction the tooltip should appear, relative to the button.
  53. */
  54. tooltipPosition: string,
  55. /**
  56. * Whether this button is visible or not.
  57. */
  58. visible: boolean
  59. |};
  60. declare var APP: Object;
  61. /**
  62. * Default style for disabled buttons.
  63. */
  64. export const defaultDisabledButtonStyles = {
  65. iconStyle: {
  66. opacity: 0.5
  67. },
  68. labelStyle: {
  69. opacity: 0.5
  70. },
  71. style: undefined,
  72. underlayColor: undefined
  73. };
  74. /**
  75. * An abstract implementation of a button.
  76. */
  77. export default class AbstractButton<P: Props, S: *> extends Component<P, S> {
  78. static defaultProps = {
  79. afterClick: undefined,
  80. disabledStyles: defaultDisabledButtonStyles,
  81. showLabel: false,
  82. styles: undefined,
  83. toggledStyles: undefined,
  84. tooltipPosition: 'top',
  85. visible: true
  86. };
  87. /**
  88. * A succinct description of what the button does. Used by accessibility
  89. * tools and torture tests.
  90. *
  91. * @abstract
  92. */
  93. accessibilityLabel: string;
  94. /**
  95. * The icon of this button.
  96. *
  97. * @abstract
  98. */
  99. icon: Object;
  100. /**
  101. * The text associated with this button. When `showLabel` is set to
  102. * {@code true}, it will be displayed alongside the icon.
  103. *
  104. * @abstract
  105. */
  106. label: string;
  107. /**
  108. * The label for this button, when toggled.
  109. */
  110. toggledLabel: string;
  111. /**
  112. * The icon of this button, when toggled.
  113. *
  114. * @abstract
  115. */
  116. toggledIcon: Object;
  117. /**
  118. * The text to display in the tooltip. Used only on web.
  119. *
  120. * @abstract
  121. */
  122. tooltip: ?string;
  123. /**
  124. * Initializes a new {@code AbstractButton} instance.
  125. *
  126. * @param {Props} props - The React {@code Component} props to initialize
  127. * the new {@code AbstractButton} instance with.
  128. */
  129. constructor(props: P) {
  130. super(props);
  131. // Bind event handlers so they are only bound once per instance.
  132. this._onClick = this._onClick.bind(this);
  133. }
  134. /**
  135. * Helper function to be implemented by subclasses, which should be used
  136. * to handle a key being down.
  137. *
  138. * @protected
  139. * @returns {void}
  140. */
  141. _onKeyDown() {
  142. // To be implemented by subclass.
  143. }
  144. /**
  145. * Helper function to be implemented by subclasses, which should be used
  146. * to handle the button being clicked / pressed.
  147. *
  148. * @protected
  149. * @returns {void}
  150. */
  151. _handleClick() {
  152. // To be implemented by subclass.
  153. }
  154. /**
  155. * Helper function to be implemented by subclasses, which may return a
  156. * new React Element to be appended at the end of the button.
  157. *
  158. * @protected
  159. * @returns {ReactElement|null}
  160. */
  161. _getElementAfter() {
  162. return null;
  163. }
  164. /**
  165. * Gets the current icon, taking the toggled state into account. If no
  166. * toggled icon is provided, the regular icon will also be used in the
  167. * toggled state.
  168. *
  169. * @private
  170. * @returns {string}
  171. */
  172. _getIcon() {
  173. return (
  174. this._isToggled() ? this.toggledIcon : this.icon
  175. ) || this.icon;
  176. }
  177. /**
  178. * Gets the current label, taking the toggled state into account. If no
  179. * toggled label is provided, the regular label will also be used in the
  180. * toggled state.
  181. *
  182. * @private
  183. * @returns {string}
  184. */
  185. _getLabel() {
  186. return (this._isToggled() ? this.toggledLabel : this.label)
  187. || this.label;
  188. }
  189. /**
  190. * Gets the current styles, taking the toggled state into account. If no
  191. * toggled styles are provided, the regular styles will also be used in the
  192. * toggled state.
  193. *
  194. * @private
  195. * @returns {?Styles}
  196. */
  197. _getStyles(): ?Styles {
  198. const { disabledStyles, styles, toggledStyles } = this.props;
  199. const buttonStyles
  200. = (this._isToggled() ? toggledStyles : styles) || styles;
  201. if (this._isDisabled() && buttonStyles && disabledStyles) {
  202. return {
  203. iconStyle: combineStyles(
  204. buttonStyles.iconStyle, disabledStyles.iconStyle),
  205. labelStyle: combineStyles(
  206. buttonStyles.labelStyle, disabledStyles.labelStyle),
  207. style: combineStyles(
  208. buttonStyles.style, disabledStyles.style),
  209. underlayColor:
  210. disabledStyles.underlayColor || buttonStyles.underlayColor
  211. };
  212. }
  213. return buttonStyles;
  214. }
  215. /**
  216. * Get the tooltip to display when hovering over the button.
  217. *
  218. * @private
  219. * @returns {string}
  220. */
  221. _getTooltip() {
  222. return this.tooltip || '';
  223. }
  224. /**
  225. * Helper function to be implemented by subclasses, which must return a
  226. * boolean value indicating if this button is disabled or not.
  227. *
  228. * @protected
  229. * @returns {boolean}
  230. */
  231. _isDisabled() {
  232. return false;
  233. }
  234. /**
  235. * Helper function to be implemented by subclasses, which must return a
  236. * {@code boolean} value indicating if this button is toggled or not or
  237. * undefined if the button is not toggleable.
  238. *
  239. * @protected
  240. * @returns {?boolean}
  241. */
  242. _isToggled() {
  243. return undefined;
  244. }
  245. _onClick: (*) => void;
  246. /**
  247. * Handles clicking / pressing the button.
  248. *
  249. * @param {Object} e - Event.
  250. * @private
  251. * @returns {void}
  252. */
  253. _onClick(e) {
  254. const { afterClick, handleClick, notifyMode, buttonKey } = this.props;
  255. if (typeof APP !== 'undefined' && notifyMode) {
  256. APP.API.notifyToolbarButtonClicked(
  257. buttonKey, notifyMode === NOTIFY_CLICK_MODE.PREVENT_AND_NOTIFY
  258. );
  259. }
  260. if (notifyMode !== NOTIFY_CLICK_MODE.PREVENT_AND_NOTIFY) {
  261. if (handleClick) {
  262. handleClick();
  263. }
  264. this._handleClick();
  265. }
  266. afterClick && afterClick(e);
  267. // blur after click to release focus from button to allow PTT.
  268. e?.currentTarget?.blur && e.currentTarget.blur();
  269. }
  270. /**
  271. * Implements React's {@link Component#render()}.
  272. *
  273. * @inheritdoc
  274. * @returns {React$Node}
  275. */
  276. render(): React$Node {
  277. const props = {
  278. ...this.props,
  279. accessibilityLabel: this.accessibilityLabel,
  280. elementAfter: this._getElementAfter(),
  281. icon: this._getIcon(),
  282. label: this._getLabel(),
  283. styles: this._getStyles(),
  284. toggled: this._isToggled(),
  285. tooltip: this._getTooltip()
  286. };
  287. return (
  288. <ToolboxItem
  289. disabled = { this._isDisabled() }
  290. onClick = { this._onClick }
  291. onKeyDown = { this._onKeyDown }
  292. { ...props } />
  293. );
  294. }
  295. }