mirror of
https://github.com/Choices-js/Choices.git
synced 2024-06-08 08:52:19 +02:00
Render select options based on items chosen
This commit is contained in:
parent
23ed4d9ecf
commit
fc1b945ac9
4
assets/scripts/dist/choices.min.js
vendored
4
assets/scripts/dist/choices.min.js
vendored
File diff suppressed because one or more lines are too long
|
@ -179,9 +179,7 @@ export class Choices {
|
|||
handleBackspace(activeItems) {
|
||||
if(this.options.removeItems && activeItems) {
|
||||
const lastItem = activeItems[activeItems.length - 1];
|
||||
const hasSelectedItems = activeItems.some((item) => {
|
||||
return item.selected === true;
|
||||
});
|
||||
const hasSelectedItems = activeItems.some((item) => item.selected === true);
|
||||
|
||||
// If editing the last item is allowed and there are not other selected items,
|
||||
// we can edit the item value. Otherwise if we can remove items, remove all selected items
|
||||
|
@ -189,13 +187,12 @@ export class Choices {
|
|||
this.input.value = lastItem.value;
|
||||
this.removeItem(lastItem);
|
||||
} else {
|
||||
this.selectItem(lastItem);
|
||||
if(!hasSelectedItems) { this.selectItem(lastItem); }
|
||||
this.removeSelectedItems();
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
/**
|
||||
* Key down event
|
||||
* @param {Object} e Event
|
||||
|
@ -237,9 +234,7 @@ export class Choices {
|
|||
break;
|
||||
|
||||
case enterKey:
|
||||
if(this.passedElement.type === 'select-one') {
|
||||
this.toggleDropdown();
|
||||
}
|
||||
if(this.passedElement.type === 'select-one') this.toggleDropdown();
|
||||
|
||||
// If enter key is pressed and the input has a value
|
||||
if(e.target.value && this.passedElement.type === 'text') {
|
||||
|
@ -381,6 +376,8 @@ export class Choices {
|
|||
*/
|
||||
onMouseDown(e) {
|
||||
|
||||
const activeItems = this.store.getItemsFilteredByActive();
|
||||
|
||||
// If click is affecting a child node of our element
|
||||
if(this.containerOuter.contains(e.target)) {
|
||||
|
||||
|
@ -388,7 +385,6 @@ export class Choices {
|
|||
// in a race condition
|
||||
e.preventDefault();
|
||||
|
||||
const activeItems = this.store.getItemsFilteredByActive();
|
||||
const hasShiftKey = e.shiftKey ? true : false;
|
||||
|
||||
// If input is not in focus, it ought to be
|
||||
|
@ -434,9 +430,10 @@ export class Choices {
|
|||
} else {
|
||||
// Click is outside of our element so close dropdown and de-select items
|
||||
const hasActiveDropdown = this.dropdown.classList.contains(this.options.classNames.activeState);
|
||||
const hasSelectedItems = activeItems.some((item) => item.selected === true);
|
||||
|
||||
// Deselect items
|
||||
this.deselectAll();
|
||||
// De-select any selected items
|
||||
if(hasSelectedItems) this.deselectAll();
|
||||
|
||||
// Remove focus state
|
||||
this.containerOuter.classList.remove(this.options.classNames.focusState);
|
||||
|
@ -945,6 +942,9 @@ export class Choices {
|
|||
notice: (label) => {
|
||||
return strToEl(`<div class="${ classNames.item } ${ classNames.itemOption }">${ label }</div>`);
|
||||
},
|
||||
selectOption: (data) => {
|
||||
return strToEl(`<option value="${ data.value }" selected>${ data.label.trim() }</option>`);
|
||||
},
|
||||
option: (data) => {
|
||||
return strToEl(`
|
||||
<div class="${ classNames.item } ${ classNames.itemOption } ${ data.disabled ? classNames.itemDisabled : classNames.itemSelectable }" data-option ${ data.disabled ? 'data-option-disabled' : 'data-option-selectable' } data-id="${ data.id }" data-value="${ data.value }">
|
||||
|
@ -1089,8 +1089,24 @@ export class Choices {
|
|||
// Simplify store data to just values
|
||||
const itemsFiltered = this.store.getItemsReducedToValues(items);
|
||||
|
||||
if(this.passedElement.type === 'text') {
|
||||
// Assign hidden input array of values
|
||||
this.passedElement.value = itemsFiltered.join(this.options.delimiter);
|
||||
} else {
|
||||
const selectedOptionsFragment = document.createDocumentFragment();
|
||||
|
||||
// Add each list item to list
|
||||
items.forEach((item) => {
|
||||
// Create new list element
|
||||
const option = this.getTemplate('selectOption', item);
|
||||
|
||||
// Append it to list
|
||||
selectedOptionsFragment.appendChild(option);
|
||||
});
|
||||
|
||||
this.passedElement.innerHTML = "";
|
||||
this.passedElement.appendChild(selectedOptionsFragment);
|
||||
}
|
||||
|
||||
// Add each list item to list
|
||||
items.forEach((item) => {
|
||||
|
@ -1116,6 +1132,7 @@ export class Choices {
|
|||
this.currentState = this.store.getState();
|
||||
|
||||
if(this.currentState !== this.prevState) {
|
||||
|
||||
// OPTIONS
|
||||
if((this.currentState.options !== this.prevState.options || this.currentState.groups !== this.prevState.groups)) {
|
||||
if(this.passedElement.type === 'select-multiple' || this.passedElement.type === 'select-one') {
|
||||
|
|
|
@ -114,8 +114,8 @@ h1, h2, h3, h4, h5, h6 {
|
|||
.choices__list--multiple .choices__item.is-selected {
|
||||
background-color: #00a5bb; }
|
||||
.is-disabled .choices__list--multiple .choices__item {
|
||||
background-color: #eaeaea;
|
||||
border: 1px solid #dddddd; }
|
||||
background-color: #aaaaaa;
|
||||
border: 1px solid #919191; }
|
||||
|
||||
.choices__list--dropdown {
|
||||
display: none;
|
||||
|
|
2
assets/styles/css/choices.min.css
vendored
2
assets/styles/css/choices.min.css
vendored
|
@ -1 +1 @@
|
|||
*,:after,:before{box-sizing:border-box}body,html{margin:0;height:100%;widows:100%}html{font-size:62.5%}body{background-color:#333;font-family:"Helvetica Neue",Helvetica,Arial,"Lucida Grande",sans-serif;font-size:1.6rem;color:#fff}label{display:block;margin-bottom:.8rem;font-size:1.4rem;font-weight:500}h1,h2,h3,h4,h5,h6{margin-top:0;margin-bottom:1.2rem;font-weight:500}.container{display:block;margin:auto;max-width:35em;padding:2.4rem}.section{background-color:#fff;padding:2.4rem;color:#333}.choices{margin-bottom:2.4rem;position:relative}.choices.is-disabled .choices__inner,.choices.is-disabled .choices__input{background-color:#eaeaea;cursor:not-allowed;-webkit-user-select:none;-moz-user-select:none;-ms-user-select:none;user-select:none}.choices.is-disabled .choices__item{cursor:not-allowed}.choices[data-type*=select-one].is-open:after{border-color:transparent transparent #333 transparent;margin-top:-7.5px}.choices[data-type*=select-one]:after{content:"";height:0;width:0;border-style:solid;border-color:#333 transparent transparent transparent;border-width:5px;position:absolute;right:1.15rem;top:50%;margin-top:-2.5px}.choices__inner{background-color:#f9f9f9;padding:.75rem .75rem .375rem;border:1px solid #ddd;border-radius:.25rem;font-size:1.4rem;cursor:text;overflow:hidden}.is-focused .choices__inner{border-color:#b7b7b7}.is-open .choices__inner{border-radius:.25rem .25rem 0 0}.is-flipped.is-open .choices__inner{border-radius:0 0 .25rem .25rem}.choices__list{margin:0;padding-left:0;list-style-type:none}.choices__list--single{display:inline-block}.choices__list--single .choices__item{padding:.4rem}.choices__list--multiple{display:inline}.choices__list--multiple .choices__item{display:inline-block;border-radius:2rem;padding:.4rem 1rem;font-size:1.2rem;margin-right:.375rem;margin-bottom:.375rem;background-color:#00bcd4;border:1px solid #00a5bb;color:#fff;word-break:break-all}.choices__list--multiple .choices__item.is-selected{background-color:#00a5bb}.is-disabled .choices__list--multiple .choices__item{background-color:#eaeaea;border:1px solid #ddd}.choices__list--dropdown{display:none;z-index:1;position:absolute;width:100%;background-color:#fff;border:1px solid #ddd;top:100%;margin-top:-1px;border-bottom-left-radius:.25rem;border-bottom-right-radius:.25rem;max-height:300px;overflow:auto;will-change:scroll-position}.choices__list--dropdown.is-active{display:block}.choices__list--dropdown .choices__item{padding:1rem;font-size:1.4rem}.choices__list--dropdown .choices__item--selectable.is-highlighted:after,.choices__list--dropdown .choices__item.is-selected{opacity:.5}.choices__list--dropdown .choices__item.is-selected:hover{background-color:#fff}.choices__list--dropdown .choices__item--selectable:after{content:"Press to select";font-size:12px;opacity:0;float:right}.choices__list--dropdown .choices__item--selectable.is-highlighted{background-color:#f2f2f2}.is-open .choices__list--dropdown{border-color:#b7b7b7}.is-flipped .choices__list--dropdown{top:auto;bottom:100%;margin-top:0;margin-bottom:-1px;border-radius:.25rem .25rem 0 0}.choices__item{cursor:default}.choices__item--selectable{cursor:pointer}.choices__item--disabled{cursor:not-allowed;-webkit-user-select:none;-moz-user-select:none;-ms-user-select:none;user-select:none;opacity:.5}.choices__group .choices__heading{font-weight:600;font-size:1.2rem;padding:1rem;border-bottom:1px solid #f7f7f7;color:gray}.choices__input{background-color:#f9f9f9;font-size:1.4rem;padding:0;margin-bottom:.5rem;display:inline-block;vertical-align:baseline;border:0;border-radius:0;max-width:100%;padding:.4rem 0 .4rem .2rem}.choices__input:focus{outline:0}
|
||||
*,:after,:before{box-sizing:border-box}body,html{margin:0;height:100%;widows:100%}html{font-size:62.5%}body{background-color:#333;font-family:"Helvetica Neue",Helvetica,Arial,"Lucida Grande",sans-serif;font-size:1.6rem;color:#fff}label{display:block;margin-bottom:.8rem;font-size:1.4rem;font-weight:500}h1,h2,h3,h4,h5,h6{margin-top:0;margin-bottom:1.2rem;font-weight:500}.container{display:block;margin:auto;max-width:35em;padding:2.4rem}.section{background-color:#fff;padding:2.4rem;color:#333}.choices{margin-bottom:2.4rem;position:relative}.choices.is-disabled .choices__inner,.choices.is-disabled .choices__input{background-color:#eaeaea;cursor:not-allowed;-webkit-user-select:none;-moz-user-select:none;-ms-user-select:none;user-select:none}.choices.is-disabled .choices__item{cursor:not-allowed}.choices[data-type*=select-one].is-open:after{border-color:transparent transparent #333 transparent;margin-top:-7.5px}.choices[data-type*=select-one]:after{content:"";height:0;width:0;border-style:solid;border-color:#333 transparent transparent transparent;border-width:5px;position:absolute;right:1.15rem;top:50%;margin-top:-2.5px}.choices__inner{background-color:#f9f9f9;padding:.75rem .75rem .375rem;border:1px solid #ddd;border-radius:.25rem;font-size:1.4rem;cursor:text;overflow:hidden}.is-focused .choices__inner{border-color:#b7b7b7}.is-open .choices__inner{border-radius:.25rem .25rem 0 0}.is-flipped.is-open .choices__inner{border-radius:0 0 .25rem .25rem}.choices__list{margin:0;padding-left:0;list-style-type:none}.choices__list--single{display:inline-block}.choices__list--single .choices__item{padding:.4rem}.choices__list--multiple{display:inline}.choices__list--multiple .choices__item{display:inline-block;border-radius:2rem;padding:.4rem 1rem;font-size:1.2rem;margin-right:.375rem;margin-bottom:.375rem;background-color:#00bcd4;border:1px solid #00a5bb;color:#fff;word-break:break-all}.choices__list--multiple .choices__item.is-selected{background-color:#00a5bb}.is-disabled .choices__list--multiple .choices__item{background-color:#aaa;border:1px solid #919191}.choices__list--dropdown{display:none;z-index:1;position:absolute;width:100%;background-color:#fff;border:1px solid #ddd;top:100%;margin-top:-1px;border-bottom-left-radius:.25rem;border-bottom-right-radius:.25rem;max-height:300px;overflow:auto;will-change:scroll-position}.choices__list--dropdown.is-active{display:block}.choices__list--dropdown .choices__item{padding:1rem;font-size:1.4rem}.choices__list--dropdown .choices__item--selectable.is-highlighted:after,.choices__list--dropdown .choices__item.is-selected{opacity:.5}.choices__list--dropdown .choices__item.is-selected:hover{background-color:#fff}.choices__list--dropdown .choices__item--selectable:after{content:"Press to select";font-size:12px;opacity:0;float:right}.choices__list--dropdown .choices__item--selectable.is-highlighted{background-color:#f2f2f2}.is-open .choices__list--dropdown{border-color:#b7b7b7}.is-flipped .choices__list--dropdown{top:auto;bottom:100%;margin-top:0;margin-bottom:-1px;border-radius:.25rem .25rem 0 0}.choices__item{cursor:default}.choices__item--selectable{cursor:pointer}.choices__item--disabled{cursor:not-allowed;-webkit-user-select:none;-moz-user-select:none;-ms-user-select:none;user-select:none;opacity:.5}.choices__group .choices__heading{font-weight:600;font-size:1.2rem;padding:1rem;border-bottom:1px solid #f7f7f7;color:gray}.choices__input{background-color:#f9f9f9;font-size:1.4rem;padding:0;margin-bottom:.5rem;display:inline-block;vertical-align:baseline;border:0;border-radius:0;max-width:100%;padding:.4rem 0 .4rem .2rem}.choices__input:focus{outline:0}
|
|
@ -138,8 +138,8 @@ $choices-disabled-color: #eaeaea;
|
|||
word-break: break-all;
|
||||
&.is-selected { background-color: darken($choices-primary-color, 5%); }
|
||||
.is-disabled & {
|
||||
background-color: $choices-disabled-color;
|
||||
border: 1px solid darken($choices-disabled-color, 5%);
|
||||
background-color: darken($choices-disabled-color, 25%);
|
||||
border: 1px solid darken($choices-disabled-color, 35%);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue