Expose fuse options in config

This commit is contained in:
Josh Johnson 2017-01-01 15:59:43 +00:00
parent 4f2a522ba0
commit d0b78438bb
2 changed files with 32 additions and 5 deletions

View file

@ -105,6 +105,11 @@ class Choices {
flippedState: 'is-flipped', flippedState: 'is-flipped',
loadingState: 'is-loading', loadingState: 'is-loading',
}, },
// Choices uses the great Fuse library for searching. You
// can find more options here: https://github.com/krisk/Fuse#options
fuseOptions: {
include: 'score',
},
callbackOnInit: null, callbackOnInit: null,
callbackOnCreateTemplates: null, callbackOnCreateTemplates: null,
}; };
@ -1138,12 +1143,10 @@ class Choices {
const haystack = this.store.getChoicesFilteredBySelectable(); const haystack = this.store.getChoicesFilteredBySelectable();
const needle = newValue; const needle = newValue;
const keys = isType('Array', this.config.sortFields) ? this.config.sortFields : [this.config.sortFields]; const keys = isType('Array', this.config.sortFields) ? this.config.sortFields : [this.config.sortFields];
const fuse = new Fuse(haystack, { const options = Object.assign(this.config.fuseOptions, { keys });
keys, const fuse = new Fuse(haystack, options);
shouldSort: true,
include: 'score',
});
const results = fuse.search(needle); const results = fuse.search(needle);
this.currentValue = newValue; this.currentValue = newValue;
this.highlightPosition = 0; this.highlightPosition = 0;
this.isSearching = true; this.isSearching = true;

View file

@ -2,6 +2,30 @@ import 'whatwg-fetch';
import 'es6-promise'; import 'es6-promise';
import Choices from '../../assets/scripts/src/choices.js'; import Choices from '../../assets/scripts/src/choices.js';
if (typeof Object.assign != 'function') {
Object.assign = function (target, varArgs) { // .length of function is 2
if (target == null) { // TypeError if undefined or null
throw new TypeError('Cannot convert undefined or null to object');
}
var to = Object(target);
for (var index = 1; index < arguments.length; index++) {
var nextSource = arguments[index];
if (nextSource != null) { // Skip over if undefined or null
for (var nextKey in nextSource) {
// Avoid bugs when hasOwnProperty is shadowed
if (Object.prototype.hasOwnProperty.call(nextSource, nextKey)) {
to[nextKey] = nextSource[nextKey];
}
}
}
}
return to;
};
}
describe('Choices', () => { describe('Choices', () => {
afterEach(function() { afterEach(function() {