@@ -23,7 +23,14 @@ function extractDigits(input: string): string {
2323}
2424
2525function formatUS ( digits : string ) : string {
26- const d = digits . slice ( 0 , 10 ) ;
26+ // Handle case where an 11-digit number with leading "1" is provided (common in autofill)
27+ let d = digits ;
28+ if ( digits . length === 11 && digits . startsWith ( '1' ) ) {
29+ d = digits . slice ( 1 ) ; // Remove the leading "1" country code
30+ } else {
31+ d = digits . slice ( 0 , 10 ) ; // Otherwise just take first 10 digits as before
32+ }
33+
2734 if ( d . length === 0 ) return '' ;
2835 if ( d . length <= 3 ) return `(${ d } ` ;
2936 if ( d . length <= 6 ) return `(${ d . slice ( 0 , 3 ) } ) ${ d . slice ( 3 ) } ` ;
@@ -62,7 +69,8 @@ export const PhoneNumberInput = ({
6269 const formatted = typer . input ( normalized ) ;
6370 setDisplay ( formatted ) ;
6471 } else {
65- const digits = extractDigits ( String ( value ) ) . slice ( 0 , 10 ) ;
72+ // Remove the slice(0, 10) to allow handling 11-digit numbers with leading 1
73+ const digits = extractDigits ( String ( value ) ) ;
6674 setDisplay ( formatUS ( digits ) ) ;
6775 }
6876 } , [ value , isInternational ] ) ;
@@ -80,10 +88,19 @@ export const PhoneNumberInput = ({
8088 return ;
8189 }
8290
83- const digits = extractDigits ( raw ) . slice ( 0 , 10 ) ;
91+ // Remove the slice(0, 10) to allow handling 11-digit numbers with leading 1
92+ const digits = extractDigits ( raw ) ;
93+ // Handle case where an 11-digit number with leading "1" is provided
94+ let normalizedDigits = digits ;
95+ if ( digits . length === 11 && digits . startsWith ( '1' ) ) {
96+ normalizedDigits = digits . slice ( 1 ) ; // Remove the leading "1" country code
97+ } else {
98+ normalizedDigits = digits . slice ( 0 , 10 ) ; // Otherwise just take first 10 digits
99+ }
100+
84101 const formatted = formatUS ( digits ) ;
85102 setDisplay ( formatted ) ;
86- onChange ?.( digits || undefined ) ;
103+ onChange ?.( normalizedDigits || undefined ) ;
87104 } ;
88105
89106 const handleKeyDown = ( e : KeyboardEvent < HTMLInputElement > ) => {
@@ -92,13 +109,17 @@ export const PhoneNumberInput = ({
92109 const isNumberKey = NUMBER_KEY_REGEX . test ( e . key ) ;
93110 const isModifier = e . ctrlKey || e . metaKey || e . altKey ;
94111 const allowed = [ 'Backspace' , 'Delete' , 'ArrowLeft' , 'ArrowRight' , 'Tab' , 'Home' , 'End' , 'Enter' ] ;
95- if ( ! isModifier && isNumberKey && currentDigits . length >= 10 ) {
112+
113+ // Allow typing if we have fewer than 10 digits or if we have 11 digits but the first is '1'
114+ const isComplete = currentDigits . length >= 10 && ! ( currentDigits . length === 11 && currentDigits . startsWith ( '1' ) ) ;
115+
116+ if ( ! isModifier && isNumberKey && isComplete ) {
96117 // Prevent adding more digits once 10-digit US number is complete
97118 e . preventDefault ( ) ;
98119 return ;
99120 }
100121 if ( allowed . includes ( e . key ) ) return ;
101- // Allow other typical keys; restriction handled by formatting and slice(0,10)
122+ // Allow other typical keys; restriction handled by formatting
102123 }
103124 } ;
104125
0 commit comments