Skip to content

Commit

Permalink
fix: do not close select overlay when opened before attached (#8402)
Browse files Browse the repository at this point in the history
  • Loading branch information
web-padawan authored Dec 27, 2024
1 parent c90066f commit f12e44c
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 15 deletions.
34 changes: 20 additions & 14 deletions packages/select/src/vaadin-select-base-mixin.js
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,6 @@ export const SelectBaseMixin = (superClass) =>
value: false,
notify: true,
reflectToAttribute: true,
observer: '_openedChanged',
},

/**
Expand Down Expand Up @@ -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)',
];
}

constructor() {
Expand All @@ -183,9 +186,8 @@ export const SelectBaseMixin = (superClass) =>
ready() {
super.ready();

this._overlayElement = this.$.overlay;

this._inputContainer = this.shadowRoot.querySelector('[part~="input-field"]');
this._overlayElement = this.$.overlay;

this._valueButtonController = new ButtonController(this);
this.addController(this._valueButtonController);
Expand Down Expand Up @@ -361,20 +363,22 @@ export const SelectBaseMixin = (superClass) =>
}

/** @private */
_openedChanged(opened, wasOpened) {
if (opened) {
// Avoid multiple announcements when a value gets selected from the dropdown
this._updateAriaLive(false);
_openedChanged(opened, overlayElement) {
if (!overlayElement) {
return;
}

if (!this._overlayElement || !this._menuElement || !this.focusElement || this.disabled || this.readonly) {
if (opened) {
if (this.disabled || this.readonly) {
// Disallow programmatic opening when disabled or readonly
this.opened = false;
return;
}

this._overlayElement.style.setProperty(
'--vaadin-select-text-field-width',
`${this._inputContainer.offsetWidth}px`,
);
// Avoid multiple announcements when a value gets selected from the dropdown
this._updateAriaLive(false);

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');
Expand All @@ -384,7 +388,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', '');
}
Expand All @@ -396,6 +400,8 @@ export const SelectBaseMixin = (superClass) =>
this._requestValidation();
}
}

this.__oldOpened = opened;
}

/** @private */
Expand Down
2 changes: 1 addition & 1 deletion packages/select/test/lit-renderer-directives.common.js
Original file line number Diff line number Diff line change
Expand Up @@ -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._overlayElement;
});

it('should set `renderer` property when the directive is attached', () => {
Expand Down
34 changes: 34 additions & 0 deletions packages/select/test/select.common.js
Original file line number Diff line number Diff line change
Expand Up @@ -521,6 +521,14 @@ describe('vaadin-select', () => {
expect(select.opened).to.be.false;
expect(select._overlayElement.opened).to.be.false;
});

it('should disallow programmatic opening when disabled', async () => {
select.readonly = true;
await nextUpdate(select);
select.opened = true;
await nextUpdate(select);
expect(select._overlayElement.opened).to.be.false;
});
});

describe('readonly', () => {
Expand All @@ -533,6 +541,14 @@ describe('vaadin-select', () => {
click(valueButton);
expect(select._overlayElement.opened).to.be.false;
});

it('should disallow programmatic opening when readonly', async () => {
select.readonly = true;
await nextUpdate(select);
select.opened = true;
await nextUpdate(select);
expect(select._overlayElement.opened).to.be.false;
});
});

describe('focus', () => {
Expand Down Expand Up @@ -780,4 +796,22 @@ describe('vaadin-select', () => {
expect(parseFloat(window.getComputedStyle(select).width)).to.eql(500);
});
});

describe('pre-opened', () => {
beforeEach(() => {
select = document.createElement('vaadin-select');
select.items = [{ label: 'Option 1', value: 'value-1' }];
});

afterEach(() => {
select.remove();
});

it('should not close overlay when opened set before adding to DOM', async () => {
select.opened = true;
document.body.appendChild(select);
await nextUpdate(select);
expect(select._overlayElement.opened).to.be.true;
});
});
});

0 comments on commit f12e44c

Please sign in to comment.