Skip to content

Commit b2bdb91

Browse files
Use published types in geocoder and draw examples (#1838)
1 parent 2e49a54 commit b2bdb91

File tree

4 files changed

+34
-80
lines changed

4 files changed

+34
-80
lines changed

examples/draw-polygon/package.json

+1
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
},
66
"dependencies": {
77
"@mapbox/mapbox-gl-draw": "^1.3.0",
8+
"@types/mapbox__mapbox-gl-draw": "^1.2.3",
89
"@turf/area": "^6.5.0",
910
"react": "^17.0.0",
1011
"react-dom": "^17.0.0",

examples/draw-polygon/src/draw-control.ts

+2-22
Original file line numberDiff line numberDiff line change
@@ -3,27 +3,7 @@ import {useControl} from 'react-map-gl';
33

44
import type {MapRef, ControlPosition} from 'react-map-gl';
55

6-
type ControlTypes =
7-
| 'point'
8-
| 'line_string'
9-
| 'polygon'
10-
| 'trash'
11-
| 'combine_features'
12-
| 'uncombine_features';
13-
14-
type DrawControlProps = {
15-
keybindings?: boolean;
16-
touchEnable?: boolean;
17-
boxSelect?: boolean;
18-
clickBuffer?: number;
19-
touchBuffer?: number;
20-
controls?: Partial<{[name in ControlTypes]: boolean}>;
21-
displayControlsDefault?: boolean;
22-
styles?: any;
23-
modes?: any;
24-
defaultMode?: string;
25-
userProperties?: boolean;
26-
6+
type DrawControlProps = ConstructorParameters<typeof MapboxDraw>[0] & {
277
position?: ControlPosition;
288

299
onCreate?: (evt: {features: object[]}) => void;
@@ -32,7 +12,7 @@ type DrawControlProps = {
3212
};
3313

3414
export default function DrawControl(props: DrawControlProps) {
35-
useControl(
15+
useControl<MapboxDraw>(
3616
({map}: {map: MapRef}) => {
3717
map.on('draw.create', props.onCreate);
3818
map.on('draw.update', props.onUpdate);

examples/geocoder/package.json

+1
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
},
66
"dependencies": {
77
"@mapbox/mapbox-gl-geocoder": "^4.7.4",
8+
"@types/mapbox__mapbox-gl-geocoder": "^4.7.2",
89
"react": "^17.0.0",
910
"react-dom": "^17.0.0",
1011
"react-map-gl": "^7.0.0",

examples/geocoder/src/geocoder-control.tsx

+30-58
Original file line numberDiff line numberDiff line change
@@ -1,43 +1,11 @@
11
import * as React from 'react';
22
import {useState} from 'react';
3-
import {useControl, Marker, ControlPosition} from 'react-map-gl';
4-
import MapboxGeocoder from '@mapbox/mapbox-gl-geocoder';
3+
import {useControl, Marker, MarkerProps, ControlPosition} from 'react-map-gl';
4+
import MapboxGeocoder, {GeocoderOptions} from '@mapbox/mapbox-gl-geocoder';
55

6-
type GeocoderControlProps = {
6+
type GeocoderControlProps = Omit<GeocoderOptions, 'accessToken' | 'mapboxgl' | 'marker'> & {
77
mapboxAccessToken: string;
8-
origin?: string;
9-
zoom?: number;
10-
flyTo?: boolean | object;
11-
placeholder?: string;
12-
proximity?: {
13-
longitude: number;
14-
latitude: number;
15-
};
16-
trackProximity?: boolean;
17-
collapsed?: boolean;
18-
clearAndBlurOnEsc?: boolean;
19-
clearOnBlur?: boolean;
20-
box?: [number, number, number, number];
21-
countries?: string;
22-
types?: string;
23-
minLength?: number;
24-
limit?: number;
25-
language?: string;
26-
filter?: (feature: object) => boolean;
27-
localGeocoder?: Function;
28-
externalGeocoder?: Function;
29-
reverseMode?: 'distance' | 'score';
30-
reverseGeocode?: boolean;
31-
enableEventLogging?: boolean;
32-
marker?: boolean | object;
33-
render?: (feature: object) => string;
34-
getItemValue?: (feature: object) => string;
35-
mode?: 'mapbox.places' | 'mapbox.places-permanent';
36-
localGeocoderOnly?: boolean;
37-
autocomplete?: boolean;
38-
fuzzyMatch?: boolean;
39-
routing?: boolean;
40-
worldview?: string;
8+
marker?: boolean | Omit<MarkerProps, 'longitude' | 'latitude'>;
419

4210
position: ControlPosition;
4311

@@ -55,6 +23,7 @@ export default function GeocoderControl(props: GeocoderControlProps) {
5523
() => {
5624
const ctrl = new MapboxGeocoder({
5725
...props,
26+
marker: false,
5827
accessToken: props.mapboxAccessToken
5928
});
6029
ctrl.on('loading', props.onLoading);
@@ -66,7 +35,7 @@ export default function GeocoderControl(props: GeocoderControlProps) {
6635
const location =
6736
result &&
6837
(result.center || (result.geometry?.type === 'Point' && result.geometry.coordinates));
69-
if (location) {
38+
if (location && props.marker) {
7039
setMarker(<Marker {...props.marker} longitude={location[0]} latitude={location[1]} />);
7140
} else {
7241
setMarker(null);
@@ -80,6 +49,7 @@ export default function GeocoderControl(props: GeocoderControlProps) {
8049
}
8150
);
8251

52+
// @ts-ignore (TS2339) private member
8353
if (geocoder._map) {
8454
if (geocoder.getProximity() !== props.proximity && props.proximity !== undefined) {
8555
geocoder.setProximity(props.proximity);
@@ -94,48 +64,50 @@ export default function GeocoderControl(props: GeocoderControlProps) {
9464
geocoder.setZoom(props.zoom);
9565
}
9666
if (geocoder.getFlyTo() !== props.flyTo && props.flyTo !== undefined) {
97-
geocoder.setFlyTo(props.zoom);
67+
geocoder.setFlyTo(props.flyTo);
9868
}
9969
if (geocoder.getPlaceholder() !== props.placeholder && props.placeholder !== undefined) {
100-
geocoder.setPlaceholder(props.zoom);
70+
geocoder.setPlaceholder(props.placeholder);
10171
}
10272
if (geocoder.getCountries() !== props.countries && props.countries !== undefined) {
103-
geocoder.setCountries(props.zoom);
73+
geocoder.setCountries(props.countries);
10474
}
10575
if (geocoder.getTypes() !== props.types && props.types !== undefined) {
106-
geocoder.setTypes(props.zoom);
76+
geocoder.setTypes(props.types);
10777
}
10878
if (geocoder.getMinLength() !== props.minLength && props.minLength !== undefined) {
109-
geocoder.setMinLength(props.zoom);
79+
geocoder.setMinLength(props.minLength);
11080
}
11181
if (geocoder.getLimit() !== props.limit && props.limit !== undefined) {
112-
geocoder.setLimit(props.zoom);
82+
geocoder.setLimit(props.limit);
11383
}
11484
if (geocoder.getFilter() !== props.filter && props.filter !== undefined) {
115-
geocoder.setFilter(props.zoom);
85+
geocoder.setFilter(props.filter);
11686
}
11787
if (geocoder.getOrigin() !== props.origin && props.origin !== undefined) {
118-
geocoder.setOrigin(props.zoom);
119-
}
120-
if (geocoder.getAutocomplete() !== props.autocomplete && props.autocomplete !== undefined) {
121-
geocoder.setAutocomplete(props.zoom);
122-
}
123-
if (geocoder.getFuzzyMatch() !== props.fuzzyMatch && props.fuzzyMatch !== undefined) {
124-
geocoder.setFuzzyMatch(props.zoom);
125-
}
126-
if (geocoder.getRouting() !== props.routing && props.routing !== undefined) {
127-
geocoder.setRouting(props.zoom);
128-
}
129-
if (geocoder.getWorldview() !== props.worldview && props.worldview !== undefined) {
130-
geocoder.setWorldview(props.zoom);
131-
}
88+
geocoder.setOrigin(props.origin);
89+
}
90+
// Types missing from @types/mapbox__mapbox-gl-geocoder
91+
// if (geocoder.getAutocomplete() !== props.autocomplete && props.autocomplete !== undefined) {
92+
// geocoder.setAutocomplete(props.autocomplete);
93+
// }
94+
// if (geocoder.getFuzzyMatch() !== props.fuzzyMatch && props.fuzzyMatch !== undefined) {
95+
// geocoder.setFuzzyMatch(props.fuzzyMatch);
96+
// }
97+
// if (geocoder.getRouting() !== props.routing && props.routing !== undefined) {
98+
// geocoder.setRouting(props.routing);
99+
// }
100+
// if (geocoder.getWorldview() !== props.worldview && props.worldview !== undefined) {
101+
// geocoder.setWorldview(props.worldview);
102+
// }
132103
}
133104
return marker;
134105
}
135106

136107
const noop = () => {};
137108

138109
GeocoderControl.defaultProps = {
110+
marker: true,
139111
onLoading: noop,
140112
onResults: noop,
141113
onResult: noop,

0 commit comments

Comments
 (0)