|
| 1 | +import Prompt, { type PromptOptions } from './prompt.ts'; |
| 2 | + |
| 3 | +interface GroupMultiSelectOptions<T extends { value: any }> |
| 4 | + extends PromptOptions<GroupMultiSelectPrompt<T>> { |
| 5 | + options: Record<string, T[]>; |
| 6 | + initialValues?: Array<T['value']>; |
| 7 | + required?: boolean; |
| 8 | + cursorAt?: T['value']; |
| 9 | + selectableGroups?: boolean; |
| 10 | +} |
| 11 | +export default class GroupMultiSelectPrompt<T extends { value: any }> extends Prompt { |
| 12 | + options: Array<T & { group: string | boolean }>; |
| 13 | + cursor: number = 0; |
| 14 | + #selectableGroups: boolean; |
| 15 | + |
| 16 | + getGroupItems(group: string): T[] { |
| 17 | + return this.options.filter((o) => o.group === group); |
| 18 | + } |
| 19 | + |
| 20 | + isGroupSelected(group: string): boolean { |
| 21 | + const items = this.getGroupItems(group); |
| 22 | + return this.#selectableGroups && items.every((i) => this.value.includes(i.value)); |
| 23 | + } |
| 24 | + |
| 25 | + private toggleValue() { |
| 26 | + const item = this.options[this.cursor]!; |
| 27 | + if (item.group === true) { |
| 28 | + const group = item.value; |
| 29 | + const groupedItems = this.getGroupItems(group); |
| 30 | + if (this.isGroupSelected(group)) { |
| 31 | + this.value = this.value.filter( |
| 32 | + (v: string) => groupedItems.findIndex((i) => i.value === v) === -1 |
| 33 | + ); |
| 34 | + } else { |
| 35 | + this.value = [...this.value, ...groupedItems.map((i) => i.value)]; |
| 36 | + } |
| 37 | + this.value = Array.from(new Set(this.value)); |
| 38 | + } else { |
| 39 | + const selected = this.value.includes(item.value); |
| 40 | + this.value = selected |
| 41 | + ? this.value.filter((v: T['value']) => v !== item.value) |
| 42 | + : [...this.value, item.value]; |
| 43 | + } |
| 44 | + } |
| 45 | + |
| 46 | + constructor(opts: GroupMultiSelectOptions<T>) { |
| 47 | + super(opts, false); |
| 48 | + const { options } = opts; |
| 49 | + this.#selectableGroups = opts.selectableGroups ?? true; |
| 50 | + this.options = Object.entries(options).flatMap(([key, option]) => [ |
| 51 | + { value: key, group: true, label: key }, |
| 52 | + ...option.map((opt) => ({ ...opt, group: key })) |
| 53 | + ]) as any; |
| 54 | + this.value = [...(opts.initialValues ?? [])]; |
| 55 | + this.cursor = Math.max( |
| 56 | + this.options.findIndex(({ value }) => value === opts.cursorAt), |
| 57 | + this.#selectableGroups ? 0 : 1 |
| 58 | + ); |
| 59 | + |
| 60 | + this.on('cursor', (key) => { |
| 61 | + switch (key) { |
| 62 | + case 'left': |
| 63 | + case 'up': |
| 64 | + this.cursor = this.cursor === 0 ? this.options.length - 1 : this.cursor - 1; |
| 65 | + if (!this.#selectableGroups && this.options[this.cursor]!.group === true) { |
| 66 | + this.cursor = this.cursor === 0 ? this.options.length - 1 : this.cursor - 1; |
| 67 | + } |
| 68 | + break; |
| 69 | + case 'down': |
| 70 | + case 'right': |
| 71 | + this.cursor = this.cursor === this.options.length - 1 ? 0 : this.cursor + 1; |
| 72 | + if (!this.#selectableGroups && this.options[this.cursor]!.group === true) { |
| 73 | + this.cursor = this.cursor === this.options.length - 1 ? 0 : this.cursor + 1; |
| 74 | + } |
| 75 | + break; |
| 76 | + case 'space': |
| 77 | + this.toggleValue(); |
| 78 | + break; |
| 79 | + } |
| 80 | + }); |
| 81 | + } |
| 82 | +} |
0 commit comments