Add backspace to search keycode regex

This commit is contained in:
Josh Johnson 2016-06-30 22:08:40 +01:00
parent ea2669312f
commit 283324c65b
6 changed files with 62 additions and 48 deletions

File diff suppressed because one or more lines are too long

View file

@ -420,12 +420,10 @@ export class Choices {
if(this.initialised === true) {
if(this.passedElement.type === 'select-one' || this.passedElement.type === 'select-multiple') {
this.containerOuter.classList.add(this.config.classNames.loadingState);
if(this.passedElement.type === 'select-one') {
const placeholderItem = this._getTemplate('item', { id: -1, value: 'Loading', label: this.config.loadingText, active: true});
this.itemList.appendChild(placeholderItem);
}
const callback = (results, value, label) => {
if(!isType('Array', results) || !value) return;
@ -435,18 +433,16 @@ export class Choices {
results.forEach((result, index) => {
// Select first choice in list if single select input
if(index === 0 && this.passedElement.type === 'select-one') {
this._addChoice(true, false, result[value], result[label]);
this._addChoice(true, false, result[value], result[label]);
} else {
this._addChoice(false, false, result[value], result[label]);
}
});
}
};
fn(callback);
}
}
return this;
}
@ -460,7 +456,6 @@ export class Choices {
if(this.passedElement.type !== 'select-one') {
this.input.style.width = getWidthOfInput(this.input);
}
return this;
}
@ -698,7 +693,7 @@ export class Choices {
const hasUnactiveChoices = choices.some((option) => option.active !== true);
// Check that we have a value to search and the input was an alphanumeric character
if(this.input.value && choices.length && /[a-zA-Z0-9-_ ]/.test(keyString)) {
if(this.input.value && choices.length && /[\b\a-zA-Z0-9-_ ]/.test(keyString)) {
const handleFilter = () => {
const newValue = this.input.value.trim();
const currentValue = this.currentValue.trim();

View file

@ -122,20 +122,21 @@ h1, h2, h3, h4, h5, h6 {
display: inline; }
.choices__list--multiple .choices__item {
display: inline-block;
vertical-align: middle;
border-radius: 2rem;
padding: .4rem 1rem;
font-size: 1.2rem;
margin-right: .375rem;
margin-bottom: .375rem;
background-color: #00BCD4;
border: 1px solid #008fa1;
border: 1px solid #00a5bb;
color: #FFFFFF;
word-break: break-all; }
.choices__list--multiple .choices__item[data-deletable] {
padding-right: .5rem; }
.choices__list--multiple .choices__item.is-highlighted {
background-color: #00a5bb;
border: 1px solid #007888; }
border: 1px solid #008fa1; }
.is-disabled .choices__list--multiple .choices__item {
background-color: #aaaaaa;
border: 1px solid #919191; }
@ -154,6 +155,14 @@ h1, h2, h3, h4, h5, h6 {
overflow: hidden; }
.choices__list--dropdown.is-active {
display: block; }
.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__list--dropdown .choices__list {
position: relative;
max-height: 300px;
@ -171,14 +180,6 @@ h1, h2, h3, h4, h5, h6 {
background-color: #f2f2f2; }
.choices__list--dropdown .choices__item--selectable.is-highlighted:after {
opacity: .5; }
.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; }
@ -214,6 +215,7 @@ h1, h2, h3, h4, h5, h6 {
background-size: 8px;
border-left: 1px solid #008fa1;
margin-left: 4px;
margin-right: -4px;
padding-left: 6px;
padding-right: 6px;
line-height: 1;

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;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-highlighted{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:after{content:"Press to select";font-size:12px;opacity:0;float:right}.choices__list--dropdown .choices__item--selectable.is-highlighted{background-color:#f2f2f2}.choices__list--dropdown .choices__item--selectable.is-highlighted:after{opacity:.5}.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;vertical-align:middle;border-radius:2rem;padding:.4rem 1rem;font-size:1.2rem;margin-right:.375rem;margin-bottom:.375rem;background-color:#00bcd4;border:1px solid #00a5bb;color:#fff;word-break:break-all}.choices__list--multiple .choices__item[data-deletable]{padding-right:.5rem}.choices__list--multiple .choices__item.is-highlighted{background-color:#00a5bb;border:1px solid #008fa1}.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}.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__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:after{content:"Press to select";font-size:12px;opacity:0;float:right}.choices__list--dropdown .choices__item--selectable.is-highlighted{background-color:#f2f2f2}.choices__list--dropdown .choices__item--selectable.is-highlighted:after{opacity:.5}.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;margin-right:-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

@ -151,19 +151,20 @@ $choices-button-icon-path: '../../icons/cross.svg';
display: inline;
.choices__item {
display: inline-block;
vertical-align: middle;
border-radius: $choices-border-radius-item;
padding: .4rem 1rem;
font-size: 1.2rem;
margin-right: .375rem;
margin-bottom: .375rem;
background-color: $choices-primary-color;
border: 1px solid darken($choices-primary-color, 10%);
border: 1px solid darken($choices-primary-color, 5%);
color: #FFFFFF;
word-break: break-all;
&[data-deletable] { padding-right: .5rem; }
&.is-highlighted {
background-color: darken($choices-primary-color, 5%);
border: 1px solid darken($choices-primary-color, 15%);
border: 1px solid darken($choices-primary-color, 10%);
}
.is-disabled & {
background-color: darken($choices-disabled-color, 25%);
@ -185,19 +186,24 @@ $choices-button-icon-path: '../../icons/cross.svg';
border-bottom-right-radius: $choices-border-radius;
overflow: hidden;
&.is-active { display: block; }
.is-open & { border-color: darken(#DDDDDD, 15%); }
.is-flipped & {
top: auto;
bottom: 100%;
margin-top: 0;
margin-bottom: -1px;
border-radius: .25rem .25rem 0 0;
}
.choices__list {
position: relative;
max-height: 300px;
overflow: auto;
will-change: scroll-position;
}
.choices__item {
padding: 1rem;
font-size: 1.4rem;
}
.choices__item--selectable {
&:after {
content: "Press to select";
@ -210,15 +216,6 @@ $choices-button-icon-path: '../../icons/cross.svg';
&:after { opacity: .5; }
}
}
.is-open & { border-color: darken(#DDDDDD, 15%); }
.is-flipped & {
top: auto;
bottom: 100%;
margin-top: 0;
margin-bottom: -1px;
border-radius: .25rem .25rem 0 0;
}
}
.choices__item { cursor: default; }
@ -251,6 +248,7 @@ $choices-button-icon-path: '../../icons/cross.svg';
background-size: 8px;
border-left: 1px solid darken($choices-primary-color, 10%);
margin-left: 4px;
margin-right: -4px;
padding-left: 6px;
padding-right: 6px;
line-height: 1;

View file

@ -41,15 +41,15 @@
<option value="Dropdown item 4" disabled>Dropdown item 4</option>
</select>
<label for="choices-9">With pre-selected option</label>
<select id="choices-9" name="choices-9" placeholder="Choose an option" multiple>
<label for="choices-8">With pre-selected option</label>
<select name="choices-8" id="choices-8" placeholder="Choose an option" multiple>
<option value="Dropdown item 1">Dropdown item 1</option>
<option value="Dropdown item 2" selected>Dropdown item 2</option>
<option value="Dropdown item 3">Dropdown item 3</option>
</select>
<label for="choices-10">Option groups</label>
<select id="choices-10" name="choices-10" placeholder="This is a placeholder" multiple>
<label for="choices-9">Option groups</label>
<select name="choices-9" id="choices-9" placeholder="This is a placeholder" multiple>
<optgroup label="UK">
<option value="London">London</option>
<option value="Manchester">Manchester</option>
@ -82,21 +82,24 @@
</optgroup>
</select>
<label for="choices-10">Options from remote source</label>
<select name="choices-10" id="choices-10" placeholder="Pick an Arctic Monkeys record" multiple></select>
<hr>
<h2>Single select input</h2>
<label for="choices-11">Default</label>
<select id="choices-11" name="choices-11" placeholder="This is a placeholder">
<select name="choices-11" id="choices-11" placeholder="This is a placeholder">
<option value="Dropdown item 1">Dropdown item 1</option>
<option value="Dropdown item 2">Dropdown item 2</option>
<option value="Dropdown item 3">Dropdown item 3</option>
</select>
<label for="choices-12">Options from remote source</label>
<select name="choices-12" id="choices-12" class="choices-ajax" placeholder="Pick an Arctic Monkeys record"></select>
<select name="choices-12" id="choices-12" placeholder="Pick an Arctic Monkeys record"></select>
<label for="choices-13">Option groups</label>
<select id="choices-13" name="choices-13" placeholder="This is a placeholder">
<select name="choices-13" id="choices-13" placeholder="This is a placeholder">
<optgroup label="UK">
<option value="London">London</option>
<option value="Manchester">Manchester</option>
@ -132,12 +135,11 @@
</div>
<script>
document.addEventListener('DOMContentLoaded', () => {
const firstElement = document.getElementById('choices-1');
const choices1 = new Choices(firstElement, {
const choices1 = new Choices(document.getElementById('choices-1'), {
delimiter: ',',
editItems: true,
maxItems: 5,
removeButton: true
removeItemButton: true
});
const choices2 = new Choices('#choices-2', {
@ -167,8 +169,26 @@
});
const choices7 = new Choices('#choices-7', { Search: false }).setValue(['Set value 1', 'Set value 2']);
const choicesAjax = new Choices('.choices-ajax', { placeholder: true, placeholderValue: 'Pick an Arctic Monkeys record'}).ajax((callback) => {
const choices10 = new Choices('#choices-10', {
placeholder: true,
placeholderValue: 'Pick an Strokes record'
}).ajax((callback) => {
fetch('https://api.discogs.com/artists/55980/releases?token=QBRmstCkwXEvCjTclCpumbtNwvVkEzGAdELXyRyW')
.then((response) => {
response.json().then(function(data) {
callback(data.releases, 'title', 'title');
});
})
.catch((error) => {
console.log(error);
});
});
const choices12 = new Choices('#choices-12', {
placeholder: true,
placeholderValue: 'Pick an Arctic Monkeys record'
}).ajax((callback) => {
fetch('https://api.discogs.com/artists/391170/releases?token=QBRmstCkwXEvCjTclCpumbtNwvVkEzGAdELXyRyW')
.then((response) => {
response.json().then(function(data) {
@ -178,13 +198,12 @@
.catch((error) => {
console.log(error);
});
})
});
const choicesMultiple = new Choices('select', {
placeholderValue: 'This is a placeholder set in the config',
removeButton: true
});
});
</script>
</body>