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

Toolbar.web.js 4.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  1. /* @flow */
  2. import React, { Component } from 'react';
  3. import { connect } from 'react-redux';
  4. import { setToolbarHovered } from '../actions';
  5. import ToolbarButton from './ToolbarButton';
  6. /**
  7. * Implements a toolbar in React/Web. It is a strip that contains a set of
  8. * toolbar items such as buttons. Toolbar is commonly placed inside of a
  9. * Toolbox.
  10. *
  11. * @class Toolbar
  12. * @extends Component
  13. */
  14. class Toolbar extends Component {
  15. _renderToolbarButton: Function;
  16. /**
  17. * Base toolbar component's property types.
  18. *
  19. * @static
  20. */
  21. static propTypes = {
  22. /**
  23. * Handler for mouse out event.
  24. */
  25. _onMouseOut: React.PropTypes.func,
  26. /**
  27. * Handler for mouse over event.
  28. */
  29. _onMouseOver: React.PropTypes.func,
  30. /**
  31. * Children of current React component.
  32. */
  33. children: React.PropTypes.element,
  34. /**
  35. * Toolbar's class name.
  36. */
  37. className: React.PropTypes.string,
  38. /**
  39. * If the toolbar requires splitter this property defines splitter
  40. * index.
  41. */
  42. splitterIndex: React.PropTypes.number,
  43. /**
  44. * Map with toolbar buttons.
  45. */
  46. toolbarButtons: React.PropTypes.instanceOf(Map),
  47. /**
  48. * Indicates the position of the tooltip.
  49. */
  50. tooltipPosition:
  51. React.PropTypes.oneOf([ 'bottom', 'left', 'right', 'top' ])
  52. };
  53. /**
  54. * Constructor of Primary toolbar class.
  55. *
  56. * @param {Object} props - Object containing React component properties.
  57. */
  58. constructor(props) {
  59. super(props);
  60. // Bind callbacks to preverse this.
  61. this._renderToolbarButton = this._renderToolbarButton.bind(this);
  62. }
  63. /**
  64. * Implements React's {@link Component#render()}.
  65. *
  66. * @inheritdoc
  67. * @returns {ReactElement}
  68. */
  69. render(): ReactElement<*> {
  70. const { className } = this.props;
  71. return (
  72. <div
  73. className = { `toolbar ${className}` }
  74. onMouseOut = { this.props._onMouseOut }
  75. onMouseOver = { this.props._onMouseOver }>
  76. {
  77. [ ...this.props.toolbarButtons.entries() ]
  78. .reduce(this._renderToolbarButton, [])
  79. }
  80. {
  81. this.props.children
  82. }
  83. </div>
  84. );
  85. }
  86. /**
  87. * Renders toolbar button. Method is passed to reduce function.
  88. *
  89. * @param {Array} acc - Toolbar buttons array.
  90. * @param {Array} keyValuePair - Key value pair containing button and its
  91. * key.
  92. * @param {number} index - Index of the key value pair in the array.
  93. * @private
  94. * @returns {Array} Array of toolbar buttons and splitter if it's on.
  95. */
  96. _renderToolbarButton(acc: Array<*>, keyValuePair: Array<*>,
  97. index: number): Array<ReactElement<*>> {
  98. const [ key, button ] = keyValuePair;
  99. if (button.component) {
  100. acc.push(
  101. <button.component
  102. key = { key }
  103. tooltipPosition = { this.props.tooltipPosition } />
  104. );
  105. return acc;
  106. }
  107. const { splitterIndex, tooltipPosition } = this.props;
  108. if (splitterIndex && index === splitterIndex) {
  109. const splitter = <span className = 'toolbar__splitter' />;
  110. acc.push(splitter);
  111. }
  112. const { onClick, onMount, onUnmount } = button;
  113. acc.push(
  114. <ToolbarButton
  115. button = { button }
  116. key = { key }
  117. onClick = { onClick }
  118. onMount = { onMount }
  119. onUnmount = { onUnmount }
  120. tooltipPosition = { tooltipPosition } />
  121. );
  122. return acc;
  123. }
  124. }
  125. /**
  126. * Maps part of Redux actions to component's props.
  127. *
  128. * @param {Function} dispatch - Redux action dispatcher.
  129. * @private
  130. * @returns {Object}
  131. */
  132. function _mapDispatchToProps(dispatch: Function): Object {
  133. return {
  134. /**
  135. * Dispatches an action signalling that toolbar is no being hovered.
  136. *
  137. * @protected
  138. * @returns {Object} Dispatched action.
  139. */
  140. _onMouseOut() {
  141. return dispatch(setToolbarHovered(false));
  142. },
  143. /**
  144. * Dispatches an action signalling that toolbar is now being hovered.
  145. *
  146. * @protected
  147. * @returns {Object} Dispatched action.
  148. */
  149. _onMouseOver() {
  150. return dispatch(setToolbarHovered(true));
  151. }
  152. };
  153. }
  154. export default connect(undefined, _mapDispatchToProps)(Toolbar);