Skip to content

Commit 8973849

Browse files
authored
Fix behaviour of Clear Icon (#153)
1 parent 3e27dd3 commit 8973849

2 files changed

Lines changed: 10 additions & 16 deletions

File tree

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
77

88
## [Unreleased]
99

10+
### Fixed
11+
12+
- Open menu on search country input clear for `TelephoneNumberInput`
13+
1014
## [3.13.0] - 2025-11-13
1115

1216
### Fixed

src/lib/components/TelephoneNumberInput/TelephoneNumberAutocomplete.tsx

Lines changed: 6 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { Dispatch, SetStateAction, useMemo, useState } from "react";
1+
import { Dispatch, SetStateAction, useMemo, useRef } from "react";
22
import { FieldValues, Path, PathValue } from "react-hook-form";
33
import { Country, getCountriesOptions, getCountryFromCountryCode } from "../../helpers/telephoneNumber";
44
import { TelephoneNumberInputProps } from "../../TelephoneNumberInput";
@@ -32,35 +32,25 @@ const TelephoneNumberAutocomplete = <T extends FieldValues>(props: TelephoneNumb
3232
setCountry,
3333
} = props;
3434

35-
const [isOpen, setIsOpen] = useState(false);
3635
const countryOptions: LabelValueOption[] = useMemo(() => getCountriesOptions(pinnedCountries, locale), [locale, pinnedCountries]);
3736
const { setValue } = useFormContext<T>();
38-
37+
const inputRef = useRef<HTMLInputElement | null>(null);
3938
return (
4039
<Autocomplete
4140
options={countryOptions}
4241
value={countryOptions.find((x) => x.value === country.region) || null}
4342
disableClearable={false}
44-
open={isOpen}
45-
onOpen={() => setIsOpen(true)}
46-
onClose={() => setIsOpen(false)}
4743
getOptionDisabled={(option) => option.disabled ?? false}
48-
renderInput={(params) => <TextField {...params} />}
44+
renderInput={(params) => <TextField {...params} inputRef={inputRef} />}
4945
sx={{ ...(useBootstrapStyle && textFieldBootstrapStyle), width: 200 }}
50-
onInputChange={(_, _value, reason) => {
51-
if (reason === "clear") {
52-
setIsOpen(true);
53-
return;
54-
}
55-
}}
5646
onChange={(_, value, reason) => {
5747
// cannot be cleared
58-
if (value === null) {
48+
if (reason === "clear") {
49+
inputRef.current?.dispatchEvent(new MouseEvent("mousedown", { bubbles: true }));
5950
return;
6051
}
6152

62-
if (reason === "clear") {
63-
setIsOpen(true);
53+
if (value === null) {
6454
return;
6555
}
6656

0 commit comments

Comments
 (0)