Skip to content

Commit

Permalink
fix: set aria-selected on selected item, not the focused one (#5402) (#…
Browse files Browse the repository at this point in the history
…5408)

Co-authored-by: Serhii Kulykov <iamkulykov@gmail.com>
  • Loading branch information
vaadin-bot and web-padawan committed Jan 26, 2023
1 parent 855bd71 commit 2065a59
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 23 deletions.
14 changes: 5 additions & 9 deletions packages/combo-box/src/vaadin-combo-box-scroller.js
Original file line number Diff line number Diff line change
Expand Up @@ -216,18 +216,13 @@ export class ComboBoxScroller extends PolymerElement {
return itemIndex !== undefined ? 'option' : false;
}

/** @private */
__getAriaSelected(focusedIndex, itemIndex) {
return this.__isItemFocused(focusedIndex, itemIndex).toString();
}

/** @private */
__isItemFocused(focusedIndex, itemIndex) {
return !this.loading && focusedIndex === itemIndex;
}

/** @private */
__isItemSelected(item, selectedItem, itemIdPath) {
/** @protected */
_isItemSelected(item, selectedItem, itemIdPath) {
if (item instanceof ComboBoxPlaceholder) {
return false;
} else if (itemIdPath && item !== undefined && selectedItem !== undefined) {
Expand Down Expand Up @@ -291,19 +286,20 @@ export class ComboBoxScroller extends PolymerElement {
__updateElement(el, index) {
const item = this.items[index];
const focusedIndex = this.focusedIndex;
const selected = this._isItemSelected(item, this.selectedItem, this.itemIdPath);

el.setProperties({
item,
index,
label: this.getItemLabel(item),
selected: this.__isItemSelected(item, this.selectedItem, this.itemIdPath),
selected,
renderer: this.renderer,
focused: this.__isItemFocused(focusedIndex, index),
});

el.id = `${this.__hostTagName}-item-${index}`;
el.setAttribute('role', this.__getAriaRole(index));
el.setAttribute('aria-selected', this.__getAriaSelected(focusedIndex, index));
el.setAttribute('aria-selected', selected.toString());
el.setAttribute('aria-posinset', index + 1);
el.setAttribute('aria-setsize', this.items.length);

Expand Down
7 changes: 4 additions & 3 deletions packages/combo-box/test/aria.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,11 +43,12 @@ describe('ARIA', () => {
expect(input.getAttribute('aria-activedescendant')).to.equal(items[1].id);
});

it('should set aria-selected on item elements depending on the focused item', () => {
arrowDownKeyDown(input); // Move focus to the 1st item.
it('should set aria-selected on item elements depending on the selected item', () => {
comboBox.value = 'foo';
expect(items[0].getAttribute('aria-selected')).to.equal('true');
expect(items[1].getAttribute('aria-selected')).to.equal('false');
arrowDownKeyDown(input); // Move focus to the 2nd item.

comboBox.value = 'bar';
expect(items[0].getAttribute('aria-selected')).to.equal('false');
expect(items[1].getAttribute('aria-selected')).to.equal('true');
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,14 +24,11 @@ class MultiSelectComboBoxScroller extends ComboBoxScroller {
this.setAttribute('aria-multiselectable', 'true');
}

/** @private */
__getAriaSelected(_focusedIndex, itemIndex) {
const item = this.items[itemIndex];
return this.__isItemSelected(item, null, this.itemIdPath).toString();
}

/** @private */
__isItemSelected(item, _selectedItem, itemIdPath) {
/**
* @protected
* @override
*/
_isItemSelected(item, _selectedItem, itemIdPath) {
if (item instanceof ComboBoxPlaceholder) {
return false;
}
Expand Down
7 changes: 4 additions & 3 deletions packages/time-picker/test/aria.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,11 +41,12 @@ describe('ARIA', () => {
expect(input.getAttribute('aria-activedescendant')).to.equal(items[1].id);
});

it('should set aria-selected on item elements depending on the focused item', () => {
arrowDownKeyDown(input); // Move focus to the 1st item.
it('should set aria-selected on item elements depending on the selected item', () => {
timePicker.value = '00:00';
expect(items[0].getAttribute('aria-selected')).to.equal('true');
expect(items[1].getAttribute('aria-selected')).to.equal('false');
arrowDownKeyDown(input); // Move focus to the 2nd item.

timePicker.value = '01:00';
expect(items[0].getAttribute('aria-selected')).to.equal('false');
expect(items[1].getAttribute('aria-selected')).to.equal('true');
});
Expand Down

0 comments on commit 2065a59

Please sign in to comment.