-
Notifications
You must be signed in to change notification settings - Fork 24
/
control.jsx
53 lines (45 loc) · 1.34 KB
/
control.jsx
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
import ReactDOM from "react-dom";
import { MapControl, withLeaflet } from "react-leaflet";
import { Control, DomUtil, DomEvent } from "leaflet";
const DumbControl = Control.extend({
options: {
className: "",
onOff: "",
handleOff: function noop() {}
},
onAdd(/* map */) {
var _controlDiv = DomUtil.create("div", this.options.className);
DomEvent.disableClickPropagation(_controlDiv);
return _controlDiv;
},
onRemove(map) {
if (this.options.onOff) {
map.off(this.options.onOff, this.options.handleOff, this);
}
return this;
}
});
export default withLeaflet(
class LeafletControl extends MapControl {
createLeafletElement(props) {
return new DumbControl(Object.assign({}, props));
}
componentDidMount() {
super.componentDidMount();
// This is needed because the control is only attached to the map in
// MapControl's componentDidMount, so the container is not available
// until this is called. We need to now force a render so that the
// portal and children are actually rendered.
this.forceUpdate();
}
render() {
if (!this.leafletElement || !this.leafletElement.getContainer()) {
return null;
}
return ReactDOM.createPortal(
this.props.children,
this.leafletElement.getContainer()
);
}
}
);