1
0
mirror of synced 2025-03-10 22:56:12 +03:00

Improved focus detection and key handling

Now the focus of the selection container is correctly monitored so
there is a consistent 1px black border on the default theme whenever
it is focused.  This requires `focusin`/`focusout` support, which is
supported by all major browsers except for Firefox.  In Firefox, the
old focus appearance is still consistent and has not been broken
by this update.

The key handling has also been improved such that some of the logic
detection that was previously done within the search handlers for
multiple select searches is now pushed back to the base selection.

This closes https://github.com/select2/select2/issues/2995.
This commit is contained in:
Kevin Brown 2015-02-14 00:37:51 -05:00
parent b382fdca9c
commit 3d013020ae
12 changed files with 170 additions and 37 deletions

View File

@ -200,6 +200,9 @@
.select2-container--default[dir="rtl"] .select2-selection--multiple .select2-selection__choice__remove { .select2-container--default[dir="rtl"] .select2-selection--multiple .select2-selection__choice__remove {
margin-left: 2px; margin-left: 2px;
margin-right: auto; } margin-right: auto; }
.select2-container--default.select2-container--focus .select2-selection--multiple {
border: solid black 1px;
outline: 0; }
.select2-container--default.select2-container--disabled .select2-selection--multiple { .select2-container--default.select2-container--disabled .select2-selection--multiple {
background-color: #eee; background-color: #eee;
cursor: default; } cursor: default; }

File diff suppressed because one or more lines are too long

View File

