Skip to content

Commit 66a1a35

Browse files
authored
Merge pull request patw0929#308 from DPOrganizer/feature/on-phone-number-focus
Add optional onPhoneNumberFocus function
2 parents ced4b09 + 0362b6b commit 66a1a35

File tree

4 files changed

+59
-24
lines changed

4 files changed

+59
-24
lines changed

.storybook/stories/3.Playground/Playground.stories.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ storiesOf('Documentation', module)
3535
preferredCountries={array('preferredCountries', defaultProps.preferredCountries)}
3636
onPhoneNumberChange={action('onPhoneNumberChange')}
3737
onPhoneNumberBlur={action('onPhoneNumberBlur')}
38+
onPhoneNumberFocus={action('onPhoneNumberFocus')}
3839
onSelectFlag={action('onSelectFlag')}
3940
disabled={boolean('disabled', defaultProps.disabled)}
4041
placeholder={text('placeholder', defaultProps.placeholder)}

src/components/IntlTelInput.js

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -840,6 +840,23 @@ class IntlTelInput extends Component {
840840
}
841841
};
842842

843+
handleOnFocus = e => {
844+
if (typeof this.props.onPhoneNumberFocus === 'function') {
845+
const value = this.state.value;
846+
const fullNumber = this.formatFullNumber(value);
847+
const isValid = this.isValidNumber(fullNumber);
848+
849+
this.props.onPhoneNumberFocus(
850+
isValid,
851+
value,
852+
this.selectedCountryData,
853+
fullNumber,
854+
this.getExtension(value),
855+
e
856+
);
857+
}
858+
};
859+
843860
bindDocumentClick = () => {
844861
this.isOpening = true;
845862
document
@@ -1293,6 +1310,7 @@ class IntlTelInput extends Component {
12931310
refCallback={this.setTelRef}
12941311
handleInputChange={this.handleInputChange}
12951312
handleOnBlur={this.handleOnBlur}
1313+
handleOnFocus={this.handleOnFocus}
12961314
className={inputClass}
12971315
disabled={this.state.disabled}
12981316
readonly={this.state.readonly}
@@ -1367,6 +1385,8 @@ IntlTelInput.propTypes = {
13671385
onPhoneNumberChange: PropTypes.func,
13681386
/** Optional validation callback function. It returns validation status, input box value and selected country data. */
13691387
onPhoneNumberBlur: PropTypes.func,
1388+
/** Optional validation callback function. It returns validation status, input box value and selected country data. */
1389+
onPhoneNumberFocus: PropTypes.func,
13701390
/** Allow main app to do things when a country is selected. */
13711391
onSelectFlag: PropTypes.func,
13721392
/** Disable this component. */
@@ -1430,6 +1450,7 @@ IntlTelInput.defaultProps = {
14301450
preferredCountries: ['us', 'gb'],
14311451
onPhoneNumberChange: null,
14321452
onPhoneNumberBlur: null,
1453+
onPhoneNumberFocus: null,
14331454
onSelectFlag: null,
14341455
disabled: false,
14351456
autoFocus: false,

src/components/TelInput.js

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ export default class TelInput extends Component {
1212
placeholder: PropTypes.string,
1313
handleInputChange: PropTypes.func,
1414
handleOnBlur: PropTypes.func,
15+
handleOnFocus: PropTypes.func,
1516
autoFocus: PropTypes.bool,
1617
autoComplete: PropTypes.string,
1718
inputProps: PropTypes.object, // eslint-disable-line react/forbid-prop-types
@@ -45,8 +46,12 @@ export default class TelInput extends Component {
4546
}
4647
};
4748

48-
handleFocus = () => {
49+
handleFocus = e => {
4950
this.setState({ hasFocus: true });
51+
52+
if (typeof this.props.handleOnFocus === 'function') {
53+
this.props.handleOnFocus(e);
54+
}
5055
};
5156

5257
render() {

src/components/__tests__/TelInput.test.js

Lines changed: 31 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -242,31 +242,39 @@ describe('TelInput', function() {
242242
expect(subject.state().value).toBe('');
243243
});
244244

245-
it('onPhoneNumberBlur', () => {
246-
let expected = '';
247-
const onPhoneNumberBlur = (
248-
isValid,
249-
newNumber,
250-
countryData,
251-
fullNumber,
252-
ext,
253-
event
254-
) => {
255-
const { type } = event;
256-
257-
expected = `${isValid},${newNumber},${
258-
countryData.iso2
259-
},${fullNumber},${ext},${type}`;
260-
};
245+
const testOnPhoneNumberEvent = ({ property, eventType }) =>
246+
it(`${property}`, () => {
247+
let expected = '';
248+
const onPhoneNumberEvent = (
249+
isValid,
250+
newNumber,
251+
countryData,
252+
fullNumber,
253+
ext,
254+
event
255+
) => {
256+
const { type } = event;
257+
258+
expected = `${isValid},${newNumber},${
259+
countryData.iso2
260+
},${fullNumber},${ext},${type}`;
261+
};
262+
263+
this.params[property] = onPhoneNumberEvent;
264+
const subject = this.makeSubject();
265+
const inputComponent = subject.find(TelInput);
261266

262-
this.params.onPhoneNumberBlur = onPhoneNumberBlur;
263-
const subject = this.makeSubject();
264-
const inputComponent = subject.find(TelInput);
267+
inputComponent.simulate('change', { target: { value: '+886911222333' } });
268+
inputComponent.simulate(eventType);
269+
expect(expected).toBe(
270+
`true,+886911222333,tw,+886 911 222 333,null,${eventType}`
271+
);
272+
});
265273

266-
inputComponent.simulate('change', { target: { value: '+886911222333' } });
267-
inputComponent.simulate('blur');
268-
expect(expected).toBe('true,+886911222333,tw,+886 911 222 333,null,blur');
269-
});
274+
[
275+
{ property: 'onPhoneNumberBlur', eventType: 'blur' },
276+
{ property: 'onPhoneNumberFocus', eventType: 'focus' },
277+
].forEach(testOnPhoneNumberEvent);
270278

271279
it('should has empty value with false nationalMode, false autoHideDialCode and false separateDialCode', () => {
272280
this.params = {

0 commit comments

Comments
 (0)