From 8a76227be4c401505430c5648bf77a390eb7f9c4 Mon Sep 17 00:00:00 2001 From: thaddmt <68032955+thaddmt@users.noreply.github.com> Date: Tue, 9 Aug 2022 10:25:09 -0700 Subject: [PATCH] fix: fix bug with fitbounds array, fix bug where clearbutton doesnt show up after pressing enter (#79) --- lib/index.js | 14 +++++++++----- test/test.ui.js | 32 ++++++++++++++++++++++++++++++-- 2 files changed, 39 insertions(+), 7 deletions(-) diff --git a/lib/index.js b/lib/index.js index b4a2728..41bd3d7 100644 --- a/lib/index.js +++ b/lib/index.js @@ -499,7 +499,7 @@ MaplibreGeocoder.prototype = { }, _showButton: function () { - if (this._typeahead.selected) this._clearEl.style.display = "block"; + if (this._inputEl.value.length > 0) this._clearEl.style.display = "block"; }, _hideButton: function () { @@ -514,6 +514,10 @@ MaplibreGeocoder.prototype = { this._collapse(); } }, + // Change events are fire by suggestions library whenever the enter key is pressed or input is blurred + // This can sometimes cause strange behavior as this function is called before our own onKeyDown handler and thus + // we cannot depend on some internal values of the suggestion state like `selected` as those will change or before + // our onKeyDown handler. _onChange: function () { var selected = this._typeahead.selected; @@ -771,7 +775,6 @@ MaplibreGeocoder.prototype = { if (res.features.length) { this._clearEl.style.display = "block"; - this._eventEmitter.emit("results", res); var processedResults = this.options.processResults( res.features, @@ -779,14 +782,15 @@ MaplibreGeocoder.prototype = { this.options.limit ); - this._eventEmitter.emit("processedResults", processedResults); - this._typeahead.update(processedResults); if ( (!this.options.showResultsWhileTyping || isSuggestion) && this.options.showResultMarkers ) this._fitBoundsForMarkers(); + + this._eventEmitter.emit("results", res); + this._eventEmitter.emit("processedResults", processedResults); } else { this._clearEl.style.display = "none"; this._typeahead.selected = null; @@ -1035,7 +1039,7 @@ MaplibreGeocoder.prototype = { bounds.extend(feature.geometry.coordinates); }); - this._map.fitBounds(bounds, flyOptions); + this._map.fitBounds(bounds.toArray(), flyOptions); } } diff --git a/test/test.ui.js b/test/test.ui.js index 492f6f5..11a632a 100644 --- a/test/test.ui.js +++ b/test/test.ui.js @@ -343,6 +343,26 @@ test("Geocoder#inputControl", function (tt) { t.end(); }); + tt.test("clear button shows up", function (t) { + t.plan(1); + setup({ + clearOnBlur: true, + }); + + geocoder.setInput("testval"); + + var wrapper = container.querySelector(".maplibregl-ctrl-geocoder"); + var hoverEvent = document.createEvent("Event"); + hoverEvent.initEvent("mouseenter", true, true); + wrapper.dispatchEvent(hoverEvent); + var clearbutton = container.querySelector( + ".maplibregl-ctrl-geocoder--button" + ); + t.equal(clearbutton.style.display, "block"); + + t.end(); + }); + tt.test("options.collapsed=true, hover", function (t) { t.plan(1); setup({ @@ -378,8 +398,9 @@ test("Geocoder#inputControl", function (tt) { tt.test( "options.showResultsWhileTyping=false, enter key press", function (t) { - t.plan(2); + t.plan(4); setup({ + features: [Features.QUEEN_STREET], collapsed: true, showResultsWhileTyping: false, maplibregl: maplibregl, @@ -387,6 +408,7 @@ test("Geocoder#inputControl", function (tt) { var wrapper = container.querySelector(".maplibregl-ctrl-geocoder input"); var searchMock = sinon.spy(geocoder, "_geocode"); + var mapFitBoundsMock = sinon.spy(map, "fitBounds"); geocoder.setInput("Paris"); t.ok( @@ -398,7 +420,13 @@ test("Geocoder#inputControl", function (tt) { geocoder.on( "results", once(function () { - t.pass("results are returned"); + var boundsArray = mapFitBoundsMock.args[0][0]; + t.ok( + mapFitBoundsMock.calledOnce, + "the map#fitBounds method was called when enter was pressed" + ); + t.equals(boundsArray[0].length, 2, "center.lng changed"); + t.equals(boundsArray[1].length, 2, "center.lat changed"); t.end(); }) );