您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

AbstractButton.js 9.5KB

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