@ttoss/google-maps 1.20.0 → 1.20.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/esm/index.js +242 -0
- package/dist/index.d.ts +77 -0
- package/dist/index.js +274 -0
- package/package.json +4 -4
- package/src/usePlacesAutocomplete/index.ts +5 -4
|
@@ -0,0 +1,242 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
// tsup.inject.js
|
|
4
|
+
import * as React from "react";
|
|
5
|
+
|
|
6
|
+
// src/GoogleMapsProvider.tsx
|
|
7
|
+
import * as React2 from "react";
|
|
8
|
+
import { useScript } from "@ttoss/hooks";
|
|
9
|
+
var GoogleMapsContext = React2.createContext({
|
|
10
|
+
status: "idle",
|
|
11
|
+
googleMaps: null
|
|
12
|
+
});
|
|
13
|
+
var GoogleMapsProvider = ({
|
|
14
|
+
children,
|
|
15
|
+
apiKey,
|
|
16
|
+
libraries,
|
|
17
|
+
language
|
|
18
|
+
}) => {
|
|
19
|
+
const src = (() => {
|
|
20
|
+
let srcTemp = `https://maps.googleapis.com/maps/api/js?key=${apiKey}`;
|
|
21
|
+
if (libraries) {
|
|
22
|
+
srcTemp = srcTemp + `&libraries=${libraries.join(",")}`;
|
|
23
|
+
}
|
|
24
|
+
if (language) {
|
|
25
|
+
srcTemp = srcTemp + `&language=${language}`;
|
|
26
|
+
}
|
|
27
|
+
return srcTemp;
|
|
28
|
+
})();
|
|
29
|
+
const { status } = useScript(src);
|
|
30
|
+
const googleMaps = React2.useMemo(() => {
|
|
31
|
+
if (status === "ready" && window.google.maps) {
|
|
32
|
+
return window.google.maps;
|
|
33
|
+
}
|
|
34
|
+
return null;
|
|
35
|
+
}, [status]);
|
|
36
|
+
const value = React2.useMemo(() => {
|
|
37
|
+
if (status === "ready" && googleMaps) {
|
|
38
|
+
return {
|
|
39
|
+
status,
|
|
40
|
+
googleMaps
|
|
41
|
+
};
|
|
42
|
+
}
|
|
43
|
+
return {
|
|
44
|
+
status,
|
|
45
|
+
googleMaps: null
|
|
46
|
+
};
|
|
47
|
+
}, [googleMaps, status]);
|
|
48
|
+
return /* @__PURE__ */ React2.createElement(GoogleMapsContext.Provider, {
|
|
49
|
+
value
|
|
50
|
+
}, children);
|
|
51
|
+
};
|
|
52
|
+
var useGoogleMaps = () => React2.useContext(GoogleMapsContext);
|
|
53
|
+
|
|
54
|
+
// src/useGeocoder.ts
|
|
55
|
+
import * as React3 from "react";
|
|
56
|
+
var useGeocoder = () => {
|
|
57
|
+
const { googleMaps } = useGoogleMaps();
|
|
58
|
+
const [isGeocoderInitialized, setIsGeocoderInitialized] = React3.useState(false);
|
|
59
|
+
const geocoder = React3.useMemo(() => {
|
|
60
|
+
if (googleMaps) {
|
|
61
|
+
const googleMapsGeocoder = new googleMaps.Geocoder();
|
|
62
|
+
setIsGeocoderInitialized(true);
|
|
63
|
+
return googleMapsGeocoder;
|
|
64
|
+
}
|
|
65
|
+
return null;
|
|
66
|
+
}, [googleMaps]);
|
|
67
|
+
return { geocoder, isGeocoderInitialized };
|
|
68
|
+
};
|
|
69
|
+
|
|
70
|
+
// src/useMap.ts
|
|
71
|
+
import * as React4 from "react";
|
|
72
|
+
import { useCallbackRef } from "use-callback-ref";
|
|
73
|
+
var useMap = (options = {}) => {
|
|
74
|
+
const [, forceUpdate] = React4.useState(0);
|
|
75
|
+
const ref = useCallbackRef(null, () => forceUpdate((n) => n + 1));
|
|
76
|
+
const { googleMaps } = useGoogleMaps();
|
|
77
|
+
const map = React4.useMemo(() => {
|
|
78
|
+
if (googleMaps && ref.current) {
|
|
79
|
+
return new googleMaps.Map(ref.current, options);
|
|
80
|
+
}
|
|
81
|
+
return null;
|
|
82
|
+
}, [googleMaps, ref.current]);
|
|
83
|
+
const optionsStringify = JSON.stringify(options);
|
|
84
|
+
React4.useEffect(() => {
|
|
85
|
+
if (map) {
|
|
86
|
+
const parsedOptions = JSON.parse(optionsStringify);
|
|
87
|
+
map.setOptions(parsedOptions);
|
|
88
|
+
}
|
|
89
|
+
}, [optionsStringify, map]);
|
|
90
|
+
return {
|
|
91
|
+
map,
|
|
92
|
+
ref
|
|
93
|
+
};
|
|
94
|
+
};
|
|
95
|
+
|
|
96
|
+
// src/usePlacesAutocomplete/index.ts
|
|
97
|
+
import { useCallback, useEffect as useEffect2, useRef as useRef2, useState as useState3 } from "react";
|
|
98
|
+
|
|
99
|
+
// src/usePlacesAutocomplete/debounce.ts
|
|
100
|
+
var debounce = (fn, delay) => {
|
|
101
|
+
let timer;
|
|
102
|
+
function debounceFn(...args) {
|
|
103
|
+
if (timer !== null) {
|
|
104
|
+
clearTimeout(timer);
|
|
105
|
+
timer = null;
|
|
106
|
+
}
|
|
107
|
+
timer = setTimeout(() => fn.apply(this, args), delay);
|
|
108
|
+
}
|
|
109
|
+
return debounceFn;
|
|
110
|
+
};
|
|
111
|
+
var debounce_default = debounce;
|
|
112
|
+
|
|
113
|
+
// src/usePlacesAutocomplete/useLatest.ts
|
|
114
|
+
import { useRef } from "react";
|
|
115
|
+
var useLatest_default = (val) => {
|
|
116
|
+
const ref = useRef(val);
|
|
117
|
+
ref.current = val;
|
|
118
|
+
return ref;
|
|
119
|
+
};
|
|
120
|
+
|
|
121
|
+
// src/usePlacesAutocomplete/index.ts
|
|
122
|
+
var usePlacesAutocomplete = ({
|
|
123
|
+
requestOptions,
|
|
124
|
+
debounce: debounce2 = 200,
|
|
125
|
+
cache = 24 * 60 * 60,
|
|
126
|
+
cacheKey,
|
|
127
|
+
callbackName,
|
|
128
|
+
defaultValue = "",
|
|
129
|
+
initOnMount = true
|
|
130
|
+
} = {}) => {
|
|
131
|
+
const [ready, setReady] = useState3(false);
|
|
132
|
+
const [value, setVal] = useState3(defaultValue);
|
|
133
|
+
const [suggestions, setSuggestions] = useState3({
|
|
134
|
+
loading: false,
|
|
135
|
+
status: "",
|
|
136
|
+
data: []
|
|
137
|
+
});
|
|
138
|
+
const asRef = useRef2(null);
|
|
139
|
+
const requestOptionsRef = useLatest_default(requestOptions);
|
|
140
|
+
const { googleMaps } = useGoogleMaps();
|
|
141
|
+
const googleMapsRef = useLatest_default(googleMaps);
|
|
142
|
+
const upaCacheKey = cacheKey ? `upa-${cacheKey}` : "upa";
|
|
143
|
+
const init = useCallback(() => {
|
|
144
|
+
if (asRef.current)
|
|
145
|
+
return;
|
|
146
|
+
if (!googleMaps) {
|
|
147
|
+
return;
|
|
148
|
+
}
|
|
149
|
+
const { current: gMaps } = googleMapsRef;
|
|
150
|
+
const placesLib = (gMaps == null ? void 0 : gMaps.places) || googleMaps.places;
|
|
151
|
+
if (!placesLib) {
|
|
152
|
+
return;
|
|
153
|
+
}
|
|
154
|
+
asRef.current = new placesLib.AutocompleteService();
|
|
155
|
+
setReady(true);
|
|
156
|
+
}, [googleMaps]);
|
|
157
|
+
const clearSuggestions = useCallback(() => {
|
|
158
|
+
setSuggestions({ loading: false, status: "", data: [] });
|
|
159
|
+
}, []);
|
|
160
|
+
const clearCache = useCallback(() => {
|
|
161
|
+
try {
|
|
162
|
+
sessionStorage.removeItem(upaCacheKey);
|
|
163
|
+
} catch (error) {
|
|
164
|
+
}
|
|
165
|
+
}, []);
|
|
166
|
+
const fetchPredictions = useCallback(debounce_default((val) => {
|
|
167
|
+
var _a;
|
|
168
|
+
if (!val) {
|
|
169
|
+
clearSuggestions();
|
|
170
|
+
return;
|
|
171
|
+
}
|
|
172
|
+
setSuggestions((prevState) => ({ ...prevState, loading: true }));
|
|
173
|
+
let cachedData = {};
|
|
174
|
+
try {
|
|
175
|
+
cachedData = JSON.parse(sessionStorage.getItem(upaCacheKey) || "{}");
|
|
176
|
+
} catch (error) {
|
|
177
|
+
}
|
|
178
|
+
if (cache) {
|
|
179
|
+
cachedData = Object.keys(cachedData).reduce((acc, key) => {
|
|
180
|
+
if (cachedData[key].maxAge - Date.now() >= 0)
|
|
181
|
+
acc[key] = cachedData[key];
|
|
182
|
+
return acc;
|
|
183
|
+
}, {});
|
|
184
|
+
if (cachedData[val]) {
|
|
185
|
+
setSuggestions({
|
|
186
|
+
loading: false,
|
|
187
|
+
status: "OK",
|
|
188
|
+
data: cachedData[val].data
|
|
189
|
+
});
|
|
190
|
+
return;
|
|
191
|
+
}
|
|
192
|
+
}
|
|
193
|
+
(_a = asRef == null ? void 0 : asRef.current) == null ? void 0 : _a.getPlacePredictions({ ...requestOptionsRef.current, input: val }, (data, status) => {
|
|
194
|
+
setSuggestions({ loading: false, status, data: data || [] });
|
|
195
|
+
if (cache && status === "OK") {
|
|
196
|
+
cachedData[val] = {
|
|
197
|
+
data,
|
|
198
|
+
maxAge: Date.now() + cache * 1e3
|
|
199
|
+
};
|
|
200
|
+
try {
|
|
201
|
+
sessionStorage.setItem(upaCacheKey, JSON.stringify(cachedData));
|
|
202
|
+
} catch (error) {
|
|
203
|
+
}
|
|
204
|
+
}
|
|
205
|
+
});
|
|
206
|
+
}, debounce2), [debounce2, clearSuggestions]);
|
|
207
|
+
const setValue = useCallback((val, shouldFetchData = true) => {
|
|
208
|
+
setVal(val);
|
|
209
|
+
if (asRef.current && shouldFetchData)
|
|
210
|
+
fetchPredictions(val);
|
|
211
|
+
}, [fetchPredictions]);
|
|
212
|
+
useEffect2(() => {
|
|
213
|
+
if (!initOnMount) {
|
|
214
|
+
return () => null;
|
|
215
|
+
}
|
|
216
|
+
if (!googleMapsRef.current && !googleMaps && callbackName) {
|
|
217
|
+
window[callbackName] = init;
|
|
218
|
+
} else {
|
|
219
|
+
init();
|
|
220
|
+
}
|
|
221
|
+
return () => {
|
|
222
|
+
if (window[callbackName])
|
|
223
|
+
delete window[callbackName];
|
|
224
|
+
};
|
|
225
|
+
}, [callbackName, init]);
|
|
226
|
+
return {
|
|
227
|
+
ready,
|
|
228
|
+
value,
|
|
229
|
+
suggestions,
|
|
230
|
+
setValue,
|
|
231
|
+
clearSuggestions,
|
|
232
|
+
clearCache,
|
|
233
|
+
init
|
|
234
|
+
};
|
|
235
|
+
};
|
|
236
|
+
export {
|
|
237
|
+
GoogleMapsProvider,
|
|
238
|
+
useGeocoder,
|
|
239
|
+
useGoogleMaps,
|
|
240
|
+
useMap,
|
|
241
|
+
usePlacesAutocomplete
|
|
242
|
+
};
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,77 @@
|
|
|
1
|
+
import * as React from 'react';
|
|
2
|
+
import { ScriptStatus } from '@ttoss/hooks';
|
|
3
|
+
|
|
4
|
+
declare type Extends<T, U extends T> = U;
|
|
5
|
+
declare type GoogleMaps = typeof google.maps;
|
|
6
|
+
declare type LoadedMapsStatus = Extends<ScriptStatus, 'ready'>;
|
|
7
|
+
declare type NotLoadedMapStatus = Extends<ScriptStatus, 'idle' | 'error' | 'loading'>;
|
|
8
|
+
declare type Libraries = 'places' | 'visualization' | 'drawing' | 'geometry';
|
|
9
|
+
declare const GoogleMapsProvider: ({ children, apiKey, libraries, language, }: {
|
|
10
|
+
children: React.ReactNode;
|
|
11
|
+
apiKey: string;
|
|
12
|
+
libraries?: Libraries[] | undefined;
|
|
13
|
+
/**
|
|
14
|
+
* https://developers.google.com/maps/faq#languagesupport
|
|
15
|
+
*/
|
|
16
|
+
language?: string | undefined;
|
|
17
|
+
}) => JSX.Element;
|
|
18
|
+
/**
|
|
19
|
+
*
|
|
20
|
+
* @returns param.googleMaps: GoogleMaps - returns the google maps object which
|
|
21
|
+
* provides access to the [Google Maps API](https://developers.google.com/maps/documentation/javascript/overview).
|
|
22
|
+
*/
|
|
23
|
+
declare const useGoogleMaps: () => {
|
|
24
|
+
status: LoadedMapsStatus;
|
|
25
|
+
googleMaps: GoogleMaps;
|
|
26
|
+
} | {
|
|
27
|
+
status: NotLoadedMapStatus;
|
|
28
|
+
googleMaps: null;
|
|
29
|
+
};
|
|
30
|
+
|
|
31
|
+
declare const useGeocoder: () => {
|
|
32
|
+
geocoder: google.maps.Geocoder | null;
|
|
33
|
+
isGeocoderInitialized: boolean;
|
|
34
|
+
};
|
|
35
|
+
|
|
36
|
+
declare const useMap: (options?: google.maps.MapOptions) => {
|
|
37
|
+
/**
|
|
38
|
+
* asss
|
|
39
|
+
*/
|
|
40
|
+
map: google.maps.Map | null;
|
|
41
|
+
/**
|
|
42
|
+
* hhhh
|
|
43
|
+
*/
|
|
44
|
+
ref: React.MutableRefObject<HTMLDivElement | null>;
|
|
45
|
+
};
|
|
46
|
+
|
|
47
|
+
interface HookArgs {
|
|
48
|
+
requestOptions?: Omit<google.maps.places.AutocompletionRequest, 'input'>;
|
|
49
|
+
debounce?: number;
|
|
50
|
+
cache?: number | false;
|
|
51
|
+
cacheKey?: string;
|
|
52
|
+
callbackName?: string;
|
|
53
|
+
defaultValue?: string;
|
|
54
|
+
initOnMount?: boolean;
|
|
55
|
+
}
|
|
56
|
+
declare type Suggestion = google.maps.places.AutocompletePrediction;
|
|
57
|
+
declare type Status = `${google.maps.places.PlacesServiceStatus}` | '';
|
|
58
|
+
interface Suggestions {
|
|
59
|
+
readonly loading: boolean;
|
|
60
|
+
readonly status: Status;
|
|
61
|
+
data: Suggestion[];
|
|
62
|
+
}
|
|
63
|
+
interface SetValue {
|
|
64
|
+
(val: string, shouldFetchData?: boolean): void;
|
|
65
|
+
}
|
|
66
|
+
interface HookReturn {
|
|
67
|
+
ready: boolean;
|
|
68
|
+
value: string;
|
|
69
|
+
suggestions: Suggestions;
|
|
70
|
+
setValue: SetValue;
|
|
71
|
+
clearSuggestions: () => void;
|
|
72
|
+
clearCache: () => void;
|
|
73
|
+
init: () => void;
|
|
74
|
+
}
|
|
75
|
+
declare const usePlacesAutocomplete: ({ requestOptions, debounce, cache, cacheKey, callbackName, defaultValue, initOnMount, }?: HookArgs) => HookReturn;
|
|
76
|
+
|
|
77
|
+
export { GoogleMapsProvider, useGeocoder, useGoogleMaps, useMap, usePlacesAutocomplete };
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,274 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __create = Object.create;
|
|
3
|
+
var __defProp = Object.defineProperty;
|
|
4
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
5
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
6
|
+
var __getProtoOf = Object.getPrototypeOf;
|
|
7
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
8
|
+
var __export = (target, all) => {
|
|
9
|
+
for (var name in all)
|
|
10
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
11
|
+
};
|
|
12
|
+
var __copyProps = (to, from, except, desc) => {
|
|
13
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
14
|
+
for (let key of __getOwnPropNames(from))
|
|
15
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
16
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
17
|
+
}
|
|
18
|
+
return to;
|
|
19
|
+
};
|
|
20
|
+
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target, mod));
|
|
21
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
22
|
+
|
|
23
|
+
// src/index.ts
|
|
24
|
+
var src_exports = {};
|
|
25
|
+
__export(src_exports, {
|
|
26
|
+
GoogleMapsProvider: () => GoogleMapsProvider,
|
|
27
|
+
useGeocoder: () => useGeocoder,
|
|
28
|
+
useGoogleMaps: () => useGoogleMaps,
|
|
29
|
+
useMap: () => useMap,
|
|
30
|
+
usePlacesAutocomplete: () => usePlacesAutocomplete
|
|
31
|
+
});
|
|
32
|
+
module.exports = __toCommonJS(src_exports);
|
|
33
|
+
|
|
34
|
+
// tsup.inject.js
|
|
35
|
+
var React = __toESM(require("react"));
|
|
36
|
+
|
|
37
|
+
// src/GoogleMapsProvider.tsx
|
|
38
|
+
var React2 = __toESM(require("react"));
|
|
39
|
+
var import_hooks = require("@ttoss/hooks");
|
|
40
|
+
var GoogleMapsContext = React2.createContext({
|
|
41
|
+
status: "idle",
|
|
42
|
+
googleMaps: null
|
|
43
|
+
});
|
|
44
|
+
var GoogleMapsProvider = ({
|
|
45
|
+
children,
|
|
46
|
+
apiKey,
|
|
47
|
+
libraries,
|
|
48
|
+
language
|
|
49
|
+
}) => {
|
|
50
|
+
const src = (() => {
|
|
51
|
+
let srcTemp = `https://maps.googleapis.com/maps/api/js?key=${apiKey}`;
|
|
52
|
+
if (libraries) {
|
|
53
|
+
srcTemp = srcTemp + `&libraries=${libraries.join(",")}`;
|
|
54
|
+
}
|
|
55
|
+
if (language) {
|
|
56
|
+
srcTemp = srcTemp + `&language=${language}`;
|
|
57
|
+
}
|
|
58
|
+
return srcTemp;
|
|
59
|
+
})();
|
|
60
|
+
const { status } = (0, import_hooks.useScript)(src);
|
|
61
|
+
const googleMaps = React2.useMemo(() => {
|
|
62
|
+
if (status === "ready" && window.google.maps) {
|
|
63
|
+
return window.google.maps;
|
|
64
|
+
}
|
|
65
|
+
return null;
|
|
66
|
+
}, [status]);
|
|
67
|
+
const value = React2.useMemo(() => {
|
|
68
|
+
if (status === "ready" && googleMaps) {
|
|
69
|
+
return {
|
|
70
|
+
status,
|
|
71
|
+
googleMaps
|
|
72
|
+
};
|
|
73
|
+
}
|
|
74
|
+
return {
|
|
75
|
+
status,
|
|
76
|
+
googleMaps: null
|
|
77
|
+
};
|
|
78
|
+
}, [googleMaps, status]);
|
|
79
|
+
return /* @__PURE__ */ React2.createElement(GoogleMapsContext.Provider, {
|
|
80
|
+
value
|
|
81
|
+
}, children);
|
|
82
|
+
};
|
|
83
|
+
var useGoogleMaps = () => React2.useContext(GoogleMapsContext);
|
|
84
|
+
|
|
85
|
+
// src/useGeocoder.ts
|
|
86
|
+
var React3 = __toESM(require("react"));
|
|
87
|
+
var useGeocoder = () => {
|
|
88
|
+
const { googleMaps } = useGoogleMaps();
|
|
89
|
+
const [isGeocoderInitialized, setIsGeocoderInitialized] = React3.useState(false);
|
|
90
|
+
const geocoder = React3.useMemo(() => {
|
|
91
|
+
if (googleMaps) {
|
|
92
|
+
const googleMapsGeocoder = new googleMaps.Geocoder();
|
|
93
|
+
setIsGeocoderInitialized(true);
|
|
94
|
+
return googleMapsGeocoder;
|
|
95
|
+
}
|
|
96
|
+
return null;
|
|
97
|
+
}, [googleMaps]);
|
|
98
|
+
return { geocoder, isGeocoderInitialized };
|
|
99
|
+
};
|
|
100
|
+
|
|
101
|
+
// src/useMap.ts
|
|
102
|
+
var React4 = __toESM(require("react"));
|
|
103
|
+
var import_use_callback_ref = require("use-callback-ref");
|
|
104
|
+
var useMap = (options = {}) => {
|
|
105
|
+
const [, forceUpdate] = React4.useState(0);
|
|
106
|
+
const ref = (0, import_use_callback_ref.useCallbackRef)(null, () => forceUpdate((n) => n + 1));
|
|
107
|
+
const { googleMaps } = useGoogleMaps();
|
|
108
|
+
const map = React4.useMemo(() => {
|
|
109
|
+
if (googleMaps && ref.current) {
|
|
110
|
+
return new googleMaps.Map(ref.current, options);
|
|
111
|
+
}
|
|
112
|
+
return null;
|
|
113
|
+
}, [googleMaps, ref.current]);
|
|
114
|
+
const optionsStringify = JSON.stringify(options);
|
|
115
|
+
React4.useEffect(() => {
|
|
116
|
+
if (map) {
|
|
117
|
+
const parsedOptions = JSON.parse(optionsStringify);
|
|
118
|
+
map.setOptions(parsedOptions);
|
|
119
|
+
}
|
|
120
|
+
}, [optionsStringify, map]);
|
|
121
|
+
return {
|
|
122
|
+
map,
|
|
123
|
+
ref
|
|
124
|
+
};
|
|
125
|
+
};
|
|
126
|
+
|
|
127
|
+
// src/usePlacesAutocomplete/index.ts
|
|
128
|
+
var import_react2 = require("react");
|
|
129
|
+
|
|
130
|
+
// src/usePlacesAutocomplete/debounce.ts
|
|
131
|
+
var debounce = (fn, delay) => {
|
|
132
|
+
let timer;
|
|
133
|
+
function debounceFn(...args) {
|
|
134
|
+
if (timer !== null) {
|
|
135
|
+
clearTimeout(timer);
|
|
136
|
+
timer = null;
|
|
137
|
+
}
|
|
138
|
+
timer = setTimeout(() => fn.apply(this, args), delay);
|
|
139
|
+
}
|
|
140
|
+
return debounceFn;
|
|
141
|
+
};
|
|
142
|
+
var debounce_default = debounce;
|
|
143
|
+
|
|
144
|
+
// src/usePlacesAutocomplete/useLatest.ts
|
|
145
|
+
var import_react = require("react");
|
|
146
|
+
var useLatest_default = (val) => {
|
|
147
|
+
const ref = (0, import_react.useRef)(val);
|
|
148
|
+
ref.current = val;
|
|
149
|
+
return ref;
|
|
150
|
+
};
|
|
151
|
+
|
|
152
|
+
// src/usePlacesAutocomplete/index.ts
|
|
153
|
+
var usePlacesAutocomplete = ({
|
|
154
|
+
requestOptions,
|
|
155
|
+
debounce: debounce2 = 200,
|
|
156
|
+
cache = 24 * 60 * 60,
|
|
157
|
+
cacheKey,
|
|
158
|
+
callbackName,
|
|
159
|
+
defaultValue = "",
|
|
160
|
+
initOnMount = true
|
|
161
|
+
} = {}) => {
|
|
162
|
+
const [ready, setReady] = (0, import_react2.useState)(false);
|
|
163
|
+
const [value, setVal] = (0, import_react2.useState)(defaultValue);
|
|
164
|
+
const [suggestions, setSuggestions] = (0, import_react2.useState)({
|
|
165
|
+
loading: false,
|
|
166
|
+
status: "",
|
|
167
|
+
data: []
|
|
168
|
+
});
|
|
169
|
+
const asRef = (0, import_react2.useRef)(null);
|
|
170
|
+
const requestOptionsRef = useLatest_default(requestOptions);
|
|
171
|
+
const { googleMaps } = useGoogleMaps();
|
|
172
|
+
const googleMapsRef = useLatest_default(googleMaps);
|
|
173
|
+
const upaCacheKey = cacheKey ? `upa-${cacheKey}` : "upa";
|
|
174
|
+
const init = (0, import_react2.useCallback)(() => {
|
|
175
|
+
if (asRef.current)
|
|
176
|
+
return;
|
|
177
|
+
if (!googleMaps) {
|
|
178
|
+
return;
|
|
179
|
+
}
|
|
180
|
+
const { current: gMaps } = googleMapsRef;
|
|
181
|
+
const placesLib = (gMaps == null ? void 0 : gMaps.places) || googleMaps.places;
|
|
182
|
+
if (!placesLib) {
|
|
183
|
+
return;
|
|
184
|
+
}
|
|
185
|
+
asRef.current = new placesLib.AutocompleteService();
|
|
186
|
+
setReady(true);
|
|
187
|
+
}, [googleMaps]);
|
|
188
|
+
const clearSuggestions = (0, import_react2.useCallback)(() => {
|
|
189
|
+
setSuggestions({ loading: false, status: "", data: [] });
|
|
190
|
+
}, []);
|
|
191
|
+
const clearCache = (0, import_react2.useCallback)(() => {
|
|
192
|
+
try {
|
|
193
|
+
sessionStorage.removeItem(upaCacheKey);
|
|
194
|
+
} catch (error) {
|
|
195
|
+
}
|
|
196
|
+
}, []);
|
|
197
|
+
const fetchPredictions = (0, import_react2.useCallback)(debounce_default((val) => {
|
|
198
|
+
var _a;
|
|
199
|
+
if (!val) {
|
|
200
|
+
clearSuggestions();
|
|
201
|
+
return;
|
|
202
|
+
}
|
|
203
|
+
setSuggestions((prevState) => ({ ...prevState, loading: true }));
|
|
204
|
+
let cachedData = {};
|
|
205
|
+
try {
|
|
206
|
+
cachedData = JSON.parse(sessionStorage.getItem(upaCacheKey) || "{}");
|
|
207
|
+
} catch (error) {
|
|
208
|
+
}
|
|
209
|
+
if (cache) {
|
|
210
|
+
cachedData = Object.keys(cachedData).reduce((acc, key) => {
|
|
211
|
+
if (cachedData[key].maxAge - Date.now() >= 0)
|
|
212
|
+
acc[key] = cachedData[key];
|
|
213
|
+
return acc;
|
|
214
|
+
}, {});
|
|
215
|
+
if (cachedData[val]) {
|
|
216
|
+
setSuggestions({
|
|
217
|
+
loading: false,
|
|
218
|
+
status: "OK",
|
|
219
|
+
data: cachedData[val].data
|
|
220
|
+
});
|
|
221
|
+
return;
|
|
222
|
+
}
|
|
223
|
+
}
|
|
224
|
+
(_a = asRef == null ? void 0 : asRef.current) == null ? void 0 : _a.getPlacePredictions({ ...requestOptionsRef.current, input: val }, (data, status) => {
|
|
225
|
+
setSuggestions({ loading: false, status, data: data || [] });
|
|
226
|
+
if (cache && status === "OK") {
|
|
227
|
+
cachedData[val] = {
|
|
228
|
+
data,
|
|
229
|
+
maxAge: Date.now() + cache * 1e3
|
|
230
|
+
};
|
|
231
|
+
try {
|
|
232
|
+
sessionStorage.setItem(upaCacheKey, JSON.stringify(cachedData));
|
|
233
|
+
} catch (error) {
|
|
234
|
+
}
|
|
235
|
+
}
|
|
236
|
+
});
|
|
237
|
+
}, debounce2), [debounce2, clearSuggestions]);
|
|
238
|
+
const setValue = (0, import_react2.useCallback)((val, shouldFetchData = true) => {
|
|
239
|
+
setVal(val);
|
|
240
|
+
if (asRef.current && shouldFetchData)
|
|
241
|
+
fetchPredictions(val);
|
|
242
|
+
}, [fetchPredictions]);
|
|
243
|
+
(0, import_react2.useEffect)(() => {
|
|
244
|
+
if (!initOnMount) {
|
|
245
|
+
return () => null;
|
|
246
|
+
}
|
|
247
|
+
if (!googleMapsRef.current && !googleMaps && callbackName) {
|
|
248
|
+
window[callbackName] = init;
|
|
249
|
+
} else {
|
|
250
|
+
init();
|
|
251
|
+
}
|
|
252
|
+
return () => {
|
|
253
|
+
if (window[callbackName])
|
|
254
|
+
delete window[callbackName];
|
|
255
|
+
};
|
|
256
|
+
}, [callbackName, init]);
|
|
257
|
+
return {
|
|
258
|
+
ready,
|
|
259
|
+
value,
|
|
260
|
+
suggestions,
|
|
261
|
+
setValue,
|
|
262
|
+
clearSuggestions,
|
|
263
|
+
clearCache,
|
|
264
|
+
init
|
|
265
|
+
};
|
|
266
|
+
};
|
|
267
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
268
|
+
0 && (module.exports = {
|
|
269
|
+
GoogleMapsProvider,
|
|
270
|
+
useGeocoder,
|
|
271
|
+
useGoogleMaps,
|
|
272
|
+
useMap,
|
|
273
|
+
usePlacesAutocomplete
|
|
274
|
+
});
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@ttoss/google-maps",
|
|
3
|
-
"version": "1.20.
|
|
3
|
+
"version": "1.20.3",
|
|
4
4
|
"author": "ttoss",
|
|
5
5
|
"contributors": [
|
|
6
6
|
{
|
|
@@ -31,8 +31,8 @@
|
|
|
31
31
|
"build": "tsup"
|
|
32
32
|
},
|
|
33
33
|
"dependencies": {
|
|
34
|
-
"@ttoss/hooks": "^1.20.
|
|
35
|
-
"@types/google.maps": "^3.
|
|
34
|
+
"@ttoss/hooks": "^1.20.2",
|
|
35
|
+
"@types/google.maps": "^3.49.2",
|
|
36
36
|
"use-callback-ref": "^1.3.0"
|
|
37
37
|
},
|
|
38
38
|
"devDependencies": {
|
|
@@ -43,5 +43,5 @@
|
|
|
43
43
|
"react": ">=16.8.0",
|
|
44
44
|
"react-dom": ">=16.8.0"
|
|
45
45
|
},
|
|
46
|
-
"gitHead": "
|
|
46
|
+
"gitHead": "6b9d0569f6dd6738005d338adea301308454ab0a"
|
|
47
47
|
}
|
|
@@ -1,8 +1,6 @@
|
|
|
1
1
|
/* eslint-disable react-hooks/exhaustive-deps */
|
|
2
|
-
import { useGoogleMaps } from './../GoogleMapsProvider';
|
|
3
|
-
|
|
4
2
|
import { useCallback, useEffect, useRef, useState } from 'react';
|
|
5
|
-
|
|
3
|
+
import { useGoogleMaps } from './../GoogleMapsProvider';
|
|
6
4
|
import _debounce from './debounce';
|
|
7
5
|
import useLatest from './useLatest';
|
|
8
6
|
|
|
@@ -166,7 +164,10 @@ export const usePlacesAutocomplete = ({
|
|
|
166
164
|
);
|
|
167
165
|
|
|
168
166
|
useEffect(() => {
|
|
169
|
-
if (!initOnMount)
|
|
167
|
+
if (!initOnMount) {
|
|
168
|
+
// eslint-disable-next-line react/display-name
|
|
169
|
+
return () => null;
|
|
170
|
+
}
|
|
170
171
|
|
|
171
172
|
if (!googleMapsRef.current && !googleMaps && callbackName) {
|
|
172
173
|
(window as any)[callbackName] = init;
|