Skip to content

Commit

Permalink
fix: make dataprovider filtering case-insensitive (#397)
Browse files Browse the repository at this point in the history
  • Loading branch information
tulioag committed May 17, 2021
1 parent 68ef8c5 commit 1eb70d0
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 9 deletions.
7 changes: 5 additions & 2 deletions packages/vaadin-combo-box/src/vaadin-combo-box-mixin.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 &&
Expand All @@ -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 || '';
Expand Down
50 changes: 44 additions & 6 deletions packages/vaadin-combo-box/test/lazy-loading.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand All @@ -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');
Expand All @@ -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');
Expand Down
2 changes: 1 addition & 1 deletion web-test-runner.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ module.exports = {
threshold: {
statements: 80,
branches: 50,
functions: 77,
functions: 70,
lines: 80
}
},
Expand Down

0 comments on commit 1eb70d0

Please sign in to comment.