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.

211 lines
5.3 KiB

  1. import React from 'react';
  2. import PropTypes from 'prop-types';
  3. import ImmutablePropTypes from 'react-immutable-proptypes';
  4. import IconButton from './icon_button';
  5. import Overlay from 'react-overlays/lib/Overlay';
  6. import Motion from '../features/ui/util/optional_motion';
  7. import spring from 'react-motion/lib/spring';
  8. import detectPassiveEvents from 'detect-passive-events';
  9. const listenerOptions = detectPassiveEvents.hasSupport ? { passive: true } : false;
  10. class DropdownMenu extends React.PureComponent {
  11. static contextTypes = {
  12. router: PropTypes.object,
  13. };
  14. static propTypes = {
  15. items: PropTypes.array.isRequired,
  16. onClose: PropTypes.func.isRequired,
  17. style: PropTypes.object,
  18. placement: PropTypes.string,
  19. arrowOffsetLeft: PropTypes.string,
  20. arrowOffsetTop: PropTypes.string,
  21. };
  22. static defaultProps = {
  23. style: {},
  24. placement: 'bottom',
  25. };
  26. handleDocumentClick = e => {
  27. if (this.node && !this.node.contains(e.target)) {
  28. this.props.onClose();
  29. }
  30. }
  31. componentDidMount () {
  32. document.addEventListener('click', this.handleDocumentClick, false);
  33. document.addEventListener('touchend', this.handleDocumentClick, listenerOptions);
  34. }
  35. componentWillUnmount () {
  36. document.removeEventListener('click', this.handleDocumentClick, false);
  37. document.removeEventListener('touchend', this.handleDocumentClick, listenerOptions);
  38. }
  39. setRef = c => {
  40. this.node = c;
  41. }
  42. handleClick = e => {
  43. const i = Number(e.currentTarget.getAttribute('data-index'));
  44. const { action, to } = this.props.items[i];
  45. this.props.onClose();
  46. if (typeof action === 'function') {
  47. e.preventDefault();
  48. action();
  49. } else if (to) {
  50. e.preventDefault();
  51. this.context.router.history.push(to);
  52. }
  53. }
  54. renderItem (option, i) {
  55. if (option === null) {
  56. return <li key={`sep-${i}`} className='dropdown-menu__separator' />;
  57. }
  58. const { text, href = '#' } = option;
  59. return (
  60. <li className='dropdown-menu__item' key={`${text}-${i}`}>
  61. <a href={href} target='_blank' rel='noopener' role='button' tabIndex='0' autoFocus={i === 0} onClick={this.handleClick} data-index={i}>
  62. {text}
  63. </a>
  64. </li>
  65. );
  66. }
  67. render () {
  68. const { items, style, placement, arrowOffsetLeft, arrowOffsetTop } = this.props;
  69. return (
  70. <Motion defaultStyle={{ opacity: 0, scaleX: 0.85, scaleY: 0.75 }} style={{ opacity: spring(1, { damping: 35, stiffness: 400 }), scaleX: spring(1, { damping: 35, stiffness: 400 }), scaleY: spring(1, { damping: 35, stiffness: 400 }) }}>
  71. {({ opacity, scaleX, scaleY }) => (
  72. <div className='dropdown-menu' style={{ ...style, opacity: opacity, transform: `scale(${scaleX}, ${scaleY})` }} ref={this.setRef}>
  73. <div className={`dropdown-menu__arrow ${placement}`} style={{ left: arrowOffsetLeft, top: arrowOffsetTop }} />
  74. <ul>
  75. {items.map((option, i) => this.renderItem(option, i))}
  76. </ul>
  77. </div>
  78. )}
  79. </Motion>
  80. );
  81. }
  82. }
  83. export default class Dropdown extends React.PureComponent {
  84. static contextTypes = {
  85. router: PropTypes.object,
  86. };
  87. static propTypes = {
  88. icon: PropTypes.string.isRequired,
  89. items: PropTypes.array.isRequired,
  90. size: PropTypes.number.isRequired,
  91. title: PropTypes.string,
  92. disabled: PropTypes.bool,
  93. status: ImmutablePropTypes.map,
  94. isUserTouching: PropTypes.func,
  95. isModalOpen: PropTypes.bool.isRequired,
  96. onModalOpen: PropTypes.func,
  97. onModalClose: PropTypes.func,
  98. };
  99. static defaultProps = {
  100. title: 'Menu',
  101. };
  102. state = {
  103. expanded: false,
  104. };
  105. handleClick = () => {
  106. if (!this.state.expanded && this.props.isUserTouching() && this.props.onModalOpen) {
  107. const { status, items } = this.props;
  108. this.props.onModalOpen({
  109. status,
  110. actions: items,
  111. onClick: this.handleItemClick,
  112. });
  113. return;
  114. }
  115. this.setState({ expanded: !this.state.expanded });
  116. }
  117. handleClose = () => {
  118. if (this.props.onModalClose) {
  119. this.props.onModalClose();
  120. }
  121. this.setState({ expanded: false });
  122. }
  123. handleKeyDown = e => {
  124. switch(e.key) {
  125. case 'Enter':
  126. this.handleClick();
  127. break;
  128. case 'Escape':
  129. this.handleClose();
  130. break;
  131. }
  132. }
  133. handleItemClick = e => {
  134. const i = Number(e.currentTarget.getAttribute('data-index'));
  135. const { action, to } = this.props.items[i];
  136. this.handleClose();
  137. if (typeof action === 'function') {
  138. e.preventDefault();
  139. action();
  140. } else if (to) {
  141. e.preventDefault();
  142. this.context.router.history.push(to);
  143. }
  144. }
  145. setTargetRef = c => {
  146. this.target = c;
  147. }
  148. findTarget = () => {
  149. return this.target;
  150. }
  151. render () {
  152. const { icon, items, size, title, disabled } = this.props;
  153. const { expanded } = this.state;
  154. return (
  155. <div onKeyDown={this.handleKeyDown}>
  156. <IconButton
  157. icon={icon}
  158. title={title}
  159. active={expanded}
  160. disabled={disabled}
  161. size={size}
  162. ref={this.setTargetRef}
  163. onClick={this.handleClick}
  164. />
  165. <Overlay show={expanded} placement='bottom' target={this.findTarget}>
  166. <DropdownMenu items={items} onClose={this.handleClose} />
  167. </Overlay>
  168. </div>
  169. );
  170. }
  171. }