Skip to content

Commit

Permalink
Support react-leaflet v3.0
Browse files Browse the repository at this point in the history
Signed-off-by: Mark Ison <mark@codecutter.uk>
  • Loading branch information
CodeCutterUK committed Nov 11, 2020
1 parent 7713479 commit 31380e0
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 167 deletions.
2 changes: 1 addition & 1 deletion dist/index.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 3 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -48,14 +48,15 @@
"terser": "^4.6.10"
},
"dependencies": {
"@react-leaflet/core": "^1.0.2",
"leaflet": "^1.6.0",
"leaflet.markercluster": "^1.4.1",
"react-leaflet": "^2.6.3"
"react-leaflet": "^3.0.0"
},
"peerDependencies": {
"leaflet": "^1.6.0",
"leaflet.markercluster": "^1.4.1",
"react-leaflet": "^2.6.3"
"react-leaflet": "^3.0.0"
},
"husky": {
"hooks": {
Expand Down
50 changes: 25 additions & 25 deletions src/react-leaflet-markercluster.js
Original file line number Diff line number Diff line change
@@ -1,34 +1,34 @@
import { MapLayer, withLeaflet } from 'react-leaflet';
import { createPathComponent } from '@react-leaflet/core';
import L from 'leaflet';

require('leaflet.markercluster');

class MarkerClusterGroup extends MapLayer {
createLeafletElement({ children, leaflet: { map }, ...props }) {
const clusterProps = {};
const clusterEvents = {};
const MarkerClusterGroup = createPathComponent((
{ children: _c, ...props },
ctx
) => {
const clusterProps = {};
const clusterEvents = {};

// Splitting props and events to different objects
Object.entries(props).forEach(
([propName, prop]) => propName.startsWith('on')
? clusterEvents[propName] = prop
: clusterProps[propName] = prop
);
// Splitting props and events to different objects
Object.entries(props).forEach(
([propName, prop]) => propName.startsWith('on')
? clusterEvents[propName] = prop
: clusterProps[propName] = prop
);

// Creating markerClusterGroup Leaflet element
const markerClusterGroup = new L.markerClusterGroup(clusterProps);
this.contextValue = { layerContainer: markerClusterGroup, map };
// Creating markerClusterGroup Leaflet element
const markerClusterGroup = new L.markerClusterGroup(clusterProps);

// Initializing event listeners
Object.entries(clusterEvents).forEach(
([eventAsProp, callback]) => {
const clusterEvent = `cluster${eventAsProp.substring(2).toLowerCase()}`;
markerClusterGroup.on(clusterEvent, callback);
},
);
// Initializing event listeners
Object.entries(clusterEvents).forEach(
([eventAsProp, callback]) => {
const clusterEvent = `cluster${eventAsProp.substring(2).toLowerCase()}`;
markerClusterGroup.on(clusterEvent, callback);
},
);

return markerClusterGroup;
}
}
return { instance: markerClusterGroup, context: { ...ctx, layerContainer: markerClusterGroup } };
});

export default withLeaflet(MarkerClusterGroup);
export default MarkerClusterGroup;
Loading

0 comments on commit 31380e0

Please sign in to comment.