forked from jgimbel/react-leaflet-div-icon
-
Notifications
You must be signed in to change notification settings - Fork 2
/
div-icon.js
64 lines (57 loc) · 1.76 KB
/
div-icon.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
import React from 'react';
import ReactDOM from 'react-dom';
import { DivIcon, marker } from 'leaflet';
import { MapLayer, withLeaflet, LeafletProvider } from 'react-leaflet';
import PropTypes from 'prop-types';
export class Divicon extends MapLayer {
static propTypes = {
opacity: PropTypes.number,
zIndexOffset: PropTypes.number,
}
constructor(props){
super(props)
super.componentDidMount();
}
// See https://github.com/PaulLeCam/react-leaflet/issues/275
createLeafletElement(newProps) {
const { map: _map, layerContainer: _lc, position, ...props } = newProps;
this.icon = new DivIcon(props);
const m = marker(position, { icon: this.icon, ...props });
this.contextValue = { ...props.leaflet, popupContainer: m }
return m
}
updateLeafletElement(fromProps, toProps) {
if (toProps.position !== fromProps.position) {
this.leafletElement.setLatLng(toProps.position);
}
if (toProps.zIndexOffset !== fromProps.zIndexOffset) {
this.leafletElement.setZIndexOffset(toProps.zIndexOffset);
}
if (toProps.opacity !== fromProps.opacity) {
this.leafletElement.setOpacity(toProps.opacity);
}
if (toProps.draggable !== fromProps.draggable) {
if (toProps.draggable) {
this.leafletElement.dragging.enable();
}
else {
this.leafletElement.dragging.disable();
}
}
}
componentDidUpdate(fromProps) {
this.updateLeafletElement(fromProps, this.props);
}
render() {
const container = this.leafletElement._icon;
if (container) {
return ReactDOM.createPortal(<LeafletProvider value={this.contextValue}>
{this.props.children}
</LeafletProvider>, container)
}
else {
return null
}
}
}
export default withLeaflet(Divicon)