Skip to content

Commit 0d99a03

Browse files
authored
Merge branch 'master' into dependabot/npm_and_yarn/ua-parser-js-0.7.33
2 parents 915ae4b + 16aece0 commit 0d99a03

File tree

7 files changed

+81
-69
lines changed

7 files changed

+81
-69
lines changed

README.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -23,10 +23,10 @@ or
2323

2424
<hr>
2525

26-
As of version 1.2.4, you can now pass an `apiKey` prop to automatically load the Google maps scripts. The api key can be found in your [google cloud console.](https://developers.google.com/maps/documentation/javascript/get-api-key)
26+
As of version 1.2.4, you can now pass an `apiKey` prop to automatically load the Google maps scripts. The api key can be found in your [google cloud console.](https://developers.google.com/maps/documentation/javascript/get-api-key). The places service hook requires both the Places API and Maps Javascript API to be enabled.
2727

2828
```js
29-
<AutoComplete
29+
<Autocomplete
3030
apiKey={YOUR_GOOGLE_MAPS_API_KEY}
3131
onPlaceSelected={(place) => console.log(place)}
3232
/>

lib/usePlacesWidget.js

+22-9
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,6 @@ function usePlacesWidget(props) {
5252
var inputRef = (0, _react.useRef)(null);
5353
var event = (0, _react.useRef)(null);
5454
var autocompleteRef = (0, _react.useRef)(null);
55-
var observerHack = (0, _react.useRef)(null);
5655
var languageQueryParam = language ? "&language=".concat(language) : "";
5756
var googleMapsScriptUrl = "".concat(googleMapsScriptBaseUrl, "?libraries=").concat(libraries, "&key=").concat(apiKey).concat(languageQueryParam);
5857
var handleLoadScript = (0, _react.useCallback)(function () {
@@ -77,7 +76,7 @@ function usePlacesWidget(props) {
7776

7877
if (typeof google === "undefined") return console.error("Google has not been found. Make sure your provide apiKey prop.");
7978
if (!((_google$maps = google.maps) !== null && _google$maps !== void 0 && _google$maps.places)) return console.error("Google maps places API must be loaded.");
80-
if (!inputRef.current instanceof HTMLInputElement) return console.error("Input ref must be HTMLInputElement.");
79+
if (!(inputRef.current instanceof HTMLInputElement)) return console.error("Input ref must be HTMLInputElement.");
8180
autocompleteRef.current = new google.maps.places.Autocomplete(inputRef.current, config);
8281

8382
if (autocompleteRef.current) {
@@ -100,24 +99,38 @@ function usePlacesWidget(props) {
10099
return function () {
101100
return event.current ? event.current.remove() : undefined;
102101
};
103-
}, []); // Autofill workaround adapted from https://stackoverflow.com/questions/29931712/chrome-autofill-covers-autocomplete-for-google-maps-api-v3/49161445#49161445
104-
102+
}, []);
105103
(0, _react.useEffect)(function () {
106-
var _React$version;
104+
if (autocompleteRef.current && onPlaceSelected) {
105+
event.current = autocompleteRef.current.addListener("place_changed", function () {
106+
if (onPlaceSelected && autocompleteRef && autocompleteRef.current) {
107+
onPlaceSelected(autocompleteRef.current.getPlace(), inputRef.current, autocompleteRef.current);
108+
}
109+
});
110+
}
107111

112+
return function () {
113+
return event.current ? event.current.remove() : undefined;
114+
};
115+
}, [onPlaceSelected]); // Autofill workaround adapted from https://stackoverflow.com/questions/29931712/chrome-autofill-covers-autocomplete-for-google-maps-api-v3/49161445#49161445
116+
117+
(0, _react.useEffect)(function () {
108118
// TODO find out why react 18(strict mode) hangs the page loading
109-
if (!(_react.default !== null && _react.default !== void 0 && (_React$version = _react.default.version) !== null && _React$version !== void 0 && _React$version.startsWith("18")) && _utils.isBrowser && window.MutationObserver && inputRef.current && inputRef.current instanceof HTMLInputElement) {
110-
observerHack.current = new MutationObserver(function () {
111-
observerHack.current.disconnect();
119+
if (_utils.isBrowser && window.MutationObserver && inputRef.current && inputRef.current instanceof HTMLInputElement) {
120+
var observerHack = new MutationObserver(function () {
121+
observerHack.disconnect();
112122

113123
if (inputRef.current) {
114124
inputRef.current.autocomplete = inputAutocompleteValue;
115125
}
116126
});
117-
observerHack.current.observe(inputRef.current, {
127+
observerHack.observe(inputRef.current, {
118128
attributes: true,
119129
attributeFilter: ["autocomplete"]
120130
});
131+
return function () {
132+
observerHack.disconnect(); // Cleanup
133+
};
121134
}
122135
}, [inputAutocompleteValue]);
123136
(0, _react.useEffect)(function () {

lib/utils.js

+5-5
Original file line numberDiff line numberDiff line change
@@ -27,13 +27,13 @@ var loadGoogleMapScript = function loadGoogleMapScript(googleMapsScriptBaseUrl,
2727
});
2828
}
2929

30+
var scriptUrl = new URL(googleMapsScriptUrl);
31+
scriptUrl.searchParams.set("callback", "__REACT_GOOGLE_AUTOCOMPLETE_CALLBACK__");
3032
var el = document.createElement("script");
31-
el.src = googleMapsScriptUrl;
32-
document.body.appendChild(el);
33+
el.src = scriptUrl.toString();
3334
return new Promise(function (resolve) {
34-
el.addEventListener("load", function () {
35-
return resolve();
36-
});
35+
window.__REACT_GOOGLE_AUTOCOMPLETE_CALLBACK__ = resolve;
36+
document.body.appendChild(el);
3737
});
3838
};
3939

package-lock.json

+21-39
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "react-google-autocomplete",
3-
"version": "2.7.2",
3+
"version": "2.7.4",
44
"description": "React component for google autocomplete.",
55
"main": "index.js",
66
"types": "index.d.ts",

src/usePlacesWidget.js

+22-9
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,6 @@ export default function usePlacesWidget(props) {
2828
const inputRef = useRef(null);
2929
const event = useRef(null);
3030
const autocompleteRef = useRef(null);
31-
const observerHack = useRef(null);
3231
const languageQueryParam = language ? `&language=${language}` : "";
3332
const googleMapsScriptUrl = `${googleMapsScriptBaseUrl}?libraries=${libraries}&key=${apiKey}${languageQueryParam}`;
3433

@@ -62,16 +61,16 @@ export default function usePlacesWidget(props) {
6261
if (!google.maps?.places)
6362
return console.error("Google maps places API must be loaded.");
6463

65-
if (!inputRef.current instanceof HTMLInputElement)
64+
if (!(inputRef.current instanceof HTMLInputElement))
6665
return console.error("Input ref must be HTMLInputElement.");
6766

6867
autocompleteRef.current = new google.maps.places.Autocomplete(
6968
inputRef.current,
7069
config
7170
);
72-
73-
if(autocompleteRef.current) {
74-
event.current = autocompleteRef.current.addListener(
71+
72+
if (autocompleteRef.current) {
73+
event.current = autocompleteRef.current.addListener(
7574
"place_changed",
7675
() => {
7776
if (onPlaceSelected && autocompleteRef && autocompleteRef.current) {
@@ -95,27 +94,41 @@ export default function usePlacesWidget(props) {
9594
return () => (event.current ? event.current.remove() : undefined);
9695
}, []);
9796

97+
useEffect(() => {
98+
if (autocompleteRef.current && onPlaceSelected) {
99+
event.current = autocompleteRef.current.addListener("place_changed", function () {
100+
if (onPlaceSelected && autocompleteRef && autocompleteRef.current) {
101+
onPlaceSelected(autocompleteRef.current.getPlace(), inputRef.current, autocompleteRef.current);
102+
}
103+
});
104+
}
105+
return () => (event.current ? event.current.remove() : undefined);
106+
}, [onPlaceSelected]);
107+
98108
// Autofill workaround adapted from https://stackoverflow.com/questions/29931712/chrome-autofill-covers-autocomplete-for-google-maps-api-v3/49161445#49161445
99109
useEffect(() => {
100110
// TODO find out why react 18(strict mode) hangs the page loading
101111
if (
102-
!React?.version?.startsWith("18") &&
103112
isBrowser &&
104113
window.MutationObserver &&
105114
inputRef.current &&
106115
inputRef.current instanceof HTMLInputElement
107116
) {
108-
observerHack.current = new MutationObserver(() => {
109-
observerHack.current.disconnect();
117+
const observerHack = new MutationObserver(() => {
118+
observerHack.disconnect();
110119

111120
if (inputRef.current) {
112121
inputRef.current.autocomplete = inputAutocompleteValue;
113122
}
114123
});
115-
observerHack.current.observe(inputRef.current, {
124+
observerHack.observe(inputRef.current, {
116125
attributes: true,
117126
attributeFilter: ["autocomplete"],
118127
});
128+
129+
return () => {
130+
observerHack.disconnect(); // Cleanup
131+
};
119132
}
120133
}, [inputAutocompleteValue]);
121134

src/utils.js

+8-4
Original file line numberDiff line numberDiff line change
@@ -24,12 +24,16 @@ export const loadGoogleMapScript = (
2424
});
2525
}
2626

27+
let scriptUrl = new URL(googleMapsScriptUrl);
28+
scriptUrl.searchParams.set(
29+
"callback",
30+
"__REACT_GOOGLE_AUTOCOMPLETE_CALLBACK__"
31+
);
2732
const el = document.createElement("script");
28-
el.src = googleMapsScriptUrl;
29-
30-
document.body.appendChild(el);
33+
el.src = scriptUrl.toString();
3134

3235
return new Promise((resolve) => {
33-
el.addEventListener("load", () => resolve());
36+
window.__REACT_GOOGLE_AUTOCOMPLETE_CALLBACK__ = resolve;
37+
document.body.appendChild(el);
3438
});
3539
};

0 commit comments

Comments
 (0)