Skip to content

Commit

Permalink
Fix focus issues when clicking off selects
Browse files Browse the repository at this point in the history
  • Loading branch information
claudiulodro committed Nov 27, 2017
1 parent 276899e commit 77e9833
Show file tree
Hide file tree
Showing 11 changed files with 49 additions and 76 deletions.
26 changes: 9 additions & 17 deletions dist/js/select2.full.js
Original file line number Diff line number Diff line change
Expand Up @@ -1475,8 +1475,14 @@ S2.define('select2/selection/base',[
}

var $element = $this.data('element');

$element.select2('close');

// Remove any focus when dropdown is closed by clicking outside the select area.
// Timeout of 1 required for close to finish wrapping up.
setTimeout(function(){
$this.find('*:focus').blur();
$target.focus();
}, 1);
});
});
};
Expand Down Expand Up @@ -1715,6 +1721,7 @@ S2.define('select2/selection/multiple',[
// Needs 1 ms delay because of other 1 ms setTimeouts when rendering.
setTimeout(function(){
// Prevent the dropdown opening again when focused from this.
// This gets reset automatically when focus is triggered.
self._keyUpPrevented = true;

self.$search.focus();
Expand Down Expand Up @@ -1994,9 +2001,7 @@ S2.define('select2/selection/search',[
evt.preventDefault();
}
} else if (evt.which === KEYS.ENTER) {
if ( ! self.isOpen() ) {
self.open();
}
container.open();
evt.preventDefault();
}
});
Expand Down Expand Up @@ -5498,19 +5503,6 @@ S2.define('select2/core',[
self.focusOnActiveElement();
}, 1000);
}

// If focus is in the search field, select the current active element on Enter key.
/* $searchField.on('keydown', function (evt) {
if (evt.which === KEYS.ENTER) {
if ( self.isOpen() ) {
self.trigger('results:select', {});
} else {
self.open();
}
evt.preventDefault();
}
});*/

} else if (self.hasFocus()) {
if (key === KEYS.ENTER || key === KEYS.SPACE ||
key === KEYS.DOWN) {
Expand Down
2 changes: 1 addition & 1 deletion dist/js/select2.full.min.js

Large diffs are not rendered by default.

26 changes: 9 additions & 17 deletions dist/js/select2.js
Original file line number Diff line number Diff line change
Expand Up @@ -1475,8 +1475,14 @@ S2.define('select2/selection/base',[
}

var $element = $this.data('element');

$element.select2('close');

// Remove any focus when dropdown is closed by clicking outside the select area.
// Timeout of 1 required for close to finish wrapping up.
setTimeout(function(){
$this.find('*:focus').blur();
$target.focus();
}, 1);
});
});
};
Expand Down Expand Up @@ -1715,6 +1721,7 @@ S2.define('select2/selection/multiple',[
// Needs 1 ms delay because of other 1 ms setTimeouts when rendering.
setTimeout(function(){
// Prevent the dropdown opening again when focused from this.
// This gets reset automatically when focus is triggered.
self._keyUpPrevented = true;

self.$search.focus();
Expand Down Expand Up @@ -1994,9 +2001,7 @@ S2.define('select2/selection/search',[
evt.preventDefault();
}
} else if (evt.which === KEYS.ENTER) {
if ( ! self.isOpen() ) {
self.open();
}
container.open();
evt.preventDefault();
}
});
Expand Down Expand Up @@ -5498,19 +5503,6 @@ S2.define('select2/core',[
self.focusOnActiveElement();
}, 1000);
}

// If focus is in the search field, select the current active element on Enter key.
/* $searchField.on('keydown', function (evt) {
if (evt.which === KEYS.ENTER) {
if ( self.isOpen() ) {
self.trigger('results:select', {});
} else {
self.open();
}
evt.preventDefault();
}
});*/

} else if (self.hasFocus()) {
if (key === KEYS.ENTER || key === KEYS.SPACE ||
key === KEYS.DOWN) {
Expand Down
2 changes: 1 addition & 1 deletion dist/js/select2.min.js

Large diffs are not rendered by default.

26 changes: 9 additions & 17 deletions dist/js/selectWoo.full.js
Original file line number Diff line number Diff line change
Expand Up @@ -1475,8 +1475,14 @@ S2.define('select2/selection/base',[
}

var $element = $this.data('element');

$element.select2('close');

// Remove any focus when dropdown is closed by clicking outside the select area.
// Timeout of 1 required for close to finish wrapping up.
setTimeout(function(){
$this.find('*:focus').blur();
$target.focus();
}, 1);
});
});
};
Expand Down Expand Up @@ -1715,6 +1721,7 @@ S2.define('select2/selection/multiple',[
// Needs 1 ms delay because of other 1 ms setTimeouts when rendering.
setTimeout(function(){
// Prevent the dropdown opening again when focused from this.
// This gets reset automatically when focus is triggered.
self._keyUpPrevented = true;

self.$search.focus();
Expand Down Expand Up @@ -1994,9 +2001,7 @@ S2.define('select2/selection/search',[
evt.preventDefault();
}
} else if (evt.which === KEYS.ENTER) {
if ( ! self.isOpen() ) {
self.open();
}
container.open();
evt.preventDefault();
}
});
Expand Down Expand Up @@ -5498,19 +5503,6 @@ S2.define('select2/core',[
self.focusOnActiveElement();
}, 1000);
}

// If focus is in the search field, select the current active element on Enter key.
/* $searchField.on('keydown', function (evt) {
if (evt.which === KEYS.ENTER) {
if ( self.isOpen() ) {
self.trigger('results:select', {});
} else {
self.open();
}
evt.preventDefault();
}
});*/

} else if (self.hasFocus()) {
if (key === KEYS.ENTER || key === KEYS.SPACE ||
key === KEYS.DOWN) {
Expand Down
2 changes: 1 addition & 1 deletion dist/js/selectWoo.full.min.js

Large diffs are not rendered by default.

26 changes: 9 additions & 17 deletions dist/js/selectWoo.js
Original file line number Diff line number Diff line change
Expand Up @@ -1475,8 +1475,14 @@ S2.define('select2/selection/base',[
}

var $element = $this.data('element');

$element.select2('close');

// Remove any focus when dropdown is closed by clicking outside the select area.
// Timeout of 1 required for close to finish wrapping up.
setTimeout(function(){
$this.find('*:focus').blur();
$target.focus();
}, 1);
});
});
};
Expand Down Expand Up @@ -1715,6 +1721,7 @@ S2.define('select2/selection/multiple',[
// Needs 1 ms delay because of other 1 ms setTimeouts when rendering.
setTimeout(function(){
// Prevent the dropdown opening again when focused from this.
// This gets reset automatically when focus is triggered.
self._keyUpPrevented = true;

self.$search.focus();
Expand Down Expand Up @@ -1994,9 +2001,7 @@ S2.define('select2/selection/search',[
evt.preventDefault();
}
} else if (evt.which === KEYS.ENTER) {
if ( ! self.isOpen() ) {
self.open();
}
container.open();
evt.preventDefault();
}
});
Expand Down Expand Up @@ -5498,19 +5503,6 @@ S2.define('select2/core',[
self.focusOnActiveElement();
}, 1000);
}

// If focus is in the search field, select the current active element on Enter key.
/* $searchField.on('keydown', function (evt) {
if (evt.which === KEYS.ENTER) {
if ( self.isOpen() ) {
self.trigger('results:select', {});
} else {
self.open();
}
evt.preventDefault();
}
});*/

} else if (self.hasFocus()) {
if (key === KEYS.ENTER || key === KEYS.SPACE ||
key === KEYS.DOWN) {
Expand Down
2 changes: 1 addition & 1 deletion dist/js/selectWoo.min.js

Large diffs are not rendered by default.

8 changes: 7 additions & 1 deletion src/js/select2/selection/base.js
Original file line number Diff line number Diff line change
Expand Up @@ -136,8 +136,14 @@ define([
}

var $element = $this.data('element');

$element.select2('close');

// Remove any focus when dropdown is closed by clicking outside the select area.
// Timeout of 1 required for close to finish wrapping up.
setTimeout(function(){
$this.find('*:focus').blur();
$target.focus();
}, 1);
});
});
};
Expand Down
1 change: 1 addition & 0 deletions src/js/select2/selection/multiple.js
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,7 @@ define([
// Needs 1 ms delay because of other 1 ms setTimeouts when rendering.
setTimeout(function(){
// Prevent the dropdown opening again when focused from this.
// This gets reset automatically when focus is triggered.
self._keyUpPrevented = true;

self.$search.focus();
Expand Down
4 changes: 1 addition & 3 deletions src/js/select2/selection/search.js
Original file line number Diff line number Diff line change
Expand Up @@ -91,9 +91,7 @@ define([
evt.preventDefault();
}
} else if (evt.which === KEYS.ENTER) {
if ( ! self.isOpen() ) {
self.open();
}
container.open();
evt.preventDefault();
}
});
Expand Down

0 comments on commit 77e9833

Please sign in to comment.