Skip to content

Commit

Permalink
Merge pull request #214 from adrienharnay/patch-2
Browse files Browse the repository at this point in the history
Provide fullNumber and isValid when onSelectFlag
  • Loading branch information
patw0929 committed Jul 23, 2018
2 parents e368560 + 4e0941a commit 2a50547
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 6 deletions.
12 changes: 9 additions & 3 deletions __tests__/FlagDropDown.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import fs from 'fs';
import IntlTelInput from '../src/components/IntlTelInputApp';
import FlagDropDown from '../src/components/FlagDropDown';
import CountryList from '../src/components/CountryList';
import TelInput from '../src/components/TelInput';

describe('FlagDropDown', function () { // eslint-disable-line func-names
let libphonenumberUtils;
Expand Down Expand Up @@ -440,26 +441,31 @@ describe('FlagDropDown', function () { // eslint-disable-line func-names

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

this.params.onSelectFlag = onSelectFlag;
const subject = this.makeSubject();
const flagComponent = subject.find(FlagDropDown);
const inputComponent = subject.find(TelInput);
const countryListComponent = subject.find(CountryList);

requests[0].respond(200,
{ 'Content-Type': 'text/javascript' },
libphonenumberUtils);
window.eval(getScript().text);

inputComponent.simulate('change', { target: { value: '+8109012345678' } });
flagComponent.simulate('click');
const japanOption = countryListComponent.find('[data-country-code="jp"]');

japanOption.simulate('click');

expect(expected).toEqual({
currentNumber: '',
currentNumber: '+8109012345678',
fullNumber: '+81 90-1234-5678',
isValid: true,
name: 'Japan (日本)',
iso2: 'jp',
dialCode: '81',
Expand Down
10 changes: 9 additions & 1 deletion src/components/IntlTelInputApp.js
Original file line number Diff line number Diff line change
Expand Up @@ -311,7 +311,15 @@ class IntlTelInputApp extends Component {
typeof this.props.onSelectFlag === 'function') {
const currentNumber = this.state.value;

this.props.onSelectFlag(currentNumber, this.selectedCountryData);
const fullNumber = this.formatFullNumber(currentNumber);
const isValid = this.isValidNumber(fullNumber);

this.props.onSelectFlag(
currentNumber,
this.selectedCountryData,
fullNumber,
isValid,
);
}
});
}
Expand Down
4 changes: 2 additions & 2 deletions src/example.js
Original file line number Diff line number Diff line change
Expand Up @@ -58,8 +58,8 @@ class DemoComponent extends Component {
});
}

selectFlagHandler(name, currentNumber, selectedCountryData) {
log(currentNumber, selectedCountryData);
selectFlagHandler(name, currentNumber, selectedCountryData, fullNumber, isValid) {
log(currentNumber, selectedCountryData, fullNumber, isValid);
this.setState({
[name]: currentNumber,
});
Expand Down

0 comments on commit 2a50547

Please sign in to comment.