all files / src/components/ CountryList.js

45.54% Statements 46/101
54.39% Branches 31/57
84.21% Functions 16/19
12.28% Lines 7/57
7 statements, 10 functions, 16 branches Ignored     
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                                                                                                                                                                                                                                                                                                                          
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 = {
    isMobile: PropTypes.bool,
    selectFlag: 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.handleChangeCountry = this.handleChangeCountry.bind(this);
    this.selectFlag = this.selectFlag.bind(this);
    this.appendListItem = this.appendListItem.bind(this);
    this.setDropdownPosition = this.setDropdownPosition.bind(this);
  }
 
  componentWillReceiveProps(nextProps) {
    if (nextProps.showDropdown && !nextProps.isMobile) {
      findDOMNode(this.refs.listElement).setAttribute('class', 'country-list v-hide');
      this.setDropdownPosition();
    }
  }
 
  shouldComponentUpdate(nextProps) {
    return !utils.shallowEquals(this.props, nextProps);
  }
 
  setDropdownPosition() {
    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');
  }
 
  appendListItem(countries, className) {
    const preferredCountriesCount = this.props.preferredCountries.length;
 
    return countries.map((country, index) => {
      if (this.props.isMobile) {
        return (
          <option key={`country-${index}`}
            data-dial-code={country.dialCode} value={country.iso2}
          >
            {`${country.name} +${country.dialCode}`}
          </option>
        );
      }
 
      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.selectFlag, country.iso2)}
        >
          <div ref="selectedFlag" className="flag">
            <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>
      );
    });
  }
 
  selectFlag(iso2) {
    this.props.selectFlag(iso2);
  }
 
  handleMouseOver(e) {
    if (e.currentTarget.getAttribute('class').indexOf('country') > -1) {
      const selectedIndex = utils.retrieveLiIndex(e.currentTarget);
      this.props.changeHighlightCountry(true, selectedIndex);
    }
  }
 
  handleChangeCountry(e) {
    this.selectFlag(e.target.value);
  }
 
  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 (this.props.isMobile) {
      options = this.appendListItem(countries, '');
 
      return (
        <select className="iti-mobile-select" onChange={this.handleChangeCountry}>
          {options}
        </select>
      );
    }
 
    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;