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: consider a value matching selected item label a known value (CP) #1018

Merged
merged 3 commits into from
Jan 19, 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
20 changes: 14 additions & 6 deletions src/vaadin-combo-box-mixin.html
Original file line number Diff line number Diff line change
Expand Up @@ -708,23 +708,31 @@
}
} else {
const toLowerCase = (item) => item && item.toLowerCase && item.toLowerCase();
const itemsMatchedByLabel = this.filteredItems
&& this.filteredItems.filter(item => toLowerCase(this._getItemLabel(item)) === toLowerCase(this._inputElementValue))
|| [];

// Try to find an item whose label matches the input value. A matching item is searched from
// the filteredItems array (if available) and the selectedItem (if available).
const itemMatchingByLabel = [...(this.filteredItems || []), this.selectedItem].filter((item) => {
return toLowerCase(this._getItemLabel(item)) === toLowerCase(this._inputElementValue);
})[0];

if (this.allowCustomValue
// to prevent a repetitive input value being saved after pressing ESC and Tab.
&& !itemsMatchedByLabel.length) {
&& !itemMatchingByLabel) {

// An item matching by label was not found, but custom values are allowed.
// Dispatch a custom-value-set event with the input value.
const e = new CustomEvent('custom-value-set', {detail: this._inputElementValue, composed: true, cancelable: true, bubbles: true});
this.dispatchEvent(e);
if (!e.defaultPrevented) {
const customValue = this._inputElementValue;
this._selectItemForValue(customValue);
this.value = customValue;
}
} else if (!this.allowCustomValue && !this.opened && itemsMatchedByLabel.length > 0) {
this.value = this._getItemValue(itemsMatchedByLabel[0]);
} else if (!this.allowCustomValue && !this.opened && itemMatchingByLabel) {
// An item matching by label was found, select it.
this.value = this._getItemValue(itemMatchingByLabel);
} else {
// Revert the input value
this._inputElementValue = this.selectedItem ? this._getItemLabel(this.selectedItem) : (this.value || '');
}
}
Expand Down
30 changes: 30 additions & 0 deletions test/vaadin-combo-box.html
Original file line number Diff line number Diff line change
Expand Up @@ -286,6 +286,36 @@
comboBox.inputElement.blur();
expect(spy).to.not.have.been.called;
});

it('should not fire when the custom value equals the label of the selected item', () => {
const spy = sinon.spy();
comboBox.addEventListener('custom-value-set', spy);
comboBox.selectedItem = {
label: 'foo',
value: 'bar'
};

comboBox.open();
comboBox.inputElement.value = 'foo';
comboBox.close();

expect(spy.called).to.be.false;
});

it('should fire when the custom value equals the value of the selected item', () => {
const spy = sinon.spy();
comboBox.addEventListener('custom-value-set', spy);
comboBox.selectedItem = {
label: 'foo',
value: 'bar'
};

comboBox.open();
comboBox.inputElement.value = 'bar';
comboBox.close();

expect(spy.calledOnce).to.be.true;
});
});
});

Expand Down