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

feat: Autocomplete inputNoChoice #272

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

laggingreflex
Copy link

Accepts user input when no matching choices found

fixes #233

@laggingreflex laggingreflex force-pushed the feat/autocomplete-inputNoChoice branch 3 times, most recently from 086d91d to 8dc8c3d Compare April 21, 2020 00:16
@trickpattyFH20
Copy link

Awesome, would love to have this feature! 👍

Accepts user input when no matching choices found
@laggingreflex laggingreflex force-pushed the feat/autocomplete-inputNoChoice branch from 8dc8c3d to 5b37b88 Compare April 26, 2020 04:24
@LoaderB0T
Copy link

Can we expect this to be merged anytime soon?

@kyusupov33
Copy link

Please, merge this feature! 👍

@alexander-heimbuch
Copy link

alexander-heimbuch commented Jun 27, 2021

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;
  }
}

@jonschlinkert
Copy link
Member

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?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

AutoComplete add user entry
6 participants