Skip to content

Commit

Permalink
ARIA activedescendant, label
Browse files Browse the repository at this point in the history
  • Loading branch information
Lewiscowles1986 committed Jan 23, 2021
1 parent de63b80 commit 3692cd7
Show file tree
Hide file tree
Showing 5 changed files with 32 additions and 2 deletions.
1 change: 1 addition & 0 deletions lib/clear-suggestions.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,6 @@ function clearSuggestions(state) {
state.selectedIndex = -1;
removeListElement(state);
state.input.removeAttribute("aria-controls");
state.input.removeAttribute("aria-activedescendant");
state.plete.setAttribute("aria-expanded", "false");
}
1 change: 1 addition & 0 deletions lib/highlight.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ function highlight(state, index) {
const item = state.list.querySelectorAll("plete-item")[index];
if (item) {
item.classList.add("highlight");
state.input.setAttribute("aria-activedescendant", item.id);
}
}
}
1 change: 1 addition & 0 deletions lib/render-item.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ function renderItem(state, value) {
const attrValue = valueIsString ? value : value.id;
const label = valueIsString ? value : value.label;

item.setAttribute("aria-label", `Choice: ${label}.`);
item.setAttribute("value", attrValue);
item.setAttribute("title", label);

Expand Down
3 changes: 2 additions & 1 deletion lib/render.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@ function render(state, data) {
addListElement(state, select);
const boundRenderItem = renderItem.bind(null, state);

data.map(boundRenderItem).forEach(function(item) {
data.map(boundRenderItem).forEach(function(item, idx) {
item.id = `plete-item-${idx}`;
state.list.appendChild(item);
});

Expand Down
28 changes: 27 additions & 1 deletion test/aria-support.test.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { assert } from "@sinonjs/referee-sinon";
import { assert, refute } from "@sinonjs/referee-sinon";
import { fireEvent, waitForElement } from "@testing-library/dom";
import { setupTest } from "./test-helper";

Expand Down Expand Up @@ -47,6 +47,32 @@ describe("Plete", function() {
});
});

it("renders the list items with an `aria-label` attribute", function() {
const listItems = this.list.querySelectorAll("plete-item");

assert.isTrue(listItems.length > 0);
listItems.forEach(function(item) {
refute.isNull(item.getAttribute("aria-label"));
});
});

it("adds an `aria-activedescendant` attribute to the input", function() {
const listItems = this.list.querySelectorAll("plete-item");

assert.isTrue(listItems.length > 0);
refute.isNull(this.input);
refute.isNull(this.input.getAttribute("aria-activedescendant"));
});

it("renders the list items with an `id` attribute", function() {
const listItems = this.list.querySelectorAll("plete-item");

assert.isTrue(listItems.length > 0);
listItems.forEach(function(item) {
refute.isNull(item.getAttribute("id"));
});
});

it("sets `aria-expanded` on the `plete` element to 'true'", function() {
const plete = this.input.closest("plete");

Expand Down

0 comments on commit 3692cd7

Please sign in to comment.