diff --git a/packages/select/src/vaadin-select-base-mixin.js b/packages/select/src/vaadin-select-base-mixin.js index 28a08ab672..a3c678904e 100644 --- a/packages/select/src/vaadin-select-base-mixin.js +++ b/packages/select/src/vaadin-select-base-mixin.js @@ -64,7 +64,6 @@ export const SelectBaseMixin = (superClass) => value: false, notify: true, reflectToAttribute: true, - observer: '_openedChanged', }, /** @@ -160,7 +159,11 @@ export const SelectBaseMixin = (superClass) => } static get observers() { - return ['_updateAriaExpanded(opened, focusElement)', '_updateSelectedItem(value, _items, placeholder)']; + return [ + '_updateAriaExpanded(opened, focusElement)', + '_updateSelectedItem(value, _items, placeholder)', + '_openedChanged(opened, _overlayElement, _menuElement, focusElement)', + ]; } constructor() { @@ -361,20 +364,16 @@ export const SelectBaseMixin = (superClass) => } /** @private */ - _openedChanged(opened, wasOpened) { + _openedChanged(opened, overlayElement, menuElement, focusElement) { + if (!overlayElement || !menuElement || !focusElement) { + return; + } + if (opened) { // Avoid multiple announcements when a value gets selected from the dropdown this._updateAriaLive(false); - if (!this._overlayElement || !this._menuElement || !this.focusElement || this.disabled || this.readonly) { - this.opened = false; - return; - } - - this._overlayElement.style.setProperty( - '--vaadin-select-text-field-width', - `${this._inputContainer.offsetWidth}px`, - ); + overlayElement.style.setProperty('--vaadin-select-text-field-width', `${this._inputContainer.offsetWidth}px`); // Preserve focus-ring to restore it later const hasFocusRing = this.hasAttribute('focus-ring'); @@ -384,7 +383,7 @@ export const SelectBaseMixin = (superClass) => if (hasFocusRing) { this.removeAttribute('focus-ring'); } - } else if (wasOpened) { + } else if (this.__oldOpened) { if (this._openedWithFocusRing) { this.setAttribute('focus-ring', ''); } @@ -396,6 +395,7 @@ export const SelectBaseMixin = (superClass) => this._requestValidation(); } } + this.__oldOpened = opened; } /** @private */ diff --git a/packages/select/test/lit-renderer-directives.common.js b/packages/select/test/lit-renderer-directives.common.js index 8748f08186..602bfec295 100644 --- a/packages/select/test/lit-renderer-directives.common.js +++ b/packages/select/test/lit-renderer-directives.common.js @@ -24,7 +24,7 @@ describe('lit renderer directives', () => { describe('basic', () => { beforeEach(async () => { select = await renderOpenedSelect(container, { content: 'Content' }); - overlay = select.shadowRoot.querySelector('vaadin-select-overlay'); + overlay = select.$.overlay; }); it('should set `renderer` property when the directive is attached', () => { diff --git a/packages/select/test/select.common.js b/packages/select/test/select.common.js index 7096f15c5f..9ed40367d2 100644 --- a/packages/select/test/select.common.js +++ b/packages/select/test/select.common.js @@ -734,6 +734,21 @@ describe('vaadin-select', () => { }); }); + describe('initially opened', () => { + beforeEach(async () => { + select = fixtureSync(``); + select.items = [ + { label: 'Option 0', value: 'option-0' }, + { label: 'Option 1', value: 'option-1' }, + ]; + await nextRender(); + }); + + it('should open the overlay when opened attribute is set initially', () => { + expect(select.$.overlay.opened).to.be.true; + }); + }); + describe('with value', () => { let menu, valueButton;