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

fix: do not close select overlay when opened before attached #8402

Merged
merged 2 commits into from
Dec 27, 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
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;
});
});
});