Skip to content

Commit

Permalink
refactor: update select to use overlay focus restoration logic (#7601) (
Browse files Browse the repository at this point in the history
  • Loading branch information
web-padawan authored Jul 31, 2024
1 parent 124e60d commit 45db560
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 3 deletions.
7 changes: 7 additions & 0 deletions packages/select/src/vaadin-lit-select-overlay.js
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,13 @@ class SelectOverlay extends PositionMixin(OverlayMixin(ThemableMixin(DirMixin(Po
`;
}

/** @protected */
ready() {
super.ready();

this.restoreFocusOnClose = true;
}

/** @protected */
updated(props) {
super.updated(props);
Expand Down
1 change: 0 additions & 1 deletion packages/select/src/vaadin-select-base-mixin.js
Original file line number Diff line number Diff line change
Expand Up @@ -367,7 +367,6 @@ export const SelectBaseMixin = (superClass) =>
this.removeAttribute('focus-ring');
}
} else if (wasOpened) {
this.focus();
if (this._openedWithFocusRing) {
this.setAttribute('focus-ring', '');
}
Expand Down
2 changes: 2 additions & 0 deletions packages/select/src/vaadin-select-overlay.js
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,8 @@ export class SelectOverlay extends PositionMixin(OverlayMixin(DirMixin(ThemableM
ready() {
super.ready();

this.restoreFocusOnClose = true;

// When setting `opened` as an attribute, the overlay is already teleported to body
// by the time when `ready()` callback of the `vaadin-select` is executed by Polymer,
// so querySelector() would return null. So we use this workaround to set properties.
Expand Down
19 changes: 17 additions & 2 deletions packages/select/test/keyboard.common.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { expect } from '@esm-bundle/chai';
import { fixtureSync, nextRender, nextUpdate } from '@vaadin/testing-helpers';
import { aTimeout, fixtureSync, nextRender, nextUpdate, outsideClick } from '@vaadin/testing-helpers';
import { sendKeys } from '@web/test-runner-commands';
import sinon from 'sinon';

Expand Down Expand Up @@ -67,7 +67,7 @@ describe('keyboard', () => {
});
});

it('should focus value button element on overlay closing', async () => {
it('should focus value button element on overlay closing with Esc', async () => {
await sendKeys({ press: 'Tab' });

await sendKeys({ press: 'Enter' });
Expand All @@ -81,6 +81,21 @@ describe('keyboard', () => {
expect(focusedSpy.calledOnce).to.be.true;
});

it('should focus value button element on overlay closing with outside click', async () => {
await sendKeys({ press: 'Tab' });

await sendKeys({ press: 'Enter' });
await nextRender();

const focusedSpy = sinon.spy(valueButton, 'focus');

outsideClick();
await nextUpdate(select);
await aTimeout(0);

expect(focusedSpy.calledOnce).to.be.true;
});

it('should restore focus-ring attribute on overlay closing', async () => {
await sendKeys({ press: 'Tab' });

Expand Down

0 comments on commit 45db560

Please sign in to comment.