From 5dfce8abffbece9cfbb5b591acf21af50766b23f Mon Sep 17 00:00:00 2001 From: Junseong Park <39112954+jsparkdev@users.noreply.github.com> Date: Tue, 8 Apr 2025 11:46:59 +0900 Subject: [PATCH] fix: use placeholder as value when input is empty (#263) Co-authored-by: Nate Moore --- .changeset/hot-turkeys-knock.md | 5 +++++ packages/core/src/prompts/prompt.ts | 5 +++++ packages/prompts/src/index.test.ts | 6 +++--- 3 files changed, 13 insertions(+), 3 deletions(-) create mode 100644 .changeset/hot-turkeys-knock.md diff --git a/.changeset/hot-turkeys-knock.md b/.changeset/hot-turkeys-knock.md new file mode 100644 index 0000000..0c86eb9 --- /dev/null +++ b/.changeset/hot-turkeys-knock.md @@ -0,0 +1,5 @@ +--- +"@clack/core": patch +--- + +Fixes an edge case for placeholder values. Previously, when pressing `enter` on an empty prompt, placeholder values would be ignored. Now, placeholder values are treated as the prompt value. diff --git a/packages/core/src/prompts/prompt.ts b/packages/core/src/prompts/prompt.ts index 00dc143..7d5c24a 100644 --- a/packages/core/src/prompts/prompt.ts +++ b/packages/core/src/prompts/prompt.ts @@ -205,6 +205,11 @@ export default class Prompt { } if (key?.name === 'return') { + if (!this.value && this.opts.placeholder) { + this.rl?.write(this.opts.placeholder); + this.emit('value', this.opts.placeholder); + } + if (this.opts.validate) { const problem = this.opts.validate(this.value); if (problem) { diff --git a/packages/prompts/src/index.test.ts b/packages/prompts/src/index.test.ts index 4e89f68..216dfa7 100644 --- a/packages/prompts/src/index.test.ts +++ b/packages/prompts/src/index.test.ts @@ -210,11 +210,11 @@ describe.each(['true', 'false'])('prompts (isCI = %s)', (isCI) => { input.emit('keypress', '', { name: 'return' }); - await result; + const value = await result; expect(output.buffer).toMatchSnapshot(); - // TODO (43081j): uncomment this when #263 is fixed - // expect(value).toBe('bar'); + + expect(value).toBe('bar'); }); test(' applies placeholder', async () => { -- 2.51.2