From 496cd49bf1999748ac71c90c66592501f90c1bd6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Oskar=20L=C3=B6fgren?= <516549+ulken@users.noreply.github.com> Date: Tue, 21 Feb 2023 12:55:41 +0100 Subject: [PATCH 1/3] fix(#67): return `Value[]` instead of `Option[]` --- packages/core/src/prompts/multi-select.ts | 8 ++++---- packages/prompts/src/index.ts | 22 ++++++++++------------ 2 files changed, 14 insertions(+), 16 deletions(-) diff --git a/packages/core/src/prompts/multi-select.ts b/packages/core/src/prompts/multi-select.ts index 8ffb13d0..67e69d58 100644 --- a/packages/core/src/prompts/multi-select.ts +++ b/packages/core/src/prompts/multi-select.ts @@ -11,13 +11,13 @@ export default class MultiSelectPrompt extends Prompt cursor: number = 0; private get _value() { - return this.options[this.cursor]; + return this.options[this.cursor].value; } private toggleValue() { - const selected = this.value.some(({ value }: T) => value === this._value.value); + const selected = this.value.includes(this._value); this.value = selected - ? this.value.filter(({ value }: T) => value !== this._value.value) + ? this.value.filter((value: T['value']) => value !== this._value) : [...this.value, this._value]; } @@ -25,7 +25,7 @@ export default class MultiSelectPrompt extends Prompt super(opts, false); this.options = opts.options; - this.value = this.options.filter(({ value }) => opts.initialValue?.includes(value)); + this.value = [...(opts.initialValue ?? [])]; this.cursor = Math.max( this.options.findIndex(({ value }) => value === opts.cursorAt), 0 diff --git a/packages/prompts/src/index.ts b/packages/prompts/src/index.ts index 3b143bcb..7bd4389f 100644 --- a/packages/prompts/src/index.ts +++ b/packages/prompts/src/index.ts @@ -221,8 +221,8 @@ export const multiselect = [], Value extends Primi initialValue: opts.initialValue, required: opts.required ?? true, cursorAt: opts.cursorAt, - validate(value) { - if (this.required && value.length === 0) + validate(selected: Value[]) { + if (this.required && selected.length === 0) return `Please select at least one option.\n${color.reset( color.dim( `Press ${color.gray(color.bgWhite(color.inverse(' space ')))} to select, ${color.gray( @@ -236,13 +236,15 @@ export const multiselect = [], Value extends Primi switch (this.state) { case 'submit': { - return `${title}${color.gray(S_BAR)} ${this.value - .map((option: Option) => opt(option, 'submitted')) + return `${title}${color.gray(S_BAR)} ${this.options + .filter(({ value }) => this.value.includes(value)) + .map((option) => opt(option, 'submitted')) .join(color.dim(', '))}`; } case 'cancel': { - const label = this.value - .map((option: Option) => opt(option, 'cancelled')) + const label = this.options + .filter(({ value }) => this.value.includes(value)) + .map((option) => opt(option, 'cancelled')) .join(color.dim(', ')); return `${title}${color.gray(S_BAR)} ${ label.trim() ? `${label}\n${color.gray(S_BAR)}` : '' @@ -257,9 +259,7 @@ export const multiselect = [], Value extends Primi .join('\n'); return `${title}${color.yellow(S_BAR)} ${this.options .map((option, i) => { - const selected = this.value.some( - ({ value }: Option) => value === option.value - ); + const selected = this.value.includes(option.value); const active = i === this.cursor; if (active && selected) { return opt(option, 'active-selected'); @@ -274,9 +274,7 @@ export const multiselect = [], Value extends Primi default: { return `${title}${color.cyan(S_BAR)} ${this.options .map((option, i) => { - const selected = this.value.some( - ({ value }: Option) => value === option.value - ); + const selected = this.value.includes(option.value); const active = i === this.cursor; if (active && selected) { return opt(option, 'active-selected'); From 861164d8faffdf71880859bfceb5aade6e8d312c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Oskar=20L=C3=B6fgren?= <516549+ulken@users.noreply.github.com> Date: Tue, 21 Feb 2023 12:56:08 +0100 Subject: [PATCH 2/3] fix type of validate parameter --- packages/core/src/prompts/prompt.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/core/src/prompts/prompt.ts b/packages/core/src/prompts/prompt.ts index 5d6f68a7..0b6fe794 100644 --- a/packages/core/src/prompts/prompt.ts +++ b/packages/core/src/prompts/prompt.ts @@ -41,7 +41,7 @@ export interface PromptOptions { render(this: Omit): string | void; placeholder?: string; initialValue?: any; - validate?: ((value: string) => string | void) | undefined; + validate?: ((value: any) => string | void) | undefined; input?: Readable; output?: Writable; debug?: boolean; From 1251132bcf4bd5f27cf5dd95e89a63c0a4fd89cf Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Oskar=20L=C3=B6fgren?= <516549+ulken@users.noreply.github.com> Date: Tue, 21 Feb 2023 13:33:07 +0100 Subject: [PATCH 3/3] changeset --- .changeset/dull-tools-cry.md | 6 ++++++ 1 file changed, 6 insertions(+) create mode 100644 .changeset/dull-tools-cry.md diff --git a/.changeset/dull-tools-cry.md b/.changeset/dull-tools-cry.md new file mode 100644 index 00000000..0119ae62 --- /dev/null +++ b/.changeset/dull-tools-cry.md @@ -0,0 +1,6 @@ +--- +"@clack/core": patch +"@clack/prompts": patch +--- + +Multiselect: return `Value[]` instead of `Option[]`.