Skip to content

Commit 68286e3

Browse files
committed
Search focus should also happen in a timeout
For the dropdown search on single select instances, the search focus should happen in a `setTimeout` call. This is because many browsers trigger the element focus on `click`, so our original call to `$search.focus` will essentially be ignored. By also focusing it in a timeout, this will delay the call to `focus` so the browser will no longer take it back. We do not have this issue for multiple select instances, as they trigger the `open` event on `click`, instead of using `mousedown`. This fixes select2#2900.
1 parent e76460a commit 68286e3

7 files changed

Lines changed: 22 additions & 2 deletions

File tree

dist/js/select2.amd.full.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2890,6 +2890,10 @@ define('select2/dropdown/search',[
28902890
self.$search.attr('tabindex', 0);
28912891

28922892
self.$search.focus();
2893+
2894+
window.setTimeout(function () {
2895+
self.$search.focus();
2896+
}, 0);
28932897
});
28942898

28952899
container.on('close', function () {

dist/js/select2.amd.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2890,6 +2890,10 @@ define('select2/dropdown/search',[
28902890
self.$search.attr('tabindex', 0);
28912891

28922892
self.$search.focus();
2893+
2894+
window.setTimeout(function () {
2895+
self.$search.focus();
2896+
}, 0);
28932897
});
28942898

28952899
container.on('close', function () {

dist/js/select2.full.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12425,6 +12425,10 @@ define('select2/dropdown/search',[
1242512425
self.$search.attr('tabindex', 0);
1242612426

1242712427
self.$search.focus();
12428+
12429+
window.setTimeout(function () {
12430+
self.$search.focus();
12431+
}, 0);
1242812432
});
1242912433

1243012434
container.on('close', function () {

dist/js/select2.full.min.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/js/select2.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3318,6 +3318,10 @@ define('select2/dropdown/search',[
33183318
self.$search.attr('tabindex', 0);
33193319

33203320
self.$search.focus();
3321+
3322+
window.setTimeout(function () {
3323+
self.$search.focus();
3324+
}, 0);
33213325
});
33223326

33233327
container.on('close', function () {

dist/js/select2.min.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/js/select2/dropdown/search.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,10 @@ define([
4040
self.$search.attr('tabindex', 0);
4141

4242
self.$search.focus();
43+
44+
window.setTimeout(function () {
45+
self.$search.focus();
46+
}, 0);
4347
});
4448

4549
container.on('close', function () {

0 commit comments

Comments
 (0)