all files / containers/ IntlTelInputApp.js

94.21% Statements 374/397
78.83% Branches 216/274
93.75% Functions 60/64
94.16% Lines 371/394
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 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080                                                                                                                                               58×   58×   58× 58× 58×   58× 58× 58×   58× 58×   58× 58× 58×   58×   58×                       58×   58×   58×                           58× 58× 58× 58× 58× 58× 58× 58× 58× 58× 58× 58× 58× 58× 58× 58× 58× 58× 58×     58× 58× 58× 58× 58× 58× 58× 58× 58× 58× 58× 58× 58× 58×       58×   58× 58× 58× 58×     58× 55×           58×       58× 58×   58× 58×               58×   58×   58×   58× 58×   58× 20×     58×       129× 18× 18×   111× 111×         129× 38×               58×     56×     56×         56×                                             58× 58×   58×                           51×       47× 47×   47×             47×                                         58×     58×     58×     58×         16531× 15563×     16531× 16531×   16531×                 486× 244×           58×       57×         56×           58× 58× 13852× 13852×   13852× 171×   2679×                 58× 58× 116× 116×   116× 116×             78×     78× 75×   73× 73×                   73×               78× 33×           58×   51× 51×                   58×   56×                                                         43× 43×                                                                               319×                               34×             34×   34×       34×                           14×           14× 14×   14×   10× 10×         10×                           14×                         85×       85×         85× 85×       85×     85× 85×               85× 85× 85× 20171× 85×       85×     85×     85×               85×     85×       85×                   18× 18×       152×                                   85×                                           11× 11× 11×   11× 11× 11× 11× 11× 11× 11×   11×             11×                                 94×   94× 16×   16× 78×   78× 59×   59×   15×     59×             94×                             38×                   43×                                 43×                       11×     11×   11×                                                     10×     10×                     187× 187×   187× 18×     187×     187×                                                                    
import React, { Component, PropTypes } from 'react';
import { findDOMNode } from 'react-dom';
import classNames from 'classnames';
import AllCountries from '../components/AllCountries';
import FlagDropDown from '../components/FlagDropDown';
import TelInput from '../components/TelInput';
import utils from '../components/utils';
import _ from 'underscore.deferred';
import '../styles/intlTelInput.scss';
 
export default class IntlTelInputApp extends Component {
  static defaultProps = {
    css: ['intl-tel-input', ''],
    fieldName: '',
    fieldId: '',
    value: '',
    // define the countries that'll be present in the dropdown
    // defaults to the data defined in `AllCountries`
    countriesData: null,
    // whether or not to allow the dropdown
    allowDropdown: true,
    // if there is just a dial code in the input: remove it on blur, and re-add it on focus
    autoHideDialCode: true,
    // add or remove input placeholder with an example number for the selected country
    autoPlaceholder: true,
    // modify the auto placeholder
    customPlaceholder: null,
    // don't display these countries
    excludeCountries: [],
    // format the input value during initialisation
    formatOnInit: true,
    // display the country dial code next to the selected flag so it's not part of the typed number
    separateDialCode: false,
    // default country
    defaultCountry: '',
    // geoIp lookup function
    geoIpLookup: null,
    // don't insert international dial codes
    nationalMode: true,
    // number type to use for placeholders
    numberType: 'MOBILE',
    // function which can catch the "no this default country" exception
    noCountryDataHandler: null,
    // display only these countries
    onlyCountries: [],
    // the countries at the top of the list. defaults to united states and united kingdom
    preferredCountries: ['us', 'gb'],
    // specify the path to the libphonenumber script to enable validation/formatting
    utilsScript: '',
    onPhoneNumberChange: null,
    onSelectFlag: null,
    disabled: false,
  };
 
  static propTypes = {
    css: PropTypes.arrayOf(PropTypes.string),
    fieldName: PropTypes.string,
    fieldId: PropTypes.string,
    value: PropTypes.string,
    countriesData: PropTypes.arrayOf(PropTypes.array),
    allowDropdown: PropTypes.bool,
    autoHideDialCode: PropTypes.bool,
    autoPlaceholder: PropTypes.bool,
    customPlaceholder: PropTypes.func,
    excludeCountries: PropTypes.arrayOf(PropTypes.string),
    formatOnInit: PropTypes.bool,
    separateDialCode: PropTypes.bool,
    defaultCountry: PropTypes.string,
    geoIpLookup: PropTypes.func,
    nationalMode: PropTypes.bool,
    numberType: PropTypes.string,
    noCountryDataHandler: PropTypes.func,
    onlyCountries: PropTypes.arrayOf(PropTypes.string),
    preferredCountries: PropTypes.arrayOf(PropTypes.string),
    utilsScript: PropTypes.string,
    onPhoneNumberChange: PropTypes.func,
    onSelectFlag: PropTypes.func,
    disabled: PropTypes.bool,
    placeholder: PropTypes.string,
  };
 
