Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

AbstractButton.js 8.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345
  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. labelProps: Object;
  95. /**
  96. * The icon of this button.
  97. *
  98. * @abstract
  99. */
  100. icon: Object;
  101. /**
  102. * The text associated with this button. When `showLabel` is set to
  103. * {@code true}, it will be displayed alongside the icon.
  104. *
  105. * @abstract
  106. */
  107. label: string;
  108. /**
  109. * The label for this button, when toggled.
  110. */
  111. toggledLabel: string;
  112. /**
  113. * The icon of this button, when toggled.
  114. *
  115. * @abstract
  116. */
  117. toggledIcon: Object;
  118. /**
  119. * The text to display in the tooltip. Used only on web.
  120. *
  121. * @abstract
  122. */
  123. tooltip: ?string;
  124. /**
  125. * Initializes a new {@code AbstractButton} instance.
  126. *
  127. * @param {Props} props - The React {@code Component} props to initialize
  128. * the new {@code AbstractButton} instance with.
  129. */
  130. constructor(props: P) {
  131. super(props);
  132. // Bind event handlers so they are only bound once per instance.
  133. this._onClick = this._onClick.bind(this);
  134. }
  135. /**
  136. * Helper function to be implemented by subclasses, which should be used
  137. * to handle a key being down.
  138. *
  139. * @protected
  140. * @returns {void}
  141. */
  142. _onKeyDown() {
  143. // To be implemented by subclass.
  144. }
  145. /**
  146. * Helper function to be implemented by subclasses, which should be used
  147. * to handle the button being clicked / pressed.
  148. *
  149. * @protected
  150. * @returns {void}
  151. */
  152. _handleClick() {
  153. // To be implemented by subclass.
  154. }
  155. /**
  156. * Helper function to be implemented by subclasses, which may return a
  157. * new React Element to be appended at the end of the button.
  158. *
  159. * @protected
  160. * @returns {ReactElement|null}
  161. */
  162. _getElementAfter() {
  163. return null;
  164. }
  165. /**
  166. * Gets the current icon, taking the toggled state into account. If no
  167. * toggled icon is provided, the regular icon will also be used in the
  168. * toggled state.
  169. *
  170. * @private
  171. * @returns {string}
  172. */
  173. _getIcon() {
  174. return (
  175. this._isToggled() ? this.toggledIcon : this.icon
  176. ) || this.icon;
  177. }
  178. /**
  179. * Gets the current label, taking the toggled state into account. If no
  180. * toggled label is provided, the regular label will also be used in the
  181. * toggled state.
  182. *
  183. * @private
  184. * @returns {string}
  185. */
  186. _getLabel() {
  187. return (this._isToggled() ? this.toggledLabel : this.label)
  188. || this.label;
  189. }
  190. /**
  191. * Gets the current styles, taking the toggled state into account. If no
  192. * toggled styles are provided, the regular styles will also be used in the
  193. * toggled state.
  194. *
  195. * @private
  196. * @returns {?Styles}
  197. */
  198. _getStyles(): ?Styles {
  199. const { disabledStyles, styles, toggledStyles } = this.props;
  200. const buttonStyles
  201. = (this._isToggled() ? toggledStyles : styles) || styles;
  202. if (this._isDisabled() && buttonStyles && disabledStyles) {
  203. return {
  204. iconStyle: combineStyles(
  205. buttonStyles.iconStyle, disabledStyles.iconStyle),
  206. labelStyle: combineStyles(
  207. buttonStyles.labelStyle, disabledStyles.labelStyle),
  208. style: combineStyles(
  209. buttonStyles.style, disabledStyles.style),
  210. underlayColor:
  211. disabledStyles.underlayColor || buttonStyles.underlayColor
  212. };
  213. }
  214. return buttonStyles;
  215. }
  216. /**
  217. * Get the tooltip to display when hovering over the button.
  218. *
  219. * @private
  220. * @returns {string}
  221. */
  222. _getTooltip() {
  223. return this.tooltip || '';
  224. }
  225. /**
  226. * Helper function to be implemented by subclasses, which must return a
  227. * boolean value indicating if this button is disabled or not.
  228. *
  229. * @protected
  230. * @returns {boolean}
  231. */
  232. _isDisabled() {
  233. return false;
  234. }
  235. /**
  236. * Helper function to be implemented by subclasses, which must return a
  237. * {@code boolean} value indicating if this button is toggled or not or
  238. * undefined if the button is not toggleable.
  239. *
  240. * @protected
  241. * @returns {?boolean}
  242. */
  243. _isToggled() {
  244. return undefined;
  245. }
  246. _onClick: (*) => void;
  247. /**
  248. * Handles clicking / pressing the button.
  249. *
  250. * @param {Object} e - Event.
  251. * @private
  252. * @returns {void}
  253. */
  254. _onClick(e) {
  255. const { afterClick, handleClick, notifyMode, buttonKey } = this.props;
  256. if (typeof APP !== 'undefined' && notifyMode) {
  257. APP.API.notifyToolbarButtonClicked(
  258. buttonKey, notifyMode === NOTIFY_CLICK_MODE.PREVENT_AND_NOTIFY
  259. );
  260. }
  261. if (notifyMode !== NOTIFY_CLICK_MODE.PREVENT_AND_NOTIFY) {
  262. if (handleClick) {
  263. handleClick();
  264. }
  265. this._handleClick();
  266. }
  267. afterClick && afterClick(e);
  268. // blur after click to release focus from button to allow PTT.
  269. e?.currentTarget?.blur && e.currentTarget.blur();
  270. }
  271. /**
  272. * Implements React's {@link Component#render()}.
  273. *
  274. * @inheritdoc
  275. * @returns {React$Node}
  276. */
  277. render(): React$Node {
  278. const props = {
  279. ...this.props,
  280. accessibilityLabel: this.accessibilityLabel,
  281. elementAfter: this._getElementAfter(),
  282. icon: this._getIcon(),
  283. label: this._getLabel(),
  284. labelProps: this.labelProps,
  285. styles: this._getStyles(),
  286. toggled: this._isToggled(),
  287. tooltip: this._getTooltip()
  288. };
  289. return (
  290. <ToolboxItem
  291. disabled = { this._isDisabled() }
  292. onClick = { this._onClick }
  293. onKeyDown = { this._onKeyDown }
  294. { ...props } />
  295. );
  296. }
  297. }