Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add option to not clear input upon validation error #706

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,7 @@ A question object is a `hash` containing question related values:
- **pageSize**: (Number) Change the number of lines that will be rendered when using `list`, `rawList`, `expand` or `checkbox`.
- **prefix**: (String) Change the default _prefix_ message.
- **suffix**: (String) Change the default _suffix_ message.
- **keepAnswerOnValidationError**: (Boolean) If true and user answer fails validation, the answer is kept in the input.

`default`, `choices`(if defined as functions), `validate`, `filter` and `when` functions can be called asynchronously. Either return a promise or use `this.async()` to get a callback you'll call with the final value.

Expand Down
5 changes: 5 additions & 0 deletions packages/inquirer/lib/prompts/input.js
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,11 @@ class InputPrompt extends Base {
}

onError(state) {
if (this.opt.keepAnswerOnValidationError && (typeof state.value !== 'number' || !isNaN(state.value))) {
this.rl.line += state.value;
this.rl.cursor = state.value.length;
}

this.render(state.isValid);
}

Expand Down