Ability to add both attribute values and object values + housekeeping

This commit is contained in:
Josh Johnson 2016-06-01 18:45:35 +01:00
parent 110741aed3
commit f361e4b34d
6 changed files with 88 additions and 74 deletions

File diff suppressed because one or more lines are too long

View file

@ -82,38 +82,37 @@ export class Choices {
this.store = new Store(this.render);
// State tracking
this.initialised = false;
this.initialised = false;
this.currentState = {};
this.prevState = {};
this.prevState = {};
// Retrieve triggering element (i.e. element with 'data-choice' trigger)
this.passedElement = isType('String', element) ? document.querySelector(element) : element;
this.highlightPosition = 0;
// Set preset items - this looks out of place
this.presetItems = [];
if(this.options.items.length) {
this.presetItems = this.options.items;
} else if(this.passedElement.value !== '') {
this.presetItems = this.passedElement.value.split(this.options.delimiter);
// Assign preset items from passed object first
this.presetItems = this.options.items;
// Then add any values passed from attribute
if(this.passedElement.value !== '') {
this.presetItems = this.presetItems.concat(this.passedElement.value.split(this.options.delimiter));
}
// Bind methods
this.init = this.init.bind(this);
this.render = this.render.bind(this);
this.init = this.init.bind(this);
this.render = this.render.bind(this);
this.destroy = this.destroy.bind(this);
this.disable = this.disable.bind(this);
// Bind event handlers
this.onFocus = this.onFocus.bind(this);
this.onBlur = this.onBlur.bind(this);
this.onKeyUp = this.onKeyUp.bind(this);
this.onKeyDown = this.onKeyDown.bind(this);
this.onFocus = this.onFocus.bind(this);
this.onBlur = this.onBlur.bind(this);
this.onKeyUp = this.onKeyUp.bind(this);
this.onKeyDown = this.onKeyDown.bind(this);
this.onMouseDown = this.onMouseDown.bind(this);
this.onMouseOver = this.onMouseOver.bind(this);
this.onPaste = this.onPaste.bind(this);
this.onInput = this.onInput.bind(this);
this.onPaste = this.onPaste.bind(this);
this.onInput = this.onInput.bind(this);
// Cutting the mustard
const cuttingTheMustard = 'querySelector' in document && 'addEventListener' in document && 'classList' in document.createElement("div");
@ -204,21 +203,21 @@ export class Choices {
if(e.target !== this.input) return;
const ctrlDownKey = e.ctrlKey || e.metaKey;
const backKey = 46;
const deleteKey = 8;
const enterKey = 13;
const aKey = 65;
const escapeKey = 27;
const upKey = 38;
const downKey = 40;
const backKey = 46;
const deleteKey = 8;
const enterKey = 13;
const aKey = 65;
const escapeKey = 27;
const upKey = 38;
const downKey = 40;
const activeItems = this.store.getItemsFilteredByActive();
const activeOptions = this.store.getOptionsFilteredByActive();
const hasFocusedInput = this.input === document.activeElement;
const activeItems = this.store.getItemsFilteredByActive();
const activeOptions = this.store.getOptionsFilteredByActive();
const hasFocusedInput = this.input === document.activeElement;
const hasActiveDropdown = this.dropdown.classList.contains(this.options.classNames.activeState);
const hasItems = this.itemList && this.itemList.children;
const keyString = String.fromCharCode(event.keyCode);
const hasItems = this.itemList && this.itemList.children;
const keyString = String.fromCharCode(event.keyCode);
// If a user is typing and the dropdown is not active
if(this.passedElement.type !== 'text' && /[a-zA-Z0-9-_ ]/.test(keyString) && !hasActiveDropdown) {
@ -250,7 +249,7 @@ export class Choices {
if(highlighted) {
const value = highlighted.getAttribute('data-value');
const label = highlighted.innerHTML;
const id = highlighted.getAttribute('data-id');
const id = highlighted.getAttribute('data-id');
this.addItem(value, label, id);
this.clearInput(this.passedElement);
}
@ -267,7 +266,7 @@ export class Choices {
case upKey:
// If up or down key is pressed, traverse through options
if(hasActiveDropdown) {
const currentEl = this.dropdown.querySelector(`.${this.options.classNames.highlightedState}`);
const currentEl = this.dropdown.querySelector(`.${this.options.classNames.highlightedState}`);
const directionInt = e.keyCode === downKey ? 1 : -1;
let nextEl;
@ -342,7 +341,7 @@ export class Choices {
if(this.options.allowSearch) {
if(this.input === document.activeElement) {
const options = this.store.getOptions();
const options = this.store.getOptions();
const hasUnactiveOptions = options.some((option) => option.active !== true);
// Check that have a value to search
@ -350,7 +349,7 @@ export class Choices {
const handleFilter = () => {
if(this.input.value.length >= 1) {
const haystack = this.store.getOptionsFiltedBySelectable();
const needle = this.input.value;
const needle = this.input.value;
const fuse = new Fuse(haystack, {
keys: ['label', 'value'],
@ -413,7 +412,7 @@ export class Choices {
if(e.target.hasAttribute('data-button')) {
if(this.options.removeItems && this.options.removeButton) {
const itemId = e.target.parentNode.getAttribute('data-id');
const itemId = e.target.parentNode.getAttribute('data-id');
const itemToRemove = activeItems.find((item) => item.id === parseInt(itemId));
this.removeItem(itemToRemove);
}
@ -450,7 +449,7 @@ export class Choices {
} else {
// Click is outside of our element so close dropdown and de-select items
const hasActiveDropdown = this.dropdown.classList.contains(this.options.classNames.activeState);
const hasSelectedItems = activeItems.some((item) => item.selected === true);
const hasSelectedItems = activeItems.some((item) => item.selected === true);
// De-select any selected items
if(hasSelectedItems) this.deselectAll();
@ -551,15 +550,15 @@ export class Choices {
scrollToOption(option, direction) {
if(!option) return;
const dropdownHeight = this.dropdown.offsetHeight;
const optionHeight = option.offsetHeight;
const dropdownHeight = this.dropdown.offsetHeight;
const optionHeight = option.offsetHeight;
// Distance from bottom of element to top of parent
const optionPos = option.offsetTop + optionHeight;
const optionPos = option.offsetTop + optionHeight;
// Scroll position of dropdown
const containerScrollPos = this.dropdown.scrollTop + dropdownHeight;
// Difference between the option and scroll position
let endPoint;
@ -696,9 +695,9 @@ export class Choices {
* @param {String} value Value to add to store
*/
addItem(value, label, optionId = -1, callback = this.options.callbackOnAddItem) {
const items = this.store.getItems();
let passedValue = value.trim();
let passedLabel = label || passedValue;
const items = this.store.getItems();
let passedValue = value.trim();
let passedLabel = label || passedValue;
let passedOptionId = optionId || -1;
// If a prepended value has been passed, prepend it
@ -740,8 +739,8 @@ export class Choices {
return;
}
const id = item.id;
const value = item.value;
const id = item.id;
const value = item.value;
const optionId = item.optionId;
this.store.dispatch(removeItem(id, optionId));
@ -813,8 +812,8 @@ export class Choices {
if(!label) { label = value; }
// Generate unique id
const options = this.store.getOptions();
const id = options.length + 1;
const options = this.store.getOptions();
const id = options.length + 1;
const isDisabled = option && (option.disabled || option.parentNode.disabled);
this.store.dispatch(addOption(value, label, id, groupId, isDisabled));
@ -831,7 +830,7 @@ export class Choices {
*/
addGroup(group, id, isFirst) {
const groupOptions = Array.from(group.getElementsByTagName('OPTION'));
const groupId = id;
const groupId = id;
if(groupOptions) {
this.store.dispatch(addGroup(group.label, groupId, true, group.disabled));
@ -1014,17 +1013,17 @@ export class Choices {
generateInput() {
const containerOuter = this.getTemplate('containerOuter');
const containerInner = this.getTemplate('containerInner');
const itemList = this.getTemplate('itemList');
const optionList = this.getTemplate('optionList');
const input = this.getTemplate('input');
const dropdown = this.getTemplate('dropdown');
const itemList = this.getTemplate('itemList');
const optionList = this.getTemplate('optionList');
const input = this.getTemplate('input');
const dropdown = this.getTemplate('dropdown');
this.containerOuter = containerOuter;
this.containerInner = containerInner;
this.input = input;
this.optionList = optionList;
this.itemList = itemList;
this.dropdown = dropdown;
this.input = input;
this.optionList = optionList;
this.itemList = itemList;
this.dropdown = dropdown;
// Hide passed input
this.passedElement.classList.add(this.options.classNames.input, this.options.classNames.hiddenState);
@ -1082,8 +1081,13 @@ export class Choices {
} else if(this.passedElement.type === 'text') {
// Add any preset values seperated by delimiter
this.presetItems.forEach((value) => {
this.addItem(value);
this.presetItems.forEach((item) => {
if(isType('Object', item)) {
if(!item.value) return;
this.addItem(item.value, item.label, item.id);
} else if(isType('String', item)) {
this.addItem(item);
}
});
}
}
@ -1131,9 +1135,10 @@ export class Choices {
}
renderItems(items, fragment) {
// Create fragment to add elements to
const itemListFragment = fragment || document.createDocumentFragment();
// Simplify store data to just values
const itemsFiltered = this.store.getItemsReducedToValues(items);
const itemsFiltered = this.store.getItemsReducedToValues(items);
if(this.passedElement.type === 'text') {
// Assign hidden input array of values
@ -1184,9 +1189,9 @@ export class Choices {
if((this.currentState.options !== this.prevState.options || this.currentState.groups !== this.prevState.groups)) {
if(this.passedElement.type === 'select-multiple' || this.passedElement.type === 'select-one') {
// Get active groups/options
const activeGroups = this.store.getGroupsFilteredByActive();
const activeOptions = this.store.getOptionsFilteredByActive();
const activeGroups = this.store.getGroupsFilteredByActive();
const activeOptions = this.store.getOptionsFilteredByActive();
const optListFragment = document.createDocumentFragment();
// Clear options
@ -1305,9 +1310,9 @@ export class Choices {
this.containerOuter.outerHTML = this.passedElement.outerHTML;
this.passedElement = null;
this.userOptions = null;
this.options = null;
this.store = null;
this.userOptions = null;
this.options = null;
this.store = null;
this.removeEventListeners();
}

View file

@ -65,6 +65,7 @@ h1, h2, h3, h4, h5, h6 {
cursor: not-allowed; }
.choices[data-type*="select-one"] .choices__inner {
cursor: pointer;
padding-bottom: .75rem; }
.choices[data-type*="select-one"] .choices__input {
@ -89,13 +90,15 @@ h1, h2, h3, h4, h5, h6 {
top: 50%;
margin-top: -2.5px; }
.choices[data-type*="select-multiple"] .choices__inner, .choices[data-type*="text"] .choices__inner {
cursor: text; }
.choices__inner {
background-color: #f9f9f9;
padding: .75rem .75rem .375rem;
border: 1px solid #DDDDDD;
border-radius: 0.25rem;
font-size: 1.4rem;
cursor: text;
overflow: hidden; }
.is-focused .choices__inner {
border-color: #b7b7b7; }

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}hr,label{display:block}label{margin-bottom:.8rem;font-size:1.4rem;font-weight:500}hr{margin:3.6rem 0;border:0;border-bottom:1px solid #eaeaea;height:1px}h1,h2,h3,h4,h5,h6{margin-top:0;margin-bottom:1.2rem;font-weight:400}.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.is-disabled .choices__inner,.choices.is-disabled .choices__input{background-color:#eaeaea;cursor:not-allowed;-webkit-user-select:none;-moz-user-select:none;-ms-user-select:none;user-select:none}.choices.is-disabled .choices__item{cursor:not-allowed}.choices[data-type*=select-one] .choices__inner{padding-bottom:.75rem}.choices[data-type*=select-one] .choices__input{display:block;width:100%;padding:1rem;margin:0}.choices[data-type*=select-one].is-open:after{border-color:transparent transparent #333 transparent;margin-top:-7.5px}.choices[data-type*=select-one]:after{content:"";height:0;width:0;border-style:solid;border-color:#333 transparent transparent transparent;border-width:5px;position:absolute;right:1.15rem;top:50%;margin-top:-2.5px}.choices__inner{background-color:#f9f9f9;padding:.75rem .75rem .375rem;border:1px solid #ddd;border-radius:.25rem;font-size:1.4rem;cursor:text;overflow:hidden}.is-focused .choices__inner{border-color:#b7b7b7}.is-open .choices__inner{border-radius:.25rem .25rem 0 0}.is-flipped.is-open .choices__inner{border-radius:0 0 .25rem .25rem}.choices__list{margin:0;padding-left:0;list-style-type:none}.choices__list--single{display:inline-block}.choices__list--single .choices__item{padding:.4rem}.choices__list--multiple{display:inline}.choices__list--multiple .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 #008fa1;color:#fff;word-break:break-all}.choices__list--multiple .choices__item[data-deletable]{padding-right:.5rem}.choices__list--multiple .choices__item.is-selected{background-color:#00a5bb;border:1px solid #007888}.is-disabled .choices__list--multiple .choices__item{background-color:#aaa;border:1px solid #919191}.choices__list--dropdown{display:none;z-index:1;position:absolute;width:100%;background-color:#fff;border:1px solid #ddd;top:100%;margin-top:-1px;border-bottom-left-radius:.25rem;border-bottom-right-radius:.25rem;max-height:300px;overflow:auto;will-change:scroll-position}.choices__list--dropdown.is-active{display:block}.choices__list--dropdown .choices__item{padding:1rem;font-size:1.4rem}.choices__list--dropdown .choices__item--selectable.is-highlighted:after,.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:after{content:"Press to select";font-size:12px;opacity:0;float:right}.choices__list--dropdown .choices__item--selectable.is-highlighted{background-color:#f2f2f2}.is-open .choices__list--dropdown{border-color:#b7b7b7}.is-flipped .choices__list--dropdown{top:auto;bottom:100%;margin-top:0;margin-bottom:-1px;border-radius:.25rem .25rem 0 0}.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;opacity:.5}.choices__group .choices__heading{font-weight:600;font-size:1.2rem;padding:1rem;border-bottom:1px solid #f7f7f7;color:gray}.choices__button{text-indent:-9999px;-webkit-appearance:none;-moz-appearance:none;appearance:none;border:0;background-color:transparent;background-image:url(../../icons/cross.svg);background-repeat:no-repeat;background-position:center;background-size:8px;border-left:1px solid #008fa1;margin-left:4px;padding-left:6px;padding-right:6px;line-height:1;cursor:pointer}.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}hr,label{display:block}label{margin-bottom:.8rem;font-size:1.4rem;font-weight:500}hr{margin:3.6rem 0;border:0;border-bottom:1px solid #eaeaea;height:1px}h1,h2,h3,h4,h5,h6{margin-top:0;margin-bottom:1.2rem;font-weight:400}.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.is-disabled .choices__inner,.choices.is-disabled .choices__input{background-color:#eaeaea;cursor:not-allowed;-webkit-user-select:none;-moz-user-select:none;-ms-user-select:none;user-select:none}.choices.is-disabled .choices__item{cursor:not-allowed}.choices[data-type*=select-one] .choices__inner{cursor:pointer;padding-bottom:.75rem}.choices[data-type*=select-one] .choices__input{display:block;width:100%;padding:1rem;margin:0}.choices[data-type*=select-one].is-open:after{border-color:transparent transparent #333 transparent;margin-top:-7.5px}.choices[data-type*=select-one]:after{content:"";height:0;width:0;border-style:solid;border-color:#333 transparent transparent transparent;border-width:5px;position:absolute;right:1.15rem;top:50%;margin-top:-2.5px}.choices[data-type*=select-multiple] .choices__inner,.choices[data-type*=text] .choices__inner{cursor:text}.choices__inner{background-color:#f9f9f9;padding:.75rem .75rem .375rem;border:1px solid #ddd;border-radius:.25rem;font-size:1.4rem;overflow:hidden}.is-focused .choices__inner{border-color:#b7b7b7}.is-open .choices__inner{border-radius:.25rem .25rem 0 0}.is-flipped.is-open .choices__inner{border-radius:0 0 .25rem .25rem}.choices__list{margin:0;padding-left:0;list-style-type:none}.choices__list--single{display:inline-block}.choices__list--single .choices__item{padding:.4rem}.choices__list--multiple{display:inline}.choices__list--multiple .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 #008fa1;color:#fff;word-break:break-all}.choices__list--multiple .choices__item[data-deletable]{padding-right:.5rem}.choices__list--multiple .choices__item.is-selected{background-color:#00a5bb;border:1px solid #007888}.is-disabled .choices__list--multiple .choices__item{background-color:#aaa;border:1px solid #919191}.choices__list--dropdown{display:none;z-index:1;position:absolute;width:100%;background-color:#fff;border:1px solid #ddd;top:100%;margin-top:-1px;border-bottom-left-radius:.25rem;border-bottom-right-radius:.25rem;max-height:300px;overflow:auto;will-change:scroll-position}.choices__list--dropdown.is-active{display:block}.choices__list--dropdown .choices__item{padding:1rem;font-size:1.4rem}.choices__list--dropdown .choices__item--selectable.is-highlighted:after,.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:after{content:"Press to select";font-size:12px;opacity:0;float:right}.choices__list--dropdown .choices__item--selectable.is-highlighted{background-color:#f2f2f2}.is-open .choices__list--dropdown{border-color:#b7b7b7}.is-flipped .choices__list--dropdown{top:auto;bottom:100%;margin-top:0;margin-bottom:-1px;border-radius:.25rem .25rem 0 0}.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;opacity:.5}.choices__group .choices__heading{font-weight:600;font-size:1.2rem;padding:1rem;border-bottom:1px solid #f7f7f7;color:gray}.choices__button{text-indent:-9999px;-webkit-appearance:none;-moz-appearance:none;appearance:none;border:0;background-color:transparent;background-image:url(../../icons/cross.svg);background-repeat:no-repeat;background-position:center;background-size:8px;border-left:1px solid #008fa1;margin-left:4px;padding-left:6px;padding-right:6px;line-height:1;cursor:pointer}.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

@ -90,7 +90,10 @@ $choices-button-icon-path: '../../icons/cross.svg';
}
.choices[data-type*="select-one"] {
.choices__inner { padding-bottom: .75rem; }
.choices__inner {
cursor: pointer;
padding-bottom: .75rem;
}
.choices__input {
display: block;
width: 100%;
@ -115,13 +118,16 @@ $choices-button-icon-path: '../../icons/cross.svg';
}
}
.choices[data-type*="select-multiple"], .choices[data-type*="text"] {
.choices__inner { cursor: text; }
}
.choices__inner {
background-color: $choices-bg-color;
padding: .75rem .75rem .375rem;
border: 1px solid $choices-keyline-color;
border-radius: $choices-border-radius;
font-size: 1.4rem;
cursor: text;
overflow: hidden;
.is-focused & { border-color: darken($choices-keyline-color, 15%); }
.is-open & { border-radius: $choices-border-radius $choices-border-radius 0 0; }

View file

@ -28,7 +28,7 @@
<input id="choices-5" type="text" value="preset-1, preset-2" placeholder="This is a placeholder">
<label for="choices-6">Preset values passed through options</label>
<input id="choices-6" type="text" placeholder="This is a placeholder">
<input id="choices-6" type="text" value="olivia@benson.com" placeholder="This is a placeholder">
<hr>
@ -163,7 +163,7 @@
}).removeActiveItems();
const choices6 = new Choices('#choices-6', {
items: ['josh@joshuajohnson.co.uk', 'joe@bloggs.co.uk'],
items: ['josh@joshuajohnson.co.uk', { value: 'joe@bloggs.co.uk', label: 'Joe Bloggs' } ],
});
const choices7 = new Choices('#choices-7');