Skip to content

Commit

Permalink
Fixed the GitHub issue (#10)
Browse files Browse the repository at this point in the history
  • Loading branch information
hovsepyan021 committed Sep 10, 2024
1 parent 76af65c commit bf8df6e
Showing 1 changed file with 23 additions and 28 deletions.
51 changes: 23 additions & 28 deletions src/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -95,8 +95,6 @@ export const GeoapifyGeocoderAutocomplete = ({
const apiKey = React.useContext<string>(GeoapifyApiKey);
let geocoderContainer: HTMLDivElement | null;

let initialized = false;

const geocoderAutocomplete: MutableRefObject<
GeocoderAutocomplete | undefined
> = useRef<GeocoderAutocomplete>();
Expand Down Expand Up @@ -159,34 +157,22 @@ export const GeoapifyGeocoderAutocomplete = ({
},[]);

useEffect(() => {
if (initialized) {
if (geocoderAutocomplete.current) {
geocoderAutocomplete.current.off("select", onSelect);
geocoderAutocomplete.current.off("suggestions", onSuggestions);
geocoderAutocomplete.current.off("input", onUserInput);
geocoderAutocomplete.current.off("close", onClose);
geocoderAutocomplete.current.off("open", onOpen);
}

return;
if(!geocoderAutocomplete.current) {
geocoderAutocomplete.current = new GeocoderAutocomplete(
geocoderContainer as HTMLDivElement,
apiKey,
{
placeholder: placeholderValue || "",
addDetails: addDetailsValue,
skipIcons: skipIconsValue,
skipSelectionOnArrowKey: skipSelectionOnArrowKeyValue,
allowNonVerifiedHouseNumber: allowNonVerifiedHouseNumberValue,
allowNonVerifiedStreet: allowNonVerifiedStreetValue,
debounceDelay: debounceDelayValue || 100
}
);
}

initialized = true;

geocoderAutocomplete.current = new GeocoderAutocomplete(
geocoderContainer as HTMLDivElement,
apiKey,
{
placeholder: placeholderValue || "",
addDetails: addDetailsValue,
skipIcons: skipIconsValue,
skipSelectionOnArrowKey: skipSelectionOnArrowKeyValue,
allowNonVerifiedHouseNumber: allowNonVerifiedHouseNumberValue,
allowNonVerifiedStreet: allowNonVerifiedStreetValue,
debounceDelay: debounceDelayValue || 100
}
);

geocoderAutocomplete.current.on("select", onSelect);
geocoderAutocomplete.current.on("suggestions", onSuggestions);
geocoderAutocomplete.current.on("input", onUserInput);
Expand All @@ -196,6 +182,15 @@ export const GeoapifyGeocoderAutocomplete = ({
if (sendGeocoderRequestFuncValue) {
geocoderAutocomplete.current.setSendGeocoderRequestFunc(sendGeocoderRequestFuncValue)
}
return () => {
if (geocoderAutocomplete.current) {
geocoderAutocomplete.current.off("select", onSelect);
geocoderAutocomplete.current.off("suggestions", onSuggestions);
geocoderAutocomplete.current.off("input", onUserInput);
geocoderAutocomplete.current.off("close", onClose);
geocoderAutocomplete.current.off("open", onOpen);
}
};
}, []);

useEffect(() => {
Expand Down

0 comments on commit bf8df6e

Please sign in to comment.