mirror of
https://github.com/Choices-js/Choices.git
synced 2024-05-18 13:36:35 +02:00
Further getters
This commit is contained in:
parent
152b740670
commit
80f4d929ac
|
@ -432,7 +432,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.getGroupsFilteredByActive();
|
||||||
const activeChoices = this.store.getChoicesFilteredByActive();
|
const activeChoices = this.store.choicesFilteredByActive;
|
||||||
|
|
||||||
let choiceListFragment = document.createDocumentFragment();
|
let choiceListFragment = document.createDocumentFragment();
|
||||||
|
|
||||||
|
@ -464,7 +464,7 @@ class Choices {
|
||||||
|
|
||||||
// If we have choices to show
|
// If we have choices to show
|
||||||
if (choiceListFragment.childNodes && choiceListFragment.childNodes.length > 0) {
|
if (choiceListFragment.childNodes && choiceListFragment.childNodes.length > 0) {
|
||||||
const activeItems = this.store.getItemsFilteredByActive();
|
const activeItems = this.store.itemsFilteredByActive;
|
||||||
const canAddItem = this._canAddItem(activeItems, this.input.value);
|
const canAddItem = this._canAddItem(activeItems, this.input.value);
|
||||||
|
|
||||||
// ...and we can select them
|
// ...and we can select them
|
||||||
|
@ -502,7 +502,7 @@ class Choices {
|
||||||
/* Items */
|
/* Items */
|
||||||
if (this.currentState.items !== this.prevState.items) {
|
if (this.currentState.items !== this.prevState.items) {
|
||||||
// Get active items (items that can be selected)
|
// Get active items (items that can be selected)
|
||||||
const activeItems = this.store.getItemsFilteredByActive() || [];
|
const activeItems = this.store.itemsFilteredByActive || [];
|
||||||
// Clear list
|
// Clear list
|
||||||
this.itemList.clear();
|
this.itemList.clear();
|
||||||
|
|
||||||
|
@ -608,7 +608,7 @@ class Choices {
|
||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
|
|
||||||
const items = this.store.getItemsFilteredByActive();
|
const items = this.store.itemsFilteredByActive;
|
||||||
|
|
||||||
items.forEach((item) => {
|
items.forEach((item) => {
|
||||||
if (item.value === value) {
|
if (item.value === value) {
|
||||||
|
@ -627,7 +627,7 @@ class Choices {
|
||||||
* @public
|
* @public
|
||||||
*/
|
*/
|
||||||
removeActiveItems(excludedId) {
|
removeActiveItems(excludedId) {
|
||||||
const items = this.store.getItemsFilteredByActive();
|
const items = this.store.itemsFilteredByActive;
|
||||||
|
|
||||||
items.forEach((item) => {
|
items.forEach((item) => {
|
||||||
if (excludedId !== item.id) {
|
if (excludedId !== item.id) {
|
||||||
|
@ -645,7 +645,7 @@ class Choices {
|
||||||
* @public
|
* @public
|
||||||
*/
|
*/
|
||||||
removeHighlightedItems(runEvent = false) {
|
removeHighlightedItems(runEvent = false) {
|
||||||
const items = this.store.getItemsFilteredByHighlighted();
|
const items = this.store.itemsFilteredByHighlighted;
|
||||||
|
|
||||||
items.forEach((item) => {
|
items.forEach((item) => {
|
||||||
this._removeItem(item);
|
this._removeItem(item);
|
||||||
|
@ -727,7 +727,7 @@ class Choices {
|
||||||
* @public
|
* @public
|
||||||
*/
|
*/
|
||||||
getValue(valueOnly = false) {
|
getValue(valueOnly = false) {
|
||||||
const items = this.store.getItemsFilteredByActive();
|
const items = this.store.itemsFilteredByActive;
|
||||||
|
|
||||||
const values = items.reduce((selectedItems, item) => {
|
const values = items.reduce((selectedItems, item) => {
|
||||||
const itemValue = valueOnly ? item.value : item;
|
const itemValue = valueOnly ? item.value : item;
|
||||||
|
@ -1243,7 +1243,7 @@ class Choices {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
const choices = this.store.getChoices();
|
const choices = this.store.choices;
|
||||||
const hasUnactiveChoices = choices.some(option => !option.active);
|
const hasUnactiveChoices = choices.some(option => !option.active);
|
||||||
|
|
||||||
// Check that we have a value to search and the input was an alphanumeric character
|
// Check that we have a value to search and the input was an alphanumeric character
|
||||||
|
@ -1322,7 +1322,7 @@ class Choices {
|
||||||
}
|
}
|
||||||
|
|
||||||
const target = e.target;
|
const target = e.target;
|
||||||
const activeItems = this.store.getItemsFilteredByActive();
|
const activeItems = this.store.itemsFilteredByActive;
|
||||||
const hasFocusedInput = this.input.isFocussed;
|
const hasFocusedInput = this.input.isFocussed;
|
||||||
const hasActiveDropdown = this.dropdown.isActive;
|
const hasActiveDropdown = this.dropdown.isActive;
|
||||||
const hasItems = this.itemList.hasChildren;
|
const hasItems = this.itemList.hasChildren;
|
||||||
|
@ -1491,7 +1491,7 @@ class Choices {
|
||||||
}
|
}
|
||||||
|
|
||||||
const value = this.input.value;
|
const value = this.input.value;
|
||||||
const activeItems = this.store.getItemsFilteredByActive();
|
const activeItems = this.store.itemsFilteredByActive;
|
||||||
const canAddItem = this._canAddItem(activeItems, value);
|
const canAddItem = this._canAddItem(activeItems, value);
|
||||||
|
|
||||||
// We are typing into a text input and have a value, we want to show a dropdown
|
// We are typing into a text input and have a value, we want to show a dropdown
|
||||||
|
@ -1587,7 +1587,7 @@ class Choices {
|
||||||
}
|
}
|
||||||
|
|
||||||
if (this.containerOuter.element.contains(target) && target !== this.input.element) {
|
if (this.containerOuter.element.contains(target) && target !== this.input.element) {
|
||||||
const activeItems = this.store.getItemsFilteredByActive();
|
const activeItems = this.store.itemsFilteredByActive;
|
||||||
const hasShiftKey = e.shiftKey;
|
const hasShiftKey = e.shiftKey;
|
||||||
|
|
||||||
const buttonTarget = findAncestorByAttrName(target, 'data-button');
|
const buttonTarget = findAncestorByAttrName(target, 'data-button');
|
||||||
|
@ -1633,7 +1633,7 @@ class Choices {
|
||||||
_onClick(e) {
|
_onClick(e) {
|
||||||
const target = e.target;
|
const target = e.target;
|
||||||
const hasActiveDropdown = this.dropdown.isActive;
|
const hasActiveDropdown = this.dropdown.isActive;
|
||||||
const activeItems = this.store.getItemsFilteredByActive();
|
const activeItems = this.store.itemsFilteredByActive;
|
||||||
|
|
||||||
// If target is something that concerns us
|
// If target is something that concerns us
|
||||||
if (this.containerOuter.element.contains(target)) {
|
if (this.containerOuter.element.contains(target)) {
|
||||||
|
@ -1720,7 +1720,7 @@ class Choices {
|
||||||
const target = e.target;
|
const target = e.target;
|
||||||
// If target is something that concerns us
|
// If target is something that concerns us
|
||||||
if (this.containerOuter.element.contains(target) && !this.isScrollingOnIe) {
|
if (this.containerOuter.element.contains(target) && !this.isScrollingOnIe) {
|
||||||
const activeItems = this.store.getItemsFilteredByActive();
|
const activeItems = this.store.itemsFilteredByActive;
|
||||||
const hasHighlightedItems = activeItems.some(item => item.highlighted);
|
const hasHighlightedItems = activeItems.some(item => item.highlighted);
|
||||||
const blurActions = {
|
const blurActions = {
|
||||||
text: () => {
|
text: () => {
|
||||||
|
@ -2021,7 +2021,7 @@ class Choices {
|
||||||
}
|
}
|
||||||
|
|
||||||
// Generate unique id
|
// Generate unique id
|
||||||
const choices = this.store.getChoices();
|
const choices = this.store.choices;
|
||||||
const choiceLabel = label || value;
|
const choiceLabel = label || value;
|
||||||
const choiceId = choices ? choices.length + 1 : 1;
|
const choiceId = choices ? choices.length + 1 : 1;
|
||||||
const choiceElementId = `${this.baseId}-${this.idNames.itemChoice}-${choiceId}`;
|
const choiceElementId = `${this.baseId}-${this.idNames.itemChoice}-${choiceId}`;
|
||||||
|
@ -2374,7 +2374,7 @@ class Choices {
|
||||||
}
|
}
|
||||||
|
|
||||||
_findAndSelectChoiceByValue(val) {
|
_findAndSelectChoiceByValue(val) {
|
||||||
const choices = this.store.getChoices();
|
const choices = this.store.choices;
|
||||||
// Check 'value' property exists and the choice isn't already selected
|
// Check 'value' property exists and the choice isn't already selected
|
||||||
const foundChoice = choices.find(choice => this.config.itemComparer(choice.value, val));
|
const foundChoice = choices.find(choice => this.config.itemComparer(choice.value, val));
|
||||||
|
|
||||||
|
|
|
@ -1107,7 +1107,7 @@ describe('choices', () => {
|
||||||
});
|
});
|
||||||
|
|
||||||
describe('getValue', () => {
|
describe('getValue', () => {
|
||||||
let getItemsFilteredByActiveStub;
|
let itemsFilteredByActiveStub;
|
||||||
const items = [
|
const items = [
|
||||||
{
|
{
|
||||||
id: '1',
|
id: '1',
|
||||||
|
@ -1120,12 +1120,11 @@ describe('choices', () => {
|
||||||
];
|
];
|
||||||
|
|
||||||
beforeEach(() => {
|
beforeEach(() => {
|
||||||
getItemsFilteredByActiveStub = stub().returns(items);
|
itemsFilteredByActiveStub = stub(instance.store, 'itemsFilteredByActive').get(() => items);
|
||||||
instance.store.getItemsFilteredByActive = getItemsFilteredByActiveStub;
|
|
||||||
});
|
});
|
||||||
|
|
||||||
afterEach(() => {
|
afterEach(() => {
|
||||||
instance.store.getItemsFilteredByActive.reset();
|
itemsFilteredByActiveStub.reset();
|
||||||
});
|
});
|
||||||
|
|
||||||
describe('passing true valueOnly flag', () => {
|
describe('passing true valueOnly flag', () => {
|
||||||
|
@ -1187,7 +1186,7 @@ describe('choices', () => {
|
||||||
});
|
});
|
||||||
|
|
||||||
describe('passing valid value', () => {
|
describe('passing valid value', () => {
|
||||||
let getItemsFilteredByActiveStub;
|
let itemsFilteredByActiveStub;
|
||||||
let removeItemStub;
|
let removeItemStub;
|
||||||
const value = 'Removed';
|
const value = 'Removed';
|
||||||
const items = [
|
const items = [
|
||||||
|
@ -1207,15 +1206,14 @@ describe('choices', () => {
|
||||||
|
|
||||||
beforeEach(() => {
|
beforeEach(() => {
|
||||||
removeItemStub = stub();
|
removeItemStub = stub();
|
||||||
getItemsFilteredByActiveStub = stub().returns(items);
|
itemsFilteredByActiveStub = stub(instance.store, 'itemsFilteredByActive').get(() => items);
|
||||||
instance.store.getItemsFilteredByActive = getItemsFilteredByActiveStub;
|
|
||||||
instance._removeItem = removeItemStub;
|
instance._removeItem = removeItemStub;
|
||||||
|
|
||||||
output = instance.removeActiveItemsByValue(value);
|
output = instance.removeActiveItemsByValue(value);
|
||||||
});
|
});
|
||||||
|
|
||||||
afterEach(() => {
|
afterEach(() => {
|
||||||
instance.store.getItemsFilteredByActive.reset();
|
itemsFilteredByActiveStub.reset();
|
||||||
instance._removeItem.reset();
|
instance._removeItem.reset();
|
||||||
});
|
});
|
||||||
|
|
||||||
|
@ -1228,7 +1226,7 @@ describe('choices', () => {
|
||||||
});
|
});
|
||||||
|
|
||||||
describe('removeActiveItems', () => {
|
describe('removeActiveItems', () => {
|
||||||
let getItemsFilteredByActiveStub;
|
let itemsFilteredByActiveStub;
|
||||||
let removeItemStub;
|
let removeItemStub;
|
||||||
const items = [
|
const items = [
|
||||||
{
|
{
|
||||||
|
@ -1247,13 +1245,12 @@ describe('choices', () => {
|
||||||
|
|
||||||
beforeEach(() => {
|
beforeEach(() => {
|
||||||
removeItemStub = stub();
|
removeItemStub = stub();
|
||||||
getItemsFilteredByActiveStub = stub().returns(items);
|
itemsFilteredByActiveStub = stub(instance.store, 'itemsFilteredByActive').get(() => items);
|
||||||
instance.store.getItemsFilteredByActive = getItemsFilteredByActiveStub;
|
|
||||||
instance._removeItem = removeItemStub;
|
instance._removeItem = removeItemStub;
|
||||||
});
|
});
|
||||||
|
|
||||||
afterEach(() => {
|
afterEach(() => {
|
||||||
instance.store.getItemsFilteredByActive.reset();
|
itemsFilteredByActiveStub.reset();
|
||||||
instance._removeItem.reset();
|
instance._removeItem.reset();
|
||||||
});
|
});
|
||||||
|
|
||||||
|
@ -1286,7 +1283,7 @@ describe('choices', () => {
|
||||||
});
|
});
|
||||||
|
|
||||||
describe('removeHighlightedItems', () => {
|
describe('removeHighlightedItems', () => {
|
||||||
let getItemsFilteredByHighlightedStub;
|
let itemsFilteredByHighlightedStub;
|
||||||
let removeItemStub;
|
let removeItemStub;
|
||||||
let triggerChangeStub;
|
let triggerChangeStub;
|
||||||
|
|
||||||
|
@ -1303,17 +1300,16 @@ describe('choices', () => {
|
||||||
|
|
||||||
|
|
||||||
beforeEach(() => {
|
beforeEach(() => {
|
||||||
getItemsFilteredByHighlightedStub = stub().returns(items);
|
itemsFilteredByHighlightedStub = stub(instance.store, 'itemsFilteredByHighlighted').get(() => items);
|
||||||
removeItemStub = stub();
|
removeItemStub = stub();
|
||||||
triggerChangeStub = stub();
|
triggerChangeStub = stub();
|
||||||
|
|
||||||
instance.store.getItemsFilteredByHighlighted = getItemsFilteredByHighlightedStub;
|
|
||||||
instance._removeItem = removeItemStub;
|
instance._removeItem = removeItemStub;
|
||||||
instance._triggerChange = triggerChangeStub;
|
instance._triggerChange = triggerChangeStub;
|
||||||
});
|
});
|
||||||
|
|
||||||
afterEach(() => {
|
afterEach(() => {
|
||||||
instance.store.getItemsFilteredByHighlighted.reset();
|
itemsFilteredByHighlightedStub.reset();
|
||||||
instance._removeItem.reset();
|
instance._removeItem.reset();
|
||||||
instance._triggerChange.reset();
|
instance._triggerChange.reset();
|
||||||
});
|
});
|
||||||
|
|
|
@ -49,29 +49,23 @@ export default class Store {
|
||||||
* Get active items from store
|
* Get active items from store
|
||||||
* @return {Array} Item objects
|
* @return {Array} Item objects
|
||||||
*/
|
*/
|
||||||
getItemsFilteredByActive() {
|
get itemsFilteredByActive() {
|
||||||
const items = this.items;
|
return this.items.filter(item => item.active === true);
|
||||||
const values = items.filter(item => item.active === true);
|
|
||||||
|
|
||||||
return values;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Get highlighted items from store
|
* Get highlighted items from store
|
||||||
* @return {Array} Item objects
|
* @return {Array} Item objects
|
||||||
*/
|
*/
|
||||||
getItemsFilteredByHighlighted() {
|
get itemsFilteredByHighlighted() {
|
||||||
const items = this.items;
|
return this.items.filter(item => item.active && item.highlighted);
|
||||||
const values = items.filter(item => item.active && item.highlighted);
|
|
||||||
|
|
||||||
return values;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Get choices from store
|
* Get choices from store
|
||||||
* @return {Array} Option objects
|
* @return {Array} Option objects
|
||||||
*/
|
*/
|
||||||
getChoices() {
|
get choices() {
|
||||||
return this.state.choices;
|
return this.state.choices;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -79,8 +73,8 @@ export default class Store {
|
||||||
* Get active choices from store
|
* Get active choices from store
|
||||||
* @return {Array} Option objects
|
* @return {Array} Option objects
|
||||||
*/
|
*/
|
||||||
getChoicesFilteredByActive() {
|
get choicesFilteredByActive() {
|
||||||
const choices = this.getChoices();
|
const choices = this.choices;
|
||||||
const values = choices.filter(choice => choice.active === true);
|
const values = choices.filter(choice => choice.active === true);
|
||||||
|
|
||||||
return values;
|
return values;
|
||||||
|
@ -91,7 +85,7 @@ export default class Store {
|
||||||
* @return {Array} Option objects
|
* @return {Array} Option objects
|
||||||
*/
|
*/
|
||||||
getChoicesFilteredBySelectable() {
|
getChoicesFilteredBySelectable() {
|
||||||
const choices = this.getChoices();
|
const choices = this.choices;
|
||||||
const values = choices.filter(choice => choice.disabled !== true);
|
const values = choices.filter(choice => choice.disabled !== true);
|
||||||
|
|
||||||
return values;
|
return values;
|
||||||
|
@ -112,7 +106,7 @@ export default class Store {
|
||||||
*/
|
*/
|
||||||
getChoiceById(id) {
|
getChoiceById(id) {
|
||||||
if (id) {
|
if (id) {
|
||||||
const choices = this.getChoicesFilteredByActive();
|
const choices = this.choicesFilteredByActive;
|
||||||
const foundChoice = choices.find(choice => choice.id === parseInt(id, 10));
|
const foundChoice = choices.find(choice => choice.id === parseInt(id, 10));
|
||||||
return foundChoice;
|
return foundChoice;
|
||||||
}
|
}
|
||||||
|
@ -124,7 +118,7 @@ export default class Store {
|
||||||
* @return {Object} Found placeholder
|
* @return {Object} Found placeholder
|
||||||
*/
|
*/
|
||||||
getPlaceholderChoice() {
|
getPlaceholderChoice() {
|
||||||
const choices = this.getChoices();
|
const choices = this.choices;
|
||||||
const placeholderChoice = [...choices]
|
const placeholderChoice = [...choices]
|
||||||
.reverse()
|
.reverse()
|
||||||
.find(choice => choice.placeholder === true);
|
.find(choice => choice.placeholder === true);
|
||||||
|
@ -146,7 +140,7 @@ export default class Store {
|
||||||
*/
|
*/
|
||||||
getGroupsFilteredByActive() {
|
getGroupsFilteredByActive() {
|
||||||
const groups = this.getGroups();
|
const groups = this.getGroups();
|
||||||
const choices = this.getChoices();
|
const choices = this.choices;
|
||||||
|
|
||||||
const values = groups.filter((group) => {
|
const values = groups.filter((group) => {
|
||||||
const isActive = group.active === true && group.disabled === false;
|
const isActive = group.active === true && group.disabled === false;
|
||||||
|
|
|
@ -55,9 +55,7 @@ describe('reducers/store', () => {
|
||||||
|
|
||||||
describe('state getter', () => {
|
describe('state getter', () => {
|
||||||
it('returns state', () => {
|
it('returns state', () => {
|
||||||
const state = {
|
const state = { items: [] };
|
||||||
items: [],
|
|
||||||
};
|
|
||||||
getStateStub.returns(state);
|
getStateStub.returns(state);
|
||||||
|
|
||||||
expect(instance.state).to.equal(state);
|
expect(instance.state).to.equal(state);
|
||||||
|
@ -163,35 +161,31 @@ describe('reducers/store', () => {
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
describe('getItemsFilteredByActive', () => {
|
describe('itemsFilteredByActive getter', () => {
|
||||||
it('returns items that are active', () => {
|
it('returns items that are active', () => {
|
||||||
const expectedResponse = state.items.filter((item => item.active));
|
const expectedResponse = state.items.filter((item => item.active));
|
||||||
const actualResponse = instance.getItemsFilteredByActive();
|
expect(instance.itemsFilteredByActive).to.eql(expectedResponse);
|
||||||
expect(actualResponse).to.eql(expectedResponse);
|
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
describe('getItemsFilteredByHighlighted', () => {
|
describe('itemsFilteredByHighlighted getter', () => {
|
||||||
it('returns items that are active and highlighted', () => {
|
it('returns items that are active and highlighted', () => {
|
||||||
const expectedResponse = state.items.filter((item => item.highlighted && item.active));
|
const expectedResponse = state.items.filter((item => item.highlighted && item.active));
|
||||||
const actualResponse = instance.getItemsFilteredByHighlighted();
|
expect(instance.itemsFilteredByHighlighted).to.eql(expectedResponse);
|
||||||
expect(actualResponse).to.eql(expectedResponse);
|
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
describe('getChoices', () => {
|
describe('choices getter', () => {
|
||||||
it('returns choices', () => {
|
it('returns choices', () => {
|
||||||
const expectedResponse = state.choices;
|
const expectedResponse = state.choices;
|
||||||
const actualResponse = instance.getChoices();
|
expect(instance.choices).to.eql(expectedResponse);
|
||||||
expect(actualResponse).to.eql(expectedResponse);
|
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
describe('getChoicesFilteredByActive', () => {
|
describe('choicesFilteredByActive getter', () => {
|
||||||
it('returns choices that are active', () => {
|
it('returns choices that are active', () => {
|
||||||
const expectedResponse = state.choices.filter((choice => choice.active));
|
const expectedResponse = state.choices.filter((choice => choice.active));
|
||||||
const actualResponse = instance.getChoicesFilteredByActive();
|
expect(instance.choicesFilteredByActive).to.eql(expectedResponse);
|
||||||
expect(actualResponse).to.eql(expectedResponse);
|
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue