Skip to content

Commit 5ac9e73

Browse files
authored
feat(prompts): support readonly options in multiselect (#614)
1 parent cbeefcb commit 5ac9e73

6 files changed

Lines changed: 48 additions & 8 deletions

File tree

‎.changeset/quiet-moons-matter.md‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
---
2+
"@clack/prompts": minor
3+
"@clack/core": patch
4+
---
5+
6+
Allow `multiselect` to accept readonly option arrays, e.g. options declared with `as const`. The `Value` type parameter is now inferred as a literal union (`'a' | 'b'`) from such arrays, and `initialValues` also accepts readonly arrays.

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,13 @@ interface OptionLike {
88

99
export interface MultiSelectOptions<T extends OptionLike>
1010
extends PromptOptions<T['value'][], MultiSelectPrompt<T>> {
11-
options: T[];
12-
initialValues?: T['value'][];
11+
options: readonly T[];
12+
initialValues?: readonly T['value'][];
1313
required?: boolean;
1414
cursorAt?: T['value'];
1515
}
1616
export default class MultiSelectPrompt<T extends OptionLike> extends Prompt<T['value'][]> {
17-
options: T[];
17+
options: readonly T[];
1818
cursor = 0;
1919

2020
private get _value(): T['value'] {

‎packages/core/src/utils/cursor.ts‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
export function findCursor<T extends { disabled?: boolean }>(
22
cursor: number,
33
delta: number,
4-
options: T[]
4+
options: readonly T[]
55
) {
66
const hasEnabledOptions = options.some((opt) => !opt.disabled);
77
if (!hasEnabledOptions) {

‎packages/prompts/src/limit-options.ts‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ export interface LimitOptionsParams<TOption> extends CommonOptions {
1010
/**
1111
* The list of options to display.
1212
*/
13-
options: TOption[];
13+
options: readonly TOption[];
1414

1515
/**
1616
* The index of the currently active/selected option.

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,8 @@ export const MULTISELECT_INSTRUCTIONS = [
2323

2424
export interface MultiSelectOptions<Value> extends CommonOptions {
2525
message: string;
26-
options: Option<Value>[];
27-
initialValues?: Value[];
26+
options: readonly Option<Value>[];
27+
initialValues?: readonly Value[];
2828
maxItems?: number;
2929
required?: boolean;
3030
cursorAt?: Value;

‎packages/prompts/test/multi-select.test.ts‎

Lines changed: 35 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,14 @@
1-
import { afterAll, afterEach, beforeAll, beforeEach, describe, expect, test, vi } from 'vitest';
1+
import {
2+
afterAll,
3+
afterEach,
4+
beforeAll,
5+
beforeEach,
6+
describe,
7+
expect,
8+
expectTypeOf,
9+
test,
10+
vi,
11+
} from 'vitest';
212
import * as prompts from '../src/index.js';
313
import { MockReadable, MockWritable } from './test-utils.js';
414

@@ -492,4 +502,28 @@ describe.each(['true', 'false'])('multiselect (isCI = %s)', (isCI) => {
492502
await result;
493503
expect(output.buffer).toMatchSnapshot();
494504
});
505+
506+
test('accepts readonly options defined with `as const`', async () => {
507+
const options = [
508+
{ value: 'opt0', label: 'Option 0' },
509+
{ value: 'opt1', label: 'Option 1' },
510+
] as const;
511+
512+
const result = prompts.multiselect({
513+
message: 'foo',
514+
options,
515+
initialValues: ['opt1'] as const,
516+
input,
517+
output,
518+
});
519+
520+
input.emit('keypress', '', { name: 'space' });
521+
input.emit('keypress', '', { name: 'return' });
522+
523+
const value = await result;
524+
525+
expect(prompts.isCancel(value)).toBe(false);
526+
expect(value).toEqual(['opt1', 'opt0']);
527+
expectTypeOf(value).toEqualTypeOf<('opt0' | 'opt1')[] | typeof prompts.CANCEL_SYMBOL>();
528+
});
495529
});

0 commit comments

Comments
 (0)