Skip to content

Commit 31e7a1d

Browse files
committed
Support the focus event on the <select> element
This adds basic support for focusing the Select2 element by triggering the `focus` event on the underlying <select> element. This implicitly adds support for <label> elements, which will trigger the `focus` event on the `<select>` if it is clicked. This also fixes the focus issue that previously existed if Select2 was opened while in a <label>. The focus would be transferred to the search dropdown, and then immediately pulled away to the <select> element. This happened because the <label> element triggers the `focus` event when a `click` event propagates its way up, and we do not stop the propagation of the `click` event when it is triggered on the selection. This closes select2#2311. This closes select2#4203. This closes select2#4235.
1 parent b10976b commit 31e7a1d

3 files changed

Lines changed: 16 additions & 0 deletions

File tree

src/js/select2/core.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -181,6 +181,10 @@ define([
181181
});
182182
});
183183

184+
this.$element.on('focus.select2', function (evt) {
185+
self.trigger('focus', evt);
186+
});
187+
184188
this._sync = Utils.bind(this._syncAttributes, this);
185189

186190
if (this.$element[0].attachEvent) {

src/js/select2/dropdown/search.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,12 @@ define([
6262
self.$search.val('');
6363
});
6464

65+
container.on('focus', function () {
66+
if (container.isOpen()) {
67+
self.$search.focus();
68+
}
69+
});
70+
6571
container.on('results:all', function (params) {
6672
if (params.query.term == null || params.query.term === '') {
6773
var showSearch = self.showSearch(params);

src/js/select2/selection/single.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,12 @@ define([
5454
// User exits the container
5555
});
5656

57+
container.on('focus', function (evt) {
58+
if (!container.isOpen()) {
59+
self.$selection.focus();
60+
}
61+
});
62+
5763
container.on('selection:update', function (params) {
5864
self.update(params.data);
5965
});

0 commit comments

Comments
 (0)