  constructor(props) {
    super(props);
 
    this.wrapperClass = {};
 
    this.autoCountry = '';
    this.tempCountry = '';
    this.startedLoadingAutoCountry = false;
 
    this.deferreds = [];
    this.autoCountryDeferred = new _.Deferred();
    this.utilsScriptDeferred = new _.Deferred();
 
    this.isOpening = false;
    this.isMobile = /Android.+Mobile|webOS|iPhone|iPad|iPod|BlackBerry|IEMobile|Opera Mini/i.test(
      navigator.userAgent);
    this.preferredCountries = [];
    this.countries = [];
    this.countryCodes = {};
 
    this.windowLoaded = false;
 
    this.keys = {
      UP: 38,
      DOWN: 40,
      ENTER: 13,
      ESC: 27,
      PLUS: 43,
      A: 65,
      Z: 90,
      SPACE: 32,
      TAB: 9,
    };
 
    this.isGoodBrowser = Boolean(document.createElement('input').setSelectionRange);
 
    this.query = '';
 
    this.state = {
      showDropdown: false,
      highlightedCountry: 0,
      value: '',
      disabled: false,
      readonly: false,
      offsetTop: 0,
      outerHeight: 0,
      placeholder: '',
      title: '',
      countryCode: 'us',
      dialCode: '',
    };
 
    this.selectedCountryData = {};
    this.addCountryCode = this.addCountryCode.bind(this);
    this.autoCountryLoaded = this.autoCountryLoaded.bind(this);
    this.getDialCode = this.getDialCode.bind(this);
    this.handleOnBlur = this.handleOnBlur.bind(this);
    this.handleSelectedFlagKeydown = this.handleSelectedFlagKeydown.bind(this);
    this.setInitialState = this.setInitialState.bind(this);
    this.setNumber = this.setNumber.bind(this);
    this.scrollTo = this.scrollTo.bind(this);
    this.notifyPhoneNumberChange = this.notifyPhoneNumberChange.bind(this);
    this.isValidNumber = this.isValidNumber.bind(this);
    this.isUnknownNanp = this.isUnknownNanp.bind(this);
    this.initRequests = this.initRequests.bind(this);
    this.updateFlagFromNumber = this.updateFlagFromNumber.bind(this);
    this.updatePlaceholder = this.updatePlaceholder.bind(this);
    this.loadAutoCountry = this.loadAutoCountry.bind(this);
    this.loadUtils = this.loadUtils.bind(this);
    this.processCountryData = this.processCountryData.bind(this);
    this.getNumber = this.getNumber.bind(this);
 
    // wrapping actions
    this.setFlag = this.setFlag.bind(this);
    this.clickSelectedFlag = this.clickSelectedFlag.bind(this);
    this.updateValFromNumber = this.updateValFromNumber.bind(this);
    this.handleWindowScroll = this.handleWindowScroll.bind(this);
    this.handleDocumentKeyDown = this.handleDocumentKeyDown.bind(this);
    this.handleDocumentClick = this.handleDocumentClick.bind(this);
    this.bindDocumentClick = this.bindDocumentClick.bind(this);
    this.unbindDocumentClick = this.unbindDocumentClick.bind(this);
    this.searchForCountry = this.searchForCountry.bind(this);
    this.handleEnterKey = this.handleEnterKey.bind(this);
    this.toggleDropdown = this.toggleDropdown.bind(this);
    this.handleUpDownKey = this.handleUpDownKey.bind(this);
    this.handleInputChange = this.handleInputChange.bind(this);
    this.changeHighlightCountry = this.changeHighlightCountry.bind(this);
  }
 
  componentDidMount() {
    this.hadInitialPlaceholder = Boolean(this.props.placeholder);
 
    this.autoHideDialCode = this.props.autoHideDialCode;
    this.allowDropdown = this.props.allowDropdown;
    this.nationalMode = this.props.nationalMode;
    this.dropdownContainer = '';
 
    // if in nationalMode, disable options relating to dial codes
    if (this.nationalMode) {
      this.autoHideDialCode = false;
    }
 
    // if separateDialCode then doesn't make sense to
    // A) insert dial code into input (autoHideDialCode), and
    // B) display national numbers (because we're displaying the country dial code next to them)
    if (this.props.separateDialCode) {
      this.autoHideDialCode = false;
      this.nationalMode = false;
      // let's force this for now for simplicity - we can support this later if need be
      this.allowDropdown = true;
    }
 
    this.processCountryData.call(this);
    this.tempCountry = this.getTempCountry(this.props.defaultCountry);
 
    if (Edocument.readyState === 'complete') {
      this.windowLoaded = true;
    } else {
      window.addEventListener('load', () => {
        this.windowLoaded = true;
      });
    }
 
    // generate the markup
    this.generateMarkup();
    // set the initial state of the input value and the selected flag
    this.setInitialState();
    // utils script, and auto country
    this.initRequests();
 
    this.deferreds.push(this.autoCountryDeferred.promise());
    this.deferreds.push(this.utilsScriptDeferred.promise());
 
    _.when(this.deferreds).done(() => {
      this.setInitialState();
    });
 
    document.addEventListener('keydown', this.handleDocumentKeyDown);
  }
 
