From 73b6e6581169147a62d7cba08f3ae90d26dd9014 Mon Sep 17 00:00:00 2001 From: tjallingt Date: Tue, 15 Jun 2021 20:15:43 +0200 Subject: [PATCH] fix: retain number prompt default (#1020) --- packages/inquirer/lib/prompts/base.js | 8 ++++++-- packages/inquirer/lib/prompts/input.js | 5 +---- packages/inquirer/test/specs/prompts/number.js | 3 ++- 3 files changed, 9 insertions(+), 7 deletions(-) diff --git a/packages/inquirer/lib/prompts/base.js b/packages/inquirer/lib/prompts/base.js index 301128f62..5be5b4e81 100644 --- a/packages/inquirer/lib/prompts/base.js +++ b/packages/inquirer/lib/prompts/base.js @@ -150,8 +150,12 @@ class Prompt { this.opt.suffix + chalk.reset(' '); - // Append the default if available, and if question isn't answered - if (this.opt.default != null && this.status !== 'answered') { + // Append the default if available, and if question isn't touched/answered + if ( + this.opt.default != null && + this.status !== 'touched' && + this.status !== 'answered' + ) { // If default password is supplied, hide it if (this.opt.type === 'password') { message += chalk.italic.dim('[hidden] '); diff --git a/packages/inquirer/lib/prompts/input.js b/packages/inquirer/lib/prompts/input.js index b46ce7f56..87f68b920 100644 --- a/packages/inquirer/lib/prompts/input.js +++ b/packages/inquirer/lib/prompts/input.js @@ -101,10 +101,7 @@ class InputPrompt extends Base { */ onKeypress() { - // If user press a key, just clear the default value - if (this.opt.default) { - this.opt.default = undefined; - } + this.state = 'touched'; this.render(); } diff --git a/packages/inquirer/test/specs/prompts/number.js b/packages/inquirer/test/specs/prompts/number.js index f996cd3e8..e9a4909b5 100644 --- a/packages/inquirer/test/specs/prompts/number.js +++ b/packages/inquirer/test/specs/prompts/number.js @@ -111,6 +111,7 @@ describe('`number` prompt', () => { done(); }); - this.rl.emit('line', ''); + this.rl.input.emit('keypress', 'a', { name: 'a' }); + this.rl.emit('line'); }); });