mirror of
https://github.com/Choices-js/Choices.git
synced 2024-06-03 06:22:25 +02:00
Reinstate option updating on item removal
This commit is contained in:
parent
d07517fe96
commit
eb80a373df
4
assets/scripts/dist/bundle.js
vendored
4
assets/scripts/dist/bundle.js
vendored
File diff suppressed because one or more lines are too long
|
@ -240,6 +240,7 @@ export class Choices {
|
||||||
const deleteKey = 8 || 46;
|
const deleteKey = 8 || 46;
|
||||||
const enterKey = 13;
|
const enterKey = 13;
|
||||||
const aKey = 65;
|
const aKey = 65;
|
||||||
|
const escapeKey = 27;
|
||||||
|
|
||||||
// If we are typing in the input
|
// If we are typing in the input
|
||||||
if(e.target === this.input) {
|
if(e.target === this.input) {
|
||||||
|
@ -255,6 +256,10 @@ export class Choices {
|
||||||
const value = this.input.value;
|
const value = this.input.value;
|
||||||
this.handleEnter(activeItems, value);
|
this.handleEnter(activeItems, value);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if(e.keyCode === escapeKey && this.dropdown) {
|
||||||
|
this.toggleDropdown();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if(inputIsFocussed) {
|
if(inputIsFocussed) {
|
||||||
|
@ -301,7 +306,7 @@ export class Choices {
|
||||||
onClick(e) {
|
onClick(e) {
|
||||||
const shiftKey = e.shiftKey;
|
const shiftKey = e.shiftKey;
|
||||||
|
|
||||||
if(this.inputType === 'multipleSelect') {
|
if(this.dropdown) {
|
||||||
this.toggleDropdown();
|
this.toggleDropdown();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -682,11 +687,15 @@ export class Choices {
|
||||||
|
|
||||||
getGroupsFilteredByActive() {
|
getGroupsFilteredByActive() {
|
||||||
const groups = this.getGroups();
|
const groups = this.getGroups();
|
||||||
const valueArray = groups.filter((group) => {
|
const options = this.getOptions();
|
||||||
return group.active === true;
|
|
||||||
},[]);
|
|
||||||
|
|
||||||
console.log(groups);
|
const valueArray = groups.filter((group) => {
|
||||||
|
const isActive = group.active === true && group.disabled === false;
|
||||||
|
const hasActiveOptions = options.some((option) => {
|
||||||
|
return option.active === true && option.disabled === false;
|
||||||
|
});
|
||||||
|
return isActive && hasActiveOptions ? true : false;
|
||||||
|
},[]);
|
||||||
|
|
||||||
return valueArray;
|
return valueArray;
|
||||||
}
|
}
|
||||||
|
@ -852,11 +861,16 @@ export class Choices {
|
||||||
render(callback = this.options.callbackOnRender) {
|
render(callback = this.options.callbackOnRender) {
|
||||||
const classNames = this.options.classNames;
|
const classNames = this.options.classNames;
|
||||||
const activeItems = this.getItemsFilteredByActive();
|
const activeItems = this.getItemsFilteredByActive();
|
||||||
const activeOptions = this.getOptionsFilteredByActive();
|
|
||||||
const activeGroups = this.getGroupsFilteredByActive();
|
|
||||||
|
|
||||||
// OPTIONS
|
// OPTIONS
|
||||||
if(this.inputType === 'multipleSelect') {
|
if(this.inputType === 'multipleSelect') {
|
||||||
|
|
||||||
|
const activeOptions = this.getOptionsFilteredByActive();
|
||||||
|
const activeGroups = this.getGroupsFilteredByActive();
|
||||||
|
|
||||||
|
console.log(activeGroups);
|
||||||
|
|
||||||
// Clear options
|
// Clear options
|
||||||
this.dropdown.innerHTML = '';
|
this.dropdown.innerHTML = '';
|
||||||
|
|
||||||
|
@ -865,32 +879,32 @@ export class Choices {
|
||||||
|
|
||||||
// If we have grouped options
|
// If we have grouped options
|
||||||
if(activeGroups.length >= 1) {
|
if(activeGroups.length >= 1) {
|
||||||
activeGroups.forEach((group) => {
|
|
||||||
|
activeGroups.forEach((group, index) => {
|
||||||
|
// Grab options that are children of this group
|
||||||
const groupOptions = activeOptions.filter((option) => {
|
const groupOptions = activeOptions.filter((option) => {
|
||||||
return option.groupId === group.id;
|
return option.groupId === group.id;
|
||||||
});
|
});
|
||||||
|
|
||||||
// IF group actually has options within it
|
const dropdownGroup = strToEl(`
|
||||||
if(groupOptions.length >= 1) {
|
<div class="${ classNames.group } ${ group.disabled ? classNames.itemDisabled : '' }" data-choice-value="${ group.value }" data-choice-group-id="${ group.id }">
|
||||||
const dropdownGroup = strToEl(`
|
<div class="${ classNames.groupHeading }">${ group.value }</div>
|
||||||
<div class="${ classNames.group } ${ group.disabled ? classNames.itemDisabled : '' }" data-choice-value="${ group.value }" data-choice-group-id="${ group.id }">
|
</div>
|
||||||
<div class="${ classNames.groupHeading }">${ group.value }</div>
|
`);
|
||||||
|
|
||||||
|
groupOptions.forEach((option) => {
|
||||||
|
const dropdownItem = strToEl(`
|
||||||
|
<div class="${ classNames.item } ${ classNames.itemOption } ${ option.selected ? classNames.selectedState + ' ' + classNames.itemDisabled : classNames.itemSelectable }" data-choice-option data-choice-id="${ option.id }" data-choice-value="${ option.value }">
|
||||||
|
${ option.label }
|
||||||
</div>
|
</div>
|
||||||
`);
|
`);
|
||||||
|
|
||||||
groupOptions.forEach((option) => {
|
dropdownGroup.appendChild(dropdownItem);
|
||||||
const dropdownItem = strToEl(`
|
});
|
||||||
<div class="${ classNames.item } ${ classNames.itemOption } ${ option.selected ? classNames.selectedState + ' ' + classNames.itemDisabled : classNames.itemSelectable }" data-choice-option data-choice-id="${ option.id }" data-choice-value="${ option.value }">
|
|
||||||
${ option.label }
|
|
||||||
</div>
|
|
||||||
`);
|
|
||||||
|
|
||||||
dropdownGroup.appendChild(dropdownItem);
|
optionListFragment.appendChild(dropdownGroup);
|
||||||
});
|
|
||||||
|
|
||||||
optionListFragment.appendChild(dropdownGroup);
|
|
||||||
}
|
|
||||||
});
|
});
|
||||||
|
|
||||||
} else if(activeOptions.length >= 1) {
|
} else if(activeOptions.length >= 1) {
|
||||||
activeOptions.forEach((option) => {
|
activeOptions.forEach((option) => {
|
||||||
const dropdownItem = strToEl(`
|
const dropdownItem = strToEl(`
|
||||||
|
@ -898,10 +912,10 @@ export class Choices {
|
||||||
${ option.label }
|
${ option.label }
|
||||||
</div>
|
</div>
|
||||||
`);
|
`);
|
||||||
|
|
||||||
optionListFragment.appendChild(dropdownItem);
|
optionListFragment.appendChild(dropdownItem);
|
||||||
});
|
});
|
||||||
} else {
|
} else {
|
||||||
|
// Show 'no options' message
|
||||||
const dropdownItem = strToEl(`<div class="${ classNames.item }">No options to select</div>`);
|
const dropdownItem = strToEl(`<div class="${ classNames.item }">No options to select</div>`);
|
||||||
optionListFragment.appendChild(dropdownItem);
|
optionListFragment.appendChild(dropdownItem);
|
||||||
}
|
}
|
||||||
|
@ -943,7 +957,7 @@ export class Choices {
|
||||||
// Run callback if it is a function
|
// Run callback if it is a function
|
||||||
if(callback){
|
if(callback){
|
||||||
if(isType('Function', callback)) {
|
if(isType('Function', callback)) {
|
||||||
callback(activeItems, activeOptions, activeGroups);
|
callback(activeItems);
|
||||||
} else {
|
} else {
|
||||||
console.error('callbackOnRender: Callback is not a function');
|
console.error('callbackOnRender: Callback is not a function');
|
||||||
}
|
}
|
||||||
|
|
|
@ -20,6 +20,20 @@ const options = (state = [], action) => {
|
||||||
return option;
|
return option;
|
||||||
});
|
});
|
||||||
|
|
||||||
|
case 'REMOVE_ITEM':
|
||||||
|
// When an item is removed and it has an associated option,
|
||||||
|
// we want to re-enable it so it can be chosen again
|
||||||
|
if(action.optionId > -1) {
|
||||||
|
return state.map((option) => {
|
||||||
|
if(option.id === parseInt(action.optionId)) {
|
||||||
|
option.selected = action.selected;
|
||||||
|
}
|
||||||
|
return option;
|
||||||
|
});
|
||||||
|
} else {
|
||||||
|
return state;
|
||||||
|
}
|
||||||
|
|
||||||
case 'FILTER_OPTIONS':
|
case 'FILTER_OPTIONS':
|
||||||
const filteredResults = action.results.items;
|
const filteredResults = action.results.items;
|
||||||
const newState = state.map((option, index) => {
|
const newState = state.map((option, index) => {
|
||||||
|
|
Loading…
Reference in a new issue