Empty dropdown handling + highlighting item improvments

This commit is contained in:
Josh Johnson 2016-04-28 20:49:49 +01:00
parent cbfe38e937
commit 93f69eb3da
5 changed files with 44 additions and 48 deletions

File diff suppressed because one or more lines are too long

View file

@ -269,14 +269,13 @@ export class Choices {
if(this.passedElement.type === 'select-multiple' && hasActiveDropDown) { if(this.passedElement.type === 'select-multiple' && hasActiveDropDown) {
const highlighted = this.dropdown.querySelector(`.${this.options.classNames.highlightedState}`); const highlighted = this.dropdown.querySelector(`.${this.options.classNames.highlightedState}`);
const value = highlighted.getAttribute('data-choice-value');
const label = highlighted.innerHTML;
const id = highlighted.getAttribute('data-choice-id');
if(highlighted) { if(highlighted) {
const value = highlighted.getAttribute('data-choice-value');
const label = highlighted.innerHTML;
const id = highlighted.getAttribute('data-choice-id');
this.addItem(value, label, id); this.addItem(value, label, id);
this.input.value = ""; this.input.value = "";
// this.highlightPosition()
} }
} }
} }
@ -303,15 +302,17 @@ export class Choices {
if(canHighlight) { if(canHighlight) {
const option = selectableOptions[this.highlightPosition]; const option = selectableOptions[this.highlightPosition];
const previousElement = this.dropdown.querySelector(`.${this.options.classNames.highlightedState}`); if(option) {
const currentElement = this.dropdown.querySelector(`[data-choice-id="${option.id}"]`); const previousElement = this.dropdown.querySelector(`.${this.options.classNames.highlightedState}`);
const currentElement = this.dropdown.querySelector(`[data-choice-id="${option.id}"]`);
if(previousElement) { if(previousElement) {
previousElement.classList.remove(this.options.classNames.highlightedState); previousElement.classList.remove(this.options.classNames.highlightedState);
} }
if(currentElement) { if(currentElement) {
currentElement.classList.add(this.options.classNames.highlightedState); currentElement.classList.add(this.options.classNames.highlightedState);
}
} }
} }
} }
@ -531,10 +532,7 @@ export class Choices {
// Generate unique id // Generate unique id
const id = this.store.getState().items.length + 1; const id = this.store.getState().items.length + 1;
// Close dropdown this.store.dispatch(addItem(passedValue, passedLabel, id, passedOptionId));
// if(this.passedElement.type === 'select-multiple' && this.dropdown.classList.contains(this.options.classNames.activeState)) {
// this.toggleDropdown();
// }
// Run callback if it is a function // Run callback if it is a function
if(callback){ if(callback){
@ -544,8 +542,6 @@ export class Choices {
console.error('callbackOnAddItem: Callback is not a function'); console.error('callbackOnAddItem: Callback is not a function');
} }
} }
this.store.dispatch(addItem(passedValue, passedLabel, id, passedOptionId));
} }
/** /**
@ -918,7 +914,6 @@ export class Choices {
* @return * @return
*/ */
render(callback = this.options.callbackOnRender) { render(callback = this.options.callbackOnRender) {
console.log('Rendering');
const classNames = this.options.classNames; const classNames = this.options.classNames;
const activeItems = this.getItemsFilteredByActive(); const activeItems = this.getItemsFilteredByActive();
@ -936,47 +931,51 @@ export class Choices {
// If we have grouped options // If we have grouped options
if(activeGroups.length >= 1) { if(activeGroups.length >= 1) {
activeGroups.forEach((group, index) => { activeGroups.forEach((group, i) => {
// Grab options that are children of this group // 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;
}); });
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>
`); `);
dropdownGroup.appendChild(dropdownItem); groupOptions.forEach((option, j) => {
}); const dropdownItem = strToEl(`
<div class="${ classNames.item } ${ classNames.itemOption } ${ option.selected ? classNames.selectedState + ' ' + classNames.itemDisabled : classNames.itemSelectable } ${ i === 0 && j === 0 ? classNames.highlightedState : ''}" data-choice-option data-choice-id="${ option.id }" data-choice-value="${ option.value }">
${ option.label }
</div>
`);
optionListFragment.appendChild(dropdownGroup); dropdownGroup.appendChild(dropdownItem);
});
optionListFragment.appendChild(dropdownGroup);
}
}); });
} else if(activeOptions.length >= 1) { } else if(activeOptions.length >= 1) {
activeOptions.forEach((option) => { activeOptions.forEach((option, i) => {
const dropdownItem = strToEl(` 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 }"> <div class="${ classNames.item } ${ classNames.itemOption } ${ option.selected ? classNames.selectedState + ' ' + classNames.itemDisabled : classNames.itemSelectable } ${ i === 0 ? classNames.highlightedState : ''}" data-choice-option data-choice-id="${ option.id }" data-choice-value="${ option.value }">
${ option.label } ${ option.label }
</div> </div>
`); `);
optionListFragment.appendChild(dropdownItem); optionListFragment.appendChild(dropdownItem);
}); });
} else {
// Show 'no options' message
const dropdownItem = strToEl(`<div class="${ classNames.item }">No options to select</div>`);
optionListFragment.appendChild(dropdownItem);
} }
this.dropdown.appendChild(optionListFragment); this.dropdown.appendChild(optionListFragment);
// If dropdown is empty, show a no content message
if(this.dropdown.innerHTML === "") {
const dropdownItem = strToEl(`<div class="${ classNames.item }">No options to select</div>`);
optionListFragment.appendChild(dropdownItem);
this.dropdown.appendChild(optionListFragment);
}
} }
// ITEMS // ITEMS

View file

@ -97,9 +97,7 @@ h1, h2, h3, h4, h5, h6 {
opacity: .5; } opacity: .5; }
.choices__list--dropdown .choices__item.is-selected:hover { .choices__list--dropdown .choices__item.is-selected:hover {
background-color: #FFFFFF; } background-color: #FFFFFF; }
.choices__list--dropdown .choices__item.is-highlighted { .choices__list--dropdown .choices__item--selectable:hover, .choices__list--dropdown .choices__item--selectable.is-highlighted {
color: #00BCD4; }
.choices__list--dropdown .choices__item--selectable:hover {
background-color: #f9f9f9; } background-color: #f9f9f9; }
.choices__list--dropdown.is-active { .choices__list--dropdown.is-active {
display: block; } display: block; }

View file

@ -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}.is-active .choices__inner{border-color:#b7b7b7}.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:#b7b7b7}.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.is-highlighted{color:#00bcd4}.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:#b7b7b7}.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:#b7b7b7}.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.is-highlighted,.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}

View file

@ -112,10 +112,9 @@ h1, h2, h3, h4, h5, h6 {
opacity: .5; opacity: .5;
&:hover { background-color: #FFFFFF; } &:hover { background-color: #FFFFFF; }
} }
&.is-highlighted { color: #00BCD4; }
} }
.choices__item--selectable { .choices__item--selectable {
&:hover { background-color: mix(#000000, #FFFFFF, 2.5%); } &:hover, &.is-highlighted { background-color: mix(#000000, #FFFFFF, 2.5%); }
} }
&.is-active { display: block; } &.is-active { display: block; }