mokkabonna / inquirer-autocomplete-prompt

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

Support the "default" option, like in a normal inquirer list #38

Closed dumbmatter closed 4 years ago

dumbmatter commented 7 years ago

Currently, inquirer-autocomplete-prompt always starts by selecting the first item in the list by default. This PR lets you pass a default option containing either a string value or an index number to use as the default selection. This option is already supported by the normal Inquirer.js "list" feature.

(Also, thanks for the very useful library!)

simonepri commented 6 years ago

@mokkabonna any news on this? This feature it would be very useful.

mokkabonna commented 6 years ago

This went off my radar, thanks for the reminder.

You do not always know beforehand how many or what results are going to be shown.

Could you not just sort the results based upon relevance?

I think anyway that a function would be more useful. If we support the function variant of the default option, we can give you the answers so far(default), and in addition the returned result. Based upon that you could determine the best default choice.

Another approach could be to support a selected flag on the choices object, then it sets the selected to the first that have this property.

simonepri commented 6 years ago

You do not always know beforehand how many or what results are going to be shown.

The check is done only at the first render, so we know how many results are going to be shown, just all.

Could you not just sort the results

Where sort is performed?

mokkabonna commented 6 years ago

Where sort is performed?

You can add a then to your sources promise and sort results there.

source: function(answersSoFar, input) {
  return myApi.searchStates(input).then(function sortItems() {
    //perform sort
  })
}
simonepri commented 6 years ago

Sorry but I don't see this code in the PR.

These are the only changes I see: image

mokkabonna commented 6 years ago

I am referencing the sources property that you supply when defining the prompt. See example.js

dumbmatter commented 6 years ago

Normal Inquirer.js lists support the default being a value or array index, just like in this PR. Sure you could imagine various other ways to set a default, but I imagine there is very little demand for more complicated ways to set the default, otherwise somebody would have added them to Inquirer.js. IMHO inquirer-autocomplete-prompt would be improved just by supporting the same methods as Inquirer.js itself.

If someone else wants to add more complicated ways to set the default, feel free. Otherwise I'm content to keep using my branch, it's working fine for me :)

simonepri commented 6 years ago

@dumbmatter It seems reasonable to me. Anyway I believe that we need to add some tests to this PR.

mokkabonna commented 6 years ago

I'll be happy to add this, but then I think all the default types should be supported.

default: (String|Number|Array|Function) Default value(s) to use if nothing is entered, or a function that returns the default value(s). If defined as a function, the first parameter will be the current inquirer session answers.

Except the array type, as you can only select one.

I also need to behave differently if suggestOnly is true. Then it should treat the default as a normal input prompt (text) treat the default.

And tests should be added as well. I highly appreciate this PR, but I'd rather have this feature unsupported than partially implemented or implemented without tests.

mokkabonna commented 4 years ago

Closed in favor of #114 , still some work to be done though.