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.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259
  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 name of the icon of this button.
  72. *
  73. * @abstract
  74. */
  75. iconName: string;
  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 name of the icon of this button, when toggled.
  89. *
  90. * @abstract
  91. */
  92. toggledIconName: string;
  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. * Gets the current icon name, taking the toggled state into account. If no
  122. * toggled icon is provided, the regular icon will also be used in the
  123. * toggled state.
  124. *
  125. * @private
  126. * @returns {string}
  127. */
  128. _getIconName() {
  129. return (this._isToggled() ? this.toggledIconName : this.iconName)
  130. || this.iconName;
  131. }
  132. /**
  133. * Gets the current label, taking the toggled state into account. If no
  134. * toggled label is provided, the regular label will also be used in the
  135. * toggled state.
  136. *
  137. * @private
  138. * @returns {string}
  139. */
  140. _getLabel() {
  141. return (this._isToggled() ? this.toggledLabel : this.label)
  142. || this.label;
  143. }
  144. /**
  145. * Gets the current styles, taking the toggled state into account. If no
  146. * toggled styles are provided, the regular styles will also be used in the
  147. * toggled state.
  148. *
  149. * @private
  150. * @returns {?Styles}
  151. */
  152. _getStyles(): ?Styles {
  153. const { disabledStyles, styles, toggledStyles } = this.props;
  154. const buttonStyles
  155. = (this._isToggled() ? toggledStyles : styles) || styles;
  156. if (this._isDisabled() && buttonStyles && disabledStyles) {
  157. return {
  158. iconStyle: combineStyles(
  159. buttonStyles.iconStyle, disabledStyles.iconStyle),
  160. labelStyle: combineStyles(
  161. buttonStyles.labelStyle, disabledStyles.labelStyle),
  162. style: combineStyles(
  163. buttonStyles.style, disabledStyles.style),
  164. underlayColor:
  165. disabledStyles.underlayColor || buttonStyles.underlayColor
  166. };
  167. }
  168. return buttonStyles;
  169. }
  170. /**
  171. * Helper function to be implemented by subclasses, which must return a
  172. * boolean value indicating if this button is disabled or not.
  173. *
  174. * @protected
  175. * @returns {boolean}
  176. */
  177. _isDisabled() {
  178. return false;
  179. }
  180. /**
  181. * Helper function to be implemented by subclasses, which must return a
  182. * {@code boolean} value indicating if this button is toggled or not.
  183. *
  184. * @protected
  185. * @returns {boolean}
  186. */
  187. _isToggled() {
  188. return false;
  189. }
  190. _onClick: (*) => void;
  191. /**
  192. * Handles clicking / pressing the button, and toggles the audio mute state
  193. * accordingly.
  194. *
  195. * @private
  196. * @returns {void}
  197. */
  198. _onClick() {
  199. const { afterClick } = this.props;
  200. this._handleClick();
  201. afterClick && afterClick();
  202. }
  203. /**
  204. * Implements React's {@link Component#render()}.
  205. *
  206. * @inheritdoc
  207. * @returns {React$Node}
  208. */
  209. render(): React$Node {
  210. const props = {
  211. ...this.props,
  212. accessibilityLabel: this.accessibilityLabel,
  213. iconName: this._getIconName(),
  214. label: this._getLabel(),
  215. styles: this._getStyles(),
  216. tooltip: this.tooltip
  217. };
  218. return (
  219. <ToolboxItem
  220. disabled = { this._isDisabled() }
  221. onClick = { this._onClick }
  222. { ...props } />
  223. );
  224. }
  225. }