google-location-autocomplete-react
Advanced tools
Comparing version 1.0.3 to 1.0.4
@@ -24,10 +24,19 @@ var __assign = (this && this.__assign) || function () { | ||
import { jsx as _jsx } from "react/jsx-runtime"; | ||
import { useEffect, useRef } from "react"; | ||
import { useCallback, useEffect, useRef } from "react"; | ||
var GoogleLocationAutocomplete = function (_a) { | ||
var apiKey = _a.apiKey, onPlaceSelected = _a.onPlaceSelected, props = __rest(_a, ["apiKey", "onPlaceSelected"]); | ||
var inputRef = useRef(null); | ||
var handlePlaceSelect = function () { | ||
if (inputRef.current) { | ||
var loadScript = function (url) { | ||
if (!document.querySelector("script[src=\"".concat(url, "\"]"))) { | ||
var script = document.createElement("script"); | ||
script.src = url; | ||
script.async = true; | ||
script.defer = true; | ||
document.head.appendChild(script); | ||
} | ||
}; | ||
var initializeAutocomplete = useCallback(function () { | ||
if (inputRef.current && window.google) { | ||
var autocomplete_1 = new window.google.maps.places.Autocomplete(inputRef.current); | ||
autocomplete_1.addListener('place_changed', function () { | ||
autocomplete_1.addListener("place_changed", function () { | ||
var place = autocomplete_1.getPlace(); | ||
@@ -39,23 +48,23 @@ if (onPlaceSelected) { | ||
} | ||
}; | ||
}, [onPlaceSelected]); | ||
useEffect(function () { | ||
if (window.google && window.initAutocomplete) { | ||
window.initAutocomplete(); | ||
return; | ||
} | ||
window.initAutocomplete = handlePlaceSelect; | ||
window.initAutocomplete = initializeAutocomplete; | ||
var url = "https://maps.googleapis.com/maps/api/js?key=".concat(apiKey, "&libraries=places&callback=initAutocomplete"); | ||
if (!document.querySelector("script[src=\"".concat(url, "\"]"))) { | ||
var script = document.createElement("script"); | ||
script.src = url; | ||
script.async = true; | ||
script.defer = true; | ||
document.head.appendChild(script); | ||
} | ||
loadScript(url); | ||
return function () { | ||
delete window.initAutocomplete; | ||
}; | ||
}, [apiKey, onPlaceSelected]); | ||
}, [apiKey, initializeAutocomplete]); | ||
useEffect(function () { | ||
var handleFocus = function () { | ||
initializeAutocomplete(); | ||
}; | ||
var input = inputRef.current; | ||
input === null || input === void 0 ? void 0 : input.addEventListener("focus", handleFocus); | ||
return function () { | ||
input === null || input === void 0 ? void 0 : input.removeEventListener("focus", handleFocus); | ||
}; | ||
}, [initializeAutocomplete]); | ||
return _jsx("input", __assign({ ref: inputRef, type: "text" }, props)); | ||
}; | ||
export default GoogleLocationAutocomplete; |
{ | ||
"name": "google-location-autocomplete-react", | ||
"version": "1.0.3", | ||
"version": "1.0.4", | ||
"main": "dist/GoogleLocationAutocomplete.js", | ||
@@ -5,0 +5,0 @@ "types": "dist/GoogleLocationAutocomplete.d.ts", |
5914
82