forked from PaulLeCam/react-leaflet
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPath.js
66 lines (58 loc) · 1.34 KB
/
Path.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 { Path as PathType } from 'leaflet'
import isEqual from 'fast-deep-equal'
import MapLayer from './MapLayer'
import type { PathOptions, PathProps } from './types'
import pick from './utils/pick'
const OPTIONS = [
'stroke',
'color',
'weight',
'opacity',
'lineCap',
'lineJoin',
'dashArray',
'dashOffset',
'fill',
'fillColor',
'fillOpacity',
'fillRule',
'bubblingMouseEvents',
'renderer',
'className',
// Interactive Layer
'interactive',
// Layer
'pane',
'attribution',
]
export default class Path<
LeafletElement: PathType,
Props: PathProps,
> extends MapLayer<LeafletElement, Props> {
constructor(props: Props) {
super(props)
if (this.contextValue == null) {
this.contextValue = {
...props.leaflet,
popupContainer: this.leafletElement,
}
}
}
componentDidUpdate(prevProps: Props) {
super.componentDidUpdate(prevProps)
this.setStyleIfChanged(prevProps, this.props)
}
getPathOptions(props: Props): PathOptions {
return pick(props, OPTIONS)
}
setStyle(options: PathOptions = {}) {
this.leafletElement.setStyle(options)
}
setStyleIfChanged(fromProps: Props, toProps: Props) {
const nextStyle = this.getPathOptions(toProps)
if (!isEqual(nextStyle, this.getPathOptions(fromProps))) {
this.setStyle(nextStyle)
}
}
}