1 | function _extends() {
|
2 | _extends = Object.assign || function(target) {
|
3 | for(var i = 1; i < arguments.length; i++){
|
4 | var source = arguments[i];
|
5 | for(var key in source){
|
6 | if (Object.prototype.hasOwnProperty.call(source, key)) {
|
7 | target[key] = source[key];
|
8 | }
|
9 | }
|
10 | }
|
11 | return target;
|
12 | };
|
13 | return _extends.apply(this, arguments);
|
14 | }
|
15 | import { LeafletProvider, createLeafletContext } from '@react-leaflet/core';
|
16 | import { Map as LeafletMap } from 'leaflet';
|
17 | import React, { forwardRef, useCallback, useEffect, useImperativeHandle, useState } from 'react';
|
18 | function MapContainerComponent({ bounds , boundsOptions , center , children , className , id , placeholder , style , whenReady , zoom , ...options }, forwardedRef) {
|
19 | const [props] = useState({
|
20 | className,
|
21 | id,
|
22 | style
|
23 | });
|
24 | const [context, setContext] = useState(null);
|
25 | useImperativeHandle(forwardedRef, ()=>context?.map ?? null, [
|
26 | context
|
27 | ]);
|
28 | const mapRef = useCallback((node)=>{
|
29 | if (node !== null && context === null) {
|
30 | const map = new LeafletMap(node, options);
|
31 | if (center != null && zoom != null) {
|
32 | map.setView(center, zoom);
|
33 | } else if (bounds != null) {
|
34 | map.fitBounds(bounds, boundsOptions);
|
35 | }
|
36 | if (whenReady != null) {
|
37 | map.whenReady(whenReady);
|
38 | }
|
39 | setContext(createLeafletContext(map));
|
40 | }
|
41 |
|
42 | }, []);
|
43 | useEffect(()=>{
|
44 | return ()=>{
|
45 | context?.map.remove();
|
46 | };
|
47 | }, [
|
48 | context
|
49 | ]);
|
50 | const contents = context ? React.createElement(LeafletProvider, {
|
51 | value: context
|
52 | }, children) : placeholder ?? null;
|
53 | return React.createElement("div", _extends({}, props, {
|
54 | ref: mapRef
|
55 | }), contents);
|
56 | }
|
57 | export const MapContainer = forwardRef(MapContainerComponent);
|