  componentWillUpdate(nextProps, nextState) {
    if (nextState.showDropdown) {
      document.addEventListener('keydown', this.handleDocumentKeyDown);
      this.bindDocumentClick();
    } else {
      document.removeEventListener('keydown', this.handleDocumentKeyDown);
      this.unbindDocumentClick();
    }
  }
 
  componentDidUpdate(prevProps, prevState) {
    if (prevState.value !== this.state.value) {
      this.notifyPhoneNumberChange(this.state.value);
    }
  }
 
  componentWillUnmount() {
    document.removeEventListener('keydown', this.handleDocumentKeyDown);
    this.unbindDocumentClick();
  }
 
  getTempCountry(countryCode) {
    if (countryCode === 'auto') {
      return 'auto';
    }
 
    let countryData = utils.getCountryData(this.countries, countryCode);
 
    // check if country is available in the list
    if (!countryData.iso2) {
      if (this.props.preferredCountries.length > 0) {
        countryData = utils.getCountryData(this.countries, this.props.preferredCountries[0]);
      } else {
        countryData = AllCountries.getCountries()[0];
      }
    }
 
    return countryData.iso2;
  }
 
  // set the input value and update the flag
  // NOTE: preventFormat arg is for public method
  setNumber(number, preventFormat) {
    // we must update the flag first, which updates this.selectedCountryData,
    // which is used for formatting the number before displaying it
    this.updateFlagFromNumber(number);
    this.updateValFromNumber(number, !preventFormat);
  }
 
  // get the extension from the current number
  getExtension() {
    if (window.intlTelInputUtils) {
      return window.intlTelInputUtils.getExtension(
        this.getFullNumber(), this.selectedCountryData.iso2);
    }
    return '';
  }
 
  // format the number to the given format
  getNumber(format) {
    if (window.intlTelInputUtils) {
      return window.intlTelInputUtils.formatNumber(
        this.getFullNumber(), this.selectedCountryData.iso2, format);
    }
    return '';
  }
 
  generateMarkup() {
    this.wrapperClass['allow-dropdown'] = this.allowDropdown;
    this.wrapperClass['separate-dial-code'] = this.props.separateDialCode;
 
    if (Ithis.isMobile) {
      utils.addClass(document.querySelector('body'), 'iti-mobile');
      // on mobile, we want a full screen dropdown, so we must append it to the body
      this.dropdownContainer = 'body';
      window.addEventListener('scroll', this.handleWindowScroll);
    }
  }
 
  // this is called when the geoip call returns
  autoCountryLoaded() {
    if (Ethis.tempCountry === 'auto') {
      this.tempCountry = this.autoCountry;
      this.autoCountryDeferred.resolve();
    }
  }
 
  loadUtils() {
    if (window.intlTelInputUtils) {
      this.utilsScriptDeferred.resolve();
      return;
    }
 
    const request = new XMLHttpRequest();
    request.open('GET', this.props.utilsScript, true);
 
    request.onload = () => {
      if (Erequest.status >= 200 && request.status < 400) {
        const data = request.responseText;
 
        if (data && !document.getElementById('intlTelInputUtils')) {
          const oBody = document.getElementsByTagName('body')[0];
          const oScript = document.createElement('script');
          oScript.id = 'intlTelInputUtils';
          oScript.text = data;
          oBody.appendChild(oScript);
        }
 
        this.utilsScriptDeferred.resolve();
      }
    };
 
    request.send();
  }
 
  handleSelectedFlagKeydown(e) {
    if (!this.state.showDropdown &&
       (e.which === this.keys.UP || e.which === this.keys.DOWN ||
        e.which === this.keys.SPACE || e.which === this.keys.ENTER)
      ) {
      // prevent form from being submitted if "ENTER" was pressed
      e.preventDefault();
 
      // prevent event from being handled again by document
      e.stopPropagation();
 
      this.toggleDropdown(true);
    }
 
    // allow navigation from dropdown to input on TAB
    if (e.which === this.keys.TAB) {
      this.toggleDropdown(false);
    }
  }
 
