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.tsx 10KB

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