Skip to content

Commit 73e2d45

Browse files
committed
feat(Source): allow add Layers as children components in Source
1 parent de99f9a commit 73e2d45

File tree

2 files changed

+41
-6
lines changed

2 files changed

+41
-6
lines changed

src/__mocks__/mapbox-gl.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,10 @@ Map.prototype.getSource = function getSource(name) {
7171
return source;
7272
};
7373

74+
Map.prototype.isSourceLoaded = function isSourceLoaded(name) {
75+
return !!this._sources[name];
76+
};
77+
7478
Map.prototype.removeSource = function removeSource(name) {
7579
delete this._sources[name];
7680
};

src/components/Source/index.js

Lines changed: 37 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,36 +2,54 @@
22

33
import { PureComponent, createElement } from 'react';
44
import type MapboxMap from 'mapbox-gl/src/ui/map';
5+
import type { ChildrenArray, Element } from 'react';
56
import type {
67
SourceSpecification,
78
VectorSourceSpecification,
89
GeoJSONSourceSpecification
910
} from 'mapbox-gl/src/style-spec/types';
1011

1112
import MapContext from '../MapContext';
13+
import Layer from '../Layer';
1214
import validateSource from '../../utils/validateSource';
1315

1416
export type Props = {
1517
/** Mapbox GL Source */
1618
...SourceSpecification,
1719

1820
/** Mapbox GL Source id */
19-
id: string
21+
id: string,
22+
23+
/** Layers */
24+
children?: ChildrenArray<Element<typeof Layer>>
25+
};
26+
27+
type State = {
28+
loaded: boolean
2029
};
2130

22-
class Source extends PureComponent<Props> {
31+
class Source extends PureComponent<Props, State> {
2332
_map: MapboxMap;
2433

2534
static displayName = 'Source';
2635

36+
state = {
37+
loaded: false
38+
};
39+
2740
componentDidMount() {
28-
const { id, ...source } = validateSource(this.props);
41+
const { id, children, ...source } = validateSource(this.props);
2942
this._map.addSource(id, source);
43+
this._map.on('sourcedata', this._onSourceData);
3044
}
3145

3246
componentDidUpdate(prevProps: Props) {
33-
const { id: prevId, ...prevSource } = validateSource(prevProps);
34-
const { id, ...source } = validateSource(this.props);
47+
const {
48+
id: prevId,
49+
children: prevChildren,
50+
...prevSource
51+
} = validateSource(prevProps);
52+
const { id, children, ...source } = validateSource(this.props);
3553

3654
if (id !== prevId || source.type !== prevSource.type) {
3755
this._map.removeSource(prevId);
@@ -57,6 +75,15 @@ class Source extends PureComponent<Props> {
5775
this._removeSource();
5876
}
5977

78+
_onSourceData = () => {
79+
if (!this._map.isSourceLoaded(this.props.id)) {
80+
return;
81+
}
82+
83+
this._map.off('sourcedata', this._onSourceData);
84+
this.setState({ loaded: true });
85+
};
86+
6087
_updateGeoJSONSource = (
6188
id: string,
6289
prevSource: GeoJSONSourceSpecification,
@@ -110,12 +137,16 @@ class Source extends PureComponent<Props> {
110137
};
111138

112139
render() {
140+
const { loaded } = this.state;
141+
const { children } = this.props;
142+
143+
// $FlowFixMe
113144
return createElement(MapContext.Consumer, {}, (map: ?MapboxMap) => {
114145
if (map) {
115146
this._map = map;
116147
}
117148

118-
return null;
149+
return loaded && children;
119150
});
120151
}
121152
}

0 commit comments

Comments
 (0)