Skip to content

Commit 6d66a6f

Browse files
committed
Upgrade mapbox-gl-geocoder to v4
1 parent 82561b0 commit 6d66a6f

File tree

3 files changed

+350
-39
lines changed

3 files changed

+350
-39
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@
3939
},
4040
"homepage": "https://github.com/SamSamskies/react-map-gl-geocoder",
4141
"dependencies": {
42-
"@mapbox/mapbox-gl-geocoder": "^2.3.0",
42+
"@mapbox/mapbox-gl-geocoder": "4.1.2",
4343
"prop-types": "^15.6.2",
4444
"viewport-mercator-project": "6.1.1"
4545
},

src/index.js

Lines changed: 57 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ function fitBounds(bounds, viewport) {
1212

1313
class Geocoder extends Component {
1414
geocoder = null
15-
cachedInputValue = ''
15+
cachedResult = null
1616

1717
componentDidMount() {
1818
this.initializeGeocoder()
@@ -33,38 +33,62 @@ class Geocoder extends Component {
3333
const {
3434
mapboxApiAccessToken,
3535
inputValue,
36+
origin,
3637
zoom,
3738
placeholder,
3839
proximity,
3940
trackProximity,
41+
collapsed,
42+
clearAndBlurOnEsc,
43+
clearOnBlur,
4044
bbox,
4145
types,
42-
country,
46+
countries,
4347
minLength,
4448
limit,
4549
language,
4650
filter,
4751
localGeocoder,
52+
reverseGeocode,
53+
enableEventLogging,
54+
render,
55+
getItemValue,
4856
onInit,
4957
position
5058
} = this.props
51-
52-
this.geocoder = new MapboxGeocoder({
59+
const options = {
5360
accessToken: mapboxApiAccessToken,
61+
origin,
5462
zoom,
5563
flyTo: false,
5664
placeholder,
5765
proximity,
5866
trackProximity,
67+
collapsed,
68+
clearAndBlurOnEsc,
69+
clearOnBlur,
5970
bbox,
6071
types,
61-
country,
72+
countries,
6273
minLength,
6374
limit,
6475
language,
6576
filter,
66-
localGeocoder
67-
})
77+
localGeocoder,
78+
reverseGeocode,
79+
enableEventLogging,
80+
marker: false
81+
}
82+
83+
if (render && typeof render === 'function') {
84+
options.render = render
85+
}
86+
87+
if (getItemValue && typeof getItemValue === 'function') {
88+
options.getItemValue = getItemValue
89+
}
90+
91+
this.geocoder = new MapboxGeocoder(options)
6892
this.subscribeEvents()
6993

7094
if (containerNode) {
@@ -74,10 +98,17 @@ class Geocoder extends Component {
7498
}
7599

76100
if (inputValue !== undefined && inputValue !== null) {
77-
this.cachedInputValue = inputValue
101+
this.geocoder.setInput(inputValue)
102+
} else if (this.cachedResult) {
103+
this.geocoder.setInput(this.cachedResult.place_name)
104+
}
105+
106+
if (this.cachedResult) {
107+
this.geocoder._typeahead.selected = this.cachedResult
108+
} else if (inputValue !== undefined && inputValue !== null) {
109+
this.geocoder._typeahead.selected = {}
78110
}
79111

80-
this.geocoder.setInput(this.cachedInputValue)
81112
onInit(this.geocoder)
82113
}
83114

@@ -122,7 +153,7 @@ class Geocoder extends Component {
122153
}
123154

124155
handleClear = () => {
125-
this.cachedInputValue = ''
156+
this.cachedResult = null
126157
this.props.onClear()
127158
}
128159

@@ -176,9 +207,7 @@ class Geocoder extends Component {
176207
})
177208
onResult(event)
178209

179-
if (result && result.place_name) {
180-
this.cachedInputValue = result.place_name
181-
}
210+
this.cachedResult = result
182211
}
183212

184213
handleError = (event) => {
@@ -199,18 +228,26 @@ class Geocoder extends Component {
199228
onViewportChange: PropTypes.func,
200229
mapboxApiAccessToken: PropTypes.string.isRequired,
201230
inputValue: PropTypes.string,
231+
origin: PropTypes.string,
202232
zoom: PropTypes.number,
203233
placeholder: PropTypes.string,
204234
proximity: PropTypes.object,
205235
trackProximity: PropTypes.bool,
236+
collapsed: PropTypes.bool,
237+
clearAndBlurOnEsc: PropTypes.bool,
238+
clearOnBlur: PropTypes.bool,
206239
bbox: PropTypes.array,
207240
types: PropTypes.string,
208-
country: PropTypes.string,
241+
countries: PropTypes.string,
209242
minLength: PropTypes.number,
210243
limit: PropTypes.number,
211244
language: PropTypes.string,
212245
filter: PropTypes.func,
213246
localGeocoder: PropTypes.func,
247+
reverseGeocode: PropTypes.bool,
248+
enableEventLogging: PropTypes.bool,
249+
render: PropTypes.func,
250+
getItemValue: PropTypes.func,
214251
position: PropTypes.oneOf(VALID_POSITIONS),
215252
onInit: PropTypes.func,
216253
onClear: PropTypes.func,
@@ -222,11 +259,17 @@ class Geocoder extends Component {
222259

223260
static defaultProps = {
224261
onViewportChange: () => {},
262+
origin: 'https://api.mapbox.com',
225263
zoom: 16,
226264
placeholder: 'Search',
227265
trackProximity: false,
266+
collapsed: false,
267+
clearAndBlurOnEsc: false,
268+
clearOnBlur: false,
228269
minLength: 2,
229270
limit: 5,
271+
reverseGeocode: false,
272+
enableEventLogging: true,
230273
position: 'top-right',
231274
onInit: () => {},
232275
onClear: () => {},

0 commit comments

Comments
 (0)