File tree Expand file tree Collapse file tree 2 files changed +13
-15
lines changed
Expand file tree Collapse file tree 2 files changed +13
-15
lines changed Load Diff This file was deleted.
Original file line number Diff line number Diff line change @@ -68,12 +68,22 @@ export function usePhoneNumberField({
6868 // Prevent re-initializing the phone input more than once
6969 const isInitializedRef = useRef < boolean > ( false ) ;
7070
71- const [ regionCode , setRegionCode ] = useState < RegionCode > ( defaultRegionCode || allowedRegionCodes ?. [ 0 ] || 'US' ) ;
72- const [ phoneNumberInputValue , setPhoneNumberInputValue ] = useState < string > ( '' ) ;
73-
7471 // Value can be either string or object based on afterRead hook, so always extract E.164 string to avoid issues
7572 const e164Value = extractE164FromValue ( value ) ;
7673
74+ const determineInitialRegionCode = ( ) => {
75+ if ( e164Value ) {
76+ const parsedPhoneNumber = parseE164ToNationalFormat ( e164Value ) ;
77+ if ( parsedPhoneNumber ?. regionCode ) {
78+ return parsedPhoneNumber . regionCode ;
79+ }
80+ }
81+ return defaultRegionCode || allowedRegionCodes ?. [ 0 ] || 'US' ;
82+ } ;
83+
84+ const [ regionCode , setRegionCode ] = useState < RegionCode > ( determineInitialRegionCode ) ;
85+ const [ phoneNumberInputValue , setPhoneNumberInputValue ] = useState < string > ( '' ) ;
86+
7787 const filteredCountryOptions = useMemo ( ( ) => {
7888 const regions = allowedRegionCodes ?. length ? allowedRegionCodes : countries . map ( ( country ) => country . regionCode ) ;
7989 return countryOptions . filter ( ( option ) => regions . includes ( option . value as RegionCode ) ) ;
You can’t perform that action at this time.
0 commit comments