All files / components/menu/menu-item index.js

0% Statements 0/90
0% Branches 0/57
0% Functions 0/18
0% Lines 0/43
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132                                                                                                                                                                                                                                                                       
import React, { PureComponent } from 'react'
import PropTypes from 'prop-types'
import cx from 'classnames'
import MenuItemStyled from './menu-item.styled'
import styled from 'styled-components'
 
class MenuItem extends PureComponent {
  constructor(props) {
    super(props);
    this.state = {
      opened: props.opened || false,
      data: props.data || {}
    }
  }
 
  handleMenuItemClick = (e) => {
    e.preventDefault()
    e.stopPropagation()
    const { data } = this.state
 
    this.setState({
      opened: !this.state.opened
    }, () => this.handleClick(data));
  }
 
  handleClick = data => {
    const { onSelect, onToggleMenu, parentIndex, ownIndex } = this.props
 
    if (data.items && data.items.length > 0) {
      onToggleMenu && onToggleMenu(data, parentIndex, ownIndex)
    }
    else {
      onSelect && onSelect(data, parentIndex, ownIndex)
    }
  }
 
  normalizeChildren = items => {
    if (items.length > 0) {
      const { opened } = this.state
      const { onSelect, onToggleMenu, prefix, parentIndex } = this.props
 
      return (
        <div
          className={cx('menu-item-submenu', {
            'menu-item-submenu-opened': opened
          })}>
          {
            items.map((item, index) => {
              return NormalizeMenuItems(item, `sub-${prefix}`, onSelect, onToggleMenu, parentIndex, index)
            })
          }
        </div>
      )
    }
    else {
      return null;
    }
  }
 
  render () {
    const { opened } = this.state;
    const { items = [] } = this.props
 
    return (
      <MenuItemStyled>
        <div
          onClick={this.handleMenuItemClick}
          className={cx('menu-item', {
            'menu-item-active': this.props.active
          })}
        >
          {this.props.children}
        </div>
        {this.normalizeChildren(items)}
      </MenuItemStyled>
    )
  }
}
 
MenuItem.propTypes = {
  active: PropTypes.bool,
  onSelectMenu: PropTypes.func,
  items: PropTypes.array,
  opened: PropTypes.bool
};
 
MenuItem.defaultProps = {
  opened: false,
  items: []
};
 
const Indicator = styled.div`
  width: 6px;
  height: 6px;
  border-radius: 50%;
  background-color: ${p => p.color};
  margin-right: 10px;
`
 
const NormalizeMenuItems = (item, prefix, onSelectMenu, onToggleMenu, parentIndex, index) => {
 
  let icon = null
  if(item.active || index == null)
  {
    const _icon = item.icon || <Indicator color="#c5d0e1" />
    icon = React.cloneElement(_icon, {active:item.active})
  }
  else
    icon = <Indicator color="transparent" />
 
  return (
    <MenuItem
      key={item.key}
      active={item.active}
      prefix={prefix}
      opened={item.opened}
      items={item.items || []}
      parentIndex={parentIndex}
      ownIndex={index}
      onSelect={onSelectMenu}
      onToggleMenu={onToggleMenu}
      data={item}
    >
      {icon}
      <span>{item.label}</span>
    </MenuItem>
  )
}
 
export { MenuItem }
export { NormalizeMenuItems }