mokkabonna / inquirer-autocomplete-prompt

Autocomplete prompt for inquirer
ISC License
354 stars 82 forks source link

Add `alwaysValidate` flag #60

Closed StevenLangbroek closed 4 years ago

StevenLangbroek commented 6 years ago

Addresses #59. There's no existing test for validate, and I'm not entirely sure how to write it other than giving a mock validator and testing whether it's been called yes or no. Any input would be greatly appreciated. Since it's a backwards compatible (it doesn't alter existing validation behaviour) change, I think it could be a minor bump.

Todo

StevenLangbroek commented 6 years ago

(not sure what's up with an old automation account of mine being involved in the commit)

StevenLangbroek commented 6 years ago

@mokkabonna would love to hear your opinion on this...

mokkabonna commented 4 years ago

Thank you for your interest and suggestion. Also sorry for the looong delay.

I have mentioned before that I try to align myself with the list prompt already present. The list prompt does not have validate. I assume because why include an item that you present to the user as selectable, to moments later fail in validation.

So for the normal list prompt you just simply exclude unselectable options.

You can do the same for this prompt.

So instead of:

{
  type: 'autocomplete',
  name: 'interstateTravelTo',
  message: 'Select a state to travel to',
  validate(state, answers) {
    return state === answers.interstateTravelFrom ? false : true
  },
  source: function name(answers, input) {
    return searchStates(input);
  }
},

You do the exact same, but in a filter

{
  type: 'autocomplete',
  name: 'interstateTravelTo',
  message: 'Select a state to travel to',
  source: function name(answers, input) {
    return searchStates(input).filter(state => state !== answers.interstateTravelFrom);
  }
},

What is missing though is the support for showing items as disabled. I am adding that soon. See https://github.com/SBoudrias/Inquirer.js/blob/master/packages/inquirer/lib/prompts/list.js#L189-L195 I will support it exactly like that.

So it will either be shown as:

Alabama
 - Alaska (Disabled)
American Samoa
.....

Or you can give a text like so:

{
      type: 'autocomplete',
      name: 'interstateTravelTo',
      message: 'Select a state to travel to',
      source: function name(answers, input) {
        return states.map(state => ({
          name: state,
          value: state,
          disabled: state === answers.interstateTravelFrom ? 'This is for interstate travel only.' : false
        }));
      }
},
mokkabonna commented 4 years ago

Adding support for disabled choices https://github.com/mokkabonna/inquirer-autocomplete-prompt/pull/115