@@ -22,15 +22,21 @@ const parsePhoneNumber: ParsePhoneNumber = (value, data, formattedNumber) => {
2222 const isoCode = data ?. countryCode ;
2323 const countryCodePattern = / \+ \d + / ;
2424 const areaCodePattern = / \( ( \d + ) \) / ;
25+ const dialCodePattern = / ^ \+ [ \d \s ] + \( [ \d \s ] + \) / ;
2526
2627 /** Parses the matching partials of the phone number by predefined regex patterns */
2728 const countryCodeMatch = formattedNumber ? ( formattedNumber . match ( countryCodePattern ) || [ ] ) : [ ] ;
2829 const areaCodeMatch = formattedNumber ? ( formattedNumber . match ( areaCodePattern ) || [ ] ) : [ ] ;
30+ const dialCodeMatch = formattedNumber ? ( formattedNumber . match ( dialCodePattern ) || [ ] ) : [ ] ;
2931
3032 /** Converts the parsed values of the country and area codes to integers if values present */
3133 const countryCode = countryCodeMatch . length > 0 ? parseInt ( countryCodeMatch [ 0 ] ) : null ;
3234 const areaCode = areaCodeMatch . length > 1 ? parseInt ( areaCodeMatch [ 1 ] ) : null ;
3335
36+ /** Obtaining the dial code for comparing to the existing one - if the country mask contains an area code */
37+ const dialCode = dialCodeMatch . length > 0 ? dialCodeMatch [ 0 ] . replaceAll ( / [ + \s ( ) ] / g, "" ) : null ;
38+ const dialChanged = dialCode !== data ?. dialCode ;
39+
3440 /** Parses the phone number by removing the country and area codes from the formatted value */
3541 const phoneNumberPattern = new RegExp ( `^${ countryCode } ${ ( areaCode || "" ) } (\\d+)` ) ;
3642 const phoneNumberMatch = value ? ( value . match ( phoneNumberPattern ) || [ ] ) : [ ] ;
@@ -43,7 +49,7 @@ const parsePhoneNumber: ParsePhoneNumber = (value, data, formattedNumber) => {
4349 rules . phoneNumber . includes ( ( phoneNumber || "" ) . toString ( ) . length ) ,
4450 ] . every ( Boolean ) ;
4551
46- return { countryCode, areaCode, phoneNumber, isoCode, valid} ;
52+ return { countryCode, areaCode, phoneNumber, isoCode, valid, dialChanged } ;
4753}
4854
4955const PhoneInput = ( {
@@ -74,21 +80,22 @@ const PhoneInput = ({
7480 } , [ inputClassProxy , size ] ) ;
7581
7682 const onChange : ReactPhoneOnChange = ( value , data , event , formattedNumber ) => {
77- const metadata = parsePhoneNumber ( value , data , formattedNumber ) ;
83+ const { dialChanged , ... metadata } = parsePhoneNumber ( value , data , formattedNumber ) ;
7884 const code = metadata . isoCode as ISO2Code ;
7985
8086 if ( code !== currentCode ) {
8187 /** Clears phone number when the country is selected manually */
82- handleChange ( { ...metadata , areaCode : null , phoneNumber : null } , event ) ;
88+ metadata . areaCode = dialChanged ? null : metadata . areaCode ;
89+ metadata . phoneNumber = null ;
90+ metadata . valid = false ;
8391 setCurrentCode ( code ) ;
84- return ;
8592 }
8693
8794 handleChange ( metadata , event ) ;
8895 }
8996
9097 const onMount : ReactPhoneOnMount = ( rawValue , { countryCode, ...event } , formattedNumber ) => {
91- const metadata = parsePhoneNumber ( rawValue , { countryCode} , formattedNumber ) ;
98+ const { dialChanged , ... metadata } = parsePhoneNumber ( rawValue , { countryCode} , formattedNumber ) ;
9299 /** Initiates the current country code with the code of initial value */
93100 setCurrentCode ( metadata . isoCode as ISO2Code ) ;
94101 /** Initializes the existing value */
0 commit comments