From 1eb70d0d9ffecbb95f0fed4a875e60ceec813a33 Mon Sep 17 00:00:00 2001 From: Tulio Garcia <28783969+tulioag@users.noreply.github.com> Date: Mon, 17 May 2021 10:56:53 +0300 Subject: [PATCH] fix: make dataprovider filtering case-insensitive (#397) --- .../src/vaadin-combo-box-mixin.js | 7 ++- .../test/lazy-loading.test.js | 50 ++++++++++++++++--- web-test-runner.config.js | 2 +- 3 files changed, 50 insertions(+), 9 deletions(-) diff --git a/packages/vaadin-combo-box/src/vaadin-combo-box-mixin.js b/packages/vaadin-combo-box/src/vaadin-combo-box-mixin.js index 6ce7ff5d3ae..1f957fc5994 100644 --- a/packages/vaadin-combo-box/src/vaadin-combo-box-mixin.js +++ b/packages/vaadin-combo-box/src/vaadin-combo-box-mixin.js @@ -635,9 +635,12 @@ export const ComboBoxMixin = (subclass) => this.value = ''; } } else { + const toLowerCase = (item) => item && item.toLowerCase && item.toLowerCase(); const itemsMatchedByLabel = (this.filteredItems && - this.filteredItems.filter((item) => this._getItemLabel(item) === this._inputElementValue)) || + this.filteredItems.filter( + (item) => toLowerCase(this._getItemLabel(item)) === toLowerCase(this._inputElementValue) + )) || []; if ( this.allowCustomValue && @@ -656,7 +659,7 @@ export const ComboBoxMixin = (subclass) => this._selectItemForValue(customValue); this.value = customValue; } - } else if (!this.allowCustomValue && !this.opened && itemsMatchedByLabel.length == 1) { + } else if (!this.allowCustomValue && !this.opened && itemsMatchedByLabel.length > 0) { this.value = this._getItemValue(itemsMatchedByLabel[0]); } else { this._inputElementValue = this.selectedItem ? this._getItemLabel(this.selectedItem) : this.value || ''; diff --git a/packages/vaadin-combo-box/test/lazy-loading.test.js b/packages/vaadin-combo-box/test/lazy-loading.test.js index 84cfeaa8d7a..e8c533cd4e7 100644 --- a/packages/vaadin-combo-box/test/lazy-loading.test.js +++ b/packages/vaadin-combo-box/test/lazy-loading.test.js @@ -890,8 +890,9 @@ describe('lazy loading', () => { comboBox.autoOpenDisabled = false; expect(comboBox.autoOpenDisabled).to.be.false; - comboBox._inputElementValue = 'item 12'; - comboBox.filter = 'item 12'; + const filterValue = 'item 12'; + comboBox._inputElementValue = filterValue; + comboBox.filter = filterValue; expect(comboBox.opened).to.be.false; expect(comboBox.hasAttribute('focused')).to.be.false; expect(comboBox.value).to.equal('item 12'); @@ -901,8 +902,44 @@ describe('lazy loading', () => { comboBox.autoOpenDisabled = true; expect(comboBox.autoOpenDisabled).to.be.true; - comboBox._inputElementValue = 'item 12'; - comboBox.filter = 'item 12'; + const filterValue = 'item 12'; + comboBox._inputElementValue = filterValue; + comboBox.filter = filterValue; + expect(comboBox.opened).to.be.false; + expect(comboBox.hasAttribute('focused')).to.be.false; + expect(comboBox.value).to.equal('item 12'); + }); + + it('should set value without auto-open-disabled even if case does not match', () => { + comboBox.autoOpenDisabled = false; + expect(comboBox.autoOpenDisabled).to.be.false; + + const filterValue = 'ItEm 12'; + comboBox._inputElementValue = filterValue; + comboBox.filter = filterValue; + expect(comboBox.opened).to.be.false; + expect(comboBox.hasAttribute('focused')).to.be.false; + expect(comboBox.value).to.equal('item 12'); + }); + + it('should set value with auto-open-disabled even if case does not match', () => { + comboBox.autoOpenDisabled = true; + expect(comboBox.autoOpenDisabled).to.be.true; + + const filterValue = 'iTem 12'; + comboBox._inputElementValue = filterValue; + comboBox.filter = filterValue; + expect(comboBox.opened).to.be.false; + expect(comboBox.hasAttribute('focused')).to.be.false; + expect(comboBox.value).to.equal('item 12'); + }); + + it('should set first value of multiple matches that differ only in case', () => { + returnedItems = ['item 12', 'IteM 12']; + + const filterValue = 'IteM 12'; + comboBox._inputElementValue = filterValue; + comboBox.filter = filterValue; expect(comboBox.opened).to.be.false; expect(comboBox.hasAttribute('focused')).to.be.false; expect(comboBox.value).to.equal('item 12'); @@ -922,8 +959,9 @@ describe('lazy loading', () => { expect(comboBox.value).to.equal('other value'); returnedItems = ['item 12']; - comboBox._inputElementValue = 'item 1'; - comboBox.filter = 'item 1'; + const filterValue = 'item 1'; + comboBox._inputElementValue = filterValue; + comboBox.filter = filterValue; expect(comboBox.opened).to.be.false; expect(comboBox.hasAttribute('focused')).to.be.false; expect(comboBox.value).to.equal('other value'); diff --git a/web-test-runner.config.js b/web-test-runner.config.js index e165e00c669..2423b6db030 100644 --- a/web-test-runner.config.js +++ b/web-test-runner.config.js @@ -15,7 +15,7 @@ module.exports = { threshold: { statements: 80, branches: 50, - functions: 77, + functions: 70, lines: 80 } },