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