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: set value on dataprovider callback #961

26 changes: 21 additions & 5 deletions src/vaadin-combo-box-data-provider-mixin.html
Original file line number Diff line number Diff line change
Expand Up @@ -107,11 +107,24 @@

/** @private */
_dataProviderFilterChanged() {
if (this.dataProvider && this.opened) {
this.size = undefined;
this._pendingRequests = {};
this.clearCache();
if (!this._shouldFetchData()) {
return;
}

this.size = undefined;
this._pendingRequests = {};
this.clearCache();
}

/** @private */
_shouldFetchData() {

if (!this.dataProvider) {
return false;
}

return this.opened ||
(this.filter && this.filter.length);
}

/** @private */
Expand Down Expand Up @@ -161,6 +174,9 @@
if (this._isValidValue(this.value) && this._getItemValue(this.selectedItem) !== this.value) {
this._selectItemForValue(this.value);
}
if (!this.opened && !this.hasAttribute('focused')) {
this._commitValue();
}
this.size = size;

delete this._pendingRequests[page];
Expand Down Expand Up @@ -197,7 +213,7 @@
filteredItems.push(this.__placeHolder);
}
this.filteredItems = filteredItems;
if (this.opened) {
if (this._shouldFetchData()) {
this._loadPage(0);
} else {
this._forceNextRequest = true;
Expand Down
9 changes: 7 additions & 2 deletions src/vaadin-combo-box-mixin.html
Original file line number Diff line number Diff line change
Expand Up @@ -552,7 +552,7 @@

/** @private */
_closeOrCommit() {
if (this.autoOpenDisabled && !this.opened) {
if (this.autoOpenDisabled && !this.opened && !this.loading) {
this._commitValue();
} else {
this.close();
Expand Down Expand Up @@ -685,9 +685,12 @@
this.value = '';
}
} else {
const itemsMatchedByLabel = this.filteredItems
&& this.filteredItems.filter(item => this._getItemLabel(item) === this._inputElementValue)
|| [];
if (this.allowCustomValue
// to prevent a repetitive input value being saved after pressing ESC and Tab.
&& !(this.filteredItems && this.filteredItems.filter(item => this._getItemLabel(item) === this._inputElementValue).length)) {
&& !itemsMatchedByLabel.length) {

const e = new CustomEvent('custom-value-set', {detail: this._inputElementValue, composed: true, cancelable: true, bubbles: true});
this.dispatchEvent(e);
Expand All @@ -696,6 +699,8 @@
this._selectItemForValue(customValue);
this.value = customValue;
}
} else if (!this.allowCustomValue && !this.opened && itemsMatchedByLabel.length == 1) {
this.value = this._getItemValue(itemsMatchedByLabel[0]);
} else {
this._inputElementValue = this.selectedItem ? this._getItemLabel(this.selectedItem) : (this.value || '');
}
Expand Down
14 changes: 14 additions & 0 deletions test/lazy-loading.html
Original file line number Diff line number Diff line change
Expand Up @@ -852,6 +852,20 @@
const pages = spyDataProvider.getCalls().map(call => call.args[0].page);
expect(pages).to.contain(1);
});

it('should set value if not opened nor focused', () => {
const dataProvider = (params, callback) => {
comboBox.blur();
callback(['item 1'], 1);
};

comboBox.dataProvider = dataProvider;
comboBox._inputElementValue = 'item 1';
comboBox.filter = 'item 1';
expect(comboBox.opened).to.be.false;
expect(comboBox.hasAttribute('focused')).to.be.false;
expect(comboBox.value).to.equal('item 1');
});
});

describe('after empty data set loaded', () => {
Expand Down