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: handle list-box set before renderer #3412

Merged
merged 1 commit into from
Feb 9, 2022
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
16 changes: 14 additions & 2 deletions packages/select/src/vaadin-select.js
Original file line number Diff line number Diff line change
Expand Up @@ -432,10 +432,14 @@ class Select extends DelegateFocusMixin(FieldMixin(SlotMixin(ElementMixin(Themab
_assignMenuElement(menuElement) {
if (menuElement && menuElement !== this.__lastMenuElement) {
this._menuElement = menuElement;

// Ensure items are initialized
this.__initMenuItems(menuElement);

menuElement.addEventListener('items-changed', () => {
this._items = menuElement.items;
this._items.forEach((item) => item.setAttribute('role', 'option'));
this.__initMenuItems(menuElement);
});

menuElement.addEventListener('selected-changed', () => this.__updateValueButton());
// Use capture phase to make it possible for `<vaadin-grid-pro-edit-select>`
// to override and handle the keydown event before the value change happens.
Expand All @@ -456,6 +460,14 @@ class Select extends DelegateFocusMixin(FieldMixin(SlotMixin(ElementMixin(Themab
}
}

/** @private */
__initMenuItems(menuElement) {
if (menuElement.items) {
this._items = menuElement.items;
this._items.forEach((item) => item.setAttribute('role', 'option'));
}
}

/** @private */
_valueChanged(value, oldValue) {
this.toggleAttribute('has-value', Boolean(value));
Expand Down
22 changes: 22 additions & 0 deletions packages/select/test/renderer.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -133,4 +133,26 @@ describe('renderer', () => {
document.body.removeChild(select);
}).to.not.throw(Error);
});

describe('child list-box', () => {
beforeEach(async () => {
// Mimic the Flow component behavior
select.appendChild(rendererContent);
// Wait for list-box items to be set
await nextFrame();
});

it('should work with list-box connected before renderer is set', () => {
select.renderer = (root) => {
const listBox = Array.from(select.children).find((el) => el.tagName.toLowerCase() === 'vaadin-list-box');
if (listBox) {
if (root.firstChild) {
root.removeChild(root.firstChild);
}
root.appendChild(listBox);
}
};
expect(select._items).to.eql(rendererContent.items);
});
});
});