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/brown-banks-play.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@clack/prompts": patch
---

Fix placeholder rendering when using autocomplete.
3 changes: 1 addition & 2 deletions packages/prompts/src/autocomplete.ts
Original file line number Diff line number Diff line change
Expand Up @@ -91,10 +91,9 @@ export const autocomplete = <Value>(opts: AutocompleteOptions<Value>) => {
// Title and message display
const headings = [`${color.gray(S_BAR)}`, `${symbol(this.state)} ${opts.message}`];
const userInput = this.userInput;
const valueAsString = String(this.value ?? '');
const options = this.options;
const placeholder = opts.placeholder;
const showPlaceholder = valueAsString === '' && placeholder !== undefined;
const showPlaceholder = userInput === '' && placeholder !== undefined;

// Handle different states
switch (this.state) {
Expand Down
33 changes: 33 additions & 0 deletions packages/prompts/test/__snapshots__/autocomplete.test.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,39 @@ exports[`autocomplete > limits displayed options when maxItems is set 1`] = `
]
`;

exports[`autocomplete > placeholder is shown if set 1`] = `
[
"<cursor.hide>",
"│
◆ Select a fruit
│
│ Search: Type to search...
│ ● Apple
│ ○ Banana
│ ○ Cherry
│ ○ Grape
│ ○ Orange
│ ↑/↓ to select • Enter: confirm • Type: to search
└",
"<cursor.backward count=999><cursor.up count=10>",
"<cursor.down count=3>",
"<erase.down>",
"│ Search: g█ (2 matches)
│ ● Grape
│ ○ Orange
│ ↑/↓ to select • Enter: confirm • Type: to search
└",
"<cursor.backward count=999><cursor.up count=7>",
"<cursor.down count=1>",
"<erase.down>",
"◇ Select a fruit
│ Grape",
"
",
"<cursor.show>",
]
`;

exports[`autocomplete > renders bottom ellipsis when items do not fit 1`] = `
[
"<cursor.hide>",
Expand Down
16 changes: 16 additions & 0 deletions packages/prompts/test/autocomplete.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -225,6 +225,22 @@ describe('autocomplete', () => {
await result;
expect(output.buffer).toMatchSnapshot();
});

test('placeholder is shown if set', async () => {
const result = autocomplete({
message: 'Select a fruit',
placeholder: 'Type to search...',
options: testOptions,
input,
output,
});

input.emit('keypress', 'g', { name: 'g' });
input.emit('keypress', '', { name: 'return' });
const value = await result;
expect(output.buffer).toMatchSnapshot();
expect(value).toBe('grape');
});
});

describe('autocompleteMultiselect', () => {
Expand Down
Loading