Optional sorting

This commit is contained in:
BroiSatse 2016-08-31 14:08:19 +01:00
parent 3cd5bf8843
commit 593d722843
2 changed files with 7 additions and 3 deletions

View file

@ -56,6 +56,7 @@ export default class Choices {
search: true, search: true,
flip: true, flip: true,
regexFilter: null, regexFilter: null,
shouldSort: true,
sortFilter: sortByAlpha, sortFilter: sortByAlpha,
sortFields: ['label', 'value'], sortFields: ['label', 'value'],
placeholder: true, placeholder: true,
@ -886,7 +887,6 @@ export default class Choices {
include: 'score', 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;
@ -1880,6 +1880,7 @@ export default class Choices {
const passedOptions = Array.from(this.passedElement.options); const passedOptions = Array.from(this.passedElement.options);
const allChoices = []; const allChoices = [];
// Create array of options from option elements // Create array of options from option elements
passedOptions.forEach((o) => { passedOptions.forEach((o) => {
allChoices.push({ allChoices.push({
@ -1959,10 +1960,12 @@ export default class Choices {
renderChoices(choices, fragment) { renderChoices(choices, fragment) {
// Create a fragment to store our list items (so we don't have to update the DOM for each item) // Create a fragment to store our list items (so we don't have to update the DOM for each item)
const choicesFragment = fragment || document.createDocumentFragment(); const choicesFragment = fragment || document.createDocumentFragment();
const filter = this.isSearching ? sortByScore : this.config.sortFilter;
if (this.config.shouldSort || this.isSearching) {
const filter = this.isSearching ? sortByScore : this.config.sortFilter;
choices.sort(filter);
}
choices choices
.sort(filter)
.forEach((choice) => { .forEach((choice) => {
const dropdownItem = this._getTemplate('choice', choice); const dropdownItem = this._getTemplate('choice', choice);
if (this.passedElement.type === 'select-one') { if (this.passedElement.type === 'select-one') {

View file

@ -59,6 +59,7 @@ describe('Choices', function() {
expect(this.choices.config.regexFilter).toEqual(null); expect(this.choices.config.regexFilter).toEqual(null);
expect(this.choices.config.sortFilter).toEqual(jasmine.any(Function)); expect(this.choices.config.sortFilter).toEqual(jasmine.any(Function));
expect(this.choices.config.sortFields).toEqual(jasmine.any(Array) || jasmine.any(String)); expect(this.choices.config.sortFields).toEqual(jasmine.any(Array) || jasmine.any(String));
expect(this.choices.config.shouldSort).toEqual(jasmine.any(Boolean));
expect(this.choices.config.placeholder).toEqual(jasmine.any(Boolean)); expect(this.choices.config.placeholder).toEqual(jasmine.any(Boolean));
expect(this.choices.config.placeholderValue).toEqual(null); expect(this.choices.config.placeholderValue).toEqual(null);
expect(this.choices.config.prependValue).toEqual(null); expect(this.choices.config.prependValue).toEqual(null);