Skip to content

Commit

Permalink
Add heading support to multiselect.
Browse files Browse the repository at this point in the history
  • Loading branch information
raineorshine committed Jun 16, 2022
1 parent a0c1777 commit d658297
Showing 1 changed file with 25 additions and 8 deletions.
33 changes: 25 additions & 8 deletions lib/elements/multiselect.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,9 @@ class MultiselectPrompt extends Prompt {
constructor(opts={}) {
super(opts);
this.msg = opts.message;
this.cursor = opts.cursor || 0;
// set the cursor to the first non-heading
this.cursorStart = opts.choices.findIndex(choice => !choice.heading);
this.cursor = opts.cursor || this.cursorStart;
this.scrollIndex = opts.cursor || 0;
this.hint = opts.hint || '';
this.warn = opts.warn || '- This option is disabled -';
Expand All @@ -39,7 +41,8 @@ class MultiselectPrompt extends Prompt {
description: ch && ch.description,
value: ch && (ch.value === undefined ? idx : ch.value),
selected: ch && ch.selected,
disabled: ch && ch.disabled
disabled: ch && ch.disabled,
heading: ch && ch.heading
};
});
this.clear = clear('', this.out.columns);
Expand All @@ -50,7 +53,7 @@ class MultiselectPrompt extends Prompt {

reset() {
this.value.map(v => !v.selected);
this.cursor = 0;
this.cursor = this.cursorStart;
this.fire();
this.render();
}
Expand Down Expand Up @@ -88,7 +91,7 @@ class MultiselectPrompt extends Prompt {
}

first() {
this.cursor = 0;
this.cursor = this.cursorStart
this.render();
}

Expand All @@ -98,23 +101,32 @@ class MultiselectPrompt extends Prompt {
}
next() {
this.cursor = (this.cursor + 1) % this.value.length;
if (this.value[this.cursor].heading) {
this.next();
}
this.render();
}

up() {
if (this.cursor === 0) {
if (this.cursor === this.cursorStart) {
this.cursor = this.value.length - 1;
} else {
this.cursor--;
if (this.value[this.cursor].heading) {
this.up();
}
}
this.render();
}

down() {
if (this.cursor === this.value.length - 1) {
this.cursor = 0;
this.cursor = this.cursorStart;
} else {
this.cursor++;
if (this.value[this.cursor].heading) {
this.down();
}
}
this.render();
}
Expand Down Expand Up @@ -150,7 +162,7 @@ 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 && !v.heading).forEach(v => v.selected = newSelected);
this.render();
}

Expand Down Expand Up @@ -184,7 +196,12 @@ class MultiselectPrompt extends Prompt {

if (v.disabled) {
title = cursor === i ? color.gray().underline(v.title) : color.strikethrough().gray(v.title);
} else {
}
else if(v.heading) {
title = v.title
return title + color.gray(desc || '');
}
else {
title = cursor === i ? color.cyan().underline(v.title) : v.title;
if (cursor === i && v.description) {
desc = ` - ${v.description}`;
Expand Down

0 comments on commit d658297

Please sign in to comment.