Skip to content

Commit

Permalink
fix: show placeholder item when an empty item is selected (#7303) (#7304
Browse files Browse the repository at this point in the history
)

Co-authored-by: Serhii Kulykov <iamkulykov@gmail.com>
  • Loading branch information
vaadin-bot and web-padawan committed Apr 8, 2024
1 parent 1f0a72e commit 045e12a
Show file tree
Hide file tree
Showing 2 changed files with 40 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 @@ -512,23 +512,21 @@ export const SelectBaseMixin = (superClass) =>

valueButton.removeAttribute('placeholder');

if (!selected) {
if (this.placeholder) {
const item = this.__createItemElement({ label: this.placeholder });
this.__appendValueItemElement(item, valueButton);
valueButton.setAttribute('placeholder', '');
}
} else {
if (this._hasContent(selected)) {
this.__attachSelectedItem(selected);
} else if (this.placeholder) {
const item = this.__createItemElement({ label: this.placeholder });
this.__appendValueItemElement(item, valueButton);
valueButton.setAttribute('placeholder', '');
}

if (!this._valueChanging) {
this._selectedChanging = true;
this.value = selected.value || '';
if (this.__dispatchChangePending) {
this.__dispatchChange();
}
delete this._selectedChanging;
if (!this._valueChanging && selected) {
this._selectedChanging = true;
this.value = selected.value || '';
if (this.__dispatchChangePending) {
this.__dispatchChange();
}
delete this._selectedChanging;
}

const labelledIdReferenceConfig =
Expand All @@ -540,6 +538,14 @@ export const SelectBaseMixin = (superClass) =>
}
}

/** @private */
_hasContent(item) {
if (!item) {
return false;
}
return Boolean(item.hasAttribute('label') ? item.getAttribute('label') : item.textContent.trim());
}

/** @private */
_updateSelectedItem(value, items) {
if (items) {
Expand Down
21 changes: 20 additions & 1 deletion packages/select/test/select.common.js
Original file line number Diff line number Diff line change
Expand Up @@ -447,9 +447,28 @@ describe('vaadin-select', () => {
});

describe('placeholder', () => {
beforeEach(() => {
select.placeholder = 'Select an item';
});

it('should set placeholder as a value node text content', async () => {
select.value = null;
select.placeholder = 'Select an item';
await nextUpdate(select);
expect(valueButton.textContent).to.equal('Select an item');
});

it('should show placeholder when selecting an item with empty label', async () => {
select.opened = true;
await nextRender();
click(select._items[4]);
await nextUpdate(select);
expect(valueButton.textContent).to.equal('Select an item');
});

it('should show placeholder when selecting an item with empty text', async () => {
select.opened = true;
await nextRender();
click(select._items[3]);
await nextUpdate(select);
expect(valueButton.textContent).to.equal('Select an item');
});
Expand Down

0 comments on commit 045e12a

Please sign in to comment.