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.

105 lines
2.8 KiB

  1. import React from 'react';
  2. import Dropdown, { DropdownTrigger, DropdownContent } from 'react-simple-dropdown';
  3. import PropTypes from 'prop-types';
  4. export default class DropdownMenu extends React.PureComponent {
  5. static contextTypes = {
  6. router: PropTypes.object,
  7. };
  8. static propTypes = {
  9. icon: PropTypes.string.isRequired,
  10. items: PropTypes.array.isRequired,
  11. size: PropTypes.number.isRequired,
  12. direction: PropTypes.string,
  13. ariaLabel: PropTypes.string,
  14. disabled: PropTypes.bool,
  15. };
  16. static defaultProps = {
  17. ariaLabel: 'Menu',
  18. };
  19. state = {
  20. direction: 'left',
  21. expanded: false,
  22. };
  23. setRef = (c) => {
  24. this.dropdown = c;
  25. }
  26. handleClick = (e) => {
  27. const i = Number(e.currentTarget.getAttribute('data-index'));
  28. const { action, to } = this.props.items[i];
  29. // Don't call e.preventDefault() when the item uses 'href' property.
  30. // ex. "Edit profile" on the account action bar
  31. if (typeof action === 'function') {
  32. e.preventDefault();
  33. action();
  34. } else if (to) {
  35. e.preventDefault();
  36. this.context.router.history.push(to);
  37. }
  38. this.dropdown.hide();
  39. }
  40. handleShow = () => this.setState({ expanded: true })
  41. handleHide = () => this.setState({ expanded: false })
  42. renderItem = (item, i) => {
  43. if (item === null) {
  44. return <li key={`sep-${i}`} className='dropdown__sep' />;
  45. }
  46. const { text, href = '#' } = item;
  47. return (
  48. <li className='dropdown__content-list-item' key={`${text}-${i}`}>
  49. <a href={href} target='_blank' rel='noopener' onClick={this.handleClick} data-index={i} className='dropdown__content-list-link'>
  50. {text}
  51. </a>
  52. </li>
  53. );
  54. }
  55. render () {
  56. const { icon, items, size, direction, ariaLabel, disabled } = this.props;
  57. const { expanded } = this.state;
  58. const directionClass = (direction === 'left') ? 'dropdown__left' : 'dropdown__right';
  59. const iconStyle = { fontSize: `${size}px`, width: `${size}px`, lineHeight: `${size}px` };
  60. const iconClassname = `fa fa-fw fa-${icon} dropdown__icon`;
  61. if (disabled) {
  62. return (
  63. <div className='icon-button disabled' style={iconStyle} aria-label={ariaLabel}>
  64. <i className={iconClassname} aria-hidden />
  65. </div>
  66. );
  67. }
  68. const dropdownItems = expanded && (
  69. <ul className='dropdown__content-list'>
  70. {items.map(this.renderItem)}
  71. </ul>
  72. );
  73. return (
  74. <Dropdown ref={this.setRef} onShow={this.handleShow} onHide={this.handleHide}>
  75. <DropdownTrigger className='icon-button' style={iconStyle} aria-label={ariaLabel}>
  76. <i className={iconClassname} aria-hidden />
  77. </DropdownTrigger>
  78. <DropdownContent className={directionClass}>
  79. {dropdownItems}
  80. </DropdownContent>
  81. </Dropdown>
  82. );
  83. }
  84. }