Skip to content

Commit 2cf64df

Browse files
authored
Set title for single select placeholder (select2#5973)
This sets the title of the rendered selection for a placeholder when it is displayed within a single select. This is not necessary for a multiple select because the placeholder is set on the search box which will properly convey the value of it and meets ARIA guidelines. Because single select boxes currently use a `textbox` role to convey the keyboard accessibility of the textbox area, and because that role is currently required in ARIA 1.1, it must be properly labelled using the `title`, `aria-label`, or `aria-labelledby` attribute. This currently happens for single selects without a placeholder, and single selects with a placeholder but with an active selection, because the `title` attribute is set to the title of the active selection. Because it only currently sets it for the active selection, this caused an edge case specific to placeholders for single selects where the selection was not properly labelled. The placeholder title follows the same rules for setting the attribute value as for the regular single select selection, except placeholders will also fall back to just using the text value of the placeholder element in the event that the `title` and `text` attributes are not specified on the placeholder data option.
1 parent af79c80 commit 2cf64df

2 files changed

Lines changed: 53 additions & 1 deletion

File tree

src/js/select2/selection/placeholder.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,15 @@ define([
2525
$placeholder[0].classList.add('select2-selection__placeholder');
2626
$placeholder[0].classList.remove('select2-selection__choice');
2727

28+
var placeholderTitle = placeholder.title ||
29+
placeholder.text ||
30+
$placeholder.text();
31+
32+
this.$selection.find('.select2-selection__rendered').attr(
33+
'title',
34+
placeholderTitle
35+
);
36+
2837
return $placeholder;
2938
};
3039

tests/selection/placeholder-tests.js

Lines changed: 44 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,6 @@ test('normalizing placeholder gives object for string', function (assert) {
4444
assert.equal(normalized.text, 'placeholder');
4545
});
4646

47-
4847
test('text is shown for placeholder option on single', function (assert) {
4948
var selection = new SinglePlaceholder(
5049
$('#qunit-fixture .single'),
@@ -60,6 +59,50 @@ test('text is shown for placeholder option on single', function (assert) {
6059
assert.equal($selection.text(), 'This is the placeholder');
6160
});
6261

62+
test('title is set for placeholder option on single', function (assert) {
63+
var selection = new SinglePlaceholder(
64+
$('#qunit-fixture .single'),
65+
placeholderOptions
66+
);
67+
68+
var $selection = selection.render();
69+
70+
selection.update([{
71+
id: 'placeholder'
72+
}]);
73+
74+
assert.equal(
75+
$selection.find('.select2-selection__rendered').attr('title'),
76+
'This is the placeholder'
77+
);
78+
});
79+
80+
test('title is used for placeholder option on single', function (assert) {
81+
var placeholderTitleOptions = new Options({
82+
placeholder: {
83+
id: 'placeholder',
84+
text: 'This is the placeholder',
85+
title: 'This is the placeholder title'
86+
}
87+
});
88+
89+
var selection = new SinglePlaceholder(
90+
$('#qunit-fixture .single'),
91+
placeholderTitleOptions
92+
);
93+
94+
var $selection = selection.render();
95+
96+
selection.update([{
97+
id: 'placeholder'
98+
}]);
99+
100+
assert.equal(
101+
$selection.find('.select2-selection__rendered').attr('title'),
102+
'This is the placeholder title'
103+
);
104+
});
105+
63106
test('placeholder is shown when no options are selected', function (assert) {
64107
var selection = new SinglePlaceholder(
65108
$('#qunit-fixture .multiple'),

0 commit comments

Comments
 (0)