The code powering m.abunchtell.com https://m.abunchtell.com
Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.
 
 
 
 

135 linhas
3.7 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. renderItem = (item, i) => {
  64. if (item === null) {
  65. return <li key={`sep-${i}`} className='dropdown__sep' />;
  66. }
  67. const { text, href = '#' } = item;
  68. return (
  69. <li className='dropdown__content-list-item' key={`${text}-${i}`}>
  70. <a href={href} target='_blank' rel='noopener' onClick={this.handleClick} data-index={i} className='dropdown__content-list-link'>
  71. {text}
  72. </a>
  73. </li>
  74. );
  75. }
  76. render () {
  77. const { icon, items, size, direction, ariaLabel, disabled } = this.props;
  78. const { expanded } = this.state;
  79. const isUserTouching = this.props.isUserTouching();
  80. const directionClass = (direction === 'left') ? 'dropdown__left' : 'dropdown__right';
  81. const iconStyle = { fontSize: `${size}px`, width: `${size}px`, lineHeight: `${size}px` };
  82. const iconClassname = `fa fa-fw fa-${icon} dropdown__icon`;
  83. if (disabled) {
  84. return (
  85. <div className='icon-button disabled' style={iconStyle} aria-label={ariaLabel}>
  86. <i className={iconClassname} aria-hidden />
  87. </div>
  88. );
  89. }
  90. const dropdownItems = expanded && (
  91. <ul className='dropdown__content-list'>
  92. {items.map(this.renderItem)}
  93. </ul>
  94. );
  95. // No need to render the actual dropdown if we use the modal. If we
  96. // don't render anything <Dropdow /> breaks, so we just put an empty div.
  97. const dropdownContent = !isUserTouching ? (
  98. <DropdownContent className={directionClass}>
  99. {dropdownItems}
  100. </DropdownContent>
  101. ) : <div />;
  102. return (
  103. <Dropdown ref={this.setRef} active={isUserTouching ? false : undefined} onShow={this.handleShow} onHide={this.handleHide}>
  104. <DropdownTrigger className='icon-button' style={iconStyle} aria-label={ariaLabel}>
  105. <i className={iconClassname} aria-hidden />
  106. </DropdownTrigger>
  107. {dropdownContent}
  108. </Dropdown>
  109. );
  110. }
  111. }