Skip to content

Commit

Permalink
Add option labelId to improve a11y (#971)
Browse files Browse the repository at this point in the history
* Add option labelId to improve a11y

* Console.log entfernt

* Add test case for aria-labeledby

Co-authored-by: Josua Vogel <josua.vogel@t-systems.com>
  • Loading branch information
brosua and Josua Vogel authored Jan 18, 2022
1 parent 06d4435 commit eb22895
Show file tree
Hide file tree
Showing 15 changed files with 89 additions and 10 deletions.
9 changes: 9 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -186,6 +186,7 @@ Or include Choices directly:
fuseOptions: {
includeScore: true
},
labelId: '',
callbackOnInit: null,
callbackOnCreateTemplates: null
});
Expand Down Expand Up @@ -601,6 +602,14 @@ const example = new Choices(element, {
};
```
### labelId
**Type:** `String` **Default:** ``
**Input types affected:** `select-one`, `select-multiple`
**Usage:** The labelId improves accessibility. If set, it will add aria-labeledby to the choices element.
### classNames
**Type:** `Object` **Default:**
Expand Down
10 changes: 8 additions & 2 deletions public/assets/scripts/choices.js
Original file line number Diff line number Diff line change
Expand Up @@ -2222,7 +2222,7 @@ function () {

Choices.prototype._createElements = function () {
this.containerOuter = new components_1.Container({
element: this._getTemplate('containerOuter', this._direction, this._isSelectElement, this._isSelectOneElement, this.config.searchEnabled, this.passedElement.element.type),
element: this._getTemplate('containerOuter', this._direction, this._isSelectElement, this._isSelectOneElement, this.config.searchEnabled, this.passedElement.element.type, this.config.labelId),
classNames: this.config.classNames,
type: this.passedElement.element.type,
position: this.config.position
Expand Down Expand Up @@ -3514,6 +3514,7 @@ exports.DEFAULT_CONFIG = {
fuseOptions: {
includeScore: true
},
labelId: '',
callbackOnInit: null,
callbackOnCreateTemplates: null,
classNames: exports.DEFAULT_CLASSNAMES
Expand Down Expand Up @@ -4586,7 +4587,7 @@ Object.defineProperty(exports, "__esModule", ({
value: true
}));
var templates = {
containerOuter: function (_a, dir, isSelectElement, isSelectOneElement, searchEnabled, passedElementType) {
containerOuter: function (_a, dir, isSelectElement, isSelectOneElement, searchEnabled, passedElementType, labelId) {
var containerOuter = _a.classNames.containerOuter;
var div = Object.assign(document.createElement('div'), {
className: containerOuter
Expand All @@ -4611,6 +4612,11 @@ var templates = {

div.setAttribute('aria-haspopup', 'true');
div.setAttribute('aria-expanded', 'false');

if (labelId) {
div.setAttribute('aria-labeledby', labelId);
}

return div;
},
containerInner: function (_a) {
Expand Down
2 changes: 1 addition & 1 deletion public/assets/scripts/choices.min.js

Large diffs are not rendered by default.

11 changes: 9 additions & 2 deletions public/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -288,7 +288,9 @@ <h2>Multiple select input</h2>
<hr />

<h2>Single select input</h2>
<label for="choices-single-default">Default</label>
<label id="choices-single-default-label" for="choices-single-default"
>Default</label
>
<select
class="form-control"
data-trigger
Expand Down Expand Up @@ -433,7 +435,11 @@ <h2>Single select input</h2>
>
</select>

<label for="choices-single-no-sorting">Options without sorting</label>
<label
id="choices-single-no-sorting-label"
for="choices-single-no-sorting"
>Options without sorting</label
>
<select
class="form-control"
name="choices-single-no-sorting"
Expand Down Expand Up @@ -802,6 +808,7 @@ <h2>Form interaction</h2>
var singleNoSorting = new Choices('#choices-single-no-sorting', {
allowHTML: true,
shouldSort: false,
labelId: 'choices-single-no-sorting-label',
});

var cities = new Choices(document.getElementById('cities'), { allowHTML: true });
Expand Down
2 changes: 1 addition & 1 deletion public/types/src/scripts/choices.d.ts.map

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion public/types/src/scripts/defaults.d.ts.map

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 4 additions & 0 deletions public/types/src/scripts/interfaces/options.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -411,6 +411,10 @@ export interface Options {
* Choices uses the great Fuse library for searching. You can find more options here: https://fusejs.io/api/options.html
*/
fuseOptions: Fuse.IFuseOptions<Choices>;
/**
* ID of the connected label to improve a11y. If set, aria-labeledby will be added.
*/
labelId: string;
/**
* Function to run once Choices initialises.
*
Expand Down
Loading

0 comments on commit eb22895

Please sign in to comment.