Skip to content

Commit

Permalink
add checks for google in load script, add language types to ts
Browse files Browse the repository at this point in the history
  • Loading branch information
ErrorPro committed May 29, 2021
1 parent 0ba11a3 commit 058684f
Show file tree
Hide file tree
Showing 6 changed files with 13 additions and 3 deletions.
1 change: 1 addition & 0 deletions index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ export interface ReactGoogleAutocompleteProps {
inputAutocompleteValue?: string;
options?: google.maps.places.AutocompleteOptions;
apiKey?: string;
language?: string;
}

export interface ReactGoogleAutocompleteInputProps
Expand Down
2 changes: 1 addition & 1 deletion lib/usePlacesWidget.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ function usePlacesWidget(props) {
var event = (0, _react.useRef)(null);
var autocompleteRef = (0, _react.useRef)(null);
var observerHack = (0, _react.useRef)(null);
var languageQueryParam = language ? "&language=".concat(language) : '';
var languageQueryParam = language ? "&language=".concat(language) : "";
var googleMapsScriptUrl = "".concat(googleMapsScriptBaseUrl, "?libraries=places&key=").concat(apiKey).concat(languageQueryParam);
var handleLoadScript = (0, _react.useCallback)(function () {
return (0, _utils.loadGoogleMapScript)(googleMapsScriptBaseUrl, googleMapsScriptUrl);
Expand Down
5 changes: 5 additions & 0 deletions lib/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,11 @@ exports.isBrowser = isBrowser;

var loadGoogleMapScript = function loadGoogleMapScript(googleMapsScriptBaseUrl, googleMapsScriptUrl) {
if (!isBrowser) return Promise.resolve();

if (typeof google !== "undefined") {
if (google.maps && google.maps.api) return Promise.resolve();
}

var scriptElements = document.querySelectorAll("script[src*=\"".concat(googleMapsScriptBaseUrl, "\""));

if (scriptElements && scriptElements.length) {
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "react-google-autocomplete",
"version": "2.3.0",
"version": "2.3.1",
"description": "React component for google autocomplete.",
"main": "index.js",
"types": "index.d.ts",
Expand Down
2 changes: 1 addition & 1 deletion src/usePlacesWidget.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ export default function usePlacesWidget(props) {
const event = useRef(null);
const autocompleteRef = useRef(null);
const observerHack = useRef(null);
const languageQueryParam = language ? `&language=${language}` : '';
const languageQueryParam = language ? `&language=${language}` : "";
const googleMapsScriptUrl = `${googleMapsScriptBaseUrl}?libraries=places&key=${apiKey}${languageQueryParam}`;

const handleLoadScript = useCallback(
Expand Down
4 changes: 4 additions & 0 deletions src/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@ export const loadGoogleMapScript = (
) => {
if (!isBrowser) return Promise.resolve();

if (typeof google !== "undefined") {
if (google.maps && google.maps.api) return Promise.resolve();
}

const scriptElements = document.querySelectorAll(
`script[src*="${googleMapsScriptBaseUrl}"`
);
Expand Down

0 comments on commit 058684f

Please sign in to comment.