diff --git a/README.md b/README.md index c51b2b6..90b6d9e 100644 --- a/README.md +++ b/README.md @@ -35,6 +35,7 @@ The extra options that this plugin provides are: * **source**: (Function) a method that called to return a promise that should be resolved with a list of choices in a similar format as the `choices` option in the original `checkbox` prompt of `Inquirer`. * **highlight**: (Boolean) if `true`, the current selected choice gets highlighted. Default: `false`. * **searchable**: (Boolean) if `true`, allow the user to filter the list. The `source` function gets called everytime the search query is changed. Default: `false`. +* **showStatus**: (Boolean) if `true`, the current selected choices are shown just after the question. # Example @@ -55,6 +56,7 @@ inquirer.prompt([{ pageSize: 10, highlight: true, searchable: true, + showStatus: true, default: ['yellow', 'red'], source: function(answersSoFar, input) { diff --git a/index.js b/index.js index b389129..45aea8b 100644 --- a/index.js +++ b/index.js @@ -41,6 +41,11 @@ class CheckboxPlusPrompt extends Base { this.opt.searchable = false; } + // Default value for the showStatus option + if (typeof this.opt.showStatus == 'undefined') { + this.opt.showStatus = false; + } + // Default value for the default option if (typeof this.opt.default == 'undefined') { this.opt.default = null; @@ -210,9 +215,19 @@ class CheckboxPlusPrompt extends Base { // Answered if (this.status === 'answered') { - message += chalk.cyan(this.selection.join(', ')); + message += chalk.cyan(' ' + this.selection.join(', ')); return this.screen.render(message, bottomContent); + // Show selected values + } else if (this.opt.showStatus) { + message += chalk.gray( + '[' + + _.chain(this.checkedChoices) + .map((choice) => _.isObject(choice) ? choice.short || choice.name : choice) + .join(', ') + .value() + + '] ' + ); } // No search query is entered before @@ -442,20 +457,15 @@ class CheckboxPlusPrompt extends Base { // Remove the choice's value from the checked values _.remove(this.value, _.isEqual.bind(null, choice.value)); - // Remove the checkedChoices with the value of the current choice - _.remove(this.checkedChoices, function(checkedChoice) { - return _.isEqual(choice.value, checkedChoice.value); - }); - choice.checked = false; // Is the choice checked if (checked) { this.value.push(choice.value); - this.checkedChoices.push(choice); choice.checked = true; } + this.checkedChoices = _.filter(this.choices.realChoices, (choice) => choice.checked); } /**