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

AbstractButton.js 7.0KB

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