mirror of
https://github.com/Choices-js/Choices.git
synced 2024-05-16 20:46:35 +02:00
Toggling dropdown menu
This commit is contained in:
parent
0c9238b72c
commit
4b889f9e4d
2
assets/scripts/dist/bundle.js
vendored
2
assets/scripts/dist/bundle.js
vendored
File diff suppressed because one or more lines are too long
|
@ -61,6 +61,7 @@ export class Choices {
|
|||
this.onKeyDown = this.onKeyDown.bind(this);
|
||||
this.onClick = this.onClick.bind(this);
|
||||
this.onFocus = this.onFocus.bind(this);
|
||||
this.onBlur = this.onBlur.bind(this);
|
||||
|
||||
// Let's have it large
|
||||
this.init();
|
||||
|
@ -225,8 +226,14 @@ export class Choices {
|
|||
}
|
||||
|
||||
onFocus(e) {
|
||||
if(this.passedInput.type === 'select-multiple') {
|
||||
console.log(e.target);
|
||||
if(this.dropdown) {
|
||||
this.toggleDropdown();
|
||||
}
|
||||
}
|
||||
|
||||
onBlur(e) {
|
||||
if(this.dropdown) {
|
||||
this.toggleDropdown();
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -393,6 +400,22 @@ export class Choices {
|
|||
}
|
||||
};
|
||||
}
|
||||
|
||||
toggleDropdown() {
|
||||
if(!this.dropdown) {
|
||||
console.error('No dropdown set');
|
||||
return;
|
||||
}
|
||||
|
||||
const isActive = this.dropdown.classList.contains('is-active');
|
||||
|
||||
this.dropdown.classList[isActive ? 'remove' : 'add']('is-active');
|
||||
}
|
||||
|
||||
addItemToDropdown(value) {
|
||||
const dropdownItem = strToEl(`<li class="choices__item choices__item--selectable" data-choice-selectable data-choice-value="${value}">${value}</li>`);
|
||||
this.dropdown.appendChild(dropdownItem);
|
||||
}
|
||||
|
||||
/* Rendering */
|
||||
|
||||
|
@ -523,6 +546,12 @@ export class Choices {
|
|||
this.addItem(value);
|
||||
});
|
||||
|
||||
const unselectedOptions = this.passedInput.options;
|
||||
for (var i = 0; i < unselectedOptions.length; i++) {
|
||||
let option = unselectedOptions[i];
|
||||
this.addItemToDropdown(option.value);
|
||||
}
|
||||
|
||||
// Subscribe to store
|
||||
this.store.subscribe(this.render);
|
||||
|
||||
|
@ -537,12 +566,14 @@ export class Choices {
|
|||
document.addEventListener('keydown', this.onKeyDown);
|
||||
this.list.addEventListener('click', this.onClick);
|
||||
this.input.addEventListener('focus', this.onFocus);
|
||||
this.input.addEventListener('blur', this.onBlur);
|
||||
}
|
||||
|
||||
removeEventListeners() {
|
||||
document.removeEventListener('keydown', this.onKeyDown);
|
||||
this.list.removeEventListener('click', this.onClick);
|
||||
this.input.removeEventListener('focus', this.onFocus);
|
||||
this.input.removeEventListener('blur', this.onBlur);
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
|
@ -89,7 +89,7 @@ h1, h2, h3, h4, h5, h6 {
|
|||
font-size: 1.4rem; }
|
||||
.choices__list--dropdown .choices__item:hover {
|
||||
background-color: #f9f9f9; }
|
||||
.choices__list--dropdown .is-active {
|
||||
.choices__list--dropdown.is-active {
|
||||
display: block; }
|
||||
|
||||
.choices__input {
|
||||
|
|
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__inner{background-color:#f9f9f9;padding:.75rem .75rem .375rem;border:1px solid #ddd;border-radius:.25rem;font-size:1.4rem}.choices__inner:focus{outline:1px solid #00bcd4;outline-offset:-1px}.choices__list{margin:0;padding-left:0;list-style-type:none}.choices__item--selectable{cursor:pointer}.choices__list--items{display:inline}.choices__list--items .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 #00b1c7;color:#fff;cursor:pointer}.choices__list--items .choices__item.is-selected{background-color:#00a5bb}.choices__list--dropdown{position:absolute;width:100%;background-color:#fff;border:1px solid #ddd;margin-top:-1px;display:none;border-bottom-left-radius:.25rem;border-bottom-right-radius:.25rem}.choices__list--dropdown .choices__item{padding:1rem;font-size:1.4rem}.choices__input,.choices__list--dropdown .choices__item:hover{background-color:#f9f9f9}.choices__list--dropdown .is-active{display:block}.choices__input{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__inner{background-color:#f9f9f9;padding:.75rem .75rem .375rem;border:1px solid #ddd;border-radius:.25rem;font-size:1.4rem}.choices__inner:focus{outline:1px solid #00bcd4;outline-offset:-1px}.choices__list{margin:0;padding-left:0;list-style-type:none}.choices__item--selectable{cursor:pointer}.choices__list--items{display:inline}.choices__list--items .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 #00b1c7;color:#fff;cursor:pointer}.choices__list--items .choices__item.is-selected{background-color:#00a5bb}.choices__list--dropdown{position:absolute;width:100%;background-color:#fff;border:1px solid #ddd;margin-top:-1px;display:none;border-bottom-left-radius:.25rem;border-bottom-right-radius:.25rem}.choices__list--dropdown .choices__item{padding:1rem;font-size:1.4rem}.choices__input,.choices__list--dropdown .choices__item:hover{background-color:#f9f9f9}.choices__list--dropdown.is-active{display:block}.choices__input{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}
|
|
@ -108,7 +108,7 @@ h1, h2, h3, h4, h5, h6 {
|
|||
background-color: mix(#000000, #FFFFFF, 2.5%);
|
||||
}
|
||||
}
|
||||
.is-active { display: block; }
|
||||
&.is-active { display: block; }
|
||||
}
|
||||
|
||||
.choices__input {
|
||||
|
|
|
@ -36,7 +36,7 @@
|
|||
<option value="Dropdown item 3">Dropdown item 3</option>
|
||||
</select>
|
||||
|
||||
|
||||
<!--
|
||||
<div class="choices choices--active">
|
||||
<div class="choices__inner">
|
||||
<input id="1" type="text" data-choice="" value="preset-1 preset-2" class="choices__input choices__input--hidden" tabindex="-1" style="display:none;" aria-hidden="true">
|
||||
|
@ -51,7 +51,7 @@
|
|||
<li class="choices__item choices__item--selectable" data-choice-selectable data-choice-value="Dropdown item 2">Dropdown item 2</li>
|
||||
<li class="choices__item choices__item--selectable" data-choice-selectable data-choice-value="Dropdown item 3">Dropdown item 3</li>
|
||||
</ul>
|
||||
</div>
|
||||
</div> -->
|
||||
|
||||
</div>
|
||||
</div>
|
||||
|
|
Loading…
Reference in a new issue