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.
 
 
 
 

147 lines
4.1 KiB

  1. import React from 'react';
  2. import ImmutablePropTypes from 'react-immutable-proptypes';
  3. import Dropdown, { DropdownTrigger, DropdownContent } from 'react-simple-dropdown';
  4. import PropTypes from 'prop-types';
  5. export default class DropdownMenu extends React.PureComponent {
  6. static contextTypes = {
  7. router: PropTypes.object,
  8. };
  9. static propTypes = {
  10. isUserTouching: PropTypes.func,
  11. isModalOpen: PropTypes.bool.isRequired,
  12. onModalOpen: PropTypes.func,
  13. onModalClose: PropTypes.func,
  14. icon: PropTypes.string.isRequired,
  15. items: PropTypes.array.isRequired,
  16. size: PropTypes.number.isRequired,
  17. direction: PropTypes.string,
  18. status: ImmutablePropTypes.map,
  19. ariaLabel: PropTypes.string,
  20. disabled: PropTypes.bool,
  21. };
  22. static defaultProps = {
  23. ariaLabel: 'Menu',
  24. isModalOpen: false,
  25. isUserTouching: () => false,
  26. };
  27. state = {
  28. direction: 'left',
  29. expanded: false,
  30. };
  31. setRef = (c) => {
  32. this.dropdown = c;
  33. }
  34. handleClick = (e) => {
  35. const i = Number(e.currentTarget.getAttribute('data-index'));
  36. const { action, to } = this.props.items[i];
  37. if (this.props.isModalOpen) {
  38. this.props.onModalClose();
  39. }
  40. // Don't call e.preventDefault() when the item uses 'href' property.
  41. // ex. "Edit profile" on the account action bar
  42. if (typeof action === 'function') {
  43. e.preventDefault();
  44. action();
  45. } else if (to) {
  46. e.preventDefault();
  47. this.context.router.history.push(to);
  48. }
  49. this.dropdown.hide();
  50. }
  51. handleShow = () => {
  52. if (this.props.isUserTouching()) {
  53. this.props.onModalOpen({
  54. status: this.props.status,
  55. actions: this.props.items,
  56. onClick: this.handleClick,
  57. });
  58. } else {
  59. this.setState({ expanded: true });
  60. }
  61. }
  62. handleHide = () => this.setState({ expanded: false })
  63. handleToggle = (e) => {
  64. if (e.key === 'Enter') {
  65. if (this.props.isUserTouching()) {
  66. this.handleShow();
  67. } else {
  68. this.setState({ expanded: !this.state.expanded });
  69. }
  70. } else if (e.key === 'Escape') {
  71. this.setState({ expanded: false });
  72. }
  73. }
  74. renderItem = (item, i) => {
  75. if (item === null) {
  76. return <li key={`sep-${i}`} className='dropdown__sep' />;
  77. }
  78. const { text, href = '#' } = item;
  79. return (
  80. <li className='dropdown__content-list-item' key={`${text}-${i}`}>
  81. <a href={href} target='_blank' rel='noopener' role='button' tabIndex='0' autoFocus={i === 0} onClick={this.handleClick} data-index={i} className='dropdown__content-list-link'>
  82. {text}
  83. </a>
  84. </li>
  85. );
  86. }
  87. render () {
  88. const { icon, items, size, direction, ariaLabel, disabled } = this.props;
  89. const { expanded } = this.state;
  90. const isUserTouching = this.props.isUserTouching();
  91. const directionClass = (direction === 'left') ? 'dropdown__left' : 'dropdown__right';
  92. const iconStyle = { fontSize: `${size}px`, width: `${size}px`, lineHeight: `${size}px` };
  93. const iconClassname = `fa fa-fw fa-${icon} dropdown__icon`;
  94. if (disabled) {
  95. return (
  96. <div className='icon-button disabled' style={iconStyle} aria-label={ariaLabel}>
  97. <i className={iconClassname} aria-hidden />
  98. </div>
  99. );
  100. }
  101. const dropdownItems = expanded && (
  102. <ul role='group' className='dropdown__content-list' onClick={this.handleHide}>
  103. {items.map(this.renderItem)}
  104. </ul>
  105. );
  106. // No need to render the actual dropdown if we use the modal. If we
  107. // don't render anything <Dropdow /> breaks, so we just put an empty div.
  108. const dropdownContent = !isUserTouching ? (
  109. <DropdownContent className={directionClass} >
  110. {dropdownItems}
  111. </DropdownContent>
  112. ) : <div />;
  113. return (
  114. <Dropdown ref={this.setRef} active={isUserTouching ? false : expanded} onShow={this.handleShow} onHide={this.handleHide}>
  115. <DropdownTrigger className='icon-button' style={iconStyle} role='button' aria-pressed={expanded} onKeyDown={this.handleToggle} tabIndex='0' aria-label={ariaLabel}>
  116. <i className={iconClassname} aria-hidden />
  117. </DropdownTrigger>
  118. {dropdownContent}
  119. </Dropdown>
  120. );
  121. }
  122. }