  // prepare all of the country data, including onlyCountries and preferredCountries options
  processCountryData() {
    // format countries data to what is necessary for component function
    // defaults to data defined in `AllCountries`
    AllCountries.initialize(this.props.countriesData);
 
    // process onlyCountries or excludeCountries array if present
    this.processAllCountries.call(this);
 
    // process the countryCodes map
    this.processCountryCodes.call(this);
 
    // set the preferredCountries property
    this.processPreferredCountries.call(this);
  }
 
  // add a country code to countryCodes
  addCountryCode(countryCodes, iso2, dialCode, priority) {
    if (!(dialCode in countryCodes)) {
      countryCodes[dialCode] = [];
    }
 
    const index = priority || 0;
    countryCodes[dialCode][index] = iso2;
 
    return countryCodes;
  }
 
  // filter the given countries using the process function
  filterCountries(countryArray, processFunc) {
    let i;
    // standardise case
    for (i = 0; i < countryArray.length; i++) {
      countryArray[i] = countryArray[i].toLowerCase();
    }
 
    // build instance country array
    this.countries = [];
    for (i = 0; i < AllCountries.getCountries().length; i++) {
      if (processFunc(countryArray.indexOf(AllCountries.getCountries()[i].iso2))) {
        this.countries.push(AllCountries.getCountries()[i]);
      }
    }
  }
 
  processAllCountries() {
    if (this.props.onlyCountries.length) {
      // process onlyCountries option
      this.filterCountries(this.props.onlyCountries, (inArray) =>
        // if country is in array
        inArray !== -1);
    } else if (this.props.excludeCountries.length) {
      // process excludeCountries option
      this.filterCountries(this.props.excludeCountries, (inArray) =>
        // if country is not in array
        inArray === -1);
    } else {
      this.countries = AllCountries.getCountries();
    }
  }
 
  // process the countryCodes map
  processCountryCodes() {
    this.countryCodes = {};
    for (let i = 0; i < this.countries.length; i++) {
      const c = this.countries[i];
      this.addCountryCode(this.countryCodes, c.iso2, c.dialCode, c.priority);
      // area codes
      if (c.areaCodes) {
        for (let j = 0; j < c.areaCodes.length; j++) {
          // full dial code is country code + dial code
          this.addCountryCode(this.countryCodes, c.iso2, c.dialCode + c.areaCodes[j]);
        }
      }
    }
  }
 
  // process preferred countries - iterate through the preferences,
  // fetching the country data for each one
  processPreferredCountries() {
    this.preferredCountries = [];
    for (let i = 0, max = this.props.preferredCountries.length; i < max; i++) {
      const countryCode = this.props.preferredCountries[i].toLowerCase();
      const countryData = utils.getCountryData(this.countries, countryCode, true);
 
      if (EcountryData) {
        this.preferredCountries.push(countryData);
      }
    }
  }
 
  // set the initial state of the input value and the selected flag
  setInitialState() {
    const val = this.props.value || '';
 
    // if we already have a dial code we can go ahead and set the flag, else fall back to default
    if (this.getDialCode(val)) {
      this.updateFlagFromNumber(val, true);
    } else if (this.tempCountry !== 'auto') {
      // see if we should select a flag
      if (Ethis.tempCountry) {
        this.setFlag(this.tempCountry, true);
      } else {
        // no dial code and no tempCountry, so default to first in list
        this.defaultCountry = this.preferredCountries.length ?
          this.preferredCountries[0].iso2 : this.countries[0].iso2;
        if (!val) {
          this.setFlag(this.defaultCountry, true);
        }
      }
      // if empty and no nationalMode and no autoHideDialCode then insert the default dial code
      if (!val && !this.nationalMode && !this.autoHideDialCode && !this.props.separateDialCode) {
        this.setState({
          value: `+${this.selectedCountryData.dialCode}`,
        });
      }
    }
 
    // NOTE: if tempCountry is set to auto, that will be handled separately
    // format
    if (val) {
      this.updateValFromNumber(val, this.props.formatOnInit);
    }
  }
 
  initRequests() {
    // if the user has specified the path to the utils script, fetch it on window.load
    if (this.props.utilsScript) {
      // if the plugin is being initialised after the window.load event has already been fired
      if (Ethis.windowLoaded) {
        this.loadUtils();
      } else {
        // wait until the load event so we don't block any other requests e.g. the flags image
        window.addEventListener('load', () => {
          this.loadUtils();
        });
      }
    } else {
      this.utilsScriptDeferred.resolve();
    }
 
    if (this.tempCountry === 'auto') {
      this.loadAutoCountry();
    } else {
      this.autoCountryDeferred.resolve();
    }
  }
 
