Skip to content

Commit

Permalink
feat: changes no value chips to italic (#58)
Browse files Browse the repository at this point in the history
* feat: changes no value chips to italic

* refs #50
  • Loading branch information
JockeCK authored Nov 12, 2019
1 parent 24c34b1 commit 4296591
Show file tree
Hide file tree
Showing 2 changed files with 67 additions and 16 deletions.
47 changes: 31 additions & 16 deletions paper-autocomplete-chips.js
Original file line number Diff line number Diff line change
Expand Up @@ -229,6 +229,29 @@ class PaperAutocompleteChips extends translatable(PolymerElement) {
value: objText
};
},
sortResult = results => {
return results.sort((a, b) => {
if (a.text === this.noValueLabel) {
return -1;
}
if (b.text === this.noValueLabel) {
return 1;
}
if (a.idx < b.idx) {
return -1;
}
if (a.idx > b.idx) {
return 1;
}
if (a.text < b.text) {
return -1;
}
if (a.text > b.text) {
return 1;
}
return 0;
});
},
hasOtherObjectValue = value => {
const prop = this.get(this.valueProperty, value);
if (prop == null) {
Expand All @@ -239,6 +262,7 @@ class PaperAutocompleteChips extends translatable(PolymerElement) {
);
};


return (datasource, query) => { // eslint-disable-line max-statements
const results = [];

Expand All @@ -259,34 +283,25 @@ class PaperAutocompleteChips extends translatable(PolymerElement) {
if (this.valueProperty && hasOtherObjectValue(result.value)) {
continue;
}

result.idx = result.text.toLowerCase().indexOf(query);
if (result.idx === -1) {
continue;
}

const escapedQuery = query.replace(/[|\\{}()[\]^$+*?.-]/gu, '\\$&');
result.html = result.text.replace(new RegExp('(' + escapedQuery + ')', 'igu'), regexpResult);
if (result.text === this.noValueLabel) {
result.html = '<i>' + result.text.replace(new RegExp('(' + escapedQuery + ')', 'igu'), regexpResult) + '</i>';
} else {
result.html = result.text.replace(new RegExp('(' + escapedQuery + ')', 'igu'), regexpResult);
}
results.push(result);

if (results.length >= maxResults) {
break;
}
}
return results.sort((a, b) => {
if (a.idx < b.idx) {
return -1;
}
if (a.idx > b.idx) {
return 1;
}
if (a.text < b.text) {
return -1;
}
if (a.text > b.text) {
return 1;
}
return 0;
});
return sortResult(results);
};
}
},
Expand Down
36 changes: 36 additions & 0 deletions test/basic.html
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,18 @@
done();
});

test('Selecting null source', done => {
chips.source = ['A', '', 'C'];

select('');
assert.deepEqual(chips.selectedItems, ['']);

select('C');
assert.deepEqual(chips.selectedItems, ['', 'C']);

done();
});

test('Selecting object source', done => {
chips.source = [
{value: 1, label: 'One'},
Expand Down Expand Up @@ -112,6 +124,30 @@
done();
});

test('Search sorts suggestions and has correctly formatted no value chip', done => {
chips.textProperty = 'label';
autocomplete.text = 'e';

window.setTimeout(() => {
assert.equal(suggestions._suggestions.length, 5);
assert.equal(suggestions._suggestions[0].value.value, '');
assert.equal(suggestions._suggestions[4].value.label, 'zzzace');
assert.equal(suggestions._suggestions[0].html, '<i>No valu<b>e</b></i>');
done();
}, 200);

suggestions._input.dispatchEvent(new CustomEvent('focus', { bubbles: true, composed: true }));

chips.source = [
{value: 'z', label: 'zab'},
{value: 'x', label: 'zzzace'},
{value: '', label: 'No value'},
{value: 'd', label: 'eaaaf'},
{value: 'e', label: 'bbbegh'},
{value: 'k', label: 'ceccji'}
];
});

test('Source changed updates suggestions list', done => {
chips.textProperty = 'label';
autocomplete.text = 'a';
Expand Down

0 comments on commit 4296591

Please sign in to comment.