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
…8404)

Co-authored-by: Serhii Kulykov <[email protected]>
  • Loading branch information
vaadin-bot and web-padawan authored Dec 27, 2024
1 parent 679a5fa commit 9e0098b
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 13 deletions.
31 changes: 19 additions & 12 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 @@ -159,7 +158,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, _inputContainer)',
];
}

constructor() {
Expand Down Expand Up @@ -358,20 +361,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, inputContainer) {
if (!overlayElement || !inputContainer) {
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', `${inputContainer.offsetWidth}px`);

// Preserve focus-ring to restore it later
const hasFocusRing = this.hasAttribute('focus-ring');
Expand All @@ -381,7 +386,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 @@ -393,6 +398,8 @@ export const SelectBaseMixin = (superClass) =>
this.validate();
}
}

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 @@ -519,6 +519,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 nextRender(select);
expect(select._overlayElement.opened).to.be.false;
});
});

describe('readonly', () => {
Expand All @@ -531,6 +539,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 nextRender(select);
expect(select._overlayElement.opened).to.be.false;
});
});

describe('focus', () => {
Expand Down Expand Up @@ -778,4 +794,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 9e0098b

Please sign in to comment.