|
1 | | -import { Dispatch, SetStateAction, useMemo, useState } from "react"; |
| 1 | +import { Dispatch, SetStateAction, useMemo, useRef } from "react"; |
2 | 2 | import { FieldValues, Path, PathValue } from "react-hook-form"; |
3 | 3 | import { Country, getCountriesOptions, getCountryFromCountryCode } from "../../helpers/telephoneNumber"; |
4 | 4 | import { TelephoneNumberInputProps } from "../../TelephoneNumberInput"; |
@@ -32,35 +32,25 @@ const TelephoneNumberAutocomplete = <T extends FieldValues>(props: TelephoneNumb |
32 | 32 | setCountry, |
33 | 33 | } = props; |
34 | 34 |
|
35 | | - const [isOpen, setIsOpen] = useState(false); |
36 | 35 | const countryOptions: LabelValueOption[] = useMemo(() => getCountriesOptions(pinnedCountries, locale), [locale, pinnedCountries]); |
37 | 36 | const { setValue } = useFormContext<T>(); |
38 | | - |
| 37 | + const inputRef = useRef<HTMLInputElement | null>(null); |
39 | 38 | return ( |
40 | 39 | <Autocomplete |
41 | 40 | options={countryOptions} |
42 | 41 | value={countryOptions.find((x) => x.value === country.region) || null} |
43 | 42 | disableClearable={false} |
44 | | - open={isOpen} |
45 | | - onOpen={() => setIsOpen(true)} |
46 | | - onClose={() => setIsOpen(false)} |
47 | 43 | getOptionDisabled={(option) => option.disabled ?? false} |
48 | | - renderInput={(params) => <TextField {...params} />} |
| 44 | + renderInput={(params) => <TextField {...params} inputRef={inputRef} />} |
49 | 45 | sx={{ ...(useBootstrapStyle && textFieldBootstrapStyle), width: 200 }} |
50 | | - onInputChange={(_, _value, reason) => { |
51 | | - if (reason === "clear") { |
52 | | - setIsOpen(true); |
53 | | - return; |
54 | | - } |
55 | | - }} |
56 | 46 | onChange={(_, value, reason) => { |
57 | 47 | // cannot be cleared |
58 | | - if (value === null) { |
| 48 | + if (reason === "clear") { |
| 49 | + inputRef.current?.dispatchEvent(new MouseEvent("mousedown", { bubbles: true })); |
59 | 50 | return; |
60 | 51 | } |
61 | 52 |
|
62 | | - if (reason === "clear") { |
63 | | - setIsOpen(true); |
| 53 | + if (value === null) { |
64 | 54 | return; |
65 | 55 | } |
66 | 56 |
|
|
0 commit comments