From e0026f6c2d258419c1d97e6fc3c2ebe1a18f539e Mon Sep 17 00:00:00 2001 From: Junseong Park <39112954+jsparkdev@users.noreply.github.com> Date: Tue, 1 Apr 2025 15:15:47 +0900 Subject: [PATCH 01/10] fix: update `text` function --- packages/prompts/src/index.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/prompts/src/index.ts b/packages/prompts/src/index.ts index 01cd736b..d4a5aa93 100644 --- a/packages/prompts/src/index.ts +++ b/packages/prompts/src/index.ts @@ -108,7 +108,7 @@ export const text = (opts: TextOptions) => { return new TextPrompt({ validate: opts.validate, placeholder: opts.placeholder, - defaultValue: opts.defaultValue, + defaultValue: opts.defaultValue || opts.placeholder, initialValue: opts.initialValue, render() { const title = `${color.gray(S_BAR)}\n${symbol(this.state)} ${opts.message}\n`; From e1af748bdc8c6d9b646e0ed571128f249f3f67b0 Mon Sep 17 00:00:00 2001 From: Junseong Park <39112954+jsparkdev@users.noreply.github.com> Date: Wed, 2 Apr 2025 22:45:13 +0900 Subject: [PATCH 02/10] update --- packages/core/src/prompts/text.ts | 2 +- packages/prompts/src/index.ts | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/packages/core/src/prompts/text.ts b/packages/core/src/prompts/text.ts index 37931275..b0febf09 100644 --- a/packages/core/src/prompts/text.ts +++ b/packages/core/src/prompts/text.ts @@ -26,7 +26,7 @@ export default class TextPrompt extends Prompt { this.on('finalize', () => { if (!this.value) { - this.value = opts.defaultValue; + this.value = opts.defaultValue || opts.placeholder || ''; } }); } diff --git a/packages/prompts/src/index.ts b/packages/prompts/src/index.ts index d4a5aa93..01cd736b 100644 --- a/packages/prompts/src/index.ts +++ b/packages/prompts/src/index.ts @@ -108,7 +108,7 @@ export const text = (opts: TextOptions) => { return new TextPrompt({ validate: opts.validate, placeholder: opts.placeholder, - defaultValue: opts.defaultValue || opts.placeholder, + defaultValue: opts.defaultValue, initialValue: opts.initialValue, render() { const title = `${color.gray(S_BAR)}\n${symbol(this.state)} ${opts.message}\n`; From 5cd18ea66abddbbf88986c0a62fff063b4579704 Mon Sep 17 00:00:00 2001 From: Junseong Park <39112954+jsparkdev@users.noreply.github.com> Date: Thu, 3 Apr 2025 15:17:04 +0900 Subject: [PATCH 03/10] update for empty string default value --- packages/core/src/prompts/text.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/core/src/prompts/text.ts b/packages/core/src/prompts/text.ts index b0febf09..603f1fa3 100644 --- a/packages/core/src/prompts/text.ts +++ b/packages/core/src/prompts/text.ts @@ -26,7 +26,7 @@ export default class TextPrompt extends Prompt { this.on('finalize', () => { if (!this.value) { - this.value = opts.defaultValue || opts.placeholder || ''; + this.value = opts.defaultValue ?? opts.placeholder ?? ''; } }); } From 885d37e354ba0522c9d6cda1a0c24fad1c9abfb0 Mon Sep 17 00:00:00 2001 From: Junseong Park <39112954+jsparkdev@users.noreply.github.com> Date: Thu, 3 Apr 2025 15:54:19 +0900 Subject: [PATCH 04/10] update logic for pressing enter key without input value --- packages/core/src/prompts/prompt.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/core/src/prompts/prompt.ts b/packages/core/src/prompts/prompt.ts index c676d1a5..4a321c8d 100644 --- a/packages/core/src/prompts/prompt.ts +++ b/packages/core/src/prompts/prompt.ts @@ -194,7 +194,7 @@ export default class Prompt { if (char && (char.toLowerCase() === 'y' || char.toLowerCase() === 'n')) { this.emit('confirm', char.toLowerCase() === 'y'); } - if (char === '\t' && this.opts.placeholder) { + if ((char === '\t' || key?.name === 'return') && this.opts.placeholder) { if (!this.value) { this.rl?.write(this.opts.placeholder); this.emit('value', this.opts.placeholder); From 651f1b13cdb5f62df8c1c93abcd4b2a254d57789 Mon Sep 17 00:00:00 2001 From: Junseong Park <39112954+jsparkdev@users.noreply.github.com> Date: Thu, 3 Apr 2025 16:05:56 +0900 Subject: [PATCH 05/10] remove changes in text prompt --- packages/core/src/prompts/text.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/core/src/prompts/text.ts b/packages/core/src/prompts/text.ts index 603f1fa3..37931275 100644 --- a/packages/core/src/prompts/text.ts +++ b/packages/core/src/prompts/text.ts @@ -26,7 +26,7 @@ export default class TextPrompt extends Prompt { this.on('finalize', () => { if (!this.value) { - this.value = opts.defaultValue ?? opts.placeholder ?? ''; + this.value = opts.defaultValue; } }); } From fe84565795d527bec1ab8a1bbdf98b73612c5393 Mon Sep 17 00:00:00 2001 From: Junseong Park <39112954+jsparkdev@users.noreply.github.com> Date: Thu, 3 Apr 2025 17:34:16 +0900 Subject: [PATCH 06/10] update prompt --- packages/core/src/prompts/prompt.ts | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/packages/core/src/prompts/prompt.ts b/packages/core/src/prompts/prompt.ts index 4a321c8d..e8b612b8 100644 --- a/packages/core/src/prompts/prompt.ts +++ b/packages/core/src/prompts/prompt.ts @@ -14,6 +14,7 @@ export interface PromptOptions { render(this: Omit): string | undefined; placeholder?: string; initialValue?: any; + defaultValue?: string; validate?: ((value: any) => string | Error | undefined) | undefined; input?: Readable; output?: Writable; @@ -196,8 +197,13 @@ export default class Prompt { } if ((char === '\t' || key?.name === 'return') && this.opts.placeholder) { if (!this.value) { - this.rl?.write(this.opts.placeholder); - this.emit('value', this.opts.placeholder); + if (this.opts.defaultValue) { + this.value = this.opts.defaultValue; + this.emit('value', this.value); + } else { + this.rl?.write(this.opts.placeholder); + this.emit('value', this.opts.placeholder); + } } } if (char) { From 8f389e925ee3eeba2d97b403c977958b9d823ea1 Mon Sep 17 00:00:00 2001 From: Junseong Park <39112954+jsparkdev@users.noreply.github.com> Date: Mon, 7 Apr 2025 18:14:12 +0900 Subject: [PATCH 07/10] update prompt code and test case --- packages/core/src/prompts/prompt.ts | 17 ++++++++--------- packages/prompts/src/index.test.ts | 4 ++-- 2 files changed, 10 insertions(+), 11 deletions(-) diff --git a/packages/core/src/prompts/prompt.ts b/packages/core/src/prompts/prompt.ts index e8b612b8..f8f5c349 100644 --- a/packages/core/src/prompts/prompt.ts +++ b/packages/core/src/prompts/prompt.ts @@ -14,7 +14,6 @@ export interface PromptOptions { render(this: Omit): string | undefined; placeholder?: string; initialValue?: any; - defaultValue?: string; validate?: ((value: any) => string | Error | undefined) | undefined; input?: Readable; output?: Writable; @@ -195,15 +194,10 @@ export default class Prompt { if (char && (char.toLowerCase() === 'y' || char.toLowerCase() === 'n')) { this.emit('confirm', char.toLowerCase() === 'y'); } - if ((char === '\t' || key?.name === 'return') && this.opts.placeholder) { + if (char === '\t' && this.opts.placeholder) { if (!this.value) { - if (this.opts.defaultValue) { - this.value = this.opts.defaultValue; - this.emit('value', this.value); - } else { - this.rl?.write(this.opts.placeholder); - this.emit('value', this.opts.placeholder); - } + this.rl?.write(this.opts.placeholder); + this.emit('value', this.opts.placeholder); } } if (char) { @@ -211,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 aa2fab52..2813d0f8 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 () => { From f256a0688be48c0542a92ab6e22ec3e3a92ca9c0 Mon Sep 17 00:00:00 2001 From: Junseong Park <39112954+jsparkdev@users.noreply.github.com> Date: Mon, 7 Apr 2025 18:15:29 +0900 Subject: [PATCH 08/10] code format --- packages/core/src/prompts/prompt.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/core/src/prompts/prompt.ts b/packages/core/src/prompts/prompt.ts index f8f5c349..089e1dc8 100644 --- a/packages/core/src/prompts/prompt.ts +++ b/packages/core/src/prompts/prompt.ts @@ -208,7 +208,7 @@ export default class Prompt { 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); From dafdf827ff7f43a54c195dfc14909ef194ba5c01 Mon Sep 17 00:00:00 2001 From: Junseong Park <39112954+jsparkdev@users.noreply.github.com> Date: Mon, 7 Apr 2025 18:48:33 +0900 Subject: [PATCH 09/10] remove comment --- packages/prompts/src/index.test.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/prompts/src/index.test.ts b/packages/prompts/src/index.test.ts index 2813d0f8..406bd933 100644 --- a/packages/prompts/src/index.test.ts +++ b/packages/prompts/src/index.test.ts @@ -213,7 +213,7 @@ describe.each(['true', 'false'])('prompts (isCI = %s)', (isCI) => { const value = await result; expect(output.buffer).toMatchSnapshot(); - // TODO (43081j): uncomment this when #263 is fixed + expect(value).toBe('bar'); }); From bc1c865a0e23f32ddffd4fa782527798cfad950e Mon Sep 17 00:00:00 2001 From: Nate Moore Date: Mon, 7 Apr 2025 21:45:13 -0500 Subject: [PATCH 10/10] Create hot-turkeys-knock.md --- .changeset/hot-turkeys-knock.md | 5 +++++ 1 file changed, 5 insertions(+) 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 00000000..0c86eb91 --- /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.