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

autocomplete: Add option fallbackAsInput #405

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
6 changes: 4 additions & 2 deletions lib/elements/autocomplete.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ const getIndex = (arr, valOrTitle) => {
* @param {Number} [opts.cursor=0] Cursor start position
* @param {String} [opts.style='default'] Render style
* @param {String} [opts.fallback] Fallback message - initial to default value
* @param {Boolean} [opts.fallbackAsInput] Returns input value when no matches found
* @param {String} [opts.initial] Index of the default value
* @param {Boolean} [opts.clearFirst] The first ESCAPE keypress will clear the input
* @param {Stream} [opts.stdin] The Readable stream to listen to
Expand All @@ -34,6 +35,7 @@ class AutocompletePrompt extends Prompt {
this.msg = opts.message;
this.suggest = opts.suggest;
this.choices = opts.choices;
this.fallbackAsInput = opts.fallbackAsInput;
this.initial = typeof opts.initial === 'number'
? opts.initial
: getIndex(opts.choices, opts.initial);
Expand Down Expand Up @@ -64,14 +66,14 @@ class AutocompletePrompt extends Prompt {
choice = this.choices[this._fb];
else if (typeof this._fb === 'string')
choice = { title: this._fb };
return choice || this._fb || { title: this.i18n.noMatches };
return this.fallbackAsInput ? {title:""} : choice || this._fb || { title: this.i18n.noMatches };
}

moveSelect(i) {
this.select = i;
if (this.suggestions.length > 0)
this.value = getVal(this.suggestions, i);
else this.value = this.fallback.value;
else this.value = this.fallbackAsInput ? this.input : this.fallback.value;
this.fire();
}

Expand Down