Psuedo fixed search box

This commit is contained in:
Josh Johnson 2016-06-02 14:37:06 +01:00
parent 013215f3f7
commit 851ddea81c
7 changed files with 116 additions and 47 deletions

File diff suppressed because one or more lines are too long

View file

@ -235,7 +235,6 @@ export class Choices {
break;
case enterKey:
if(this.passedElement.type === 'select-one') this.toggleDropdown();
// If enter key is pressed and the input has a value
if(e.target.value && this.passedElement.type === 'text') {
@ -252,6 +251,12 @@ export class Choices {
const id = highlighted.getAttribute('data-id');
this.addItem(value, label, id);
this.clearInput(this.passedElement);
if(this.passedElement.type === 'select-one') {
this.isSearching = false;
this.store.dispatch(activateOptions());
this.toggleDropdown();
}
}
}
break;
@ -279,7 +284,7 @@ export class Choices {
if(nextEl) {
// We prevent default to stop the cursor moving
// when pressing the arrow
if(!isScrolledIntoView(nextEl, this.dropdown, directionInt)) {
if(!isScrolledIntoView(nextEl, this.optionList, directionInt)) {
this.scrollToOption(nextEl, directionInt);
}
this.highlightOption(nextEl);
@ -290,7 +295,7 @@ export class Choices {
case backKey:
case deleteKey:
// If backspace or delete key is pressed and the input has no value
if(hasFocusedInput && !e.target.value) {
if(hasFocusedInput && !e.target.value && this.passedElement.type !== 'select-one') {
this.handleBackspace(activeItems);
e.preventDefault();
}
@ -441,6 +446,9 @@ export class Choices {
if(!option.selected && !option.disabled) {
this.addItem(option.value, option.label, option.id);
if(this.passedElement.type === 'select-one') {
this.input.value = "";
this.isSearching = false;
this.store.dispatch(activateOptions(true));
this.toggleDropdown();
}
}
@ -551,17 +559,17 @@ export class Choices {
scrollToOption(option, direction) {
if(!option) return;
const dropdownHeight = this.dropdown.offsetHeight;
const dropdownHeight = this.optionList.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;
const containerScrollPos = this.optionList.scrollTop + dropdownHeight;
// Difference between the option and scroll position
let endPoint;
let endPoint = direction > 0 ? ((this.optionList.scrollTop + optionPos) - containerScrollPos) : option.offsetTop;
const animateScroll = (time, endPoint, direction) => {
let continueAnimation = false;
@ -569,19 +577,19 @@ export class Choices {
const strength = 4;
if(direction > 0) {
easing = (endPoint - this.dropdown.scrollTop)/strength;
easing = (endPoint - this.optionList.scrollTop)/strength;
distance = easing > 1 ? easing : 1;
this.dropdown.scrollTop = this.dropdown.scrollTop + distance;
if(this.dropdown.scrollTop < endPoint) {
this.optionList.scrollTop = this.optionList.scrollTop + distance;
if(this.optionList.scrollTop < endPoint) {
continueAnimation = true;
}
} else {
easing = (this.dropdown.scrollTop - endPoint)/strength;
easing = (this.optionList.scrollTop - endPoint)/strength;
distance = easing > 1 ? easing : 1;
this.dropdown.scrollTop = this.dropdown.scrollTop - distance;
if(this.dropdown.scrollTop > endPoint) {
this.optionList.scrollTop = this.optionList.scrollTop - distance;
if(this.optionList.scrollTop > endPoint) {
continueAnimation = true;
}
}
@ -593,18 +601,9 @@ export class Choices {
}
};
// Scroll dropdown to top of option
if(direction > 0) {
endPoint = (this.dropdown.scrollTop + optionPos) - containerScrollPos;
requestAnimationFrame((time) => {
animateScroll(time, endPoint, 1);
});
} else {
endPoint = option.offsetTop;
requestAnimationFrame((time) => {
animateScroll(time, endPoint, -1);
});
}
requestAnimationFrame((time) => {
animateScroll(time, endPoint, direction);
});
}
/**
@ -910,7 +909,10 @@ export class Choices {
*/
ajax(fn) {
this.containerOuter.classList.add('is-loading');
this.input.placeholder = this.options.loadingText;
// this.input.placeholder = this.options.loadingText;
const placeholderItem = this.getTemplate('item', { id: -1, value: 'Loading', label: this.options.loadingText, active: true});
this.itemList.appendChild(placeholderItem);
const callback = (results, value, label) => {
if(results && results.length) {
@ -1041,9 +1043,9 @@ export class Choices {
// If placeholder has been enabled and we have a value
if (this.options.placeholder && (this.options.placeholderValue || this.passedElement.placeholder)) {
const placeholder = this.options.placeholderValue || this.passedElement.placeholder;
input.placeholder = placeholder;
if(this.passedElement.type !== 'select-one') {
const placeholder = this.options.placeholderValue || this.passedElement.placeholder;
input.placeholder = placeholder;
input.style.width = getWidthOfInput(input);
}
}
@ -1139,7 +1141,7 @@ export class Choices {
// 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

View file

@ -72,6 +72,8 @@ h1, h2, h3, h4, h5, h6 {
display: block;
width: 100%;
padding: 1rem;
border-bottom: 1px solid #DDDDDD;
background-color: #FFFFFF;
margin: 0; }
.choices[data-type*="select-one"].is-open:after {
@ -100,7 +102,7 @@ h1, h2, h3, h4, h5, h6 {
border-radius: 0.25rem;
font-size: 1.4rem;
overflow: hidden; }
.is-focused .choices__inner {
.is-focused .choices__inner, .is-open .choices__inner {
border-color: #b7b7b7; }
.is-open .choices__inner {
border-radius: 0.25rem 0.25rem 0 0; }
@ -113,9 +115,8 @@ h1, h2, h3, h4, h5, h6 {
list-style-type: none; }
.choices__list--single {
display: inline-block; }
.choices__list--single .choices__item {
padding: .4rem; }
display: inline-block;
padding: .4rem; }
.choices__list--multiple {
display: inline; }
@ -150,11 +151,14 @@ h1, h2, h3, h4, h5, h6 {
margin-top: -1px;
border-bottom-left-radius: 0.25rem;
border-bottom-right-radius: 0.25rem;
max-height: 300px;
overflow: auto;
will-change: scroll-position; }
overflow: hidden; }
.choices__list--dropdown.is-active {
display: block; }
.choices__list--dropdown .choices__list {
position: relative;
max-height: 300px;
overflow: auto;
will-change: scroll-position; }
.choices__list--dropdown .choices__item {
padding: 1rem;
font-size: 1.4rem; }

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{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}
*,: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;border-bottom:1px solid #ddd;background-color:#fff;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,.is-open .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;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;overflow:hidden}.choices__list--dropdown.is-active{display:block}.choices__list--dropdown .choices__list{position:relative;max-height:300px;overflow:auto;will-change:scroll-position}.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

@ -98,6 +98,8 @@ $choices-button-icon-path: '../../icons/cross.svg';
display: block;
width: 100%;
padding: 1rem;
border-bottom: 1px solid $choices-keyline-color;
background-color: #FFFFFF;
margin: 0;
}
&.is-open:after {
@ -129,7 +131,7 @@ $choices-button-icon-path: '../../icons/cross.svg';
border-radius: $choices-border-radius;
font-size: 1.4rem;
overflow: hidden;
.is-focused & { border-color: darken($choices-keyline-color, 15%); }
.is-focused &, .is-open & { border-color: darken($choices-keyline-color, 15%); }
.is-open & { border-radius: $choices-border-radius $choices-border-radius 0 0; }
.is-flipped.is-open & { border-radius: 0 0 $choices-border-radius $choices-border-radius; }
}
@ -142,7 +144,7 @@ $choices-button-icon-path: '../../icons/cross.svg';
.choices__list--single {
display: inline-block;
.choices__item { padding: .4rem; }
padding: .4rem;
}
.choices__list--multiple {
@ -181,11 +183,16 @@ $choices-button-icon-path: '../../icons/cross.svg';
margin-top: -1px;
border-bottom-left-radius: $choices-border-radius;
border-bottom-right-radius: $choices-border-radius;
max-height: 300px;
overflow: auto;
will-change: scroll-position;
overflow: hidden;
&.is-active { display: block; }
.choices__list {
position: relative;
max-height: 300px;
overflow: auto;
will-change: scroll-position;
}
.choices__item {
padding: 1rem;
font-size: 1.4rem;

56
build/config.gypi Normal file
View file

@ -0,0 +1,56 @@
# Do not edit. File was generated by node-gyp's "configure" step
{
"target_defaults": {
"cflags": [],
"default_configuration": "Release",
"defines": [],
"include_dirs": [],
"libraries": []
},
"variables": {
"asan": 0,
"host_arch": "x64",
"icu_data_file": "icudt57l.dat",
"icu_data_in": "../../deps/icu-small/source/data/in/icudt57l.dat",
"icu_endianness": "l",
"icu_gyp_path": "tools/icu/icu-generic.gyp",
"icu_locales": "en,root",
"icu_path": "deps/icu-small",
"icu_small": "true",
"icu_ver_major": "57",
"llvm_version": 0,
"node_byteorder": "little",
"node_enable_v8_vtunejit": "false",
"node_install_npm": "true",
"node_no_browser_globals": "false",
"node_prefix": "/",
"node_release_urlbase": "https://nodejs.org/download/release/",
"node_shared_cares": "false",
"node_shared_http_parser": "false",
"node_shared_libuv": "false",
"node_shared_openssl": "false",
"node_shared_zlib": "false",
"node_tag": "",
"node_use_dtrace": "true",
"node_use_etw": "false",
"node_use_lttng": "false",
"node_use_openssl": "true",
"node_use_perfctr": "false",
"openssl_fips": "",
"openssl_no_asm": 0,
"target_arch": "x64",
"uv_parent_path": "/deps/uv/",
"uv_use_dtrace": "true",
"v8_enable_gdbjit": 0,
"v8_enable_i18n_support": 1,
"v8_no_strict_aliasing": 1,
"v8_optimized_debug": 0,
"v8_random_seed": 0,
"v8_use_snapshot": "true",
"want_separate_host_toolset": 0,
"xcode_version": "7.0",
"nodedir": "/Users/joshjohnson/.node-gyp/6.2.0",
"copy_dev_lib": "true",
"standalone_static_library": 1
}
}

View file

@ -166,9 +166,9 @@
items: ['josh@joshuajohnson.co.uk', { value: 'joe@bloggs.co.uk', label: 'Joe Bloggs' } ],
});
const choices7 = new Choices('#choices-7');
const choices7 = new Choices('#choices-7', { allowSearch: false });
const choicesAjax = new Choices('#choices-12', { allowSearch: false }).ajax((callback) => {
const choicesAjax = new Choices('#choices-12').ajax((callback) => {
fetch('https://api.discogs.com/artists/391170/releases?token=QBRmstCkwXEvCjTclCpumbtNwvVkEzGAdELXyRyW')
.then((response) => {
response.json().then(function(data) {