Highlighting options + traversing through option els

This commit is contained in:
Josh Johnson 2016-05-02 13:22:53 +01:00
parent 336ffc9e87
commit 56cd08d81c
6 changed files with 60 additions and 36 deletions

File diff suppressed because one or more lines are too long

View file

@ -3,7 +3,7 @@
import { createStore } from 'redux';
import rootReducer from './reducers/index.js';
import { addItem, removeItem, selectItem, addOption, filterOptions, activateOptions, addGroup } from './actions/index';
import { wrap, isType, strToEl, extend, getWidthOfInput, debounce } from './lib/utils.js';
import { getAdjacentEl, findAncestor, wrap, isType, strToEl, extend, getWidthOfInput, debounce } from './lib/utils.js';
import Sifter from 'sifter';
/**
@ -275,32 +275,25 @@ export class Choices {
case upKey:
// If up or down key is pressed, traverse through options
if(this.passedElement.type === 'select-multiple' && hasActiveDropdown) {
const selectableOptions = activeOptions.filter((option) => {
return !option.selected;
});
let canHighlight = true;
const currentEl = this.dropdown.querySelector(`.${this.options.classNames.highlightedState}`);
let nextEl;
if(e.keyCode === downKey) {
this.highlightPosition < (selectableOptions.length - 1) ? this.highlightPosition++ : canHighlight = false;
} else if(e.keyCode === upKey) {
this.highlightPosition > 0 ? this.highlightPosition-- : canHighlight = false;
}
if(canHighlight) {
const option = selectableOptions[this.highlightPosition];
if(option) {
const previousElement = this.dropdown.querySelector(`.${this.options.classNames.highlightedState}`);
const currentElement = this.dropdown.querySelector(`[data-choice-id="${option.id}"]`);
if(previousElement) {
previousElement.classList.remove(this.options.classNames.highlightedState);
}
if(currentElement) {
currentElement.classList.add(this.options.classNames.highlightedState);
}
if(currentEl) {
if(e.keyCode === downKey) {
nextEl = getAdjacentEl(currentEl, '[data-choice-option]', 1);
} else if(e.keyCode === upKey) {
nextEl = getAdjacentEl(currentEl, '[data-choice-option]', -1);
}
} else {
nextEl = this.dropdown.querySelector('[data-choice-option]');
}
if(nextEl) {
if(currentEl) {
currentEl.classList.remove(this.options.classNames.highlightedState);
}
nextEl.classList.add(this.options.classNames.highlightedState);
}
}
break
@ -342,6 +335,7 @@ export class Choices {
sort: [{field: 'value', direction: 'asc'}],
limit: 10
});
this.store.dispatch(filterOptions(results));
}, 100)
@ -427,8 +421,17 @@ export class Choices {
* @return
*/
onMouseOver(e) {
if(this.dropdown && e.target === this.dropdown) {
console.log('hover');
// If we have a dropdown and it is either the target or one of its children is the target
if(this.dropdown && (e.target === this.dropdown || findAncestor(e.target, this.options.classNames.listDropdown))) {
if(e.target.hasAttribute('data-choice-option')) {
const highlightedOptions = this.dropdown.querySelectorAll(`.${this.options.classNames.highlightedState}`);
// Remove any highlighted options
Array.from(highlightedOptions).forEach((element) => {
element.classList.remove(this.options.classNames.highlightedState);
});
e.target.classList.add(this.options.classNames.highlightedState);
}
}
}
@ -921,7 +924,6 @@ export class Choices {
render(callback = this.options.callbackOnRender) {
const classNames = this.options.classNames;
const activeItems = this.getItemsFilteredByActive();
// OPTIONS
if(this.passedElement.type === 'select-multiple') {
@ -947,7 +949,6 @@ export class Choices {
groupOptions.forEach((option, j) => {
const dropdownItem = this.createTemplate('option', option);
dropdownGroup.appendChild(dropdownItem);
});
@ -969,6 +970,10 @@ export class Choices {
optionListFragment.appendChild(dropdownItem);
this.dropdown.appendChild(optionListFragment);
} else {
// Highlight first element in dropdown
const firstEl = this.dropdown.querySelector('[data-choice-option]');
firstEl.classList.add(this.options.classNames.highlightedState);
}
}

View file

@ -273,7 +273,7 @@ export const getElemDistance = function(el) {
/**
* Determine element height multiplied by any offsets
* @private
* @param {Node} el Element to test for
* @param {HTMLElement} el Element to test for
* @return {Number} Height of element
*/
export const getElementOffset = function(el, offset) {
@ -284,6 +284,25 @@ export const getElementOffset = function(el, offset) {
return Math.max(el.offsetHeight*elOffset);
};
/**
* Get the next or previous element from a given start point
* @param {HTMLElement} startEl Element to start position from
* @param {String} className The class we will look through
* @param {Number} direction Positive next element, negative previous element
* @return {[HTMLElement} Found element
*/
export const getAdjacentEl = (startEl, className, direction = 1) => {
if(!startEl || !className) return;
const parent = startEl.parentNode.parentNode;
const children = Array.from(parent.querySelectorAll(className));
const startPos = children.indexOf(startEl);
const operatorDirection = direction > 0 ? 1 : -1;
return children[startPos + operatorDirection];
};
/**
* Get scroll position based on top/bottom position
* @private
@ -301,7 +320,7 @@ export const getScrollPosition = function(position) {
/**
* Determine whether an element is within the viewport
* @param {Node} el Element to test for
* @param {HTMLElement} el Element to test for
* @return {String} Position of scroll
* @return {Boolean}
*/
@ -353,7 +372,7 @@ export const getRandomNumber = function(min, max) {
/**
* Turn a string into a node
* @param {String} String to convert
* @return {Node} Converted node element
* @return {HTMLElement} Converted node element
*/
export const strToEl = (function() {
var tmpEl = document.createElement('div');

View file

@ -97,7 +97,7 @@ h1, h2, h3, h4, h5, h6 {
opacity: .5; }
.choices__list--dropdown .choices__item.is-selected:hover {
background-color: #FFFFFF; }
.choices__list--dropdown .choices__item--selectable:hover, .choices__list--dropdown .choices__item--selectable.is-highlighted {
.choices__list--dropdown .choices__item--selectable.is-highlighted {
background-color: #f9f9f9; }
.choices__list--dropdown.is-active {
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--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}
*,: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{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

@ -114,7 +114,7 @@ h1, h2, h3, h4, h5, h6 {
}
}
.choices__item--selectable {
&:hover, &.is-highlighted { background-color: mix(#000000, #FFFFFF, 2.5%); }
&.is-highlighted { background-color: mix(#000000, #FFFFFF, 2.5%); }
}
&.is-active { display: block; }