Skip to content

Commit 6db8e1b

Browse files
committed
feat(a11y): Add ariaLabel option.
- Removes old aria-labelledby logic
1 parent bfc207a commit 6db8e1b

File tree

3 files changed

+13
-53
lines changed

3 files changed

+13
-53
lines changed

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -316,7 +316,7 @@ When initializing an autocomplete, there are a number of options you can configu
316316
* `keyboardShortcuts` - Array of shortcut that will focus the input. For example if you want to bind `s` and `/`
317317
you can specify: `keyboardShortcuts: ['s', '/']`
318318

319-
* `ariaLabelledBy` - An optional id to use for the `aria-labelledby` attribute. Specify `false` to exclude the attribute. Defaults to using the `placeholder` as the label if a `placeholder` is specified.
319+
* `ariaLabel` - An optional string that will populate the `aria-label` attribute.
320320

321321
```html
322322
<script type="text/template" id="my-custom-menu-template">
@@ -776,7 +776,7 @@ Autocomplete.js is accessible to screen readers, and here's how to test how most
776776
1. Type a search query
777777
1. Use the arrow keys to navigate through the results
778778
779-
✔ SUCCESS: results are read (not necessarily in sync with the visually selected cursor)
779+
✔ SUCCESS: results are read (not necessarily in sync with the visually selected cursor)
780780
𐄂 FAIL: no text is read or the screen reader keeps reading the typed query
781781
782782
#### Recommended testing platforms

src/autocomplete/typeahead.js

Lines changed: 1 addition & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -544,17 +544,6 @@ function buildDom(options) {
544544
type: $input.attr('type')
545545
});
546546

547-
// Use ariaLabelledBy option if specified
548-
var ariaLabelledBy = options.ariaLabelledBy;
549-
if (ariaLabelledBy === false) {
550-
// If it is explicity false, null the field
551-
ariaLabelledBy = null;
552-
} else if (!ariaLabelledBy && $input.attr('placeholder')) {
553-
// If a placeholder is set, label this field with itself, which in this case,
554-
// is an explicit pointer to use the placeholder attribute value.
555-
ariaLabelledBy = $input.attr('id');
556-
}
557-
558547
$input
559548
.addClass(_.className(options.cssClasses.prefix, options.cssClasses.input, true))
560549
.attr({
@@ -573,7 +562,7 @@ function buildDom(options) {
573562
options.datasets[0] && options.datasets[0].displayKey ? 'both' : 'list'),
574563
// Indicates whether the dropdown it controls is currently expanded or collapsed
575564
'aria-expanded': 'false',
576-
'aria-labelledby': ariaLabelledBy,
565+
'aria-label': options.ariaLabel,
577566
// Explicitly point to the listbox,
578567
// which is a list of suggestions (aka options)
579568
'aria-owns': options.listboxId

test/unit/typeahead_spec.js

Lines changed: 10 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -884,55 +884,26 @@ describe('Typeahead', function() {
884884
});
885885
});
886886

887-
describe('when ariaLabelledBy is set', function() {
887+
describe('when aria-label is set', function() {
888888
beforeEach(function() {
889889
this.view.destroy();
890890
});
891891

892-
describe('when set to a specific id', function() {
893-
it('should set aria-labelledby to the specified id', function() {
894-
this.view = new Typeahead({
895-
input: this.$input,
896-
ariaLabelledBy: 'custom-id-attr'
897-
});
898-
899-
expect(this.$input.attr('aria-labelledby')).toBe('custom-id-attr');
892+
it('should set aria-label to the specified string', function() {
893+
this.view = new Typeahead({
894+
input: this.$input,
895+
ariaLabel: 'custom-aria-label'
900896
});
901-
});
902897

903-
describe('when set to false', function() {
904-
it('should set aria-labelledby to null', function() {
905-
this.view = new Typeahead({
906-
input: this.$input,
907-
ariaLabelledBy: false
908-
});
909-
910-
expect(this.$input.attr('aria-labelledby')).toBeUndefined();
911-
});
898+
expect(this.$input.attr('aria-label')).toBe('custom-aria-label');
912899
});
913900

914-
describe('when not set', function() {
915-
beforeEach(function() {
916-
this.$input.attr('id', 'custom-input-id');
917-
});
918-
919-
it('should set aria-labelledby to null if no placeholder specified', function() {
920-
this.view = new Typeahead({
921-
input: this.$input
922-
});
923-
924-
expect(this.$input.attr('aria-labelledby')).toBeUndefined();
901+
it('should not set an aria-label if no value is specified', function() {
902+
this.view = new Typeahead({
903+
input: this.$input
925904
});
926905

927-
it('should set aria-labelledby to the input id if a placeholder is specified', function() {
928-
this.$input.attr('placeholder', 'custom placeholder');
929-
930-
this.view = new Typeahead({
931-
input: this.$input
932-
});
933-
934-
expect(this.$input.attr('aria-labelledby')).toBe('custom-input-id');
935-
});
906+
expect(this.$input.attr('aria-label')).toBeUndefined();
936907
});
937908
});
938909
});

0 commit comments

Comments
 (0)