  loadAutoCountry() {
    // check for localStorage
    const lsAutoCountry =
      (window.localStorage !== undefined) ? window.localStorage.getItem('itiAutoCountry') : '';
    if (lsAutoCountry) {
      this.autoCountry = lsAutoCountry;
    }
 
    // 3 options:
    // 1) already loaded (we're done)
    // 2) not already started loading (start)
    // 3) already started loading (do nothing - just wait for loading callback to fire)
    if (this.autoCountry) {
      this.autoCountryLoaded();
    } else Eif (!this.startedLoadingAutoCountry) {
      // don't do this twice!
      this.startedLoadingAutoCountry = true;
 
      if (Etypeof this.props.geoIpLookup === 'function') {
        this.props.geoIpLookup((countryCode) => {
          this.autoCountry = countryCode.toLowerCase();
          if (Ewindow.localStorage !== undefined) {
            window.localStorage.setItem('itiAutoCountry', this.autoCountry);
          }
          // tell all instances the auto country is ready
          // TODO: this should just be the current instances
          // UPDATE: use setTimeout in case their geoIpLookup function calls this
          // callback straight away (e.g. if they have already done the geo ip lookup
          // somewhere else).
          // Using setTimeout means that the current thread of execution will finish before
          // executing this, which allows the plugin to finish initialising.
          this.autoCountryLoaded();
        });
      }
    }
  }
 
  cap(number) {
    const max = findDOMNode(this.refs.telInput).getAttribute('maxlength');
    return max && number.length > max ? number.substr(0, max) : number;
  }
 
  removeEmptyDialCode() {
    const value = this.state.value;
    const startsPlus = value.charAt(0) === '+';
 
    if (EstartsPlus) {
      const numeric = utils.getNumeric(value);
      // if just a plus, or if just a dial code
      if (E!numeric || this.selectedCountryData.dialCode === numeric) {
        this.setState({
          value: '',
        });
      }
    }
  }
 
  // highlight the next/prev item in the list (and ensure it is visible)
  handleUpDownKey(key) {
    const current = findDOMNode(this.refs.flagDropDown).querySelectorAll('.highlight')[0];
    const prevElement = (current) ? current.previousElementSibling : undefined;
    const nextElement = (current) ? current.nextElementSibling : undefined;
    let next = (key === this.keys.UP) ? prevElement : nextElement;
 
    if (Enext) {
      // skip the divider
      if (Inext.getAttribute('class').indexOf('divider') > -1) {
        next = (key === this.keys.UP) ? next.previousElementSibling : next.nextElementSibling;
      }
 
      this.scrollTo(next);
 
      const selectedIndex = utils.retrieveLiIndex(next);
 
      this.setState({
        showDropdown: true,
        highlightedCountry: selectedIndex,
      });
    }
  }
 
  // select the currently highlighted item
  handleEnterKey() {
    const current = findDOMNode(this.refs.flagDropDown).querySelectorAll('.highlight')[0];
    if (Ecurrent) {
      const selectedIndex = utils.retrieveLiIndex(current);
      const countryCode = current.getAttribute('data-country-code');
 
      this.setState({
        showDropdown: false,
        highlightedCountry: selectedIndex,
        countryCode,
      }, () => {
        this.setFlag(this.state.countryCode);
        this.unbindDocumentClick();
      });
    }
  }
 
  // find the first list item whose name starts with the query string
  searchForCountry(query) {
    for (let i = 0, max = this.countries.length; i < max; i++) {
      if (utils.startsWith(this.countries[i].name, query)) {
        const listItem = findDOMNode(this.refs.flagDropDown).querySelector(
          `.country-list [data-country-code="${this.countries[i].iso2}"]:not(.preferred)`);
 
        const selectedIndex = utils.retrieveLiIndex(listItem);
 
        // update highlighting and scroll
        this.setState({
          showDropdown: true,
          highlightedCountry: selectedIndex,
        });
        this.scrollTo(listItem, true);
        break;
      }
    }
  }
 
  // update the input's value to the given val (format first if possible)
  // NOTE: this is called from _setInitialState, handleUtils and setNumber
  updateValFromNumber(number, doFormat) {
    if (doFormat && window.intlTelInputUtils && this.selectedCountryData) {
      const format = !this.props.separateDialCode &&
        (this.nationalMode || number.charAt(0) !== '+') ?
          window.intlTelInputUtils.numberFormat.NATIONAL :
          window.intlTelInputUtils.numberFormat.INTERNATIONAL;
      number = window.intlTelInputUtils.formatNumber(number,
        this.selectedCountryData.iso2, format);
    }
 
    number = this.beforeSetNumber(number);
 
    this.setState({
      showDropdown: false,
      value: number,
    }, () => {
      this.unbindDocumentClick();
    });
  }
 
