Skip to content

Commit

Permalink
Fix incorrect style scope on selected item for initial value
Browse files Browse the repository at this point in the history
  • Loading branch information
web-padawan committed Feb 9, 2018
1 parent a8f399d commit d9fc92f
Show file tree
Hide file tree
Showing 2 changed files with 66 additions and 15 deletions.
53 changes: 38 additions & 15 deletions src/vaadin-dropdown-menu.html
Original file line number Diff line number Diff line change
Expand Up @@ -479,6 +479,35 @@
}
}

_hasContent(selected) {
if (!selected) {
return false;
}
return Boolean(
selected.hasAttribute('label') ?
selected.getAttribute('label') :
selected.textContent.trim() || selected.children.length
);
}

_attachSelectedItem(selected) {
if (!selected) {
return;
}
let labelItem;
if (selected.hasAttribute('label')) {
labelItem = document.createElement('vaadin-item');
labelItem.textContent = selected.getAttribute('label');
labelItem.selected = true;
} else {
labelItem = selected.cloneNode(true);
}

labelItem.removeAttribute('tabindex');

this._valueElement.appendChild(labelItem);
}

_updateAriaExpanded(opened, toggleElement) {
toggleElement && toggleElement.setAttribute('aria-expanded', opened);
}
Expand All @@ -488,25 +517,19 @@
this._valueElement.innerHTML = '';

const selected = this._items[this._menuElement.selected];
let hasContent;
if (selected) {
let labelItem;
if (selected.hasAttribute('label')) {
hasContent = selected.getAttribute('label');
labelItem = document.createElement('vaadin-item');
labelItem.textContent = hasContent;
labelItem.selected = true;
} else {
hasContent = selected.textContent.trim() || selected.children.length;
labelItem = selected.cloneNode(true);
}
labelItem.removeAttribute('tabindex');
this._valueElement.appendChild(labelItem);
}

const hasContent = this._hasContent(selected);

// Toggle visibility of _valueElement vs fallback input with placeholder
this._valueElement.slot = hasContent ? 'value' : '';

// Ensure the slot distribution to apply correct style scope for cloned item
if (hasContent && window.ShadyDOM) {
window.ShadyDOM.flush();
}

this._attachSelectedItem(selected);

if (!this._valueChanging) {
this._selectedChanging = true;
this.value = selected && selected.value || '';
Expand Down
28 changes: 28 additions & 0 deletions test/dropdown-menu-test.html
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,19 @@
</template>
</test-fixture>

<test-fixture id="style-scope">
<template>
<vaadin-dropdown-menu value="v1">
<template>
<vaadin-list-box>
<mock-item value="v1">t1</mock-item>
<mock-item value="v2">t2</mock-item>
</vaadin-list-box>
</template>
</vaadin-dropdown-menu>
</template>
</test-fixture>

<test-fixture id="dropdown-in-flexbox">
<template>
<div style="display: flex; flex-direction: column; width:500px;">
Expand Down Expand Up @@ -131,6 +144,21 @@
});
});

describe('vaadin-dropdown-menu style scope', () => {
let dropdown, menu;

beforeEach(done => {
dropdown = fixture('style-scope');
menu = dropdown._menuElement;
Polymer.RenderStatus.afterNextRender(menu, () => setTimeout(done));
});

it('should not apply overlay style scope to the clone of selected item', () => {
const valueElement = dropdown._valueElement.firstChild;
expect(valueElement.classList.contains('vaadin-dropdown-menu-overlay')).to.be.false;
});
});

describe('vaadin-dropdown-menu', () => {
let dropdown, input;

Expand Down

0 comments on commit d9fc92f

Please sign in to comment.