Skip to content
Extraits de code Groupes Projets
dropdown_menu.js 2,49 ko
Newer Older
  • Learn to ignore specific revisions
  • import React from 'react';
    
    import Dropdown, { DropdownTrigger, DropdownContent } from 'react-simple-dropdown';
    
    import PropTypes from 'prop-types';
    
    class DropdownMenu extends React.PureComponent {
    
      static contextTypes = {
    
        router: PropTypes.object,
    
      static propTypes = {
        icon: PropTypes.string.isRequired,
        items: PropTypes.array.isRequired,
        size: PropTypes.number.isRequired,
        direction: PropTypes.string,
    
        ariaLabel: PropTypes.string,
    
        ariaLabel: 'Menu',
    
        direction: 'left',
        expanded: false,
    
        this.dropdown = c;
    
      handleClick = (e) => {
        const i = Number(e.currentTarget.getAttribute('data-index'));
    
        const { action, to } = this.props.items[i];
    
    
        // Don't call e.preventDefault() when the item uses 'href' property.
        // ex. "Edit profile" on the account action bar
    
    Eugen Rochko's avatar
    Eugen Rochko a validé
    
        if (typeof action === 'function') {
    
          e.preventDefault();
    
    Eugen Rochko's avatar
    Eugen Rochko a validé
          action();
    
          e.preventDefault();
    
          this.context.router.history.push(to);
    
    Eugen Rochko's avatar
    Eugen Rochko a validé
        }
    
    Eugen Rochko's avatar
    Eugen Rochko a validé
    
    
      handleShow = () => this.setState({ expanded: true })
    
      handleHide = () => this.setState({ expanded: false })
    
    
      renderItem = (item, i) => {
    
    Eugen Rochko's avatar
    Eugen Rochko a validé
        if (item === null) {
    
          return <li key={`sep-${i}`} className='dropdown__sep' />;
    
    Eugen Rochko's avatar
    Eugen Rochko a validé
        }
    
        const { text, action, href = '#' } = item;
    
        return (
    
          <li className='dropdown__content-list-item' key={`${text}-${i}`}>
    
            <a href={href} target='_blank' rel='noopener' onClick={this.handleClick} data-index={i} className='dropdown__content-list-link'>
    
    Eugen Rochko's avatar
    Eugen Rochko a validé
              {text}
            </a>
          </li>
        );
    
    Eugen Rochko's avatar
    Eugen Rochko a validé
    
    
      render () {
    
        const { icon, items, size, direction, ariaLabel } = this.props;
    
        const { expanded } = this.state;
    
        const directionClass = (direction === 'left') ? 'dropdown__left' : 'dropdown__right';
    
        const dropdownItems = expanded && (
          <ul className='dropdown__content-list'>
            {items.map(this.renderItem)}
          </ul>
        );
    
    
          <Dropdown ref={this.setRef} onShow={this.handleShow} onHide={this.handleHide}>
    
            <DropdownTrigger className='icon-button' style={{ fontSize: `${size}px`, width: `${size}px`, lineHeight: `${size}px` }} aria-label={ariaLabel}>
    
              <i className={`fa fa-fw fa-${icon} dropdown__icon`}  aria-hidden />
    
            </DropdownTrigger>
    
    
            <DropdownContent className={directionClass}>
    
              {dropdownItems}
    
            </DropdownContent>
          </Dropdown>
        );
      }
    
    
    export default DropdownMenu;