Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -55,6 +56,7 @@ inquirer.prompt([{
pageSize: 10,
highlight: true,
searchable: true,
showStatus: true,
default: ['yellow', 'red'],
source: function(answersSoFar, input) {

Expand Down
24 changes: 17 additions & 7 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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);
}

/**
Expand Down