1
0
mirror of synced 2025-02-03 21:59:24 +03:00

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 https://github.com/select2/select2/issues/2311.
This closes https://github.com/select2/select2/issues/4203.
This closes https://github.com/select2/select2/pull/4235.
This commit is contained in:
Kevin Brown 2016-04-23 19:58:04 -04:00
parent b10976bd25
commit 31e7a1d4c5
3 changed files with 16 additions and 0 deletions

View File

@ -181,6 +181,10 @@ define([
});
});
this.$element.on('focus.select2', function (evt) {
self.trigger('focus', evt);
});
this._sync = Utils.bind(this._syncAttributes, this);
if (this.$element[0].attachEvent) {

View File

@ -62,6 +62,12 @@ define([
self.$search.val('');
});
container.on('focus', function () {
if (container.isOpen()) {
self.$search.focus();
}
});
container.on('results:all', function (params) {
if (params.query.term == null || params.query.term === '') {
var showSearch = self.showSearch(params);

View File

@ -54,6 +54,12 @@ define([
// User exits the container
});
container.on('focus', function (evt) {
if (!container.isOpen()) {
self.$selection.focus();
}
});
container.on('selection:update', function (params) {
self.update(params.data);
});