mirror of
https://github.com/Choices-js/Choices.git
synced 2024-05-18 13:36:35 +02:00
Final few store getters
This commit is contained in:
parent
80f4d929ac
commit
f02abdaacf
|
@ -431,7 +431,7 @@ class Choices {
|
||||||
|
|
||||||
if (this.isSelectElement) {
|
if (this.isSelectElement) {
|
||||||
// Get active groups/choices
|
// Get active groups/choices
|
||||||
const activeGroups = this.store.getGroupsFilteredByActive();
|
const activeGroups = this.store.groupsFilteredByActive;
|
||||||
const activeChoices = this.store.choicesFilteredByActive;
|
const activeChoices = this.store.choicesFilteredByActive;
|
||||||
|
|
||||||
let choiceListFragment = document.createDocumentFragment();
|
let choiceListFragment = document.createDocumentFragment();
|
||||||
|
@ -898,7 +898,7 @@ class Choices {
|
||||||
* Select placeholder choice
|
* Select placeholder choice
|
||||||
*/
|
*/
|
||||||
_selectPlaceholderChoice() {
|
_selectPlaceholderChoice() {
|
||||||
const placeholderChoice = this.store.getPlaceholderChoice();
|
const placeholderChoice = this.store.placeholderChoice;
|
||||||
|
|
||||||
if (placeholderChoice) {
|
if (placeholderChoice) {
|
||||||
this._addItem(
|
this._addItem(
|
||||||
|
@ -1215,7 +1215,7 @@ class Choices {
|
||||||
}
|
}
|
||||||
|
|
||||||
// If new value matches the desired length and is not the same as the current value with a space
|
// If new value matches the desired length and is not the same as the current value with a space
|
||||||
const haystack = this.store.getSearchableChoices();
|
const haystack = this.store.searchableChoices;
|
||||||
const needle = newValue;
|
const needle = newValue;
|
||||||
const keys = isType('Array', this.config.searchFields) ?
|
const keys = isType('Array', this.config.searchFields) ?
|
||||||
this.config.searchFields :
|
this.config.searchFields :
|
||||||
|
|
|
@ -84,20 +84,51 @@ export default class Store {
|
||||||
* Get selectable choices from store
|
* Get selectable choices from store
|
||||||
* @return {Array} Option objects
|
* @return {Array} Option objects
|
||||||
*/
|
*/
|
||||||
getChoicesFilteredBySelectable() {
|
get choicesFilteredBySelectable() {
|
||||||
const choices = this.choices;
|
return this.choices.filter(choice => choice.disabled !== true);
|
||||||
const values = choices.filter(choice => choice.disabled !== true);
|
|
||||||
|
|
||||||
return values;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Get choices that can be searched (excluding placeholders)
|
* Get choices that can be searched (excluding placeholders)
|
||||||
* @return {Array} Option objects
|
* @return {Array} Option objects
|
||||||
*/
|
*/
|
||||||
getSearchableChoices() {
|
get searchableChoices() {
|
||||||
const filtered = this.getChoicesFilteredBySelectable();
|
return this.choicesFilteredBySelectable.filter(choice => choice.placeholder !== true);
|
||||||
return filtered.filter(choice => choice.placeholder !== true);
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Get placeholder choice from store
|
||||||
|
* @return {Object} Found placeholder
|
||||||
|
*/
|
||||||
|
get placeholderChoice() {
|
||||||
|
return [...this.choices]
|
||||||
|
.reverse()
|
||||||
|
.find(choice => choice.placeholder === true);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Get groups from store
|
||||||
|
* @return {Array} Group objects
|
||||||
|
*/
|
||||||
|
get groups() {
|
||||||
|
return this.state.groups;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Get active groups from store
|
||||||
|
* @return {Array} Group objects
|
||||||
|
*/
|
||||||
|
get groupsFilteredByActive() {
|
||||||
|
const groups = this.groups;
|
||||||
|
const choices = this.choices;
|
||||||
|
|
||||||
|
return groups.filter((group) => {
|
||||||
|
const isActive = group.active === true && group.disabled === false;
|
||||||
|
const hasActiveOptions = choices.some(choice =>
|
||||||
|
choice.active === true && choice.disabled === false,
|
||||||
|
);
|
||||||
|
return isActive && hasActiveOptions;
|
||||||
|
}, []);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -113,55 +144,12 @@ export default class Store {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* Get placeholder choice from store
|
|
||||||
* @return {Object} Found placeholder
|
|
||||||
*/
|
|
||||||
getPlaceholderChoice() {
|
|
||||||
const choices = this.choices;
|
|
||||||
const placeholderChoice = [...choices]
|
|
||||||
.reverse()
|
|
||||||
.find(choice => choice.placeholder === true);
|
|
||||||
|
|
||||||
return placeholderChoice;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Get groups from store
|
|
||||||
* @return {Array} Group objects
|
|
||||||
*/
|
|
||||||
getGroups() {
|
|
||||||
return this.state.groups;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Get active groups from store
|
|
||||||
* @return {Array} Group objects
|
|
||||||
*/
|
|
||||||
getGroupsFilteredByActive() {
|
|
||||||
const groups = this.getGroups();
|
|
||||||
const choices = this.choices;
|
|
||||||
|
|
||||||
const values = groups.filter((group) => {
|
|
||||||
const isActive = group.active === true && group.disabled === false;
|
|
||||||
const hasActiveOptions = choices.some(choice =>
|
|
||||||
choice.active === true && choice.disabled === false,
|
|
||||||
);
|
|
||||||
return isActive && hasActiveOptions;
|
|
||||||
}, []);
|
|
||||||
|
|
||||||
return values;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Get group by group id
|
* Get group by group id
|
||||||
* @param {Number} id Group ID
|
* @param {Number} id Group ID
|
||||||
* @return {Object} Group data
|
* @return {Object} Group data
|
||||||
*/
|
*/
|
||||||
getGroupById(id) {
|
getGroupById(id) {
|
||||||
const groups = this.getGroups();
|
return this.groups.find(group => group.id === parseInt(id, 10));
|
||||||
const foundGroup = groups.find(group => group.id === parseInt(id, 10));
|
|
||||||
|
|
||||||
return foundGroup;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -189,19 +189,17 @@ describe('reducers/store', () => {
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
describe('getChoicesFilteredBySelectable', () => {
|
describe('choicesFilteredBySelectable getter', () => {
|
||||||
it('returns choices that are not disabled', () => {
|
it('returns choices that are not disabled', () => {
|
||||||
const expectedResponse = state.choices.filter((choice => !choice.disabled));
|
const expectedResponse = state.choices.filter((choice => !choice.disabled));
|
||||||
const actualResponse = instance.getChoicesFilteredBySelectable();
|
expect(instance.choicesFilteredBySelectable).to.eql(expectedResponse);
|
||||||
expect(actualResponse).to.eql(expectedResponse);
|
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
describe('getSearchableChoices', () => {
|
describe('searchableChoices getter', () => {
|
||||||
it('returns choices that are not placeholders and are selectable', () => {
|
it('returns choices that are not placeholders and are selectable', () => {
|
||||||
const expectedResponse = state.choices.filter((choice => !choice.disabled && !choice.placeholder));
|
const expectedResponse = state.choices.filter((choice => !choice.disabled && !choice.placeholder));
|
||||||
const actualResponse = instance.getSearchableChoices();
|
expect(instance.searchableChoices).to.eql(expectedResponse);
|
||||||
expect(actualResponse).to.eql(expectedResponse);
|
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
|
@ -223,27 +221,24 @@ describe('reducers/store', () => {
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
describe('getPlaceholderChoice', () => {
|
describe('placeholderChoice getter', () => {
|
||||||
it('returns placeholder choice', () => {
|
it('returns placeholder choice', () => {
|
||||||
const expectedResponse = state.choices.reverse().find(choice => choice.placeholder);
|
const expectedResponse = state.choices.reverse().find(choice => choice.placeholder);
|
||||||
const actualResponse = instance.getPlaceholderChoice();
|
expect(instance.getPlaceholderChoice).to.eql(expectedResponse);
|
||||||
expect(actualResponse).to.eql(expectedResponse);
|
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
describe('getGroups', () => {
|
describe('groups getter', () => {
|
||||||
it('returns groups', () => {
|
it('returns groups', () => {
|
||||||
const expectedResponse = state.groups;
|
const expectedResponse = state.groups;
|
||||||
const actualResponse = instance.getGroups();
|
expect(instance.groups).to.eql(expectedResponse);
|
||||||
expect(actualResponse).to.eql(expectedResponse);
|
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
describe('getGroupsFilteredByActive', () => {
|
describe('groupsFilteredByActive getter', () => {
|
||||||
it('returns active groups', () => {
|
it('returns active groups', () => {
|
||||||
const expectedResponse = state.groups.filter(group => group.active);
|
const expectedResponse = state.groups.filter(group => group.active);
|
||||||
const actualResponse = instance.getGroupsFilteredByActive();
|
expect(instance.groupsFilteredByActive).to.eql(expectedResponse);
|
||||||
expect(actualResponse).to.eql(expectedResponse);
|
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue