Skip to content

Commit 9fa2013

Browse files
authored
fix(#67): multiselect return (#70)
2 parents 71965b4 + 1251132 commit 9fa2013

File tree

4 files changed

+21
-17
lines changed

4 files changed

+21
-17
lines changed

.changeset/dull-tools-cry.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
---
2+
"@clack/core": patch
3+
"@clack/prompts": patch
4+
---
5+
6+
Multiselect: return `Value[]` instead of `Option[]`.

packages/core/src/prompts/multi-select.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,21 +11,21 @@ export default class MultiSelectPrompt<T extends { value: any }> extends Prompt
1111
cursor: number = 0;
1212

1313
private get _value() {
14-
return this.options[this.cursor];
14+
return this.options[this.cursor].value;
1515
}
1616

1717
private toggleValue() {
18-
const selected = this.value.some(({ value }: T) => value === this._value.value);
18+
const selected = this.value.includes(this._value);
1919
this.value = selected
20-
? this.value.filter(({ value }: T) => value !== this._value.value)
20+
? this.value.filter((value: T['value']) => value !== this._value)
2121
: [...this.value, this._value];
2222
}
2323

2424
constructor(opts: MultiSelectOptions<T>) {
2525
super(opts, false);
2626

2727
this.options = opts.options;
28-
this.value = this.options.filter(({ value }) => opts.initialValue?.includes(value));
28+
this.value = [...(opts.initialValue ?? [])];
2929
this.cursor = Math.max(
3030
this.options.findIndex(({ value }) => value === opts.cursorAt),
3131
0

packages/core/src/prompts/prompt.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ export interface PromptOptions<Self extends Prompt> {
4141
render(this: Omit<Self, 'prompt'>): string | void;
4242
placeholder?: string;
4343
initialValue?: any;
44-
validate?: ((value: string) => string | void) | undefined;
44+
validate?: ((value: any) => string | void) | undefined;
4545
input?: Readable;
4646
output?: Writable;
4747
debug?: boolean;

packages/prompts/src/index.ts

Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -254,8 +254,8 @@ export const multiselect = <Options extends Option<Value>[], Value extends Primi
254254
initialValue: opts.initialValue,
255255
required: opts.required ?? true,
256256
cursorAt: opts.cursorAt,
257-
validate(value) {
258-
if (this.required && value.length === 0)
257+
validate(selected: Value[]) {
258+
if (this.required && selected.length === 0)
259259
return `Please select at least one option.\n${color.reset(
260260
color.dim(
261261
`Press ${color.gray(color.bgWhite(color.inverse(' space ')))} to select, ${color.gray(
@@ -269,13 +269,15 @@ export const multiselect = <Options extends Option<Value>[], Value extends Primi
269269

270270
switch (this.state) {
271271
case 'submit': {
272-
return `${title}${color.gray(S_BAR)} ${this.value
273-
.map((option: Option<Value>) => opt(option, 'submitted'))
272+
return `${title}${color.gray(S_BAR)} ${this.options
273+
.filter(({ value }) => this.value.includes(value))
274+
.map((option) => opt(option, 'submitted'))
274275
.join(color.dim(', '))}`;
275276
}
276277
case 'cancel': {
277-
const label = this.value
278-
.map((option: Option<Value>) => opt(option, 'cancelled'))
278+
const label = this.options
279+
.filter(({ value }) => this.value.includes(value))
280+
.map((option) => opt(option, 'cancelled'))
279281
.join(color.dim(', '));
280282
return `${title}${color.gray(S_BAR)} ${
281283
label.trim() ? `${label}\n${color.gray(S_BAR)}` : ''
@@ -290,9 +292,7 @@ export const multiselect = <Options extends Option<Value>[], Value extends Primi
290292
.join('\n');
291293
return `${title}${color.yellow(S_BAR)} ${this.options
292294
.map((option, i) => {
293-
const selected = this.value.some(
294-
({ value }: Option<Value>) => value === option.value
295-
);
295+
const selected = this.value.includes(option.value);
296296
const active = i === this.cursor;
297297
if (active && selected) {
298298
return opt(option, 'active-selected');
@@ -307,9 +307,7 @@ export const multiselect = <Options extends Option<Value>[], Value extends Primi
307307
default: {
308308
return `${title}${color.cyan(S_BAR)} ${this.options
309309
.map((option, i) => {
310-
const selected = this.value.some(
311-
({ value }: Option<Value>) => value === option.value
312-
);
310+
const selected = this.value.includes(option.value);
313311
const active = i === this.cursor;
314312
if (active && selected) {
315313
return opt(option, 'active-selected');

0 commit comments

Comments
 (0)