  // check if need to select a new flag based on the given number
  // Note: called from _setInitialState, keyup handler, setNumber
  updateFlagFromNumber(number, isInit) {
    // if we're in nationalMode and we already have US/Canada selected,
    // make sure the number starts with a +1 so getDialCode will be
    // able to extract the area code
    // update: if we dont yet have selectedCountryData,
    // but we're here (trying to update the flag from the number),
    // that means we're initialising the plugin with a number that already
    // has a dial code, so fine to ignore this bit
    if (number && this.nationalMode && this.selectedCountryData &&
        this.selectedCountryData.dialCode === '1' && number.charAt(0) !== '+') {
      if (Enumber.charAt(0) !== '1') {
        number = `1${number}`;
      }
      number = `+${number}`;
    }
 
    // try and extract valid dial code from input
    const dialCode = this.getDialCode(number);
    let countryCode = null;
 
    if (dialCode) {
      // check if one of the matching countries is already selected
      const countryCodes = this.countryCodes[utils.getNumeric(dialCode)];
      const alreadySelected = this.selectedCountryData &&
        countryCodes.indexOf(this.selectedCountryData.iso2) !== -1;
 
      // if a matching country is not already selected
      // (or this is an unknown NANP area code): choose the first in the list
      if (!alreadySelected || this.isUnknownNanp(number, dialCode)) {
        // if using onlyCountries option, countryCodes[0] may be empty,
        // so we must find the first non-empty index
        for (let j = 0; j < countryCodes.length; j++) {
          if (EcountryCodes[j]) {
            countryCode = countryCodes[j];
            break;
          }
        }
      }
    } else Iif (number.charAt(0) === '+' && utils.getNumeric(number).length) {
      // invalid dial code, so empty
      // Note: use getNumeric here because the number has not been
      // formatted yet, so could contain bad chars
      countryCode = '';
    } else if (!number || number === '+') {
      // empty, or just a plus, so default
      countryCode = this.defaultCountry;
    }
 
    if (countryCode !== null) {
      this.setFlag(countryCode, isInit);
    }
  }
 
  // check if the given number contains an unknown area code from
  // the North American Numbering Plan i.e. the only dialCode that
  // could be extracted was +1 but the actual number's length is >=4
  isUnknownNanp(number, dialCode) {
    return (dialCode === '+1' && utils.getNumeric(number).length >= 4);
  }
 
  // select the given flag, update the placeholder and the active list item
  // Note: called from setInitialState, updateFlagFromNumber, selectListItem, setCountry
  setFlag(countryCode, isInit) {
    const prevCountry = this.selectedCountryData &&
      this.selectedCountryData.iso2 ? this.selectedCountryData : {};
 
    // do this first as it will throw an error and stop if countryCode is invalid
    this.selectedCountryData = countryCode ?
      utils.getCountryData(this.countries, countryCode, false, false,
        this.props.noCountryDataHandler) : {};
 
    // update the defaultCountry - we only need the iso2 from now on, so just store that
    if (Ethis.selectedCountryData.iso2) {
      this.defaultCountry = this.selectedCountryData.iso2;
    }
 
    // update the selected country's title attribute
    const title = countryCode ?
      `${this.selectedCountryData.name}: +${this.selectedCountryData.dialCode}` : 'Unknown';
 
    let dialCode = this.state.dialCode;
    if (this.props.separateDialCode) {
      dialCode = this.selectedCountryData.dialCode ?
        `+${this.selectedCountryData.dialCode}` : '';
 
      if (prevCountry.dialCode) {
        delete this.wrapperClass[`iti-sdc-${(prevCountry.dialCode.length + 1)}`];
      }
 
      if (EdialCode) {
        this.wrapperClass[`iti-sdc-${dialCode.length}`] = true;
      }
    }
 
    let selectedIndex = 0;
    if (EcountryCode && countryCode !== 'auto') {
      for (let i = 0, max = this.countries.length; i < max; i++) {
        if (this.countries[i].iso2 === countryCode) {
          selectedIndex = i;
        }
      }
 
      selectedIndex += this.preferredCountries.length;
    }
 
    if (this.state.showDropdown) {
      findDOMNode(this.refs.telInput).focus();
    }
 
    this.setState({
      showDropdown: false,
      highlightedCountry: selectedIndex,
      countryCode,
      title,
      dialCode,
    }, () => {
      // and the input's placeholder
      this.updatePlaceholder();
 
      // update the active list item
      this.wrapperClass.active = false;
 
      // on change flag, trigger a custom event
      // Allow Main app to do things when a country is selected
      if (!isInit && prevCountry.iso2 !== countryCode &&
          typeof this.props.onSelectFlag === 'function') {
        this.props.onSelectFlag(this.selectedCountryData);
      }
    });
  }
 
  handleOnBlur() {
    this.removeEmptyDialCode();
  }
 
  bindDocumentClick() {
    this.isOpening = true;
    document.querySelector('html').addEventListener('click', this.handleDocumentClick);
  }
 
