diff --git a/lib/elements/multiselect.js b/lib/elements/multiselect.js index 99b393f..dde54f9 100644 --- a/lib/elements/multiselect.js +++ b/lib/elements/multiselect.js @@ -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 -'; @@ -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); @@ -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(); } @@ -88,7 +91,7 @@ class MultiselectPrompt extends Prompt { } first() { - this.cursor = 0; + this.cursor = this.cursorStart this.render(); } @@ -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(); } @@ -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(); } @@ -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}`;