all files / components/ CountryList.js

100% Statements 54/54
60% Branches 12/20
100% Functions 10/10
100% Lines 51/51
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   21563× 21563×                               58× 58× 58× 58×       129× 18× 18×         129×       18×     18× 18×     18×   18× 18× 18×   18×     18×   18× 18×             235×   235× 21563× 21563×       21563× 21563× 21563×     21563×                                             147× 147× 147× 147× 147×       147×   147× 88×   88×       147×     147×                  
import React, { Component, PropTypes } from 'react';
import { findDOMNode } from 'react-dom';
import classNames from 'classnames';
import utils from './utils';
 
function partial(fn, ...args) {
  return fn.bind(fn, ...args);
}
 
class CountryList extends Component {
  static propTypes = {
    dropdownContainer: PropTypes.string,
    setFlag: PropTypes.func,
    countries: PropTypes.array,
    inputTop: PropTypes.number,
    inputOuterHeight: PropTypes.number,
    preferredCountries: PropTypes.array,
    highlightedCountry: PropTypes.number,
    changeHighlightCountry: PropTypes.func,
    showDropdown: PropTypes.bool,
  };
 
  constructor() {
    super();
    this.handleMouseOver = this.handleMouseOver.bind(this);
    this.setFlag = this.setFlag.bind(this);
    this.appendListItem = this.appendListItem.bind(this);
    this.setDropdownPosition = this.setDropdownPosition.bind(this);
  }
 
  componentWillReceiveProps(nextProps) {
    if (nextProps.showDropdown) {
      findDOMNode(this.refs.listElement).setAttribute('class', 'country-list v-hide');
      this.setDropdownPosition();
    }
  }
 
  shouldComponentUpdate(nextProps) {
    return !utils.shallowEquals(this.props, nextProps);
  }
 
  setDropdownPosition() {
    utils.removeClass(
      findDOMNode(this.refs.listElement), 'hide');
 
    const inputTop = this.props.inputTop;
    const windowTop = (window.pageYOffset !== undefined) ?
      window.pageYOffset :
      (document.documentElement || document.body.parentNode || document.body).scrollTop;
    const windowHeight = window.innerHeight || document.documentElement.clientHeight ||
                         document.body.clientHeight;
    const inputOuterHeight = this.props.inputOuterHeight;
    const countryListOuterHeight = utils.getOuterHeight(findDOMNode(this.refs.listElement));
    const dropdownFitsBelow =
      (inputTop + inputOuterHeight + countryListOuterHeight < windowTop + windowHeight);
    const dropdownFitsAbove = (inputTop - countryListOuterHeight > windowTop);
 
    // dropdownHeight - 1 for border
    const cssTop = (!dropdownFitsBelow && dropdownFitsAbove) ?
                   `-${(countryListOuterHeight - 1)}px` : '';
    findDOMNode(this.refs.listElement).style.top = cssTop;
    findDOMNode(this.refs.listElement).setAttribute('class', 'country-list');
  }
 
  setFlag(iso2) {
    this.props.setFlag(iso2);
  }
 
  appendListItem(countries, className) {
    const preferredCountriesCount = this.props.preferredCountries.length;
 
    return countries.map((country, index) => {
      const actualIndex = (className === 'preferred') ? index : index + preferredCountriesCount;
      const countryClassObj = {
        country: true,
        highlight: (this.props.highlightedCountry === actualIndex),
      };
      let countryClass = undefined;
      countryClassObj[className] = true;
      countryClass = classNames(countryClassObj);
 
      return (
        <li key={`country-${index}`}
          className={countryClass}
          data-dial-code={country.dialCode}
          data-country-code={country.iso2}
          onMouseOver={this.handleMouseOver}
          onClick={partial(this.setFlag, country.iso2)}
        >
          <div ref="selectedFlag" className="flag-box">
            <div ref="selectedFlagInner" className={`iti-flag ${country.iso2}`}></div>
          </div>
 
          <span className="country-name">{country.name}</span>
          <span className="dial-code">+{country.dialCode}</span>
        </li>
      );
    });
  }
 
  handleMouseOver(e) {
    if (Ee.currentTarget.getAttribute('class').indexOf('country') > -1) {
      const selectedIndex = utils.retrieveLiIndex(e.currentTarget);
      this.props.changeHighlightCountry(true, selectedIndex);
    }
  }
 
  render() {
    let options = '';
    const preferredCountries = this.props.preferredCountries;
    let preferredOptions = undefined;
    const countries = this.props.countries;
    const className = classNames({
      'country-list': true,
      hide: !this.props.showDropdown,
    });
    let divider = undefined;
 
    if (preferredCountries.length) {
      preferredOptions = this.appendListItem(preferredCountries, 'preferred');
      divider = (
        <div className="divider"></div>
      );
    }
 
    options = this.appendListItem(countries, '');
 
    return (
      <ul ref="listElement" className={className}>
        {preferredOptions}
        {divider}
        {options}
      </ul>
    );
  }
}
 
export default CountryList;