Only clear choices if select input

This commit is contained in:
Josh Johnson 2016-09-27 14:13:41 +01:00
parent 1de237de85
commit b6fcefb4a5
2 changed files with 27 additions and 41 deletions

View file

@ -1128,10 +1128,12 @@ export default class Choices {
if (this.config.callbackOnItemSearch) {
const callback = this.config.callbackOnItemSearch;
if (isType('Function', callback)) {
// Reset choices
this._clearChoices();
// Reset loading state/text
this._handleLoadingState();
if(this.passedElement.type !== 'text') {
// Reset choices
this._clearChoices();
// Reset loading state/text
this._handleLoadingState();
}
// Run callback
callback(value, this._getAjaxCallback(), this.passedElement);
} else {
@ -1953,11 +1955,9 @@ export default class Choices {
const classNames = this.config.classNames;
const templates = {
containerOuter: (direction) => {
return strToEl(
`
return strToEl(`
<div class="${classNames.containerOuter}" data-type="${this.passedElement.type}" ${this.passedElement.type === 'select-one' ? 'tabindex="0"' : ''} aria-haspopup="true" aria-expanded="false" dir="${direction}"></div>
`
);
`);
},
containerInner: () => {
return strToEl(`
@ -1965,11 +1965,9 @@ export default class Choices {
`);
},
itemList: () => {
return strToEl(
`
return strToEl(`
<div class="${classNames.list} ${this.passedElement.type === 'select-one' ? classNames.listSingle : classNames.listItems}"></div>
`
);
`);
},
placeholder: (value) => {
return strToEl(`
@ -1978,57 +1976,44 @@ export default class Choices {
},
item: (data) => {
if (this.config.removeItemButton) {
return strToEl(
`
return strToEl(`
<div class="${classNames.item} ${data.highlighted ? classNames.highlightedState : ''} ${!data.disabled ? classNames.itemSelectable : ''}" data-item data-id="${data.id}" data-value="${data.value}" ${data.active ? 'aria-selected="true"' : ''} ${data.disabled ? 'aria-disabled="true"' : ''} data-deletable>
${data.label}<button class="${classNames.button}" data-button>Remove item</button>
</div>
`
);
`);
}
return strToEl(
`
return strToEl(`
<div class="${classNames.item} ${data.highlighted ? classNames.highlightedState : classNames.itemSelectable}" data-item data-id="${data.id}" data-value="${data.value}" ${data.active ? 'aria-selected="true"' : ''} ${data.disabled ? 'aria-disabled="true"' : ''}>
${data.label}
</div>
`
);
`);
},
choiceList: () => {
return strToEl(
`
return strToEl(`
<div class="${classNames.list}" dir="ltr" role="listbox" ${this.passedElement.type !== 'select-one' ? 'aria-multiselectable="true"' : ''}></div>
`
);
`);
},
choiceGroup: (data) => {
return strToEl(
`
return strToEl(`
<div class="${classNames.group} ${data.disabled ? classNames.itemDisabled : ''}" data-group data-id="${data.id}" data-value="${data.value}" role="group" ${data.disabled ? 'aria-disabled="true"' : ''}>
<div class="${classNames.groupHeading}">${data.value}</div>
</div>
`
);
`);
},
choice: (data) => {
return strToEl(
`
<div class="${classNames.item} ${classNames.itemChoice} ${data.disabled ? classNames.itemDisabled : classNames.itemSelectable}" data-select-text="${this.config.itemSelectText}" data-choice ${data.disabled ? 'data-choice-disabled aria-disabled="true"' : 'data-choice-selectable'} data-id="${data.id}" data-value="${data.value}" ${data.groupId > 0 ? 'role="treeitem"' : 'role="option"'}>
return strToEl(`
<div class="${classNames.item} ${classNames.itemChoice} ${data.disabled ? classNames.itemDisabled : classNames.itemSelectable}" data-select-text="${this.config.itemSelectText}" data-choice ${data.disabled ? 'data-choice-disabled aria-disabled="true"' : 'data-choice-selectable'} data-id="${data.id}" data-value="${data.value}" ${data.groupId > 0 ? 'role="treeitem"' : 'role="option"'}>
${data.label}
</div>
`
);
`);
},
input: () => {
return strToEl(
`
<input type="text" class="${classNames.input} ${classNames.inputCloned}" autocomplete="off" autocapitalize="off" spellcheck="false" role="textbox" aria-autocomplete="list">
`
);
return strToEl(`
<input type="text" class="${classNames.input} ${classNames.inputCloned}" autocomplete="off" autocapitalize="off" spellcheck="false" role="textbox" aria-autocomplete="list">
`);
},
dropdown: () => {
return strToEl(
`
return strToEl(`
<div class="${classNames.list} ${classNames.listDropdown}" aria-expanded="false"></div>
`);
},

View file

@ -140,6 +140,7 @@
<select class="form-control" name="choices-single-remote-fetch" id="choices-single-remote-fetch" placeholder="Pick an Arctic Monkeys record"></select>
<label for="choices-single-remove-xhr">Options from remote source (XHR) &amp; remove button</label>
<p><small>If this doesn't work, the Discogs rate limit has probably been reached. Try again later!</small></p>
<select class="form-control" name="choices-single-remove-xhr" id="choices-single-remove-xhr" placeholder="Pick a Smiths record"></select>
<label for="choices-single-groups">Option groups</label>
@ -335,7 +336,7 @@
removeItemButton: true,
callbackOnItemSearch: function (value, fn, passedInput) {
var request = new XMLHttpRequest();
var url = 'https://api.discogs.com/database/search?token=QBRmstCkwXEvCjTclCpumbtNwvVkEzGAdELXyRyW&q=' + value;
var url = 'https://api.discogs.com/database/search?token=QBRmstCkwXEvCjTclCpumbtNwvVkEzGAdELXyRyW&type=artist?q=' + value;
request.open('get', url, true);
request.onreadystatechange = function() {
var status;