Foundation work for highlighting options

This commit is contained in:
Josh Johnson 2016-04-26 14:36:02 +01:00
parent 04ab5f6a98
commit 35b9670ac5
7 changed files with 66 additions and 16 deletions

File diff suppressed because one or more lines are too long

View file

@ -24,13 +24,15 @@ export const selectItem = (id, selected) => {
}
};
export const addOption = (value, label, id, groupId) => {
export const addOption = (value, label, id, groupId, highlighted, disabled) => {
return {
type: 'ADD_OPTION',
value,
label,
id,
groupId,
highlighted,
disabled,
}
};
@ -42,6 +44,13 @@ export const selectOption = (id, selected) => {
}
};
export const highlightOption = (id) => {
return {
type: 'HIGHLIGHT_OPTION',
id,
}
};
export const filterOptions = (results) => {
return {
type: 'FILTER_OPTIONS',

View file

@ -2,7 +2,7 @@
import { createStore } from 'redux';
import rootReducer from './reducers/index.js';
import { addItem, removeItem, selectItem, addOption, selectOption, filterOptions, activateOptions, addGroup } from './actions/index';
import { addItem, removeItem, selectItem, addOption, selectOption, highlightOption, filterOptions, activateOptions, addGroup } from './actions/index';
import { hasClass, wrap, getSiblings, isType, strToEl, extend, getWidthOfInput, debounce } from './lib/utils.js';
import Sifter from 'sifter';
@ -71,6 +71,7 @@ export class Choices {
groupHeading : 'choices__heading',
activeState: 'is-active',
disabledState: 'is-disabled',
highlightedState: 'is-highlighted',
hiddenState: 'is-hidden',
flippedState: 'is-flipped',
selectedState: 'is-selected'
@ -235,12 +236,16 @@ export class Choices {
*/
onKeyDown(e) {
const activeItems = this.getItemsFilteredByActive();
const activeOptions = this.getOptionsFilteredByActive();
const inputIsFocussed = this.input === document.activeElement;
const ctrlDownKey = e.ctrlKey || e.metaKey;
const deleteKey = 8 || 46;
const enterKey = 13;
const aKey = 65;
const escapeKey = 27;
const upKey = 38;
const downKey = 40;
const hasActiveDropDown = this.dropdown && this.dropdown.classList.contains(this.options.classNames.activeState);
// If we are typing in the input
if(e.target === this.input) {
@ -257,9 +262,14 @@ export class Choices {
this.handleEnter(activeItems, value);
}
if(e.keyCode === escapeKey && this.dropdown) {
if(e.keyCode === escapeKey && hasActiveDropDown) {
this.toggleDropdown();
}
if((e.keyCode === downKey || e.keyCode === upKey) && hasActiveDropDown) {
let option = activeOptions[0];
this.highlightOption(option.id);
}
}
if(inputIsFocussed) {
@ -454,6 +464,11 @@ export class Choices {
this.store.dispatch(selectOption(id, value));
}
highlightOption(id) {
if(!id) return;
this.store.dispatch(highlightOption(id));
}
/**
* Add item to store with correct value
* @param {String} value Value to add to store
@ -593,7 +608,7 @@ export class Choices {
}
}
addOption(option, groupId = -1) {
addOption(option, groupId = -1, highlighted = false, disabled = false) {
// Generate unique id
const state = this.store.getState();
const id = state.options.length + 1;
@ -601,7 +616,7 @@ export class Choices {
const label = option.innerHTML;
const isSelected = option.selected;
this.store.dispatch(addOption(value, label, id, groupId));
this.store.dispatch(addOption(value, label, id, groupId, highlighted, disabled));
if(isSelected) {
this.selectOption(id);
@ -665,7 +680,7 @@ export class Choices {
getOptionsFilteredByActive() {
const options = this.getOptions();
const valueArray = options.filter((option) => {
return option.active === true;
return option.active === true && option.disabled === false;
},[]);
return valueArray;
@ -811,8 +826,16 @@ export class Choices {
if(groupOptions) {
this.addGroup(group.label, groupId, true, group.disabled);
groupOptions.forEach((option) => {
this.addOption(option, groupId);
groupOptions.forEach((option, optionIndex) => {
// We want to pre-highlight the first option
const highlighted = index === 0 && optionIndex === 0 ? true : false;
// If group is disabled, disable all of its children
if(group.disabled) {
this.addOption(option, groupId, highlighted, true);
} else {
this.addOption(option, groupId, highlighted);
}
});
} else {
this.addGroup(group.label, groupId, false, group.disabled);
@ -869,8 +892,6 @@ export class Choices {
const activeOptions = this.getOptionsFilteredByActive();
const activeGroups = this.getGroupsFilteredByActive();
console.log(activeGroups);
// Clear options
this.dropdown.innerHTML = '';
@ -893,8 +914,9 @@ export class Choices {
`);
groupOptions.forEach((option) => {
console.log(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 }">
<div class="${ classNames.item } ${ classNames.itemOption } ${ option.selected ? classNames.selectedState + ' ' + classNames.itemDisabled : classNames.itemSelectable } ${ option.highlighted ? classNames.highlightedState : '' }" data-choice-option data-choice-id="${ option.id }" data-choice-value="${ option.value }">
${ option.label }
</div>
`);
@ -908,7 +930,7 @@ export class Choices {
} else if(activeOptions.length >= 1) {
activeOptions.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 }">
<div class="${ classNames.item } ${ classNames.itemOption } ${ option.selected ? classNames.selectedState + ' ' + classNames.itemDisabled : classNames.itemSelectable } ${ option.highlighted ? classNames.highlightedState : '' }" data-choice-option data-choice-id="${ option.id }" data-choice-value="${ option.value }">
${ option.label }
</div>
`);
@ -1005,6 +1027,7 @@ export class Choices {
// Trigger event listeners
this.addEventListeners();
// Run callback if it is a function
if(callback){
if(isType('Function', callback)) {
@ -1020,9 +1043,12 @@ export class Choices {
* @return
*/
destroy() {
this.passedElement = null;
this.userOptions = null;
this.options = null;
this.passedElement = null;
this.initialised = null;
this.store = null;
}
};

View file

@ -6,11 +6,23 @@ const options = (state = [], action) => {
groupId: action.groupId,
value: action.value,
label: action.label,
disabled: false,
highlighted: action.highlighted,
disabled: action.disabled,
selected: false,
active: true,
}];
case 'HIGHLIGHT_OPTION':
return state.map((option) => {
if(option.id === parseInt(action.id)) {
option.highlighted = true;
} else {
option.highlighted = false;
}
return option;
});
case 'SELECT_OPTION':
return state.map((option) => {
if(option.id === parseInt(action.id)) {

View file

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

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: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.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}

View file

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