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 6.9KB

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