Ability to pass function to no choice/result notices

This commit is contained in:
Josh Johnson 2017-02-09 13:49:30 +00:00
parent 473f010998
commit ccfad3cf9b
2 changed files with 15 additions and 7 deletions

View file

@ -341,18 +341,18 @@ const example = new Choices(element, {
**Usage:** The text that is shown whilst choices are being populated via AJAX.
### noResultsText
**Type:** `String` **Default:** `No results found`
**Type:** `String/Function` **Default:** `No results found`
**Input types affected:** `select-one`, `select-multiple`
**Usage:** The text that is shown when a user's search has returned no results.
**Usage:** The text that is shown when a user's search has returned no results. Optionally pass a function returning a string.
### noChoicesText
**Type:** `String` **Default:** `No choices to choose from`
**Type:** `String/Function` **Default:** `No choices to choose from`
**Input types affected:** `select-multiple`
**Usage:** The text that is shown when a user has selected all possible choices.
**Usage:** The text that is shown when a user has selected all possible choices. Optionally pass a function returning a string.
### itemSelectText
**Type:** `String` **Default:** `Press to select`

View file

@ -412,9 +412,17 @@ class Choices {
this._highlightChoice();
} else {
// Otherwise show a notice
const dropdownItem = this.isSearching ?
this._getTemplate('notice', this.config.noResultsText) :
this._getTemplate('notice', this.config.noChoicesText);
let dropdownItem;
let notice;
if (this.isSearching) {
notice = isType('Function', this.config.noResultsText) ? this.config.noResultsText() : this.config.noResultsText;
dropdownItem = this._getTemplate('notice', notice);
} else {
notice = isType('Function', this.config.noChoicesText) ? this.config.noChoicesText() : this.config.noChoicesText;
dropdownItem = this._getTemplate('notice', notice);
}
this.choiceList.appendChild(dropdownItem);
}
}