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

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