Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
19 changes: 16 additions & 3 deletions packages/core/src/prompts/group-multiselect.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,12 @@ interface GroupMultiSelectOptions<T extends { value: any }>
initialValues?: T['value'][];
required?: boolean;
cursorAt?: T['value'];
selectableGroups?: boolean;
}
export default class GroupMultiSelectPrompt<T extends { value: any }> extends Prompt {
options: (T & { group: string | boolean })[];
cursor = 0;
#selectableGroups: boolean;

getGroupItems(group: string): T[] {
return this.options.filter((o) => o.group === group);
Expand Down Expand Up @@ -44,26 +46,37 @@ export default class GroupMultiSelectPrompt<T extends { value: any }> extends Pr
constructor(opts: GroupMultiSelectOptions<T>) {
super(opts, false);
const { options } = opts;
this.#selectableGroups = opts.selectableGroups !== false;
this.options = Object.entries(options).flatMap(([key, option]) => [
{ value: key, group: true, label: key },
...option.map((opt) => ({ ...opt, group: key })),
]) as any;
this.value = [...(opts.initialValues ?? [])];
this.cursor = Math.max(
this.options.findIndex(({ value }) => value === opts.cursorAt),
0
this.#selectableGroups ? 0 : 1
);

this.on('cursor', (key) => {
switch (key) {
case 'left':
case 'up':
case 'up': {
this.cursor = this.cursor === 0 ? this.options.length - 1 : this.cursor - 1;
const currentIsGroup = this.options[this.cursor]?.group === true;
if (!this.#selectableGroups && currentIsGroup) {
this.cursor = this.cursor === 0 ? this.options.length - 1 : this.cursor - 1;
}
break;
}
case 'down':
case 'right':
case 'right': {
this.cursor = this.cursor === this.options.length - 1 ? 0 : this.cursor + 1;
const currentIsGroup = this.options[this.cursor]?.group === true;
if (!this.#selectableGroups && currentIsGroup) {
this.cursor = this.cursor === this.options.length - 1 ? 0 : this.cursor + 1;
}
break;
}
case 'space':
this.toggleValue();
break;
Expand Down
2 changes: 2 additions & 0 deletions packages/prompts/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -462,6 +462,7 @@ export interface GroupMultiSelectOptions<Value> {
initialValues?: Value[];
required?: boolean;
cursorAt?: Value;
selectableGroups?: boolean;
}
export const groupMultiselect = <Value>(opts: GroupMultiSelectOptions<Value>) => {
const opt = (
Expand Down Expand Up @@ -516,6 +517,7 @@ export const groupMultiselect = <Value>(opts: GroupMultiSelectOptions<Value>) =>
initialValues: opts.initialValues,
required: opts.required ?? true,
cursorAt: opts.cursorAt,
selectableGroups: opts.selectableGroups,
validate(selected: Value[]) {
if (this.required && selected.length === 0)
return `Please select at least one option.\n${color.reset(
Expand Down