FIX: makes select-kit match searches with accents (filter and content)

This commit is contained in:
Joffrey JAFFEUX 2018-06-26 12:19:14 +02:00 committed by GitHub
parent 7a77a3c1dd
commit eb9b99e519
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 65 additions and 21 deletions

View File

@ -31,9 +31,8 @@ export default ComboBoxComponent.extend({
} }
const _matchFunction = (f, text) => { const _matchFunction = (f, text) => {
return text.toLowerCase().indexOf(f) > -1; return this._normalize(text).indexOf(f) > -1;
}; };
const lowerFilter = filter.toLowerCase();
return computedContent.filter(c => { return computedContent.filter(c => {
const category = Category.findById(get(c, "value")); const category = Category.findById(get(c, "value"));
@ -41,11 +40,10 @@ export default ComboBoxComponent.extend({
if (category && category.get("parentCategory")) { if (category && category.get("parentCategory")) {
const categoryName = category.get("parentCategory.name"); const categoryName = category.get("parentCategory.name");
return ( return (
_matchFunction(lowerFilter, text) || _matchFunction(filter, text) || _matchFunction(filter, categoryName)
_matchFunction(lowerFilter, categoryName)
); );
} else { } else {
return _matchFunction(lowerFilter, text); return _matchFunction(filter, text);
} }
}); });
}, },

View File

@ -36,9 +36,9 @@ export default MultiSelectComponent.extend({
}, },
filterComputedContent(computedContent, computedValues, filter) { filterComputedContent(computedContent, computedValues, filter) {
const regex = new RegExp(filter.toLowerCase(), "i"); const regex = new RegExp(filter, "i");
return computedContent.filter(category => return computedContent.filter(category =>
Ember.get(category, "name").match(regex) this._normalize(Ember.get(category, "name")).match(regex)
); );
}, },

View File

@ -114,13 +114,8 @@ export default SelectKitComponent.extend({
mutateContent() {}, mutateContent() {},
filterComputedContent(computedContent, computedValues, filter) { filterComputedContent(computedContent, computedValues, filter) {
const lowerFilter = filter.toLowerCase();
return computedContent.filter(c => { return computedContent.filter(c => {
return ( return this._normalize(get(c, "name")).indexOf(filter) > -1;
get(c, "name")
.toLowerCase()
.indexOf(lowerFilter) > -1
);
}); });
}, },
@ -147,7 +142,7 @@ export default SelectKitComponent.extend({
computedContent = this.filterComputedContent( computedContent = this.filterComputedContent(
computedContent, computedContent,
computedValues, computedValues,
filter this._normalize(filter)
); );
} }

View File

@ -86,13 +86,8 @@ export default SelectKitComponent.extend({
}, },
filterComputedContent(computedContent, computedValue, filter) { filterComputedContent(computedContent, computedValue, filter) {
const lowerFilter = filter.toLowerCase();
return computedContent.filter(c => { return computedContent.filter(c => {
return ( return this._normalize(get(c, "name")).indexOf(filter) > -1;
get(c, "name")
.toLowerCase()
.indexOf(lowerFilter) > -1
);
}); });
}, },
@ -136,7 +131,7 @@ export default SelectKitComponent.extend({
computedContent = this.filterComputedContent( computedContent = this.filterComputedContent(
computedContent, computedContent,
computedValue, computedValue,
filter this._normalize(filter)
); );
} }

View File

@ -27,6 +27,16 @@ export default Ember.Mixin.create({
return !isNaN(parseFloat(input)) && isFinite(input); return !isNaN(parseFloat(input)) && isFinite(input);
}, },
_normalize(input) {
input = input.toLowerCase();
if (typeof input.normalize === "function") {
input = input.normalize("NFD").replace(/[\u0300-\u036f]/g, "");
}
return input;
},
_cast(value) { _cast(value) {
if (value === this.noneValue) return value; if (value === this.noneValue) return value;
return this._castInteger(this._castBoolean(value)); return this._castInteger(this._castBoolean(value));

View File

@ -779,3 +779,49 @@ componentTest("with minimumLabel", {
}); });
} }
}); });
componentTest("with accents in filter", {
template: "{{single-select content=content filterable=true}}",
beforeEach() {
this.set("content", ["sam", "jeff", "neil"]);
},
test(assert) {
this.get("subject").expand();
this.get("subject").fillInFilter("jéff");
andThen(() => {
assert.equal(this.get("subject").rows().length, 1);
assert.equal(
this.get("subject")
.rowByIndex(0)
.name(),
"jeff"
);
});
}
});
componentTest("with accents in content", {
template: "{{single-select content=content filterable=true}}",
beforeEach() {
this.set("content", ["sam", "jéff", "neil"]);
},
test(assert) {
this.get("subject").expand();
this.get("subject").fillInFilter("jeff");
andThen(() => {
assert.equal(this.get("subject").rows().length, 1);
assert.equal(
this.get("subject")
.rowByIndex(0)
.name(),
"jéff"
);
});
}
});