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: show placeholder item when an empty item is selected #7303

Merged
merged 1 commit into from
Apr 8, 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 @@ -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
Loading