The code powering m.abunchtell.com https://m.abunchtell.com
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.
 
 
 
 

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