Skip to content

Commit a99c458

Browse files
committed
feat: support initialValue for text prompt
1 parent d14172b commit a99c458

5 files changed

Lines changed: 16 additions & 3 deletions

File tree

.changeset/lazy-mirrors-kick.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
---
2+
"@clack/prompts": patch
3+
"@clack/core": patch
4+
---
5+
6+
Support `initialValue` option for text prompt

examples/basic/index.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ async function main() {
2222

2323
const dir = await text({
2424
message: "Where should we create your project?",
25-
placeholder: "./sparkling-slop",
25+
placeholder: "./sparkling-solid",
2626
});
2727

2828
if (isCancel(dir)) {
@@ -61,6 +61,7 @@ async function main() {
6161

6262
const install = await confirm({
6363
message: "Install dependencies?",
64+
initialValue: false
6465
});
6566

6667
if (isCancel(install)) {
@@ -79,7 +80,7 @@ async function main() {
7980

8081
note(nextSteps, 'Next steps.');
8182

82-
await setTimeout(3000);
83+
await setTimeout(1000);
8384

8485
outro(`Problems? ${color.underline(color.cyan('https://example.com/issues'))}`);
8586
}

packages/core/src/prompts/prompt.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ export default class Prompt {
5656
public value: any;
5757
public error: string = '';
5858

59-
constructor({ render, input = stdin, output = stdout, initialValue, ...opts }: PromptOptions<Prompt>, trackValue: boolean = true) {
59+
constructor({ render, input = stdin, output = stdout, ...opts }: PromptOptions<Prompt>, trackValue: boolean = true) {
6060
this.opts = opts;
6161
this.onKeypress = this.onKeypress.bind(this);
6262
this.close = this.close.bind(this);
@@ -89,6 +89,9 @@ export default class Prompt {
8989
})
9090
readline.emitKeypressEvents(this.input, this.rl);
9191
this.rl.prompt();
92+
if (this.opts.initialValue !== undefined && this._track) {
93+
this.rl.write(this.opts.initialValue);
94+
}
9295

9396
this.input.on('keypress', this.onKeypress);
9497
setRawMode(this.input, true);

packages/prompts/README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@ import { text } from "@clack/prompts";
5454
const meaning = await text({
5555
message: "What is the meaning of life?",
5656
placeholder: "Not sure",
57+
initialValue: "42",
5758
validate(value) {
5859
if (value.length === 0) return `Value is required!`;
5960
},

packages/prompts/src/index.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,12 +27,14 @@ const barEnd = "└";
2727
export interface TextOptions {
2828
message: string;
2929
placeholder?: string;
30+
initialValue?: string;
3031
validate?: (value: string) => string | void;
3132
}
3233
export const text = (opts: TextOptions) => {
3334
return new TextPrompt({
3435
validate: opts.validate,
3536
placeholder: opts.placeholder,
37+
initialValue: opts.initialValue,
3638
render() {
3739
const title = `${color.gray(bar)}\n${symbol(this.state)} ${
3840
opts.message

0 commit comments

Comments
 (0)