|
| 1 | +import { GroupMultiSelectPrompt } from '@clack/core'; |
| 2 | +import color from 'picocolors'; |
| 3 | +import { |
| 4 | + type CommonOptions, |
| 5 | + S_BAR, |
| 6 | + S_BAR_END, |
| 7 | + S_CHECKBOX_ACTIVE, |
| 8 | + S_CHECKBOX_INACTIVE, |
| 9 | + S_CHECKBOX_SELECTED, |
| 10 | + symbol, |
| 11 | +} from './common.js'; |
| 12 | +import type { Option } from './select.js'; |
| 13 | + |
| 14 | +export interface GroupMultiSelectOptions<Value> extends CommonOptions { |
| 15 | + message: string; |
| 16 | + options: Record<string, Option<Value>[]>; |
| 17 | + initialValues?: Value[]; |
| 18 | + required?: boolean; |
| 19 | + cursorAt?: Value; |
| 20 | + selectableGroups?: boolean; |
| 21 | + groupSpacing?: number; |
| 22 | +} |
| 23 | +export const groupMultiselect = <Value>(opts: GroupMultiSelectOptions<Value>) => { |
| 24 | + const { selectableGroups = true, groupSpacing = 0 } = opts; |
| 25 | + const opt = ( |
| 26 | + option: Option<Value>, |
| 27 | + state: |
| 28 | + | 'inactive' |
| 29 | + | 'active' |
| 30 | + | 'selected' |
| 31 | + | 'active-selected' |
| 32 | + | 'group-active' |
| 33 | + | 'group-active-selected' |
| 34 | + | 'submitted' |
| 35 | + | 'cancelled', |
| 36 | + options: Option<Value>[] = [] |
| 37 | + ) => { |
| 38 | + const label = option.label ?? String(option.value); |
| 39 | + const isItem = typeof (option as any).group === 'string'; |
| 40 | + const next = isItem && (options[options.indexOf(option) + 1] ?? { group: true }); |
| 41 | + const isLast = isItem && (next as any).group === true; |
| 42 | + const prefix = isItem ? (selectableGroups ? `${isLast ? S_BAR_END : S_BAR} ` : ' ') : ''; |
| 43 | + const spacingPrefix = |
| 44 | + groupSpacing > 0 && !isItem ? `\n${color.cyan(S_BAR)} `.repeat(groupSpacing) : ''; |
| 45 | + |
| 46 | + if (state === 'active') { |
| 47 | + return `${spacingPrefix}${color.dim(prefix)}${color.cyan(S_CHECKBOX_ACTIVE)} ${label} ${ |
| 48 | + option.hint ? color.dim(`(${option.hint})`) : '' |
| 49 | + }`; |
| 50 | + } |
| 51 | + if (state === 'group-active') { |
| 52 | + return `${spacingPrefix}${prefix}${color.cyan(S_CHECKBOX_ACTIVE)} ${color.dim(label)}`; |
| 53 | + } |
| 54 | + if (state === 'group-active-selected') { |
| 55 | + return `${spacingPrefix}${prefix}${color.green(S_CHECKBOX_SELECTED)} ${color.dim(label)}`; |
| 56 | + } |
| 57 | + if (state === 'selected') { |
| 58 | + const selectedCheckbox = isItem || selectableGroups ? color.green(S_CHECKBOX_SELECTED) : ''; |
| 59 | + return `${spacingPrefix}${color.dim(prefix)}${selectedCheckbox} ${color.dim(label)} ${ |
| 60 | + option.hint ? color.dim(`(${option.hint})`) : '' |
| 61 | + }`; |
| 62 | + } |
| 63 | + if (state === 'cancelled') { |
| 64 | + return `${color.strikethrough(color.dim(label))}`; |
| 65 | + } |
| 66 | + if (state === 'active-selected') { |
| 67 | + return `${spacingPrefix}${color.dim(prefix)}${color.green(S_CHECKBOX_SELECTED)} ${label} ${ |
| 68 | + option.hint ? color.dim(`(${option.hint})`) : '' |
| 69 | + }`; |
| 70 | + } |
| 71 | + if (state === 'submitted') { |
| 72 | + return `${color.dim(label)}`; |
| 73 | + } |
| 74 | + const unselectedCheckbox = isItem || selectableGroups ? color.dim(S_CHECKBOX_INACTIVE) : ''; |
| 75 | + return `${spacingPrefix}${color.dim(prefix)}${unselectedCheckbox} ${color.dim(label)}`; |
| 76 | + }; |
| 77 | + |
| 78 | + return new GroupMultiSelectPrompt({ |
| 79 | + options: opts.options, |
| 80 | + input: opts.input, |
| 81 | + output: opts.output, |
| 82 | + initialValues: opts.initialValues, |
| 83 | + required: opts.required ?? true, |
| 84 | + cursorAt: opts.cursorAt, |
| 85 | + selectableGroups, |
| 86 | + validate(selected: Value[]) { |
| 87 | + if (this.required && selected.length === 0) |
| 88 | + return `Please select at least one option.\n${color.reset( |
| 89 | + color.dim( |
| 90 | + `Press ${color.gray(color.bgWhite(color.inverse(' space ')))} to select, ${color.gray( |
| 91 | + color.bgWhite(color.inverse(' enter ')) |
| 92 | + )} to submit` |
| 93 | + ) |
| 94 | + )}`; |
| 95 | + }, |
| 96 | + render() { |
| 97 | + const title = `${color.gray(S_BAR)}\n${symbol(this.state)} ${opts.message}\n`; |
| 98 | + |
| 99 | + switch (this.state) { |
| 100 | + case 'submit': { |
| 101 | + return `${title}${color.gray(S_BAR)} ${this.options |
| 102 | + .filter(({ value }) => this.value.includes(value)) |
| 103 | + .map((option) => opt(option, 'submitted')) |
| 104 | + .join(color.dim(', '))}`; |
| 105 | + } |
| 106 | + case 'cancel': { |
| 107 | + const label = this.options |
| 108 | + .filter(({ value }) => this.value.includes(value)) |
| 109 | + .map((option) => opt(option, 'cancelled')) |
| 110 | + .join(color.dim(', ')); |
| 111 | + return `${title}${color.gray(S_BAR)} ${ |
| 112 | + label.trim() ? `${label}\n${color.gray(S_BAR)}` : '' |
| 113 | + }`; |
| 114 | + } |
| 115 | + case 'error': { |
| 116 | + const footer = this.error |
| 117 | + .split('\n') |
| 118 | + .map((ln, i) => |
| 119 | + i === 0 ? `${color.yellow(S_BAR_END)} ${color.yellow(ln)}` : ` ${ln}` |
| 120 | + ) |
| 121 | + .join('\n'); |
| 122 | + return `${title}${color.yellow(S_BAR)} ${this.options |
| 123 | + .map((option, i, options) => { |
| 124 | + const selected = |
| 125 | + this.value.includes(option.value) || |
| 126 | + (option.group === true && this.isGroupSelected(`${option.value}`)); |
| 127 | + const active = i === this.cursor; |
| 128 | + const groupActive = |
| 129 | + !active && |
| 130 | + typeof option.group === 'string' && |
| 131 | + this.options[this.cursor].value === option.group; |
| 132 | + if (groupActive) { |
| 133 | + return opt(option, selected ? 'group-active-selected' : 'group-active', options); |
| 134 | + } |
| 135 | + if (active && selected) { |
| 136 | + return opt(option, 'active-selected', options); |
| 137 | + } |
| 138 | + if (selected) { |
| 139 | + return opt(option, 'selected', options); |
| 140 | + } |
| 141 | + return opt(option, active ? 'active' : 'inactive', options); |
| 142 | + }) |
| 143 | + .join(`\n${color.yellow(S_BAR)} `)}\n${footer}\n`; |
| 144 | + } |
| 145 | + default: { |
| 146 | + return `${title}${color.cyan(S_BAR)} ${this.options |
| 147 | + .map((option, i, options) => { |
| 148 | + const selected = |
| 149 | + this.value.includes(option.value) || |
| 150 | + (option.group === true && this.isGroupSelected(`${option.value}`)); |
| 151 | + const active = i === this.cursor; |
| 152 | + const groupActive = |
| 153 | + !active && |
| 154 | + typeof option.group === 'string' && |
| 155 | + this.options[this.cursor].value === option.group; |
| 156 | + if (groupActive) { |
| 157 | + return opt(option, selected ? 'group-active-selected' : 'group-active', options); |
| 158 | + } |
| 159 | + if (active && selected) { |
| 160 | + return opt(option, 'active-selected', options); |
| 161 | + } |
| 162 | + if (selected) { |
| 163 | + return opt(option, 'selected', options); |
| 164 | + } |
| 165 | + return opt(option, active ? 'active' : 'inactive', options); |
| 166 | + }) |
| 167 | + .join(`\n${color.cyan(S_BAR)} `)}\n${color.cyan(S_BAR_END)}\n`; |
| 168 | + } |
| 169 | + } |
| 170 | + }, |
| 171 | + }).prompt() as Promise<Value[] | symbol>; |
| 172 | +}; |
0 commit comments