Skip to content

Commit 9487567

Browse files
committed
Automatically open search when typing
This fixes an issue where the dropdown would not automatically open when something was typed in the search box on a multiple select. This was most noticeable when the dropdown closed and the search box was focused and the user started to type, as the text would appear in the search box, queries would be sent out to retrieve results, but the dropdown would not be opened to display the results. This also fixes an issue introduced in a recent commit where search queries would be sent out twice on modern browsers that supported both the `keyup` and `input` event. The `keyup` event is now properly debounced for these browsers and the queries are only going out once. This closes select2#3036.
1 parent 017c201 commit 9487567

8 files changed

Lines changed: 35 additions & 15 deletions

File tree

dist/js/select2.amd.full.js

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1318,10 +1318,10 @@ define('select2/selection/search',[
13181318
// both the `keyup` and `input` events.
13191319
this.$selection.on('input', '.select2-search--inline', function (evt) {
13201320
// Unbind the duplicated `keyup` event
1321-
$(this).off('keyup');
1321+
self.$selection.off('keyup.search');
13221322
});
13231323

1324-
this.$selection.on('keyup input', '.select2-search--inline',
1324+
this.$selection.on('keyup.search input', '.select2-search--inline',
13251325
function (evt) {
13261326
self.handleSearch(evt);
13271327
});
@@ -4471,6 +4471,10 @@ define('select2/core',[
44714471
});
44724472

44734473
this.on('query', function (params) {
4474+
if (!self.isOpen()) {
4475+
self.trigger('open');
4476+
}
4477+
44744478
this.data.query(params, function (data) {
44754479
self.trigger('results:all', {
44764480
data: data,

dist/js/select2.amd.js

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1318,10 +1318,10 @@ define('select2/selection/search',[
13181318
// both the `keyup` and `input` events.
13191319
this.$selection.on('input', '.select2-search--inline', function (evt) {
13201320
// Unbind the duplicated `keyup` event
1321-
$(this).off('keyup');
1321+
self.$selection.off('keyup.search');
13221322
});
13231323

1324-
this.$selection.on('keyup input', '.select2-search--inline',
1324+
this.$selection.on('keyup.search input', '.select2-search--inline',
13251325
function (evt) {
13261326
self.handleSearch(evt);
13271327
});
@@ -4471,6 +4471,10 @@ define('select2/core',[
44714471
});
44724472

44734473
this.on('query', function (params) {
4474+
if (!self.isOpen()) {
4475+
self.trigger('open');
4476+
}
4477+
44744478
this.data.query(params, function (data) {
44754479
self.trigger('results:all', {
44764480
data: data,

dist/js/select2.full.js

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1757,10 +1757,10 @@ define('select2/selection/search',[
17571757
// both the `keyup` and `input` events.
17581758
this.$selection.on('input', '.select2-search--inline', function (evt) {
17591759
// Unbind the duplicated `keyup` event
1760-
$(this).off('keyup');
1760+
self.$selection.off('keyup.search');
17611761
});
17621762

1763-
this.$selection.on('keyup input', '.select2-search--inline',
1763+
this.$selection.on('keyup.search input', '.select2-search--inline',
17641764
function (evt) {
17651765
self.handleSearch(evt);
17661766
});
@@ -4910,6 +4910,10 @@ define('select2/core',[
49104910
});
49114911

49124912
this.on('query', function (params) {
4913+
if (!self.isOpen()) {
4914+
self.trigger('open');
4915+
}
4916+
49134917
this.data.query(params, function (data) {
49144918
self.trigger('results:all', {
49154919
data: data,

dist/js/select2.full.min.js

Lines changed: 3 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/js/select2.js

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1757,10 +1757,10 @@ define('select2/selection/search',[
17571757
// both the `keyup` and `input` events.
17581758
this.$selection.on('input', '.select2-search--inline', function (evt) {
17591759
// Unbind the duplicated `keyup` event
1760-
$(this).off('keyup');
1760+
self.$selection.off('keyup.search');
17611761
});
17621762

1763-
this.$selection.on('keyup input', '.select2-search--inline',
1763+
this.$selection.on('keyup.search input', '.select2-search--inline',
17641764
function (evt) {
17651765
self.handleSearch(evt);
17661766
});
@@ -4910,6 +4910,10 @@ define('select2/core',[
49104910
});
49114911

49124912
this.on('query', function (params) {
4913+
if (!self.isOpen()) {
4914+
self.trigger('open');
4915+
}
4916+
49134917
this.data.query(params, function (data) {
49144918
self.trigger('results:all', {
49154919
data: data,

dist/js/select2.min.js

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/js/select2/core.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -272,6 +272,10 @@ define([
272272
});
273273

274274
this.on('query', function (params) {
275+
if (!self.isOpen()) {
276+
self.trigger('open');
277+
}
278+
275279
this.data.query(params, function (data) {
276280
self.trigger('results:all', {
277281
data: data,

src/js/select2/selection/search.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -84,10 +84,10 @@ define([
8484
// both the `keyup` and `input` events.
8585
this.$selection.on('input', '.select2-search--inline', function (evt) {
8686
// Unbind the duplicated `keyup` event
87-
$(this).off('keyup');
87+
self.$selection.off('keyup.search');
8888
});
8989

90-
this.$selection.on('keyup input', '.select2-search--inline',
90+
this.$selection.on('keyup.search input', '.select2-search--inline',
9191
function (evt) {
9292
self.handleSearch(evt);
9393
});

0 commit comments

Comments
 (0)