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
15 changes: 14 additions & 1 deletion lib/elements/multiselect.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ class MultiselectPrompt extends Prompt {
this.maxChoices = opts.max;
this.instructions = opts.instructions;
this.optionsPerPage = opts.optionsPerPage || 10;
this.selectionIndex = 0;
this.value = opts.choices.map((ch, idx) => {
if (typeof ch === 'string')
ch = {title: ch, value: idx};
Expand All @@ -39,6 +40,7 @@ class MultiselectPrompt extends Prompt {
description: ch && ch.description,
value: ch && (ch.value === undefined ? idx : ch.value),
selected: ch && ch.selected,
selectionIndex: ch && ch.selectionIndex,
disabled: ch && ch.disabled
};
});
Expand Down Expand Up @@ -121,12 +123,15 @@ class MultiselectPrompt extends Prompt {

left() {
this.value[this.cursor].selected = false;
this.value[this.cursor].selectionIndex = -1;
this.render();
}

right() {
if (this.value.filter(e => e.selected).length >= this.maxChoices) return this.bell();
this.value[this.cursor].selected = true;
this.value[this.cursor].selectionIndex = this.selectionIndex;
this.selectionIndex++;
this.render();
}

Expand All @@ -135,11 +140,14 @@ class MultiselectPrompt extends Prompt {

if (v.selected) {
v.selected = false;
v.selectionIndex = -1;
this.render();
} else if (v.disabled || this.value.filter(e => e.selected).length >= this.maxChoices) {
return this.bell();
} else {
v.selected = true;
v.selectionIndex = this.selectionIndex;
this.selectionIndex++;
this.render();
}
}
Expand All @@ -150,7 +158,11 @@ class MultiselectPrompt extends Prompt {
}

const newSelected = !this.value[this.cursor].selected;
this.value.filter(v => !v.disabled).forEach(v => v.selected = newSelected);
this.value.filter(v => !v.disabled).forEach(v => {
v.selected = newSelected;
v.selectionIndex = newSelected ? this.selectionIndex : -1;
this.selectionIndex = newSelected ? this.selectionIndex + 1 : 0;
});
this.render();
}

Expand Down Expand Up @@ -233,6 +245,7 @@ class MultiselectPrompt extends Prompt {
if (this.done) {
return this.value
.filter(e => e.selected)
.sort((a, b) => a.selectionIndex - b.selectionIndex)
.map(v => v.title)
.join(', ');
}
Expand Down
2 changes: 1 addition & 1 deletion lib/prompts.js
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,7 @@ $.select = args => toPrompt('SelectPrompt', args);
*/
$.multiselect = args => {
args.choices = [].concat(args.choices || []);
const toSelected = items => items.filter(item => item.selected).map(item => item.value);
const toSelected = items => items.filter(item => item.selected).sort((a, b) => a.selectionIndex - b.selectionIndex).map(item => item.value);
return toPrompt('MultiselectPrompt', args, {
onAbort: toSelected,
onSubmit: toSelected
Expand Down