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.

ToolbarButton.web.js 6.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253
  1. /* @flow */
  2. import AKInlineDialog from '@atlaskit/inline-dialog';
  3. import { Tooltip } from '@atlaskit/tooltip';
  4. import PropTypes from 'prop-types';
  5. import React, { Component } from 'react';
  6. import { translate } from '../../base/i18n';
  7. import { TOOLTIP_TO_POPUP_POSITION } from '../constants';
  8. import { isButtonEnabled } from '../functions';
  9. import StatelessToolbarButton from './StatelessToolbarButton';
  10. declare var APP: Object;
  11. /**
  12. * Represents a button in Toolbar on React.
  13. *
  14. * @class ToolbarButton
  15. * @extends AbstractToolbarButton
  16. */
  17. class ToolbarButton extends Component {
  18. button: Object;
  19. _onClick: Function;
  20. _onMouseOut: Function;
  21. _onMouseOver: Function;
  22. state: {
  23. /**
  24. * Whether or not the tooltip for the button should be displayed.
  25. *
  26. * @type {boolean}
  27. */
  28. showTooltip: boolean
  29. }
  30. /**
  31. * Toolbar button component's property types.
  32. *
  33. * @static
  34. */
  35. static propTypes = {
  36. ...StatelessToolbarButton.propTypes,
  37. /**
  38. * Object describing button.
  39. */
  40. button: PropTypes.object.isRequired,
  41. /**
  42. * Handler for component mount.
  43. */
  44. onMount: PropTypes.func,
  45. /**
  46. * Handler for component unmount.
  47. */
  48. onUnmount: PropTypes.func,
  49. /**
  50. * Translation helper function.
  51. */
  52. t: PropTypes.func,
  53. /**
  54. * Indicates the position of the tooltip.
  55. */
  56. tooltipPosition: PropTypes.oneOf([ 'bottom', 'left', 'right', 'top' ])
  57. };
  58. /**
  59. * Initializes new ToolbarButton instance.
  60. *
  61. * @param {Object} props - The read-only properties with which the new
  62. * instance is to be initialized.
  63. */
  64. constructor(props: Object) {
  65. super(props);
  66. this.state = {
  67. showTooltip: false
  68. };
  69. // Bind methods to save the context
  70. this._onClick = this._onClick.bind(this);
  71. this._onMouseOut = this._onMouseOut.bind(this);
  72. this._onMouseOver = this._onMouseOver.bind(this);
  73. }
  74. /**
  75. * Sets shortcut/tooltip
  76. * after mounting of the component.
  77. *
  78. * @inheritdoc
  79. * @returns {void}
  80. */
  81. componentDidMount(): void {
  82. this._setShortcut();
  83. if (this.props.onMount) {
  84. this.props.onMount();
  85. }
  86. }
  87. /**
  88. * Invokes on unmount handler if it was passed to the props.
  89. *
  90. * @inheritdoc
  91. * @returns {void}
  92. */
  93. componentWillUnmount(): void {
  94. if (this.props.onUnmount) {
  95. this.props.onUnmount();
  96. }
  97. }
  98. /**
  99. * Implements React's {@link Component#render()}.
  100. *
  101. * @inheritdoc
  102. * @returns {ReactElement}
  103. */
  104. render(): ReactElement<*> {
  105. const { button, t, tooltipPosition } = this.props;
  106. const props = {
  107. ...this.props,
  108. onClick: this._onClick
  109. };
  110. const buttonComponent = ( // eslint-disable-line no-extra-parens
  111. <Tooltip
  112. description = { button.tooltipText || t(button.tooltipKey) }
  113. onMouseOut = { this._onMouseOut }
  114. onMouseOver = { this._onMouseOver }
  115. position = { tooltipPosition }
  116. visible = { this.state.showTooltip }>
  117. <StatelessToolbarButton { ...props }>
  118. { this.props.children }
  119. </StatelessToolbarButton>
  120. </Tooltip>
  121. );
  122. let children = buttonComponent;
  123. const popupConfig = this._getPopupDisplayConfiguration();
  124. if (popupConfig) {
  125. const { dataAttr, dataInterpolate, position } = popupConfig;
  126. children = ( // eslint-disable-line no-extra-parens
  127. <AKInlineDialog
  128. content = { t(dataAttr, dataInterpolate) }
  129. isOpen = { Boolean(popupConfig) }
  130. position = { position }>
  131. { buttonComponent }
  132. </AKInlineDialog>
  133. );
  134. }
  135. return (
  136. <div className = { `toolbar-button-wrapper ${button.id}-wrapper` }>
  137. { children }
  138. </div>
  139. );
  140. }
  141. /**
  142. * Wrapper on on click handler props for current button.
  143. *
  144. * @param {Event} event - Click event object.
  145. * @returns {void}
  146. * @private
  147. */
  148. _onClick(event) {
  149. this.props.onClick(event);
  150. this.setState({ showTooltip: false });
  151. }
  152. /**
  153. * Parses the props and state to find any popup that should be displayed
  154. * and returns an object describing how the popup should display.
  155. *
  156. * @private
  157. * @returns {Object|null}
  158. */
  159. _getPopupDisplayConfiguration() {
  160. const { button, tooltipPosition } = this.props;
  161. const { popups, popupDisplay } = button;
  162. if (!popups || !popupDisplay) {
  163. return null;
  164. }
  165. const { popupID } = popupDisplay;
  166. const currentPopup = popups.find(popup => popup.id === popupID);
  167. return Object.assign(
  168. {},
  169. currentPopup || {},
  170. {
  171. position: TOOLTIP_TO_POPUP_POSITION[tooltipPosition]
  172. });
  173. }
  174. /**
  175. * Hides any displayed tooltip.
  176. *
  177. * @private
  178. * @returns {void}
  179. */
  180. _onMouseOut(): void {
  181. this.setState({ showTooltip: false });
  182. }
  183. /**
  184. * Hides any displayed tooltip.
  185. *
  186. * @private
  187. * @returns {void}
  188. */
  189. _onMouseOver(): void {
  190. const { button } = this.props;
  191. this.setState({
  192. showTooltip: isButtonEnabled(button.buttonName)
  193. && !button.unclickable
  194. });
  195. }
  196. /**
  197. * Sets shortcut and tooltip for current toolbar button.
  198. *
  199. * @private
  200. * @returns {void}
  201. */
  202. _setShortcut(): void {
  203. const { button } = this.props;
  204. if (button.shortcut && APP && APP.keyboardshortcut) {
  205. APP.keyboardshortcut.registerShortcut(
  206. button.shortcut,
  207. button.shortcutAttr,
  208. button.shortcutFunc,
  209. button.shortcutDescription
  210. );
  211. }
  212. }
  213. }
  214. export default translate(ToolbarButton);