Skip to content

Commit 8957615

Browse files
authored
Mirror disabled state through aria-disabled on selection (select2#5579)
This is needed to screen readers know that the Select2 is disabled when focus is put on the selection container. Because we were mirroring the disabled state to the search input on a multiple select in the past, this is really only needed for single select elements which would not otherwise has the disabled property. This was identified in a previous accessibility audit as being something which Select2 did not properly report because we were not setting the attributes properly. Fixes select2#4575
1 parent eeefa1e commit 8957615

2 files changed

Lines changed: 35 additions & 0 deletions

File tree

src/js/select2/selection/base.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ define([
2929

3030
$selection.attr('title', this.$element.attr('title'));
3131
$selection.attr('tabindex', this._tabindex);
32+
$selection.attr('aria-disabled', 'false');
3233

3334
this.$selection = $selection;
3435

@@ -88,10 +89,12 @@ define([
8889

8990
container.on('enable', function () {
9091
self.$selection.attr('tabindex', self._tabindex);
92+
self.$selection.attr('aria-disabled', 'false');
9193
});
9294

9395
container.on('disable', function () {
9496
self.$selection.attr('tabindex', '-1');
97+
self.$selection.attr('aria-disabled', 'true');
9598
});
9699
};
97100

tests/a11y/selection-tests.js

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,38 @@ test('a custom tabindex is copied', function (assert) {
131131
);
132132
});
133133

134+
test('aria-disabled should reflected disabled state', function (assert) {
135+
var $select = $('#qunit-fixture .single');
136+
137+
var selection = new BaseSelection($select, options);
138+
var $selection = selection.render();
139+
140+
var container = new MockContainer();
141+
selection.bind(container, $('<span></span>'));
142+
143+
assert.equal(
144+
$selection.attr('aria-disabled'),
145+
'false',
146+
'The tab index should match the original tab index'
147+
);
148+
149+
container.trigger('disable');
150+
151+
assert.equal(
152+
$selection.attr('aria-disabled'),
153+
'true',
154+
'The selection should be dropped out of the tab order when disabled'
155+
);
156+
157+
container.trigger('enable');
158+
159+
assert.equal(
160+
$selection.attr('aria-disabled'),
161+
'false',
162+
'The tab index should be restored when re-enabled'
163+
);
164+
});
165+
134166
module('Accessibility - Single');
135167

136168
test('aria-labelledby should match the rendered container', function (assert) {

0 commit comments

Comments
 (0)