Skip to content

Commit 3e0bffd

Browse files
authored
fix(prompts): preserve falsy autocomplete initial values (#618)
1 parent 85f6aff commit 3e0bffd

3 files changed

Lines changed: 24 additions & 1 deletion

File tree

‎.changeset/curly-mails-glow.md‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@clack/prompts": patch
3+
---
4+
5+
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.

‎packages/prompts/src/autocomplete.ts‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -129,7 +129,7 @@ export interface AutocompleteOptions<Value> extends AutocompleteSharedOptions<Va
129129
export const autocomplete = <Value>(opts: AutocompleteOptions<Value>) => {
130130
const prompt = new AutocompletePrompt({
131131
options: opts.options,
132-
initialValue: opts.initialValue ? [opts.initialValue] : undefined,
132+
initialValue: opts.initialValue !== undefined ? [opts.initialValue] : undefined,
133133
initialUserInput: opts.initialUserInput,
134134
placeholder: opts.placeholder,
135135
completeOnTab: opts.completeOnTab,

‎packages/prompts/test/autocomplete.test.ts‎

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -185,6 +185,24 @@ describe('autocomplete', () => {
185185
expect(output.buffer).toMatchSnapshot();
186186
});
187187

188+
test('supports falsy initialValue values', async () => {
189+
for (const initialValue of [0, false, '']) {
190+
const input = new MockReadable();
191+
const output = new MockWritable();
192+
const result = autocomplete({
193+
message: 'Select a value',
194+
options: [{ value: 'other' }, { value: initialValue }],
195+
initialValue,
196+
input,
197+
output,
198+
});
199+
200+
input.emit('keypress', '', { name: 'return' });
201+
202+
expect(await result).toBe(initialValue);
203+
}
204+
});
205+
188206
test('can be aborted by a signal', async () => {
189207
const controller = new AbortController();
190208
const result = autocomplete({

0 commit comments

Comments
 (0)