-
Notifications
You must be signed in to change notification settings - Fork 51
/
Copy pathindex.js
74 lines (58 loc) · 1.7 KB
/
index.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
67
68
69
70
71
72
73
74
// @flow
import { PureComponent, createElement } from 'react';
import type MapboxMap from 'mapbox-gl/src/ui/map';
import MapContext from '../MapContext';
type Props = {
/** Unique id of the feature. */
id: string | number,
/** The Id of the vector source or GeoJSON source for the feature. */
source: string,
/** For vector tile sources, the sourceLayer is required. */
sourceLayer: string,
/** A set of key-value pairs. The values should be valid JSON types. */
state: Object
};
/**
* A `FeatureState` component sets the state of a feature.
*/
class FeatureState extends PureComponent<Props> {
_map: MapboxMap;
componentDidMount() {
const map: MapboxMap = this._map;
const { id, source, sourceLayer, state } = this.props;
map.setFeatureState({ id, source, sourceLayer }, state);
}
componentDidUpdate(prevProps: Props) {
const map = this._map;
const { id, source, sourceLayer, state } = this.props;
if (
id !== prevProps.id ||
source !== prevProps.source ||
sourceLayer !== prevProps.sourceLayer ||
state !== prevProps.state
) {
map.removeFeatureState({
id: prevProps.id,
source: prevProps.source,
sourceLayer: prevProps.sourceLayer
});
map.setFeatureState({ id, source, sourceLayer }, state);
}
}
componentWillUnmount() {
if (!this._map || !this._map.getStyle()) {
return;
}
const { id, source, sourceLayer } = this.props;
this._map.removeFeatureState({ id, source, sourceLayer });
}
render() {
return createElement(MapContext.Consumer, {}, (map) => {
if (map) {
this._map = map;
}
return null;
});
}
}
export default FeatureState;