  unbindDocumentClick() {
    document.querySelector('html').removeEventListener('click', this.handleDocumentClick);
  }
 
  clickSelectedFlag() {
    if (E!this.state.showDropdown &&
        !this.state.disabled &&
        !this.state.readonly) {
      this.setState({
        showDropdown: true,
        offsetTop: utils.offset(findDOMNode(this.refs.telInput)).top,
        outerHeight: utils.getOuterHeight(findDOMNode(this.refs.telInput)),
      }, () => {
        const highlightItem = findDOMNode(this.refs.flagDropDown).querySelector('.highlight');
        if (EhighlightItem) {
          this.scrollTo(highlightItem, true);
        }
      });
    }
  }
 
  // update the input placeholder to an
  // example number from the currently selected country
  updatePlaceholder() {
    if (window.intlTelInputUtils && !this.hadInitialPlaceholder &&
        this.props.autoPlaceholder && this.selectedCountryData) {
      const numberType = window.intlTelInputUtils.numberType[this.props.numberType];
      let placeholder = this.selectedCountryData.iso2 ?
        window.intlTelInputUtils.getExampleNumber(this.selectedCountryData.iso2,
          this.nationalMode, numberType) : '';
 
      placeholder = this.beforeSetNumber(placeholder);
 
      if (typeof this.props.customPlaceholder === 'function') {
        placeholder = this.props.customPlaceholder(placeholder, this.selectedCountryData);
      }
 
      this.setState({
        placeholder,
      });
    }
  }
 
  toggleDropdown(status) {
    this.setState({
      showDropdown: !!status,
    }, () => {
      if (!this.state.showDropdown) {
        this.unbindDocumentClick();
      }
    });
  }
 
  // check if an element is visible within it's container, else scroll until it is
  scrollTo(element, middle) {
    try {
      const container = findDOMNode(this.refs.flagDropDown).querySelector('.country-list');
      const containerHeight = parseFloat(
        window.getComputedStyle(container).getPropertyValue('height'), 10);
      const containerTop = utils.offset(container).top;
      const containerBottom = containerTop + containerHeight;
      const elementHeight = utils.getOuterHeight(element);
      const elementTop = utils.offset(element).top;
      const elementBottom = elementTop + elementHeight;
      const middleOffset = (containerHeight / 2) - (elementHeight / 2);
      let newScrollTop = elementTop - containerTop + container.scrollTop;
 
      if (IelementTop < containerTop) {
        // scroll up
        if (middle) {
          newScrollTop -= middleOffset;
        }
 
        container.scrollTop = newScrollTop;
      } else Iif (elementBottom > containerBottom) {
        // scroll down
        if (middle) {
          newScrollTop += middleOffset;
        }
 
        const heightDifference = containerHeight - elementHeight;
        container.scrollTop = newScrollTop - heightDifference;
      }
    } catch (err) {
      // do nothing
    }
  }
 
  // try and extract a valid international dial code from a full telephone number
  // Note: returns the raw string inc plus character and any whitespace/dots etc
  getDialCode(number) {
    let dialCode = '';
    // only interested in international numbers (starting with a plus)
    if (number.charAt(0) === '+') {
      let numericChars = '';
      // iterate over chars
      for (let i = 0, max = number.length; i < max; i++) {
        const c = number.charAt(i);
        // if char is number
        if (utils.isNumeric(c)) {
          numericChars += c;
          // if current numericChars make a valid dial code
          if (this.countryCodes[numericChars]) {
            // store the actual raw string (useful for matching later)
            dialCode = number.substr(0, i + 1);
          }
          // longest dial code is 4 chars
          if (numericChars.length === 4) {
            break;
          }
        }
      }
    }
 
    return dialCode;
  }
 
  // get the input val, adding the dial code if separateDialCode is enabled
  getFullNumber() {
    const prefix = this.props.separateDialCode ?
      `+${this.selectedCountryData.dialCode}` : '';
    return prefix + this.state.value;
  }
 
  // validate the input val - assumes the global function isValidNumber (from utilsScript)
  isValidNumber(number) {
    const val = utils.trim(number);
    const countryCode = (this.nationalMode) ? this.selectedCountryData.iso2 : '';
 
    if (window.intlTelInputUtils) {
      return window.intlTelInputUtils.isValidNumber(val, countryCode);
    }
    return false;
  }
 
  notifyPhoneNumberChange(newNumber) {
    if (typeof this.props.onPhoneNumberChange === 'function') {
      const result = this.isValidNumber(newNumber);
      const fullNumber = window.intlTelInputUtils ?
        this.getNumber(window.intlTelInputUtils.numberFormat.INTERNATIONAL) :
        newNumber;
      this.props.onPhoneNumberChange(
        result, newNumber, this.selectedCountryData,
        fullNumber, this.getExtension());
    }
  }
 
