diff --git a/.changeset/curly-mails-glow.md b/.changeset/curly-mails-glow.md new file mode 100644 index 00000000..4d2306a8 --- /dev/null +++ b/.changeset/curly-mails-glow.md @@ -0,0 +1,5 @@ +--- +"@clack/prompts": patch +--- + +Use an explicit undefined check when forwarding autocomplete initialValue so 0, false, and empty strings remain selectable instead of falling back to the first option. diff --git a/packages/prompts/src/autocomplete.ts b/packages/prompts/src/autocomplete.ts index f3dd1465..0cfc5165 100644 --- a/packages/prompts/src/autocomplete.ts +++ b/packages/prompts/src/autocomplete.ts @@ -129,7 +129,7 @@ export interface AutocompleteOptions extends AutocompleteSharedOptions(opts: AutocompleteOptions) => { const prompt = new AutocompletePrompt({ options: opts.options, - initialValue: opts.initialValue ? [opts.initialValue] : undefined, + initialValue: opts.initialValue !== undefined ? [opts.initialValue] : undefined, initialUserInput: opts.initialUserInput, placeholder: opts.placeholder, completeOnTab: opts.completeOnTab, diff --git a/packages/prompts/test/autocomplete.test.ts b/packages/prompts/test/autocomplete.test.ts index fe55444c..7351bab2 100644 --- a/packages/prompts/test/autocomplete.test.ts +++ b/packages/prompts/test/autocomplete.test.ts @@ -185,6 +185,24 @@ describe('autocomplete', () => { expect(output.buffer).toMatchSnapshot(); }); + test('supports falsy initialValue values', async () => { + for (const initialValue of [0, false, '']) { + const input = new MockReadable(); + const output = new MockWritable(); + const result = autocomplete({ + message: 'Select a value', + options: [{ value: 'other' }, { value: initialValue }], + initialValue, + input, + output, + }); + + input.emit('keypress', '', { name: 'return' }); + + expect(await result).toBe(initialValue); + } + }); + test('can be aborted by a signal', async () => { const controller = new AbortController(); const result = autocomplete({