@ -792,8 +792,13 @@ define('select2/selection/base',[
'</span>' '</span>'
); );
this._tabindex = this.$element.data('old-tabindex') || this._tabindex = 0;
this.$element.attr('tabindex') || 0;
if (this.$element.data('old-tabindex') != null) {
this._tabindex = this.$element.data('old-tabindex');
} else if (this.$element.attr('tabindex') != null) {
this._tabindex = this.$element.attr('tabindex');
}
$selection.attr('title', this.$element.attr('title')); $selection.attr('title', this.$element.attr('title'));
$selection.attr('tabindex', this._tabindex); $selection.attr('tabindex', this._tabindex);
@ -813,6 +818,14 @@ define('select2/selection/base',[
this.$selection.attr('aria-owns', resultsId); this.$selection.attr('aria-owns', resultsId);
this.$selection.on('focus', function (evt) {
self.trigger('focus', evt);
});
this.$selection.on('blur', function (evt) {
self.trigger('blur', evt);
});
this.$selection.on('keydown', function (evt) { this.$selection.on('keydown', function (evt) {
self.trigger('keypress', evt); self.trigger('keypress', evt);
@ -1267,13 +1280,17 @@ define('select2/selection/search',[
self.$search.prop('disabled', true); self.$search.prop('disabled', true);
}); });
this.$selection.on('focusin', '.select2-search--inline', function (evt) {
self.trigger('focus', evt);
});
this.$selection.on('focusout', '.select2-search--inline', function (evt) {
self.trigger('blur', evt);
});
this.$selection.on('keydown', '.select2-search--inline', function (evt) { this.$selection.on('keydown', '.select2-search--inline', function (evt) {
evt.stopPropagation(); evt.stopPropagation();
if (!container.isOpen()) {
self.trigger('open');
}
self.trigger('keypress', evt); self.trigger('keypress', evt);
self._keyUpPrevented = evt.isDefaultPrevented(); self._keyUpPrevented = evt.isDefaultPrevented();
@ -4394,6 +4411,14 @@ define('select2/core',[
self.$container.addClass('select2-container--disabled'); self.$container.addClass('select2-container--disabled');
}); });
this.on('focus', function () {
self.$container.addClass('select2-container--focus');
});
this.on('blur', function () {
self.$container.removeClass('select2-container--focus');
});
this.on('query', function (params) { this.on('query', function (params) {
this.data.query(params, function (data) { this.data.query(params, function (data) {
self.trigger('results:all', { self.trigger('results:all', {

View File

@ -792,8 +792,13 @@ define('select2/selection/base',[
'</span>' '</span>'
); );
this._tabindex = this.$element.data('old-tabindex') || this._tabindex = 0;
this.$element.attr('tabindex') || 0;
if (this.$element.data('old-tabindex') != null) {
this._tabindex = this.$element.data('old-tabindex');
} else if (this.$element.attr('tabindex') != null) {
this._tabindex = this.$element.attr('tabindex');
}
$selection.attr('title', this.$element.attr('title')); $selection.attr('title', this.$element.attr('title'));
$selection.attr('tabindex', this._tabindex); $selection.attr('tabindex', this._tabindex);
@ -813,6 +818,14 @@ define('select2/selection/base',[
this.$selection.attr('aria-owns', resultsId); this.$selection.attr('aria-owns', resultsId);
this.$selection.on('focus', function (evt) {
self.trigger('focus', evt);
});
this.$selection.on('blur', function (evt) {
self.trigger('blur', evt);
});
this.$selection.on('keydown', function (evt) { this.$selection.on('keydown', function (evt) {
self.trigger('keypress', evt); self.trigger('keypress', evt);
@ -1267,13 +1280,17 @@ define('select2/selection/search',[
self.$search.prop('disabled', true); self.$search.prop('disabled', true);
}); });
this.$selection.on('focusin', '.select2-search--inline', function (evt) {
self.trigger('focus', evt);
});
this.$selection.on('focusout', '.select2-search--inline', function (evt) {
self.trigger('blur', evt);
});
this.$selection.on('keydown', '.select2-search--inline', function (evt) { this.$selection.on('keydown', '.select2-search--inline', function (evt) {
evt.stopPropagation(); evt.stopPropagation();
if (!container.isOpen()) {
self.trigger('open');
}
self.trigger('keypress', evt); self.trigger('keypress', evt);
self._keyUpPrevented = evt.isDefaultPrevented(); self._keyUpPrevented = evt.isDefaultPrevented();
@ -4394,6 +4411,14 @@ define('select2/core',[
self.$container.addClass('select2-container--disabled'); self.$container.addClass('select2-container--disabled');
}); });
this.on('focus', function () {
self.$container.addClass('select2-container--focus');
});
this.on('blur', function () {
self.$container.removeClass('select2-container--focus');
});
this.on('query', function (params) { this.on('query', function (params) {
this.data.query(params, function (data) { this.data.query(params, function (data) {
self.trigger('results:all', { self.trigger('results:all', {

View File

@ -1231,8 +1231,13 @@ define('select2/selection/base',[
'</span>' '</span>'
); );
this._tabindex = this.$element.data('old-tabindex') || this._tabindex = 0;
this.$element.attr('tabindex') || 0;
if (this.$element.data('old-tabindex') != null) {
this._tabindex = this.$element.data('old-tabindex');
} else if (this.$element.attr('tabindex') != null) {
this._tabindex = this.$element.attr('tabindex');
}
$selection.attr('title', this.$element.attr('title')); $selection.attr('title', this.$element.attr('title'));
$selection.attr('tabindex', this._tabindex); $selection.attr('tabindex', this._tabindex);
@ -1252,6 +1257,14 @@ define('select2/selection/base',[
this.$selection.attr('aria-owns', resultsId); this.$selection.attr('aria-owns', resultsId);
this.$selection.on('focus', function (evt) {
self.trigger('focus', evt);
});
this.$selection.on('blur', function (evt) {
self.trigger('blur', evt);
});
this.$selection.on('keydown', function (evt) { this.$selection.on('keydown', function (evt) {
self.trigger('keypress', evt); self.trigger('keypress', evt);
@ -1706,13 +1719,17 @@ define('select2/selection/search',[
self.$search.prop('disabled', true); self.$search.prop('disabled', true);
}); });
this.$selection.on('focusin', '.select2-search--inline', function (evt) {
self.trigger('focus', evt);
});
this.$selection.on('focusout', '.select2-search--inline', function (evt) {
self.trigger('blur', evt);
});
this.$selection.on('keydown', '.select2-search--inline', function (evt) { this.$selection.on('keydown', '.select2-search--inline', function (evt) {
evt.stopPropagation(); evt.stopPropagation();
if (!container.isOpen()) {
self.trigger('open');
}
self.trigger('keypress', evt); self.trigger('keypress', evt);
self._keyUpPrevented = evt.isDefaultPrevented(); self._keyUpPrevented = evt.isDefaultPrevented();
@ -4833,6 +4850,14 @@ define('select2/core',[
self.$container.addClass('select2-container--disabled'); self.$container.addClass('select2-container--disabled');
}); });
this.on('focus', function () {
self.$container.addClass('select2-container--focus');
});
this.on('blur', function () {
self.$container.removeClass('select2-container--focus');
});
this.on('query', function (params) { this.on('query', function (params) {
this.data.query(params, function (data) { this.data.query(params, function (data) {
self.trigger('results:all', { self.trigger('results:all', {

File diff suppressed because one or more lines are too long

37
dist/js/select2.js vendored
View File

@ -1231,8 +1231,13 @@ define('select2/selection/base',[
'</span>' '</span>'
); );
this._tabindex = this.$element.data('old-tabindex') || this._tabindex = 0;
this.$element.attr('tabindex') || 0;
if (this.$element.data('old-tabindex') != null) {
this._tabindex = this.$element.data('old-tabindex');
} else if (this.$element.attr('tabindex') != null) {
this._tabindex = this.$element.attr('tabindex');
}
$selection.attr('title', this.$element.attr('title')); $selection.attr('title', this.$element.attr('title'));
$selection.attr('tabindex', this._tabindex); $selection.attr('tabindex', this._tabindex);
@ -1252,6 +1257,14 @@ define('select2/selection/base',[
this.$selection.attr('aria-owns', resultsId); this.$selection.attr('aria-owns', resultsId);
this.$selection.on('focus', function (evt) {
self.trigger('focus', evt);
});
this.$selection.on('blur', function (evt) {
self.trigger('blur', evt);
});
this.$selection.on('keydown', function (evt) { this.$selection.on('keydown', function (evt) {
self.trigger('keypress', evt); self.trigger('keypress', evt);
@ -1706,13 +1719,17 @@ define('select2/selection/search',[
self.$search.prop('disabled', true); self.$search.prop('disabled', true);
}); });
this.$selection.on('focusin', '.select2-search--inline', function (evt) {
self.trigger('focus', evt);
});
this.$selection.on('focusout', '.select2-search--inline', function (evt) {
self.trigger('blur', evt);
});
this.$selection.on('keydown', '.select2-search--inline', function (evt) { this.$selection.on('keydown', '.select2-search--inline', function (evt) {
evt.stopPropagation(); evt.stopPropagation();
if (!container.isOpen()) {
self.trigger('open');
}
self.trigger('keypress', evt); self.trigger('keypress', evt);
self._keyUpPrevented = evt.isDefaultPrevented(); self._keyUpPrevented = evt.isDefaultPrevented();
@ -4833,6 +4850,14 @@ define('select2/core',[
self.$container.addClass('select2-container--disabled'); self.$container.addClass('select2-container--disabled');
}); });
this.on('focus', function () {
self.$container.addClass('select2-container--focus');
});
this.on('blur', function () {
self.$container.removeClass('select2-container--focus');
});
this.on('query', function (params) { this.on('query', function (params) {
this.data.query(params, function (data) { this.data.query(params, function (data) {
self.trigger('results:all', { self.trigger('results:all', {

File diff suppressed because one or more lines are too long

View File

@ -263,6 +263,14 @@ define([
self.$container.addClass('select2-container--disabled'); self.$container.addClass('select2-container--disabled');
}); });
this.on('focus', function () {
self.$container.addClass('select2-container--focus');
});
this.on('blur', function () {
self.$container.removeClass('select2-container--focus');
});
this.on('query', function (params) { this.on('query', function (params) {
this.data.query(params, function (data) { this.data.query(params, function (data) {
self.trigger('results:all', { self.trigger('results:all', {

View File

@ -19,8 +19,13 @@ define([
'</span>' '</span>'
); );
this._tabindex = this.$element.data('old-tabindex') || this._tabindex = 0;
this.$element.attr('tabindex') || 0;
if (this.$element.data('old-tabindex') != null) {
this._tabindex = this.$element.data('old-tabindex');
} else if (this.$element.attr('tabindex') != null) {
this._tabindex = this.$element.attr('tabindex');
}
$selection.attr('title', this.$element.attr('title')); $selection.attr('title', this.$element.attr('title'));
$selection.attr('tabindex', this._tabindex); $selection.attr('tabindex', this._tabindex);
@ -40,6 +45,14 @@ define([
this.$selection.attr('aria-owns', resultsId); this.$selection.attr('aria-owns', resultsId);
this.$selection.on('focus', function (evt) {
self.trigger('focus', evt);
});
this.$selection.on('blur', function (evt) {
self.trigger('blur', evt);
});
this.$selection.on('keydown', function (evt) { this.$selection.on('keydown', function (evt) {
self.trigger('keypress', evt); self.trigger('keypress', evt);

View File

@ -50,13 +50,17 @@ define([
self.$search.prop('disabled', true); self.$search.prop('disabled', true);
}); });
this.$selection.on('focusin', '.select2-search--inline', function (evt) {
self.trigger('focus', evt);
});
this.$selection.on('focusout', '.select2-search--inline', function (evt) {
self.trigger('blur', evt);
});
this.$selection.on('keydown', '.select2-search--inline', function (evt) { this.$selection.on('keydown', '.select2-search--inline', function (evt) {
evt.stopPropagation(); evt.stopPropagation();
if (!container.isOpen()) {
self.trigger('open');
}
self.trigger('keypress', evt); self.trigger('keypress', evt);
self._keyUpPrevented = evt.isDefaultPrevented(); self._keyUpPrevented = evt.isDefaultPrevented();

View File

@ -75,14 +75,19 @@
} }
} }
&.select2-container--focus {
.select2-selection--multiple {
border: solid black 1px;
outline: 0;
}
}
&.select2-container--disabled { &.select2-container--disabled {
.select2-selection--multiple { .select2-selection--multiple {
background-color: #eee; background-color: #eee;
cursor: default; cursor: default;
} }
.select2-selection__choice__remove { .select2-selection__choice__remove {
display: none; display: none;
} }