選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

AbstractButton.tsx 10KB

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