Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/curly-mails-glow.md
Original file line number Diff line number Diff line change
@@ -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.
2 changes: 1 addition & 1 deletion packages/prompts/src/autocomplete.ts
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ export interface AutocompleteOptions<Value> extends AutocompleteSharedOptions<Va
export const autocomplete = <Value>(opts: AutocompleteOptions<Value>) => {
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,
Expand Down
18 changes: 18 additions & 0 deletions packages/prompts/test/autocomplete.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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({
Expand Down
Loading