Skip to content

Commit

Permalink
fix: focus first non-disabled item if all items have tabindex -1 (#103)
Browse files Browse the repository at this point in the history
  • Loading branch information
web-padawan committed Mar 18, 2024
1 parent 7c5e16b commit eb87a96
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 2 deletions.
17 changes: 17 additions & 0 deletions test/vaadin-list-mixin.html
Original file line number Diff line number Diff line change
Expand Up @@ -298,6 +298,23 @@
list.focus();
expect(spy.calledOnce).to.be.true;
});

it('should call focus() on the first non-disabled item if all items have tabindex -1', () => {
list.items.forEach((item) => {
item.tabIndex = -1;
});
const spy = sinon.spy(list.items[2], 'focus');
list.focus();
expect(spy.calledOnce).to.be.true;
});

it('should change tabindex from -1 to 0 on first non-disabled item when focusing it', () => {
list.items.forEach((item) => {
item.tabIndex = -1;
});
list.focus();
[-1, -1, 0, -1].forEach((val, idx) => expect(list.items[idx].tabIndex).to.equal(val));
});
});

describe('tabIndex when all the items are disabled', () => {
Expand Down
11 changes: 9 additions & 2 deletions vaadin-list-mixin.html
Original file line number Diff line number Diff line change
Expand Up @@ -288,8 +288,15 @@
focus() {
// In initialisation (e.g vaadin-select) observer might not been run yet.
this._observer && this._observer.flush();
const firstItem = this.querySelector('[tabindex="0"]') || (this.items ? this.items[0] : null);
firstItem && firstItem.focus();
const firstItem = this.querySelector('[tabindex="0"]');
if (firstItem) {
firstItem.focus();
} else if (this.items) {
const idx = this._getAvailableIndex(0, null, item => !item.disabled);
if (idx >= 0) {
this._focus(idx);
}
}
}

/**
Expand Down

0 comments on commit eb87a96

Please sign in to comment.