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

AbstractButton.js 6.5KB

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