mirror of
https://github.com/Choices-js/Choices.git
synced 2024-05-21 15:06:45 +02:00
Search within dropdown on single search boxes
This commit is contained in:
parent
e2ff2d8f97
commit
110741aed3
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
|
@ -11,7 +11,6 @@ import Store from './store/index.js';
|
|||
* To do:
|
||||
* - Pagination
|
||||
* - Single select box search in dropdown
|
||||
* - Restoring input on .destroy()
|
||||
*/
|
||||
export class Choices {
|
||||
constructor(element = '[data-choice]', userOptions = {}) {
|
||||
|
@ -142,7 +141,7 @@ export class Choices {
|
|||
let canUpdate = true;
|
||||
|
||||
if(this.options.addItems) {
|
||||
if (this.options.maxItems && this.options.maxItems <= this.list.children.length) {
|
||||
if (this.options.maxItems && this.options.maxItems <= this.itemList.children.length) {
|
||||
// If there is a max entry limit and we have reached that limit
|
||||
// don't update
|
||||
canUpdate = false;
|
||||
|
@ -218,7 +217,7 @@ export class Choices {
|
|||
|
||||
const hasFocusedInput = this.input === document.activeElement;
|
||||
const hasActiveDropdown = this.dropdown.classList.contains(this.options.classNames.activeState);
|
||||
const hasItems = this.list && this.list.children;
|
||||
const hasItems = this.itemList && this.itemList.children;
|
||||
const keyString = String.fromCharCode(event.keyCode);
|
||||
|
||||
// If a user is typing and the dropdown is not active
|
||||
|
@ -231,7 +230,7 @@ export class Choices {
|
|||
// If CTRL + A or CMD + A have been pressed and there are items to select
|
||||
if(ctrlDownKey && hasItems) {
|
||||
if(this.options.removeItems && !this.input.value && this.options.selectAll && this.input === document.activeElement) {
|
||||
this.selectAll(this.list.children);
|
||||
this.selectAll(this.itemList.children);
|
||||
}
|
||||
}
|
||||
break;
|
||||
|
@ -321,7 +320,7 @@ export class Choices {
|
|||
const activeItems = this.store.getItemsFilteredByActive();
|
||||
const isUnique = !activeItems.some((item) => item.value === this.input.value);
|
||||
|
||||
if (this.options.maxItems && this.options.maxItems <= this.list.children.length) {
|
||||
if (this.options.maxItems && this.options.maxItems <= this.itemList.children.length) {
|
||||
dropdownItem = this.getTemplate('notice', `Only ${ this.options.maxItems } options can be added.`);
|
||||
} else if(!this.options.allowDuplicates && !isUnique) {
|
||||
dropdownItem = this.getTemplate('notice', `Only unique values can be added.`);
|
||||
|
@ -378,9 +377,10 @@ export class Choices {
|
|||
}
|
||||
|
||||
onInput(e) {
|
||||
if(this.passedElement.type !== 'select-one') {
|
||||
this.input.style.width = getWidthOfInput(this.input);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* Click event
|
||||
|
@ -526,8 +526,10 @@ export class Choices {
|
|||
*/
|
||||
clearInput() {
|
||||
if (this.input.value) this.input.value = '';
|
||||
if(this.passedElement.type !== 'select-one') {
|
||||
this.input.style.width = getWidthOfInput(this.input);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Tests value against a regular expression
|
||||
|
@ -952,9 +954,12 @@ export class Choices {
|
|||
containerInner: () => {
|
||||
return strToEl(`<div class="${ classNames.containerInner }"></div>`);
|
||||
},
|
||||
list: () => {
|
||||
itemList: () => {
|
||||
return strToEl(`<div class="${ classNames.list } ${ this.passedElement.type === 'select-one' ? classNames.listSingle : classNames.listItems }"></div>`);
|
||||
},
|
||||
optionList: () => {
|
||||
return strToEl(`<div class="${ classNames.list }"></div>`);
|
||||
},
|
||||
input: () => {
|
||||
return strToEl(`<input type="text" class="${ classNames.input } ${ classNames.inputCloned }">`);
|
||||
},
|
||||
|
@ -1009,14 +1014,16 @@ export class Choices {
|
|||
generateInput() {
|
||||
const containerOuter = this.getTemplate('containerOuter');
|
||||
const containerInner = this.getTemplate('containerInner');
|
||||
const list = this.getTemplate('list');
|
||||
const itemList = this.getTemplate('itemList');
|
||||
const optionList = this.getTemplate('optionList');
|
||||
const input = this.getTemplate('input');
|
||||
const dropdown = this.getTemplate('dropdown');
|
||||
|
||||
this.containerOuter = containerOuter;
|
||||
this.containerInner = containerInner;
|
||||
this.input = input;
|
||||
this.list = list;
|
||||
this.optionList = optionList;
|
||||
this.itemList = itemList;
|
||||
this.dropdown = dropdown;
|
||||
|
||||
// Hide passed input
|
||||
|
@ -1041,14 +1048,18 @@ export class Choices {
|
|||
}
|
||||
}
|
||||
|
||||
if(!this.options.addItems) {
|
||||
this.disable();
|
||||
}
|
||||
if(!this.options.addItems) this.disable();
|
||||
|
||||
containerOuter.appendChild(containerInner);
|
||||
containerOuter.appendChild(dropdown);
|
||||
containerInner.appendChild(list);
|
||||
containerInner.appendChild(itemList);
|
||||
dropdown.appendChild(optionList);
|
||||
|
||||
if(this.passedElement.type === 'select-multiple' || this.passedElement.type === 'text') {
|
||||
containerInner.appendChild(input);
|
||||
} else {
|
||||
dropdown.insertBefore(input, dropdown.firstChild);
|
||||
}
|
||||
|
||||
if(this.passedElement.type === 'select-multiple' || this.passedElement.type === 'select-one') {
|
||||
this.highlightPosition = 0;
|
||||
|
@ -1098,6 +1109,8 @@ export class Choices {
|
|||
this.renderOptions(groupOptions, groupFragment);
|
||||
}
|
||||
});
|
||||
|
||||
return groupFragment;
|
||||
}
|
||||
|
||||
renderOptions(options, fragment) {
|
||||
|
@ -1113,6 +1126,8 @@ export class Choices {
|
|||
optsFragment.appendChild(dropdownItem);
|
||||
}
|
||||
});
|
||||
|
||||
return optsFragment;
|
||||
}
|
||||
|
||||
renderItems(items, fragment) {
|
||||
|
@ -1150,10 +1165,10 @@ export class Choices {
|
|||
});
|
||||
|
||||
// Clear list
|
||||
this.list.innerHTML = '';
|
||||
this.itemList.innerHTML = '';
|
||||
|
||||
// Update list
|
||||
this.list.appendChild(itemListFragment);
|
||||
this.itemList.appendChild(itemListFragment);
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -1175,7 +1190,7 @@ export class Choices {
|
|||
const optListFragment = document.createDocumentFragment();
|
||||
|
||||
// Clear options
|
||||
this.dropdown.innerHTML = '';
|
||||
this.optionList.innerHTML = '';
|
||||
|
||||
// If we have grouped options
|
||||
if(activeGroups.length >= 1 && this.isSearching !== true) {
|
||||
|
@ -1187,13 +1202,13 @@ export class Choices {
|
|||
if(optListFragment.children.length) {
|
||||
// If we actually have anything to add to our dropdown
|
||||
// append it and highlight the first option
|
||||
this.dropdown.appendChild(optListFragment);
|
||||
this.optionList.appendChild(optListFragment);
|
||||
this.highlightOption();
|
||||
} else {
|
||||
// Otherwise show a notice
|
||||
const dropdownItem = this.isSearching ? this.getTemplate('notice', 'No results found') : this.getTemplate('notice', 'No options to select');
|
||||
|
||||
this.dropdown.appendChild(dropdownItem);
|
||||
this.optionList.appendChild(dropdownItem);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -64,6 +64,15 @@ h1, h2, h3, h4, h5, h6 {
|
|||
.choices.is-disabled .choices__item {
|
||||
cursor: not-allowed; }
|
||||
|
||||
.choices[data-type*="select-one"] .choices__inner {
|
||||
padding-bottom: .75rem; }
|
||||
|
||||
.choices[data-type*="select-one"] .choices__input {
|
||||
display: block;
|
||||
width: 100%;
|
||||
padding: 1rem;
|
||||
margin: 0; }
|
||||
|
||||
.choices[data-type*="select-one"].is-open:after {
|
||||
border-color: transparent transparent #333333 transparent;
|
||||
margin-top: -7.5px; }
|
||||
|
|
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}hr,label{display:block}label{margin-bottom:.8rem;font-size:1.4rem;font-weight:500}hr{margin:3.6rem 0;border:0;border-bottom:1px solid #eaeaea;height:1px}h1,h2,h3,h4,h5,h6{margin-top:0;margin-bottom:1.2rem;font-weight:400}.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 #008fa1;color:#fff;word-break:break-all}.choices__list--multiple .choices__item[data-deletable]{padding-right:.5rem}.choices__list--multiple .choices__item.is-selected{background-color:#00a5bb;border:1px solid #007888}.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__button{text-indent:-9999px;-webkit-appearance:none;-moz-appearance:none;appearance:none;border:0;background-color:transparent;background-image:url(../../icons/cross.svg);background-repeat:no-repeat;background-position:center;background-size:8px;border-left:1px solid #008fa1;margin-left:4px;padding-left:6px;padding-right:6px;line-height:1;cursor:pointer}.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}hr,label{display:block}label{margin-bottom:.8rem;font-size:1.4rem;font-weight:500}hr{margin:3.6rem 0;border:0;border-bottom:1px solid #eaeaea;height:1px}h1,h2,h3,h4,h5,h6{margin-top:0;margin-bottom:1.2rem;font-weight:400}.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] .choices__inner{padding-bottom:.75rem}.choices[data-type*=select-one] .choices__input{display:block;width:100%;padding:1rem;margin:0}.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 #008fa1;color:#fff;word-break:break-all}.choices__list--multiple .choices__item[data-deletable]{padding-right:.5rem}.choices__list--multiple .choices__item.is-selected{background-color:#00a5bb;border:1px solid #007888}.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__button{text-indent:-9999px;-webkit-appearance:none;-moz-appearance:none;appearance:none;border:0;background-color:transparent;background-image:url(../../icons/cross.svg);background-repeat:no-repeat;background-position:center;background-size:8px;border-left:1px solid #008fa1;margin-left:4px;padding-left:6px;padding-right:6px;line-height:1;cursor:pointer}.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}
|
|
@ -90,6 +90,13 @@ $choices-button-icon-path: '../../icons/cross.svg';
|
|||
}
|
||||
|
||||
.choices[data-type*="select-one"] {
|
||||
.choices__inner { padding-bottom: .75rem; }
|
||||
.choices__input {
|
||||
display: block;
|
||||
width: 100%;
|
||||
padding: 1rem;
|
||||
margin: 0;
|
||||
}
|
||||
&.is-open:after {
|
||||
border-color: transparent transparent $choices-text-color transparent;
|
||||
margin-top: -7.5px;
|
||||
|
|
Loading…
Reference in a new issue