Skip to content

Commit 8f389e9

Browse files
committed
update prompt code and test case
1 parent 9d4aaed commit 8f389e9

File tree

2 files changed

+10
-11
lines changed

2 files changed

+10
-11
lines changed

packages/core/src/prompts/prompt.ts

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@ export interface PromptOptions<Self extends Prompt> {
1414
render(this: Omit<Self, 'prompt'>): string | undefined;
1515
placeholder?: string;
1616
initialValue?: any;
17-
defaultValue?: string;
1817
validate?: ((value: any) => string | Error | undefined) | undefined;
1918
input?: Readable;
2019
output?: Writable;
@@ -195,22 +194,22 @@ export default class Prompt {
195194
if (char && (char.toLowerCase() === 'y' || char.toLowerCase() === 'n')) {
196195
this.emit('confirm', char.toLowerCase() === 'y');
197196
}
198-
if ((char === '\t' || key?.name === 'return') && this.opts.placeholder) {
197+
if (char === '\t' && this.opts.placeholder) {
199198
if (!this.value) {
200-
if (this.opts.defaultValue) {
201-
this.value = this.opts.defaultValue;
202-
this.emit('value', this.value);
203-
} else {
204-
this.rl?.write(this.opts.placeholder);
205-
this.emit('value', this.opts.placeholder);
206-
}
199+
this.rl?.write(this.opts.placeholder);
200+
this.emit('value', this.opts.placeholder);
207201
}
208202
}
209203
if (char) {
210204
this.emit('key', char.toLowerCase());
211205
}
212206

213207
if (key?.name === 'return') {
208+
if (!this.value && this.opts.placeholder) {
209+
this.rl?.write(this.opts.placeholder);
210+
this.emit('value', this.opts.placeholder);
211+
}
212+
214213
if (this.opts.validate) {
215214
const problem = this.opts.validate(this.value);
216215
if (problem) {

packages/prompts/src/index.test.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -210,11 +210,11 @@ describe.each(['true', 'false'])('prompts (isCI = %s)', (isCI) => {
210210

211211
input.emit('keypress', '', { name: 'return' });
212212

213-
await result;
213+
const value = await result;
214214

215215
expect(output.buffer).toMatchSnapshot();
216216
// TODO (43081j): uncomment this when #263 is fixed
217-
// expect(value).toBe('bar');
217+
expect(value).toBe('bar');
218218
});
219219

220220
test('<tab> applies placeholder', async () => {

0 commit comments

Comments
 (0)