  // remove the dial code if separateDialCode is enabled
  beforeSetNumber(number) {
    if (this.props.separateDialCode) {
      let dialCode = this.getDialCode(number);
      if (EdialCode) {
        // US dialCode is "+1", which is what we want
        // CA dialCode is "+1 123", which is wrong - should be "+1"
        // (as it has multiple area codes)
        // AS dialCode is "+1 684", which is what we want
        // Solution: if the country has area codes, then revert to just the dial code
        if (Ithis.selectedCountryData.areaCodes !== null) {
          dialCode = `+${this.selectedCountryData.dialCode}`;
        }
        // a lot of numbers will have a space separating the dial code
        // and the main number, and some NANP numbers will have a hyphen
        // e.g. +1 684-733-1234 - in both cases we want to get rid of it
        // NOTE: don't just trim all non-numerics as may want to preserve
        // an open parenthesis etc
        const start = number[dialCode.length] === ' ' ||
          number[dialCode.length] === '-' ? dialCode.length + 1 : dialCode.length;
        number = number.substr(start);
      }
    }
 
    return this.cap(number);
  }
 
  handleWindowScroll() {
    this.setState({
      showDropdown: false,
    }, () => {
      window.removeEventListener('scroll', this.handleDocumentScroll);
    });
  }
 
  handleDocumentKeyDown(e) {
    let queryTimer;
    // prevent down key from scrolling the whole page,
    // and enter key from submitting a form etc
    e.preventDefault();
 
    if (e.which === this.keys.UP || e.which === this.keys.DOWN) {
      // up and down to navigate
      this.handleUpDownKey(e.which);
    } else if (e.which === this.keys.ENTER) {
      // enter to select
      this.handleEnterKey();
    } else if (e.which === this.keys.ESC) {
      // esc to close
      this.setState({
        showDropdown: false,
      });
    } else Eif ((e.which >= this.keys.A && e.which <= this.keys.Z) || e.which === this.keys.SPACE) {
      // upper case letters (note: keyup/keydown only return upper case letters)
      // jump to countries that start with the query string
      if (IqueryTimer) {
        clearTimeout(queryTimer);
      }
 
      if (!this.query) {
        this.query = '';
      }
      this.query += String.fromCharCode(e.which);
      this.searchForCountry(this.query);
      // if the timer hits 1 second, reset the query
      queryTimer = setTimeout(() => {
        this.query = '';
      }, 1000);
    }
  }
 
  handleDocumentClick(e) {
    // Click at the outside of country list
    if (Ee.target.getAttribute('class') === null ||
      (e.target.getAttribute('class') &&
       e.target.getAttribute('class').indexOf('country') === -1)) {
      this.isOpening = false;
    }
 
    if (E!this.isOpening) {
      this.toggleDropdown(false);
    }
    this.isOpening = false;
  }
 
  handleInputChange(e) {
    this.setState({
      value: e.target.value,
    }, () => {
      this.updateFlagFromNumber(this.state.value);
    });
  }
 
  changeHighlightCountry(showDropdown, selectedIndex) {
    this.setState({
      showDropdown,
      highlightedCountry: selectedIndex,
    });
  }
 
  render() {
    this.wrapperClass[this.props.css[0]] = true;
    const inputClass = this.props.css[1];
 
    if (this.state.showDropdown) {
      this.wrapperClass.expanded = true;
    }
 
    let wrapperClass = classNames(this.wrapperClass);
 
    return (
      <div className={wrapperClass}>
        <FlagDropDown ref="flagDropDown"
          allowDropdown={this.allowDropdown}
          dropdownContainer={this.dropdownContainer}
          separateDialCode={this.props.separateDialCode}
          dialCode={this.state.dialCode}
          clickSelectedFlag={this.clickSelectedFlag}
          setFlag={this.setFlag}
          countryCode={this.state.countryCode}
          isMobile={this.isMobile}
          handleSelectedFlagKeydown={this.handleSelectedFlagKeydown}
          changeHighlightCountry={this.changeHighlightCountry}
          countries={this.countries}
          showDropdown={this.state.showDropdown}
          inputTop={this.state.offsetTop}
          inputOuterHeight={this.state.outerHeight}
          preferredCountries={this.preferredCountries}
          highlightedCountry={this.state.highlightedCountry}
        />
        <TelInput ref="telInput"
          handleInputChange={this.handleInputChange}
          handleOnBlur={this.handleOnBlur}
          className={inputClass}
          disabled={this.state.disabled}
          readonly={this.state.readonly}
          fieldName={this.props.fieldName}
          fieldId={this.props.fieldId}
          value={this.state.value}
          placeholder={this.state.placeholder}
        />
      </div>
    );
  }
}