mirror of
https://github.com/Choices-js/Choices.git
synced 2024-05-20 22:46:33 +02:00
Active state handling plus positioning of dropdown
This commit is contained in:
parent
5ab0075908
commit
7774e8bb0d
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
|
@ -71,6 +71,7 @@ export class Choices {
|
|||
activeState: 'is-active',
|
||||
disabledState: 'is-disabled',
|
||||
hiddenState: 'is-hidden',
|
||||
flippedState: 'is-flipped',
|
||||
selectedState: 'is-selected'
|
||||
},
|
||||
callbackOnInit: function() {},
|
||||
|
@ -103,6 +104,9 @@ export class Choices {
|
|||
this.init = this.init.bind(this);
|
||||
this.render = this.render.bind(this);
|
||||
this.destroy = this.destroy.bind(this);
|
||||
|
||||
this.onFocus = this.onFocus.bind(this);
|
||||
this.onBlur = this.onBlur.bind(this);
|
||||
this.onKeyDown = this.onKeyDown.bind(this);
|
||||
this.onClick = this.onClick.bind(this);
|
||||
this.onPaste = this.onPaste.bind(this);
|
||||
|
@ -277,12 +281,14 @@ export class Choices {
|
|||
}
|
||||
|
||||
if(e.target.hasAttribute('data-choice-item')) {
|
||||
// If we are clicking on an item
|
||||
const activeItems = this.getItemsFilteredByActive();
|
||||
const target = e.target;
|
||||
|
||||
this.handleClick(activeItems, target, shiftKey);
|
||||
|
||||
} else if(e.target.hasAttribute('data-choice-selectable')) {
|
||||
} else if(e.target.hasAttribute('data-choice-option')) {
|
||||
// If we are clicking on an option
|
||||
const options = this.getOptions();
|
||||
const id = e.target.getAttribute('data-choice-id');
|
||||
const option = options.find((option) => {
|
||||
|
@ -308,11 +314,20 @@ export class Choices {
|
|||
}
|
||||
|
||||
onPaste(e) {
|
||||
// Disable pasting into the input if option has been set
|
||||
if(!this.options.allowPaste) {
|
||||
e.preventDefault();
|
||||
}
|
||||
}
|
||||
|
||||
onFocus(e) {
|
||||
this.containerOuter.classList.add('is-active');
|
||||
}
|
||||
|
||||
onBlur(e) {
|
||||
this.containerOuter.classList.remove('is-active');
|
||||
}
|
||||
|
||||
/* Methods */
|
||||
|
||||
/**
|
||||
|
@ -508,15 +523,36 @@ export class Choices {
|
|||
});
|
||||
}
|
||||
|
||||
toggleDropdown() {
|
||||
if(!this.dropdown) {
|
||||
console.error('No dropdown set');
|
||||
return;
|
||||
showDropdown() {
|
||||
this.dropdown.classList.add('is-active');
|
||||
|
||||
const dimensions = this.dropdown.getBoundingClientRect();
|
||||
if(dimensions.top + dimensions.height >= document.body.offsetHeight) {
|
||||
this.dropdown.classList.add('is-flipped');
|
||||
} else {
|
||||
this.dropdown.classList.remove('is-flipped');
|
||||
}
|
||||
}
|
||||
|
||||
hideDropdown() {
|
||||
const isFlipped = this.dropdown.classList.contains('is-flipped');
|
||||
|
||||
this.dropdown.classList.remove('is-active');
|
||||
if(isFlipped) {
|
||||
this.dropdown.classList.remove('is-flipped');
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
toggleDropdown() {
|
||||
if(!this.dropdown) return;
|
||||
const isActive = this.dropdown.classList.contains('is-active');
|
||||
|
||||
this.dropdown.classList[isActive ? 'remove' : 'add']('is-active');
|
||||
if(isActive) {
|
||||
this.hideDropdown();
|
||||
} else {
|
||||
this.showDropdown();
|
||||
}
|
||||
}
|
||||
|
||||
addOption(option, groupId = -1) {
|
||||
|
@ -723,6 +759,9 @@ export class Choices {
|
|||
document.addEventListener('keydown', this.onKeyDown);
|
||||
document.addEventListener('click', this.onClick);
|
||||
document.addEventListener('paste', this.onPaste);
|
||||
|
||||
this.input.addEventListener('focus', this.onFocus);
|
||||
this.input.addEventListener('blur', this.onBlur);
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -732,6 +771,9 @@ export class Choices {
|
|||
document.removeEventListener('keydown', this.onKeyDown);
|
||||
document.removeEventListener('click', this.onClick);
|
||||
document.removeEventListener('paste', this.onPaste);
|
||||
|
||||
this.input.removeEventListener('focus', this.onFocus);
|
||||
this.input.removeEventListener('blur', this.onBlur);
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -769,7 +811,7 @@ export class Choices {
|
|||
if(childOptions) {
|
||||
childOptions.forEach((option) => {
|
||||
const dropdownItem = strToEl(`
|
||||
<div class="${ classNames.item } ${ classNames.itemOption } ${ option.selected ? classNames.selectedState + ' ' + classNames.itemDisabled : classNames.itemSelectable }" data-choice-selectable data-choice-id="${ option.id }" data-choice-value="${ option.value }">
|
||||
<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>
|
||||
`);
|
||||
|
@ -787,7 +829,7 @@ export class Choices {
|
|||
if(options) {
|
||||
options.forEach((option) => {
|
||||
const dropdownItem = strToEl(`
|
||||
<div class="${ classNames.item } ${ classNames.itemOption } ${ option.selected ? classNames.selectedState + ' ' + classNames.itemDisabled : classNames.itemSelectable }" data-choice-selectable data-choice-id="${ option.id }" data-choice-value="${ option.value }">
|
||||
<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>
|
||||
`);
|
||||
|
|
|
@ -48,6 +48,8 @@ h1, h2, h3, h4, h5, h6 {
|
|||
border-radius: .25rem;
|
||||
font-size: 1.4rem;
|
||||
cursor: text; }
|
||||
.is-active .choices__inner {
|
||||
border-color: #c4c4c4; }
|
||||
.choices__inner:focus {
|
||||
outline: 1px solid #00BCD4;
|
||||
outline-offset: -1px; }
|
||||
|
@ -83,7 +85,11 @@ h1, h2, h3, h4, h5, h6 {
|
|||
margin-top: -1px;
|
||||
display: none;
|
||||
border-bottom-left-radius: .25rem;
|
||||
border-bottom-right-radius: .25rem; }
|
||||
border-bottom-right-radius: .25rem;
|
||||
max-height: 300px;
|
||||
overflow: auto; }
|
||||
.is-active .choices__list--dropdown {
|
||||
border-color: #c4c4c4; }
|
||||
.choices__list--dropdown .choices__item {
|
||||
padding: 1rem;
|
||||
font-size: 1.4rem; }
|
||||
|
|
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;cursor:text}.choices__inner:focus{outline:1px solid #00bcd4;outline-offset:-1px}.choices__list{margin:0;padding-left:0;list-style-type:none}.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;word-break:break-all}.choices__list--items .choices__item.is-selected{background-color:#00a5bb}.choices__list--dropdown{z-index:1;position:absolute;width:100%;background-color:#fff;border:1px solid #ddd;top:100%;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__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:hover{background-color:#f9f9f9}.choices__list--dropdown.is-active{display:block}.choices__list--dropdown.is-flipped{top:auto;bottom:100%;margin-top:0;margin-bottom:-1px;border-bottom-left-radius:0;border-bottom-right-radius:0;border-top-left-radius:.25rem;border-top-right-radius:.25rem}.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}.choices__group .choices__heading{font-size:1.2rem;padding:1rem;border-bottom:1px solid #eaeaea;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__inner{background-color:#f9f9f9;padding:.75rem .75rem .375rem;border:1px solid #ddd;border-radius:.25rem;font-size:1.4rem;cursor:text}.is-active .choices__inner{border-color:#c4c4c4}.choices__inner:focus{outline:1px solid #00bcd4;outline-offset:-1px}.choices__list{margin:0;padding-left:0;list-style-type:none}.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;word-break:break-all}.choices__list--items .choices__item.is-selected{background-color:#00a5bb}.choices__list--dropdown{z-index:1;position:absolute;width:100%;background-color:#fff;border:1px solid #ddd;top:100%;margin-top:-1px;display:none;border-bottom-left-radius:.25rem;border-bottom-right-radius:.25rem;max-height:300px;overflow:auto}.is-active .choices__list--dropdown{border-color:#c4c4c4}.choices__list--dropdown .choices__item{padding:1rem;font-size:1.4rem}.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:hover{background-color:#f9f9f9}.choices__list--dropdown.is-active{display:block}.choices__list--dropdown.is-flipped{top:auto;bottom:100%;margin-top:0;margin-bottom:-1px;border-bottom-left-radius:0;border-bottom-right-radius:0;border-top-left-radius:.25rem;border-top-right-radius:.25rem}.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}.choices__group .choices__heading{font-size:1.2rem;padding:1rem;border-bottom:1px solid #eaeaea;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}
|
|
@ -59,6 +59,7 @@ h1, h2, h3, h4, h5, h6 {
|
|||
border-radius: .25rem;
|
||||
font-size: 1.4rem;
|
||||
cursor: text;
|
||||
.is-active & { border-color: darken(#DDDDDD, 10%); }
|
||||
&:focus {
|
||||
outline: 1px solid #00BCD4;
|
||||
outline-offset: -1px;
|
||||
|
@ -101,6 +102,9 @@ h1, h2, h3, h4, h5, h6 {
|
|||
display: none;
|
||||
border-bottom-left-radius: .25rem;
|
||||
border-bottom-right-radius: .25rem;
|
||||
max-height: 300px;
|
||||
overflow: auto;
|
||||
.is-active & { border-color: darken(#DDDDDD, 10%); }
|
||||
.choices__item {
|
||||
padding: 1rem;
|
||||
font-size: 1.4rem;
|
||||
|
|
|
@ -22,7 +22,7 @@
|
|||
<label for="choices-4">Text input that disables adding items</label>
|
||||
<input id="choices-4" type="text" value="josh@joshuajohnson.co.uk, joe@bloggs.co.uk" placeholder="This is a placeholder">
|
||||
|
||||
<label for="choices-5">Text input that prepends and appends a value to each item</label>
|
||||
<label for="choices-5">Text input that prepends and appends a value to each items return value</label>
|
||||
<input id="choices-5" type="text" value="preset-1, preset-2" placeholder="This is a placeholder">
|
||||
|
||||
<label for="choices-6">Text input with preset values passed through options</label>
|
||||
|
|
Loading…
Reference in a new issue