Skip to content

Commit 00ae294

Browse files
authored
Enable multiple libraries from Google Maps API (#135)
Enable multiple libraries from Google Maps API. Default: `places`. The following change allows developers to include other Google Maps API libraries that they may need. `libraries` will be an array of Libraries represented as strings. Eventually the array is returned as a string with `libraries.toString()`. E.g) ``` libraries = ["places", "geometry"] libraries.toString() // outputs: places,geometry ``` In many use cases with Google Maps a developer may need to use more than one Library and the following PR covers these cases.
1 parent f697ad0 commit 00ae294

8 files changed

+21
-5
lines changed

index.d.ts

+1
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ export interface ReactGoogleAutocompleteProps {
88
) => void;
99
inputAutocompleteValue?: string;
1010
options?: google.maps.places.AutocompleteOptions;
11+
libraries?: string[];
1112
apiKey?: string;
1213
language?: string;
1314
googleMapsScriptBaseUrl?: string;

lib/ReactGoogleAutocomplete.js

+4-1
Original file line numberDiff line numberDiff line change
@@ -28,18 +28,20 @@ function _objectWithoutPropertiesLoose(source, excluded) { if (source == null) r
2828
function ReactGoogleAutocomplete(props) {
2929
var onPlaceSelected = props.onPlaceSelected,
3030
apiKey = props.apiKey,
31+
libraries = props.libraries,
3132
inputAutocompleteValue = props.inputAutocompleteValue,
3233
options = props.options,
3334
googleMapsScriptBaseUrl = props.googleMapsScriptBaseUrl,
3435
refProp = props.refProp,
3536
language = props.language,
36-
rest = _objectWithoutProperties(props, ["onPlaceSelected", "apiKey", "inputAutocompleteValue", "options", "googleMapsScriptBaseUrl", "refProp", "language"]);
37+
rest = _objectWithoutProperties(props, ["onPlaceSelected", "apiKey", "libraries", "inputAutocompleteValue", "options", "googleMapsScriptBaseUrl", "refProp", "language"]);
3738

3839
var _usePlacesWidget = (0, _usePlacesWidget2.default)({
3940
ref: refProp,
4041
googleMapsScriptBaseUrl: googleMapsScriptBaseUrl,
4142
onPlaceSelected: onPlaceSelected,
4243
apiKey: apiKey,
44+
libraries: libraries,
4345
inputAutocompleteValue: inputAutocompleteValue,
4446
options: options,
4547
language: language
@@ -53,6 +55,7 @@ function ReactGoogleAutocomplete(props) {
5355

5456
ReactGoogleAutocomplete.propTypes = {
5557
apiKey: _propTypes.default.string,
58+
libraries: _propTypes.default.arrayOf(_propTypes.default.string),
5659
ref: _propTypes.default.oneOfType([// Either a function
5760
_propTypes.default.func, // Or anything shaped { current: any }
5861
_propTypes.default.shape({

lib/usePlacesAutocompleteService.js

+3-1
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ function _arrayWithHoles(arr) { if (Array.isArray(arr)) return arr; }
3535

3636
function usePlacesAutocompleteService(_ref) {
3737
var apiKey = _ref.apiKey,
38+
libraries = _ref.libraries,
3839
_ref$googleMapsScript = _ref.googleMapsScriptBaseUrl,
3940
googleMapsScriptBaseUrl = _ref$googleMapsScript === void 0 ? _constants.GOOGLE_MAP_SCRIPT_BASE_URL : _ref$googleMapsScript,
4041
_ref$debounce = _ref.debounce,
@@ -44,7 +45,8 @@ function usePlacesAutocompleteService(_ref) {
4445
sessionToken = _ref.sessionToken,
4546
language = _ref.language;
4647
var languageQueryParam = language ? "&language=".concat(language) : "";
47-
var googleMapsScriptUrl = "".concat(googleMapsScriptBaseUrl, "?key=").concat(apiKey, "&libraries=places").concat(languageQueryParam);
48+
var librariesDefault = libraries ? "".concat(libraries.toString()) : "places";
49+
var googleMapsScriptUrl = "".concat(googleMapsScriptBaseUrl, "?key=").concat(apiKey, "&libraries=").concat(librariesDefault).concat(languageQueryParam);
4850

4951
var _useState = (0, _react.useState)([]),
5052
_useState2 = _slicedToArray(_useState, 2),

lib/usePlacesWidget.js

+3-1
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ function usePlacesWidget(props) {
2525
var ref = props.ref,
2626
onPlaceSelected = props.onPlaceSelected,
2727
apiKey = props.apiKey,
28+
libraries = props.libraries,
2829
_props$inputAutocompl = props.inputAutocompleteValue,
2930
inputAutocompleteValue = _props$inputAutocompl === void 0 ? "new-password" : _props$inputAutocompl,
3031
_props$options = props.options;
@@ -46,7 +47,8 @@ function usePlacesWidget(props) {
4647
var autocompleteRef = (0, _react.useRef)(null);
4748
var observerHack = (0, _react.useRef)(null);
4849
var languageQueryParam = language ? "&language=".concat(language) : "";
49-
var googleMapsScriptUrl = "".concat(googleMapsScriptBaseUrl, "?libraries=places&key=").concat(apiKey).concat(languageQueryParam);
50+
var librariesDefault = libraries ? "".concat(libraries.toString()) : "places";
51+
var googleMapsScriptUrl = "".concat(googleMapsScriptBaseUrl, "?libraries=").concat(librariesDefault, "&key=").concat(apiKey).concat(languageQueryParam);
5052
var handleLoadScript = (0, _react.useCallback)(function () {
5153
return (0, _utils.loadGoogleMapScript)(googleMapsScriptBaseUrl, googleMapsScriptUrl);
5254
}, [googleMapsScriptBaseUrl, googleMapsScriptUrl]);

src/ReactGoogleAutocomplete.js

+3
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ function ReactGoogleAutocomplete(props) {
77
const {
88
onPlaceSelected,
99
apiKey,
10+
libraries,
1011
inputAutocompleteValue,
1112
options,
1213
googleMapsScriptBaseUrl,
@@ -20,6 +21,7 @@ function ReactGoogleAutocomplete(props) {
2021
googleMapsScriptBaseUrl,
2122
onPlaceSelected,
2223
apiKey,
24+
libraries,
2325
inputAutocompleteValue,
2426
options,
2527
language
@@ -30,6 +32,7 @@ function ReactGoogleAutocomplete(props) {
3032

3133
ReactGoogleAutocomplete.propTypes = {
3234
apiKey: PropTypes.string,
35+
libraries: PropTypes.arrayOf(PropTypes.string),
3336
ref: PropTypes.oneOfType([
3437
// Either a function
3538
PropTypes.func,

src/usePlacesAutocompleteService.d.ts

+1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
interface usePlacesAutocompleteServiceConfig {
22
apiKey?: string;
3+
libraries?: string[];
34
googleMapsScriptBaseUrl?: string;
45
debounce?: number;
56
options?: google.maps.places.AutocompletionRequest;

src/usePlacesAutocompleteService.js

+3-1
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,16 @@ import { GOOGLE_MAP_SCRIPT_BASE_URL } from "./constants";
66

77
export default function usePlacesAutocompleteService({
88
apiKey,
9+
libraries,
910
googleMapsScriptBaseUrl = GOOGLE_MAP_SCRIPT_BASE_URL,
1011
debounce = 300,
1112
options = {},
1213
sessionToken,
1314
language,
1415
}) {
1516
const languageQueryParam = language ? `&language=${language}` : "";
16-
const googleMapsScriptUrl = `${googleMapsScriptBaseUrl}?key=${apiKey}&libraries=places${languageQueryParam}`;
17+
const librariesDefault = libraries ? `${libraries.toString()}` : "places";
18+
const googleMapsScriptUrl = `${googleMapsScriptBaseUrl}?key=${apiKey}&libraries=${librariesDefault}${languageQueryParam}`;
1719
const [placePredictions, setPlacePredictions] = useState([]);
1820
const [isPlacePredsLoading, setIsPlacePredsLoading] = useState(false);
1921
const [placeInputValue, setPlaceInputValue] = useState(null);

src/usePlacesWidget.js

+3-1
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ export default function usePlacesWidget(props) {
88
ref,
99
onPlaceSelected,
1010
apiKey,
11+
libraries,
1112
inputAutocompleteValue = "new-password",
1213
options: {
1314
types = ["(cities)"],
@@ -29,7 +30,8 @@ export default function usePlacesWidget(props) {
2930
const autocompleteRef = useRef(null);
3031
const observerHack = useRef(null);
3132
const languageQueryParam = language ? `&language=${language}` : "";
32-
const googleMapsScriptUrl = `${googleMapsScriptBaseUrl}?libraries=places&key=${apiKey}${languageQueryParam}`;
33+
const librariesDefault = libraries ? `${libraries.toString()}` : "places";
34+
const googleMapsScriptUrl = `${googleMapsScriptBaseUrl}?libraries=${librariesDefault}&key=${apiKey}${languageQueryParam}`;
3335

3436
const handleLoadScript = useCallback(
3537
() => loadGoogleMapScript(googleMapsScriptBaseUrl, googleMapsScriptUrl),

0 commit comments

Comments
 (0)