Skip to content

Commit 6dd4ffc

Browse files
committed
refactor: simplify option applying hooks
1 parent c49414f commit 6dd4ffc

4 files changed

Lines changed: 30 additions & 283 deletions

File tree

src/components/GoogleMap/index.tsx

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -66,15 +66,15 @@ export const GoogleMap = forwardRef<google.maps.Map, GoogleMapProps>(
6666
ref,
6767
) {
6868
const containerRef = useRef<HTMLDivElement>(null);
69-
const maps = useImportLibrary('maps');
69+
const mapsLib = useImportLibrary('maps');
7070
const [map, setMap] = useState<google.maps.Map | null>(null);
7171

7272
useEffect(() => {
73-
if (!maps?.Map) {
73+
if (!mapsLib?.Map) {
7474
return;
7575
}
7676

77-
const map = new maps.Map(containerRef.current!, {
77+
const map = new mapsLib.Map(containerRef.current!, {
7878
center: initialCenter || center || mapOptions.center,
7979
zoom: initialZoom || zoom || mapOptions.zoom,
8080
...mapOptions,
@@ -84,7 +84,7 @@ export const GoogleMap = forwardRef<google.maps.Map, GoogleMapProps>(
8484
onLoad?.(map);
8585

8686
passRef(ref, map);
87-
}, [maps?.Map]);
87+
}, [mapsLib?.Map]);
8888

8989
useApplyMapOptions(map, {
9090
...mapOptions,

src/components/InfoWindow/index.tsx

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ export const InfoWindow = forwardRef<google.maps.InfoWindow, InfoWindowProps>(
3131
ref,
3232
) {
3333
const map = useMapContext();
34-
const maps = useImportLibrary('maps');
34+
const mapsLib = useImportLibrary('maps');
3535

3636
const [infoWindow, setInfoWindow] = useState<google.maps.InfoWindow | null>(
3737
null,
@@ -48,11 +48,11 @@ export const InfoWindow = forwardRef<google.maps.InfoWindow, InfoWindowProps>(
4848
);
4949

5050
useEffect(() => {
51-
if (!maps?.InfoWindow) {
51+
if (!mapsLib?.InfoWindow) {
5252
return;
5353
}
5454

55-
const infoWindow = new maps.InfoWindow({
55+
const infoWindow = new mapsLib.InfoWindow({
5656
ariaLabel,
5757
content,
5858
disableAutoPan,
@@ -65,7 +65,7 @@ export const InfoWindow = forwardRef<google.maps.InfoWindow, InfoWindowProps>(
6565

6666
setInfoWindow(infoWindow);
6767
passRef(ref, infoWindow);
68-
}, [maps?.InfoWindow]);
68+
}, [mapsLib?.InfoWindow]);
6969

7070
useEffect(() => {
7171
if (!infoWindow) {

src/hooks/useApplyMapOptions.ts

Lines changed: 15 additions & 247 deletions
Original file line numberDiff line numberDiff line change
@@ -4,299 +4,67 @@ export const useApplyMapOptions = (
44
map: google.maps.Map | null,
55
{
66
center,
7-
clickableIcons,
87
heading,
98
mapTypeId,
109
streetView,
1110
tilt,
1211
zoom,
13-
backgroundColor,
14-
controlSize,
15-
disableDefaultUI,
16-
disableDoubleClickZoom,
17-
draggableCursor,
18-
draggingCursor,
19-
fullscreenControl,
20-
fullscreenControlOptions,
21-
gestureHandling,
22-
isFractionalZoomEnabled,
23-
keyboardShortcuts,
24-
mapTypeControl,
25-
mapTypeControlOptions,
26-
maxZoom,
27-
minZoom,
28-
noClear,
29-
panControl,
30-
panControlOptions,
31-
restriction,
32-
rotateControl,
33-
rotateControlOptions,
34-
scaleControl,
35-
scaleControlOptions,
36-
scrollwheel,
37-
streetViewControl,
38-
streetViewControlOptions,
39-
styles,
40-
zoomControl,
41-
zoomControlOptions,
12+
...options
4213
}: google.maps.MapOptions,
4314
) => {
44-
const useMapOptionEffect = (fn, deps) =>
15+
const useMapOptionEffect = (
16+
fn: (map: google.maps.Map) => void,
17+
deps: unknown[],
18+
) =>
4519
useEffect(() => {
4620
if (map) {
4721
fn(map);
4822
}
4923
}, deps);
5024

25+
useEffect(() => {
26+
map?.setOptions(options);
27+
}, Object.values(options));
28+
5129
useMapOptionEffect(
52-
(map: google.maps.Map) => {
30+
(map) => {
5331
center && map.setCenter(center);
5432
},
5533
[center],
5634
);
5735

5836
useMapOptionEffect(
59-
(map: google.maps.Map) => {
60-
map.setClickableIcons(clickableIcons ?? true);
61-
},
62-
[clickableIcons],
63-
);
64-
65-
useMapOptionEffect(
66-
(map: google.maps.Map) => {
37+
(map) => {
6738
typeof heading === 'number' && map.setHeading(heading);
6839
},
6940
[heading],
7041
);
7142

7243
useMapOptionEffect(
73-
(map: google.maps.Map) => {
44+
(map) => {
7445
mapTypeId && map.setMapTypeId(mapTypeId);
7546
},
7647
[mapTypeId],
7748
);
7849

7950
useMapOptionEffect(
80-
(map: google.maps.Map) => {
51+
(map) => {
8152
streetView && map.setStreetView(streetView);
8253
},
8354
[streetView],
8455
);
8556

8657
useMapOptionEffect(
87-
(map: google.maps.Map) => {
58+
(map) => {
8859
typeof tilt === 'number' && map.setTilt(tilt);
8960
},
9061
[tilt],
9162
);
9263

9364
useMapOptionEffect(
94-
(map: google.maps.Map) => {
65+
(map) => {
9566
typeof zoom === 'number' && map.setZoom(zoom);
9667
},
9768
[zoom],
9869
);
99-
100-
useMapOptionEffect(
101-
(map: google.maps.Map) => {
102-
map.setOptions({ backgroundColor });
103-
},
104-
[backgroundColor],
105-
);
106-
107-
useMapOptionEffect(
108-
(map: google.maps.Map) => {
109-
map.setOptions({ controlSize });
110-
},
111-
[controlSize],
112-
);
113-
114-
useMapOptionEffect(
115-
(map: google.maps.Map) => {
116-
map.setOptions({ disableDefaultUI });
117-
},
118-
[disableDefaultUI],
119-
);
120-
121-
useMapOptionEffect(
122-
(map: google.maps.Map) => {
123-
map.setOptions({ disableDoubleClickZoom });
124-
},
125-
[disableDoubleClickZoom],
126-
);
127-
128-
useMapOptionEffect(
129-
(map: google.maps.Map) => {
130-
map.setOptions({ draggableCursor });
131-
},
132-
[draggableCursor],
133-
);
134-
135-
useMapOptionEffect(
136-
(map: google.maps.Map) => {
137-
map.setOptions({ draggingCursor });
138-
},
139-
[draggingCursor],
140-
);
141-
142-
useMapOptionEffect(
143-
(map: google.maps.Map) => {
144-
map.setOptions({ fullscreenControl });
145-
},
146-
[fullscreenControl],
147-
);
148-
149-
useMapOptionEffect(
150-
(map: google.maps.Map) => {
151-
map.setOptions({ fullscreenControlOptions });
152-
},
153-
[fullscreenControlOptions?.position],
154-
);
155-
156-
useMapOptionEffect(
157-
(map: google.maps.Map) => {
158-
map.setOptions({ gestureHandling });
159-
},
160-
[gestureHandling],
161-
);
162-
163-
useMapOptionEffect(
164-
(map: google.maps.Map) => {
165-
map.setOptions({ isFractionalZoomEnabled });
166-
},
167-
[isFractionalZoomEnabled],
168-
);
169-
170-
useMapOptionEffect(
171-
(map: google.maps.Map) => {
172-
map.setOptions({ keyboardShortcuts });
173-
},
174-
[keyboardShortcuts],
175-
);
176-
177-
useMapOptionEffect(
178-
(map: google.maps.Map) => {
179-
map.setOptions({ mapTypeControl });
180-
},
181-
[mapTypeControl],
182-
);
183-
184-
useMapOptionEffect(
185-
(map: google.maps.Map) => {
186-
map.setOptions({ mapTypeControlOptions });
187-
},
188-
[mapTypeControlOptions],
189-
);
190-
191-
useMapOptionEffect(
192-
(map: google.maps.Map) => {
193-
map.setOptions({ maxZoom });
194-
},
195-
[maxZoom],
196-
);
197-
198-
useMapOptionEffect(
199-
(map: google.maps.Map) => {
200-
map.setOptions({ minZoom });
201-
},
202-
[minZoom],
203-
);
204-
205-
useMapOptionEffect(
206-
(map: google.maps.Map) => {
207-
map.setOptions({ noClear });
208-
},
209-
[noClear],
210-
);
211-
212-
useMapOptionEffect(
213-
(map: google.maps.Map) => {
214-
map.setOptions({ panControl });
215-
},
216-
[panControl],
217-
);
218-
219-
useMapOptionEffect(
220-
(map: google.maps.Map) => {
221-
map.setOptions({ panControlOptions });
222-
},
223-
[panControlOptions],
224-
);
225-
226-
useMapOptionEffect(
227-
(map: google.maps.Map) => {
228-
map.setOptions({ restriction });
229-
},
230-
[restriction],
231-
);
232-
233-
useMapOptionEffect(
234-
(map: google.maps.Map) => {
235-
map.setOptions({ rotateControl });
236-
},
237-
[rotateControl],
238-
);
239-
240-
useMapOptionEffect(
241-
(map: google.maps.Map) => {
242-
map.setOptions({ rotateControlOptions });
243-
},
244-
[rotateControlOptions],
245-
);
246-
247-
useMapOptionEffect(
248-
(map: google.maps.Map) => {
249-
map.setOptions({ scaleControl });
250-
},
251-
[scaleControl],
252-
);
253-
254-
useMapOptionEffect(
255-
(map: google.maps.Map) => {
256-
map.setOptions({ scaleControlOptions });
257-
},
258-
[scaleControlOptions],
259-
);
260-
261-
useMapOptionEffect(
262-
(map: google.maps.Map) => {
263-
map.setOptions({ scrollwheel });
264-
},
265-
[scrollwheel],
266-
);
267-
268-
useMapOptionEffect(
269-
(map: google.maps.Map) => {
270-
map.setOptions({ streetViewControl });
271-
},
272-
[streetViewControl],
273-
);
274-
275-
useMapOptionEffect(
276-
(map: google.maps.Map) => {
277-
map.setOptions({ streetViewControlOptions });
278-
},
279-
[streetViewControlOptions],
280-
);
281-
282-
useMapOptionEffect(
283-
(map: google.maps.Map) => {
284-
map.setOptions({ styles });
285-
},
286-
[styles],
287-
);
288-
289-
useMapOptionEffect(
290-
(map: google.maps.Map) => {
291-
map.setOptions({ zoomControl });
292-
},
293-
[zoomControl],
294-
);
295-
296-
useMapOptionEffect(
297-
(map: google.maps.Map) => {
298-
map.setOptions({ zoomControlOptions });
299-
},
300-
[zoomControlOptions?.position],
301-
);
30270
};

0 commit comments

Comments
 (0)