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.

75 lines
1.9 KiB

  1. import React from 'react';
  2. import Dropdown, { DropdownTrigger, DropdownContent } from 'react-simple-dropdown';
  3. import PropTypes from 'prop-types';
  4. class DropdownMenu extends React.PureComponent {
  5. static propTypes = {
  6. icon: PropTypes.string.isRequired,
  7. items: PropTypes.array.isRequired,
  8. size: PropTypes.number.isRequired,
  9. direction: PropTypes.string,
  10. ariaLabel: PropTypes.string
  11. };
  12. static defaultProps = {
  13. ariaLabel: "Menu"
  14. };
  15. state = {
  16. direction: 'left'
  17. };
  18. setRef = (c) => {
  19. this.dropdown = c;
  20. }
  21. handleClick = (e) => {
  22. const i = Number(e.currentTarget.getAttribute('data-index'));
  23. const { action } = this.props.items[i];
  24. if (typeof action === 'function') {
  25. e.preventDefault();
  26. action();
  27. this.dropdown.hide();
  28. }
  29. }
  30. renderItem = (item, i) => {
  31. if (item === null) {
  32. return <li key={ 'sep' + i } className='dropdown__sep' />;
  33. }
  34. const { text, action, href = '#' } = item;
  35. return (
  36. <li className='dropdown__content-list-item' key={ text + i }>
  37. <a href={href} target='_blank' rel='noopener' onClick={this.handleClick} data-index={i} className='dropdown__content-list-link'>
  38. {text}
  39. </a>
  40. </li>
  41. );
  42. }
  43. render () {
  44. const { icon, items, size, direction, ariaLabel } = this.props;
  45. const directionClass = (direction === "left") ? "dropdown__left" : "dropdown__right";
  46. return (
  47. <Dropdown ref={this.setRef}>
  48. <DropdownTrigger className='icon-button' style={{ fontSize: `${size}px`, width: `${size}px`, lineHeight: `${size}px` }} aria-label={ariaLabel}>
  49. <i className={ `fa fa-fw fa-${icon} dropdown__icon` } aria-hidden={true} />
  50. </DropdownTrigger>
  51. <DropdownContent className={directionClass}>
  52. <ul className='dropdown__content-list'>
  53. {items.map(this.renderItem)}
  54. </ul>
  55. </DropdownContent>
  56. </Dropdown>
  57. );
  58. }
  59. }
  60. export default DropdownMenu;