Skip to content

Commit

Permalink
Add isValidNumberForRegion helper update libphonenumber v7.4.3
Browse files Browse the repository at this point in the history
  • Loading branch information
viqh committed Aug 28, 2016
1 parent ed781ed commit fd39e98
Show file tree
Hide file tree
Showing 8 changed files with 97 additions and 123 deletions.
6 changes: 3 additions & 3 deletions example/assets/example.js

Large diffs are not rendered by default.

Binary file modified example/assets/images/flags.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified example/assets/images/flags@2x.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
94 changes: 38 additions & 56 deletions example/assets/libphonenumber.js

Large diffs are not rendered by default.

1 change: 0 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@
},
"dependencies": {
"classnames": "^2.2.5",
"google-libphonenumber": "^1.1.0",
"underscore.deferred": "^0.4.0"
},
"devDependencies": {
Expand Down
18 changes: 13 additions & 5 deletions src/containers/IntlTelInputApp.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,6 @@ import utils from '../components/utils';
import _ from 'underscore.deferred';
import '../styles/intlTelInput.scss';

const phoneUtil = require('google-libphonenumber').PhoneNumberUtil.getInstance();

export default class IntlTelInputApp extends Component {
static defaultProps = {
css: ['intl-tel-input', ''],
Expand Down Expand Up @@ -144,6 +142,7 @@ export default class IntlTelInputApp extends Component {
this.scrollTo = this.scrollTo.bind(this);
this.notifyPhoneNumberChange = this.notifyPhoneNumberChange.bind(this);
this.isValidNumber = this.isValidNumber.bind(this);
this.isValidNumberForRegion = this.isValidNumberForRegion.bind(this);
this.isUnknownNanp = this.isUnknownNanp.bind(this);
this.initRequests = this.initRequests.bind(this);
this.updateFlagFromNumber = this.updateFlagFromNumber.bind(this);
Expand Down Expand Up @@ -773,10 +772,10 @@ export default class IntlTelInputApp extends Component {
// Allow Main app to do things when a country is selected
if (!isInit && prevCountry.iso2 !== countryCode &&
typeof this.props.onSelectFlag === 'function') {
const number = phoneUtil.parse(this.state.value);
const currentNumber = this.state.value;
const regionCode = this.selectedCountryData.iso2;
const status = phoneUtil.isValidNumberForRegion(number, regionCode);
this.props.onSelectFlag(status, this.state.value, this.selectedCountryData);
const status = this.isValidNumberForRegion(currentNumber, regionCode);
this.props.onSelectFlag(status, currentNumber, this.selectedCountryData);
}
});
}
Expand Down Expand Up @@ -925,6 +924,15 @@ export default class IntlTelInputApp extends Component {
return false;
}

isValidNumberForRegion(number, regionCode) {
const val = utils.trim(number);

if (window.intlTelInputUtils) {
return window.intlTelInputUtils.isValidNumberForRegion(val, regionCode);
}
return false;
}

notifyPhoneNumberChange(newNumber) {
if (typeof this.props.onPhoneNumberChange === 'function') {
const result = this.isValidNumber(newNumber);
Expand Down
94 changes: 38 additions & 56 deletions src/libphonenumber.js

Large diffs are not rendered by default.

7 changes: 5 additions & 2 deletions tests/FlagDropDown-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -339,8 +339,8 @@ describe('FlagDropDown', () => {

it('onSelectFlag', () => {
let expected = '';
const onSelectFlag = (countryData) => {
expected = countryData;
const onSelectFlag = (status, currentNumber, countryData) => {
expected = Object.assign({}, { status, currentNumber, ...countryData });
};

const parent = ReactTestUtils.renderIntoDocument(
Expand All @@ -366,7 +366,10 @@ describe('FlagDropDown', () => {
const japanOption = findDOMNode(
parentDropDownComponent).querySelector('[data-country-code="jp"]');
ReactTestUtils.Simulate.click(japanOption);

assert.deepEqual(expected, {
status: false,
currentNumber: '',
name: 'Japan (日本)',
iso2: 'jp',
dialCode: '81',
Expand Down

0 comments on commit fd39e98

Please sign in to comment.