Skip to content

Commit d658297

Browse files
committed
Add heading support to multiselect.
1 parent a0c1777 commit d658297

File tree

1 file changed

+25
-8
lines changed

1 file changed

+25
-8
lines changed

lib/elements/multiselect.js

+25-8
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,9 @@ class MultiselectPrompt extends Prompt {
2222
constructor(opts={}) {
2323
super(opts);
2424
this.msg = opts.message;
25-
this.cursor = opts.cursor || 0;
25+
// set the cursor to the first non-heading
26+
this.cursorStart = opts.choices.findIndex(choice => !choice.heading);
27+
this.cursor = opts.cursor || this.cursorStart;
2628
this.scrollIndex = opts.cursor || 0;
2729
this.hint = opts.hint || '';
2830
this.warn = opts.warn || '- This option is disabled -';
@@ -39,7 +41,8 @@ class MultiselectPrompt extends Prompt {
3941
description: ch && ch.description,
4042
value: ch && (ch.value === undefined ? idx : ch.value),
4143
selected: ch && ch.selected,
42-
disabled: ch && ch.disabled
44+
disabled: ch && ch.disabled,
45+
heading: ch && ch.heading
4346
};
4447
});
4548
this.clear = clear('', this.out.columns);
@@ -50,7 +53,7 @@ class MultiselectPrompt extends Prompt {
5053

5154
reset() {
5255
this.value.map(v => !v.selected);
53-
this.cursor = 0;
56+
this.cursor = this.cursorStart;
5457
this.fire();
5558
this.render();
5659
}
@@ -88,7 +91,7 @@ class MultiselectPrompt extends Prompt {
8891
}
8992

9093
first() {
91-
this.cursor = 0;
94+
this.cursor = this.cursorStart
9295
this.render();
9396
}
9497

@@ -98,23 +101,32 @@ class MultiselectPrompt extends Prompt {
98101
}
99102
next() {
100103
this.cursor = (this.cursor + 1) % this.value.length;
104+
if (this.value[this.cursor].heading) {
105+
this.next();
106+
}
101107
this.render();
102108
}
103109

104110
up() {
105-
if (this.cursor === 0) {
111+
if (this.cursor === this.cursorStart) {
106112
this.cursor = this.value.length - 1;
107113
} else {
108114
this.cursor--;
115+
if (this.value[this.cursor].heading) {
116+
this.up();
117+
}
109118
}
110119
this.render();
111120
}
112121

113122
down() {
114123
if (this.cursor === this.value.length - 1) {
115-
this.cursor = 0;
124+
this.cursor = this.cursorStart;
116125
} else {
117126
this.cursor++;
127+
if (this.value[this.cursor].heading) {
128+
this.down();
129+
}
118130
}
119131
this.render();
120132
}
@@ -150,7 +162,7 @@ class MultiselectPrompt extends Prompt {
150162
}
151163

152164
const newSelected = !this.value[this.cursor].selected;
153-
this.value.filter(v => !v.disabled).forEach(v => v.selected = newSelected);
165+
this.value.filter(v => !v.disabled && !v.heading).forEach(v => v.selected = newSelected);
154166
this.render();
155167
}
156168

@@ -184,7 +196,12 @@ class MultiselectPrompt extends Prompt {
184196

185197
if (v.disabled) {
186198
title = cursor === i ? color.gray().underline(v.title) : color.strikethrough().gray(v.title);
187-
} else {
199+
}
200+
else if(v.heading) {
201+
title = v.title
202+
return title + color.gray(desc || '');
203+
}
204+
else {
188205
title = cursor === i ? color.cyan().underline(v.title) : v.title;
189206
if (cursor === i && v.description) {
190207
desc = ` - ${v.description}`;

0 commit comments

Comments
 (0)