Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: update select to use overlay focus restoration logic (#7601) (CP: 24.3) #7605

Merged
merged 1 commit into from
Jul 31, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading