11import TextField from "@mui/material/TextField" ;
22import PopupState , { bindPopover } from "material-ui-popup-state" ;
3- import { FieldError , FieldValues , get , Path , PathValue , useController } from "react-hook-form" ;
3+ import { FieldError , FieldValues , get , useController } from "react-hook-form" ;
44import { useMarkOnFocusHandler } from "../../hooks/useMarkOnFocusHandler" ;
55import { textFieldBootstrapStyle } from "../../helpers/mui" ;
66import { useFormContext } from "../../context/FormContext" ;
77import { useEffect , useMemo , useRef , useState } from "react" ;
88import Popover from "@mui/material/Popover" ;
99import { TelephoneNumberInputProps } from "../../TelephoneNumberInput" ;
10- import { LabelValueOption } from "../../types/LabelValueOption" ;
11- import { PhoneNumberUtil , RegionCode } from "google-libphonenumber" ;
12- import { getName , langs } from "i18n-iso-countries" ;
13- import {
14- Country ,
15- extractCountryCodeFromTelephoneNumber ,
16- extractNationalNumberFromTelephoneNumber ,
17- getCountryFromCountryCode ,
18- } from "../../helpers/telephoneNumber" ;
19- import { isNullOrWhitespace } from "@neolution-ch/javascript-utils" ;
20- import Autocomplete from "@mui/material/Autocomplete" ;
10+
11+ import { Country , extractCountryCodeFromTelephoneNumber , extractNationalNumberFromTelephoneNumber } from "../../helpers/telephoneNumber" ;
2112import { TelephoneNumberInputAdornment } from "./TelephoneNumberInputAdornment" ;
13+ import { isNullOrWhitespace } from "@neolution-ch/javascript-utils" ;
14+ import { TelephoneNumberAutocomplete } from "./TelephoneNumberAutocomplete" ;
2215
2316const TelephoneNumberInputInternal = < T extends FieldValues > ( props : TelephoneNumberInputProps < T > ) => {
2417 const {
@@ -37,13 +30,11 @@ const TelephoneNumberInputInternal = <T extends FieldValues>(props: TelephoneNum
3730 useBootstrapStyle = false ,
3831 hideValidationMessage,
3932 placeholder,
40- locale,
4133 } = props ;
4234 const {
4335 control,
4436 disabled : formDisabled ,
4537 getFieldState,
46- setValue,
4738 requiredFields,
4839 formState : { errors } ,
4940 hideValidationMessages,
@@ -70,22 +61,6 @@ const TelephoneNumberInputInternal = <T extends FieldValues>(props: TelephoneNum
7061 const fieldIsRequired = label && typeof label === "string" && requiredFields . includes ( name ) ;
7162 const finalLabel = useMemo ( ( ) => ( fieldIsRequired ? `${ String ( label ) } *` : label ) , [ fieldIsRequired , label ] ) ;
7263
73- const countryOptions : LabelValueOption [ ] = useMemo ( ( ) => {
74- const phoneNumberUtil = new PhoneNumberUtil ( ) ;
75- const registeredLocales = langs ( ) ;
76- const internalLocale =
77- registeredLocales . length === 1
78- ? registeredLocales [ 0 ]
79- : ! isNullOrWhitespace ( locale ) && registeredLocales . includes ( locale as string )
80- ? locale
81- : undefined ;
82-
83- return phoneNumberUtil . getSupportedRegions ( ) . map ( ( region ) => ( {
84- label : isNullOrWhitespace ( internalLocale ) ? region : getName ( region , internalLocale as string ) || region ,
85- value : region ,
86- } ) ) ;
87- } , [ locale ] ) ;
88-
8964 // we need to control the country in the case the value inside the form is undefined
9065 const [ country , setCountry ] = useState < Country > ( extractCountryCodeFromTelephoneNumber ( field . value as string | undefined , defaultCountry ) ) ;
9166 const resetCountry = useRef ( true ) ;
@@ -160,34 +135,12 @@ const TelephoneNumberInputInternal = <T extends FieldValues>(props: TelephoneNum
160135 } }
161136 >
162137 { renderAutocompleteField (
163- < Autocomplete
164- options = { countryOptions }
165- value = { countryOptions . find ( ( x ) => x . value === country . region ) || null }
166- disableClearable = { false }
167- renderInput = { ( params ) => < TextField { ...params } /> }
168- sx = { { ...( useBootstrapStyle && textFieldBootstrapStyle ) , width : 200 } }
169- onChange = { ( _ , value , reason ) => {
170- // cannot be cleared
171- if ( value === null || reason === "clear" ) {
172- return ;
173- }
174-
175- const country = getCountryFromCountryCode ( value . value as RegionCode ) ;
176- setCountry ( country ) ;
177- // the value in the form is probably undefined, therefore do not touch the form value
178- if ( isNullOrWhitespace ( nationalPhoneNumber ) ) {
179- // nothing to do, value in the form is not changing
180- } else {
181- const telephoneNumber = `+${ country . code } ${ nationalPhoneNumber || "" } ` ;
182-
183- if ( propsOnChange ) {
184- propsOnChange ( telephoneNumber ) ;
185- }
186-
187- setValue ( name , telephoneNumber as PathValue < T , Path < T > > ) ;
188- }
189- popupState . close ( ) ;
190- } }
138+ < TelephoneNumberAutocomplete < T >
139+ { ...props }
140+ popupState = { popupState }
141+ nationalPhoneNumber = { nationalPhoneNumber }
142+ country = { country }
143+ setCountry = { setCountry }
191144 /> ,
192145 ) }
193146 </ Popover >
0 commit comments