Skip to content

issue with common dial codes #48

Open
@ujwal-setlur

Description

@ujwal-setlur

When country is switched to another country that has the same international prefix, say USA and Canada, then when you start typing the phone number, the flag switches back to the first one. This is due to this code in index.js:

  updateFlagAndFormatNumber(number, actionAfterSetState = null) {
    const { allowZeroAfterCountryCode, initialCountry } = this.props;
    let iso2 = initialCountry;
    let phoneNumber = number;

    if (number) {
      if (phoneNumber[0] !== "+") phoneNumber = `+${phoneNumber}`;
      phoneNumber = allowZeroAfterCountryCode
        ? phoneNumber
        : this.possiblyEliminateZeroAfterCountryCode(phoneNumber);
      iso2 = PhoneNumber.getCountryCodeOfNumber(phoneNumber);
    }
    this.setState({ iso2, formattedNumber: phoneNumber }, actionAfterSetState);
  }

This in turn calls PhoneNumber.getCountryCodeOfNumber from phoneNumber.js:

  getCountryCodeOfNumber(number) {
    const dialCode = this.getDialCode(number);
    const numeric = this.getNumeric(dialCode);
    const countryCode = Country.getCountryCodes()[numeric];

    // countryCode[0] can be null -> get first element that is not null
    if (countryCode) {
      return _.first(countryCode.filter(iso2 => iso2));
    }

    return '';
  }

You will notice that the first element (in this case US) gets returned. We should allow for overlapping prefixes.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions