-
-
Notifications
You must be signed in to change notification settings - Fork 282
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
feat: Autocomplete inputNoChoice #272
base: master
Are you sure you want to change the base?
feat: Autocomplete inputNoChoice #272
Conversation
086d91d
to
8dc8c3d
Compare
Awesome, would love to have this feature! 👍 |
Accepts user input when no matching choices found
8dc8c3d
to
5b37b88
Compare
Can we expect this to be merged anytime soon? |
Please, merge this feature! 👍 |
Very useful feature, would love to see this merged! In the meantime Im using this little workaround: const { AutoComplete } = require('enquirer');
export class AutoSuggest extends AutoComplete {
constructor(options) {
super(options);
}
suggest(input = this.input, choices = this.state._choices) {
if (typeof this.options.suggest === 'function') {
return this.options.suggest.call(this, input, choices);
}
let str = input.toLowerCase();
const filtered = choices.filter((ch) => !ch._userInput).filter((ch) => ch.message.toLowerCase().includes(str));
if (!filtered.length && this.options.inputNoChoice) {
filtered.push({ name: input, message: input, value: input, _userInput: true });
}
return filtered;
}
} |
I agree that this should be allowed. But I need to think about this approach. For now, you can do this: const { AutoComplete } = require('enquirer');
const fakeChoice = input => ({ value: input, message: '', name: input });
const prompt = new AutoComplete({
type: 'autocomplete',
name: 'flavor',
message: 'Pick your favorite flavor',
suggest(typed, choices) {
const maches = choices.filter(choice => choice.message.includes(typed));
return maches.length ? maches : [fakeChoice(typed)];
},
choices: [
'almond',
'apple',
'banana'
]
});
prompt.run()
.then(output => console.log({ output }))
.catch(console.log); IMHO this might be a better option anyway, since it doesn't complicate the lib further and demonstrates that you can do anything you need to with custom code already. Thoughts? |
Accepts user input when no matching choices found
fixes #233