-
Notifications
You must be signed in to change notification settings - Fork 890
/
Copy pathMapLayer.js
66 lines (53 loc) · 1.74 KB
/
MapLayer.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
65
66
// @flow
import type { Layer } from 'leaflet'
import React, { Fragment } from 'react'
import { LeafletProvider } from './context'
import MapComponent from './MapComponent'
import type { LeafletContext, MapLayerProps } from './types'
export default class MapLayer<
LeafletElement: Layer,
Props: MapLayerProps,
> extends MapComponent<LeafletElement, Props> {
contextValue: ?LeafletContext
constructor(props: Props) {
super(props)
this.leafletElement = this.createLeafletElement(props)
}
get layerContainer(): Layer {
return this.props.leaflet.layerContainer || this.props.leaflet.map
}
createLeafletElement(_props: Props): LeafletElement {
throw new Error('createLeafletElement() must be implemented')
}
updateLeafletElement(_fromProps: Props, _toProps: Props) {}
componentDidMount() {
super.componentDidMount()
this.layerContainer.addLayer(this.leafletElement)
}
componentDidUpdate(prevProps: Props) {
super.componentDidUpdate(prevProps)
if (this.props.attribution !== prevProps.attribution) {
const { map } = this.props.leaflet
if (map != null && map.attributionControl != null) {
map.attributionControl.removeAttribution(prevProps.attribution)
map.attributionControl.addAttribution(this.props.attribution)
}
}
this.updateLeafletElement(prevProps, this.props)
}
componentWillUnmount() {
super.componentWillUnmount()
this.layerContainer.removeLayer(this.leafletElement)
}
render() {
const { children } = this.props
if (children == null) {
return null
}
return this.contextValue == null ? (
<Fragment>{children}</Fragment>
) : (
<LeafletProvider value={this.contextValue}>{children}</LeafletProvider>
)
}
}