From ccfad3cf9b8e2d019bd3cdbb0323a45efa1eb765 Mon Sep 17 00:00:00 2001 From: Josh Johnson Date: Thu, 9 Feb 2017 13:49:30 +0000 Subject: [PATCH] Ability to pass function to no choice/result notices --- README.md | 8 ++++---- assets/scripts/src/choices.js | 14 +++++++++++--- 2 files changed, 15 insertions(+), 7 deletions(-) diff --git a/README.md b/README.md index 0d2618c..0ca32eb 100644 --- a/README.md +++ b/README.md @@ -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` diff --git a/assets/scripts/src/choices.js b/assets/scripts/src/choices.js index 4fe96ef..f40c5d9 100644 --- a/assets/scripts/src/choices.js +++ b/assets/scripts/src/choices.js @@ -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); } }