Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[v4] fix focus trap #220

Merged
merged 5 commits into from
Mar 20, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
- `trackProximity` turned on by default [#195](https://github.com/mapbox/mapbox-gl-geocoder/issues/195)
- Bump suggestions to v1.3.4
- Fix duplicate event bug
- Fix trapped focus [#220](https://github.com/mapbox/mapbox-gl-geocoder/issues/220)

## v3.1.4

Expand Down
25 changes: 13 additions & 12 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ MapboxGeocoder.prototype = {
this._inputEl.type = 'text';
this._inputEl.placeholder = this._getPlaceholderText();

this._inputEl.addEventListener('keydown', this._onKeyDown);
this._inputEl.addEventListener('keydown', debounce(this._onKeyDown, 200));
this._inputEl.addEventListener('change', this._onChange);

var actions = document.createElement('div');
Expand Down Expand Up @@ -148,16 +148,17 @@ MapboxGeocoder.prototype = {
return this;
},

_onKeyDown: debounce(function(e) {
_onKeyDown: function(e) {

// if target has shadowRoot, then get the actual active element inside the shadowRoot
var target = e.target.shadowRoot
var target = e.target && e.target.shadowRoot
? e.target.shadowRoot.activeElement
: e.target;
if (!target.value) {
var value = target ? target.value : '';
if (!value) {
this.fresh = true;
// the user has removed all the text
this._clear(e);
if (e.keyCode !== 9) this._clear(e);
return (this._clearEl.style.display = 'none');
}

Expand All @@ -168,7 +169,7 @@ MapboxGeocoder.prototype = {
if (target.value.length >= this.options.minLength) {
this._geocode(target.value);
}
}, 200),
},

_onChange: function() {
if (this._inputEl.value) this._clearEl.style.display = 'block';
Expand Down Expand Up @@ -397,23 +398,23 @@ MapboxGeocoder.prototype = {

/**
* Get the language to use in UI elements and when making search requests
*
*
* Look first at the explicitly set options otherwise use the browser's language settings
*
*
* @returns {String} The language used by the geocoder
*/
getLanguage: function(){
var browserLocale = navigator.language || navigator.userLanguage || navigator.browserLanguage;
return this.options.language || browserLocale;
},

/**
/**
* Get the text to use as the search bar placeholder
*
*
* If placeholder is provided in options, then use options.placeholder
* Otherwise, if language is provided in options, then use the localized string of the first language if available
* Otherwise use the default
*
*
* @returns {String} the value to use as the search bar placeholder
* @private
*/
Expand Down
35 changes: 35 additions & 0 deletions test/test.ui.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ var once = require('lodash.once');
var MapboxGeocoder = require('../lib/index');
var mapboxgl = require('mapbox-gl');
var test = require('tape');
var sinon = require('sinon');

mapboxgl.accessToken = process.env.MapboxAccessToken;

Expand Down Expand Up @@ -123,5 +124,39 @@ test('Geocoder#inputControl', function(tt) {
t.end();
})

tt.test('_clear is not called on keydown (tab), no focus trap', function(t){
t.plan(3);
setup({});

var inputEl = container.querySelector('.mapboxgl-ctrl-geocoder input');
var focusSpy = sinon.spy(inputEl, 'focus');
inputEl.focus();
t.equal(focusSpy.called, true, 'input is focused');
var keySpy = sinon.spy(geocoder,'_onKeyDown');
var clearSpy = sinon.spy(geocoder, '_clear');
geocoder._onKeyDown(new KeyboardEvent('keydown',{ code: 9, keyCode: 9 }));
t.equal(keySpy.called, true, '_onKeyDown called');
t.equal(clearSpy.called, false, '_clear should not be called');

t.end();
});

tt.test('_clear is called on keydown (not tab)', function(t){
t.plan(3);
setup({});

var inputEl = container.querySelector('.mapboxgl-ctrl-geocoder input');
var focusSpy = sinon.spy(inputEl, 'focus');
inputEl.focus();
t.equal(focusSpy.called, true, 'input is focused');
var keySpy = sinon.spy(geocoder,'_onKeyDown');
var clearSpy = sinon.spy(geocoder, '_clear');
geocoder._onKeyDown(new KeyboardEvent('keydown',{ code: 1, keyCode: 1 }));
t.equal(keySpy.called, true, '_onKeyDown called');
t.equal(clearSpy.called, true, '_clear should be called');

t.end();
});

tt.end();
});