1
0
mirror of synced 2025-03-10 14:46:10 +03:00

Add back keyboard support within allowClear

This adds back keyboard support, so you can now clear a selected
item using either the backspace or delete key.  This only work when
the container is closed, to prevent issues with the selection being
clear while a user is searching.

This was a regression in accessibility from 3.x.

This closes https://github.com/select2/select2/issues/3224.
This commit is contained in:
Kevin Brown 2015-04-02 11:12:11 -04:00
parent 00a78bdb1e
commit a8e6cbc0c9
5 changed files with 180 additions and 98 deletions

View File

@ -1638,8 +1638,9 @@ S2.define('select2/selection/placeholder',[
}); });
S2.define('select2/selection/allowClear',[ S2.define('select2/selection/allowClear',[
'jquery' 'jquery',
], function ($) { '../keys'
], function ($, KEYS) {
function AllowClear () { } function AllowClear () { }
AllowClear.prototype.bind = function (decorated, container, $container) { AllowClear.prototype.bind = function (decorated, container, $container) {
@ -1647,8 +1648,8 @@ S2.define('select2/selection/allowClear',[
decorated.call(this, container, $container); decorated.call(this, container, $container);
if (self.placeholder == null) { if (this.placeholder == null) {
if (self.options.get('debug') && window.console && console.error) { if (this.options.get('debug') && window.console && console.error) {
console.error( console.error(
'Select2: The `allowClear` option should be used in combination ' + 'Select2: The `allowClear` option should be used in combination ' +
'with the `placeholder` option.' 'with the `placeholder` option.'
@ -1658,34 +1659,61 @@ S2.define('select2/selection/allowClear',[
this.$selection.on('mousedown', '.select2-selection__clear', this.$selection.on('mousedown', '.select2-selection__clear',
function (evt) { function (evt) {
// Ignore the event if it is disabled self._handleClear(evt);
if (self.options.get('disabled')) {
return;
}
evt.stopPropagation();
var data = $(this).data('data');
for (var d = 0; d < data.length; d++) {
var unselectData = {
data: data[d]
};
// Trigger the `unselect` event, so people can prevent it from being
// cleared.
self.trigger('unselect', unselectData);
// If the event was prevented, don't clear it out.
if (unselectData.prevented) {
return;
}
}
self.$element.val(self.placeholder.id).trigger('change');
self.trigger('toggle');
}); });
container.on('keypress', function (evt) {
self._handleKeyboardClear(evt, container);
});
};
AllowClear.prototype._handleClear = function (_, evt) {
console.log(arguments);
// Ignore the event if it is disabled
if (this.options.get('disabled')) {
return;
}
var $clear = this.$selection.find('.select2-selection__clear');
// Ignore the event if nothing has been selected
if ($clear.length === 0) {
return;
}
evt.stopPropagation();
var data = $clear.data('data');
for (var d = 0; d < data.length; d++) {
var unselectData = {
data: data[d]
};
// Trigger the `unselect` event, so people can prevent it from being
// cleared.
this.trigger('unselect', unselectData);
// If the event was prevented, don't clear it out.
if (unselectData.prevented) {
return;
}
}
this.$element.val(this.placeholder.id).trigger('change');
this.trigger('toggle');
};
AllowClear.prototype._handleKeyboardClear = function (_, evt, container) {
if (container.isOpen()) {
return;
}
if (evt.which == KEYS.DELETE || evt.which == KEYS.BACKSPACE) {
this._handleClear(evt);
}
}; };
AllowClear.prototype.update = function (decorated, data) { AllowClear.prototype.update = function (decorated, data) {

File diff suppressed because one or more lines are too long

90
dist/js/select2.js vendored
View File

@ -1638,8 +1638,9 @@ S2.define('select2/selection/placeholder',[
}); });
S2.define('select2/selection/allowClear',[ S2.define('select2/selection/allowClear',[
'jquery' 'jquery',
], function ($) { '../keys'
], function ($, KEYS) {
function AllowClear () { } function AllowClear () { }
AllowClear.prototype.bind = function (decorated, container, $container) { AllowClear.prototype.bind = function (decorated, container, $container) {
@ -1647,8 +1648,8 @@ S2.define('select2/selection/allowClear',[
decorated.call(this, container, $container); decorated.call(this, container, $container);
if (self.placeholder == null) { if (this.placeholder == null) {
if (self.options.get('debug') && window.console && console.error) { if (this.options.get('debug') && window.console && console.error) {
console.error( console.error(
'Select2: The `allowClear` option should be used in combination ' + 'Select2: The `allowClear` option should be used in combination ' +
'with the `placeholder` option.' 'with the `placeholder` option.'
@ -1658,34 +1659,61 @@ S2.define('select2/selection/allowClear',[
this.$selection.on('mousedown', '.select2-selection__clear', this.$selection.on('mousedown', '.select2-selection__clear',
function (evt) { function (evt) {
// Ignore the event if it is disabled self._handleClear(evt);
if (self.options.get('disabled')) {
return;
}
evt.stopPropagation();
var data = $(this).data('data');
for (var d = 0; d < data.length; d++) {
var unselectData = {
data: data[d]
};
// Trigger the `unselect` event, so people can prevent it from being
// cleared.
self.trigger('unselect', unselectData);
// If the event was prevented, don't clear it out.
if (unselectData.prevented) {
return;
}
}
self.$element.val(self.placeholder.id).trigger('change');
self.trigger('toggle');
}); });
container.on('keypress', function (evt) {
self._handleKeyboardClear(evt, container);
});
};
AllowClear.prototype._handleClear = function (_, evt) {
console.log(arguments);
// Ignore the event if it is disabled
if (this.options.get('disabled')) {
return;
}
var $clear = this.$selection.find('.select2-selection__clear');
// Ignore the event if nothing has been selected
if ($clear.length === 0) {
return;
}
evt.stopPropagation();
var data = $clear.data('data');
for (var d = 0; d < data.length; d++) {
var unselectData = {
data: data[d]
};
// Trigger the `unselect` event, so people can prevent it from being
// cleared.
this.trigger('unselect', unselectData);
// If the event was prevented, don't clear it out.
if (unselectData.prevented) {
return;
}
}
this.$element.val(this.placeholder.id).trigger('change');
this.trigger('toggle');
};
AllowClear.prototype._handleKeyboardClear = function (_, evt, container) {
if (container.isOpen()) {
return;
}
if (evt.which == KEYS.DELETE || evt.which == KEYS.BACKSPACE) {
this._handleClear(evt);
}
}; };
AllowClear.prototype.update = function (decorated, data) { AllowClear.prototype.update = function (decorated, data) {

File diff suppressed because one or more lines are too long

View File

@ -1,6 +1,7 @@
define([ define([
'jquery' 'jquery',
], function ($) { '../keys'
], function ($, KEYS) {
function AllowClear () { } function AllowClear () { }
AllowClear.prototype.bind = function (decorated, container, $container) { AllowClear.prototype.bind = function (decorated, container, $container) {
@ -8,8 +9,8 @@ define([
decorated.call(this, container, $container); decorated.call(this, container, $container);
if (self.placeholder == null) { if (this.placeholder == null) {
if (self.options.get('debug') && window.console && console.error) { if (this.options.get('debug') && window.console && console.error) {
console.error( console.error(
'Select2: The `allowClear` option should be used in combination ' + 'Select2: The `allowClear` option should be used in combination ' +
'with the `placeholder` option.' 'with the `placeholder` option.'
@ -19,34 +20,59 @@ define([
this.$selection.on('mousedown', '.select2-selection__clear', this.$selection.on('mousedown', '.select2-selection__clear',
function (evt) { function (evt) {
// Ignore the event if it is disabled self._handleClear(evt);
if (self.options.get('disabled')) {
return;
}
evt.stopPropagation();
var data = $(this).data('data');
for (var d = 0; d < data.length; d++) {
var unselectData = {
data: data[d]
};
// Trigger the `unselect` event, so people can prevent it from being
// cleared.
self.trigger('unselect', unselectData);
// If the event was prevented, don't clear it out.
if (unselectData.prevented) {
return;
}
}
self.$element.val(self.placeholder.id).trigger('change');
self.trigger('toggle');
}); });
container.on('keypress', function (evt) {
self._handleKeyboardClear(evt, container);
});
};
AllowClear.prototype._handleClear = function (_, evt) {
// Ignore the event if it is disabled
if (this.options.get('disabled')) {
return;
}
var $clear = this.$selection.find('.select2-selection__clear');
// Ignore the event if nothing has been selected
if ($clear.length === 0) {
return;
}
evt.stopPropagation();
var data = $clear.data('data');
for (var d = 0; d < data.length; d++) {
var unselectData = {
data: data[d]
};
// Trigger the `unselect` event, so people can prevent it from being
// cleared.
this.trigger('unselect', unselectData);
// If the event was prevented, don't clear it out.
if (unselectData.prevented) {
return;
}
}
this.$element.val(this.placeholder.id).trigger('change');
this.trigger('toggle');
};
AllowClear.prototype._handleKeyboardClear = function (_, evt, container) {
if (container.isOpen()) {
return;
}
if (evt.which == KEYS.DELETE || evt.which == KEYS.BACKSPACE) {
this._handleClear(evt);
}
}; };
AllowClear.prototype.update = function (decorated, data) { AllowClear.prototype.update = function (decorated, data) {