All files Dropdown.js

92.31% Statements 24/26
77.78% Branches 21/27
87.5% Functions 7/8
92.31% Lines 24/26
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 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187                30x 30x         56x                                                                   206x   206x 206x   206x                                                               17x   17x 2x     17x 1x         69x   69x   206x 206x     206x           206x   206x                       86x   86x       86x                                     2x                                 2x                                      
import React, { Component } from 'react';
import PropTypes from 'prop-types';
import classNames from 'classnames';
 
import css from './Dropdown.css';
 
export default class Dropdown extends Component {
  constructor(props) {
    super(props);
    this.state = {
      highlightedItem: props.highlightedOption
    };
  }
  componentWillReceiveProps(nextProp) {
    this.setState({
      highlightedItem: nextProp.highlightedOption
    });
  }
 
  /**
   * Returns an optionGroup
   *
   * @param {object} option
   * @returns {DOM}
   */
  getOptionGroup(option) {
    return (
      <li
        key={option.optionGroup}
        className={css['dropdown-option-group-title']}
      >
        {this.props.optionGroupTitles[option.optionGroup]}
      </li>
    );
  }
 
  /**
   * Returns an option item
   *
   * @param {object} option
   * @returns {DOM}
   */
  getOptionItem(option) {
    let {
      highlightedOption,
      orderOptionsBy,
      darkTheme,
      onSelect
    } = this.props;
 
    let selector = option.key ? 'key' : this.props.orderOptionsBy;
    let isHighlighted = highlightedOption && highlightedOption[selector] === option[selector];
 
    return (
      <li
        key={option.key || option.id}
        ref={isHighlighted ? 'highlightedOption' : null}
        // eslint-disable-next-line react/jsx-no-bind
        onClick={onSelect.bind(null, option)}
        className={classNames(css.option,
          {
            [css.darkTheme]: darkTheme,
            [css[option.className]]: option.className,
            [css.highlight]: isHighlighted,
            [`ship-select--option-${option.optionGroup}`]: !!option.optionGroup
          }
        )}
      >
        {option.icon ?
          React.cloneElement(option.icon, { className: classNames(option.icon.props.className, css['option-icon']) })
          : null}
        {option[orderOptionsBy]}
        {option.body ?
          <span className={css['option-body']}>{option.body}</span>
          : null}
      </li>
    );
  }
 
  /**
   * Render
   * @param  {string} msg  the message
   * @return {JSX}
   */
  renderNoOptionsMessage() {
    let message = this.props.noOptionsMessage || `No options matching "${this.props.filterText}"`;
 
    if (this.props.loading) {
      message = 'Loading...';
    }
 
    if (this.props.filterText.length > 0) {
      return <li className={css['no-results']}>{message}</li>;
    }
  }
 
  renderOptions() {
    let { options } = this.props;
 
    return options
      .map((option) => {
        let currentOptionGroup = -1;
        let elements = [];
 
        // If there is any option groups
        Iif (option.optionGroup && (currentOptionGroup < 0
          || currentOptionGroup !== option.optionGroup)) {
          elements.push(this.getOptionGroup(option));
        }
 
        // Get each option
        elements.push(this.getOptionItem(option));
 
        return elements;
      });
  }
 
  render() {
    let {
      togglePosition,
      darkTheme,
      open,
      style,
      onKeyDown,
      options
    } = this.props;
 
    let togglePositionClass = togglePosition === 'left' ?
      css['left-toggle'] :
      css['right-toggle'];
 
    return (
      <ul
        ref='optionContainer'
        className={classNames(css.dropdown, togglePositionClass, {
          [css.open]: open,
          [css.darkTheme]: darkTheme
        })}
        style={style}
        onKeyDown={onKeyDown}
      >
        {options.length > 0 ?
          this.renderOptions() :
          this.renderNoOptionsMessage()}
      </ul>
    );
  }
}
 
// default props
Dropdown.defaultProps = {
  open:               false,
  loading:            false,
  darkTheme:          false,
 
  filterText:          '',
  togglePosition:     'left',
  noOptionsMessage:   '',
 
  style:              {},
  highlightedOption:  {},
 
  options:            [],
  optionGroupTitles:  []
};
 
// prop types checking
Dropdown.propTypes = {
  open:               PropTypes.bool,
  loading:            PropTypes.bool,
  darkTheme:          PropTypes.bool,
 
  filterText:          PropTypes.string,
  togglePosition:     PropTypes.string,
  noOptionsMessage:   PropTypes.string,
  orderOptionsBy:     PropTypes.string.isRequired,
 
  style:              PropTypes.object,
  highlightedOption:  PropTypes.object,
 
  options:            PropTypes.array,
  optionGroupTitles:  PropTypes.array,
 
  onKeyDown:          PropTypes.func.isRequired,
  onSelect:           PropTypes.func.isRequired
};