Hi,
I edit the first prompt in examples/list.js
/**
* List prompt example
*/
'use strict';
var inquirer = require('..');
inquirer.prompt([
{
type: 'list',
name: 'theme',
message: 'What do you want to do?',
choices: [
'Order a pizza',
'Make a reservation',
new inquirer.Separator(),
'Ask for opening hours',
{
name: 'Contact support',
disabled: 'Unavailable at this time'
},
'Talk to the receptionist'
],
validate: function () { // <==== HERE
return 'Please select another choice';
}
},
{
type: 'list',
name: 'size',
message: 'What size do you need?',
choices: ['Jumbo', 'Large', 'Standard', 'Medium', 'Small', 'Micro'],
filter: function (val) {
return val.toLowerCase();
}
}
]).then(function (answers) {
console.log(JSON.stringify(answers, null, ' '));
});
Prompt freeze after select one choice. Should it show the message 'Please select another choice' ?
For other question type like "input", "checkbox", "password" and "rawlist" validate function works as expected
Maybe a error handler miss here
https://github.com/SBoudrias/Inquirer.js/blob/master/lib/prompts/list.js#L69
Explicitly the list prompt doesn't support validation. It present a list of choices, so there's no reason a user could select an invalid value.
That being said, it should ignore the validation function altogether.
Explicitly the list prompt doesn't support validation. It present a list of choices, so there's no reason a user could select an invalid value.
Make sense ^^'
Explicitly the list prompt doesn't support validation.
it make sense indeed but why not? If there're several lists I'd better show user all options but in case an incompatible combination I'll tell why it can't be used.
Please add support for filter for list prompts.
Or at least update docs to say it's not supported.
You could just make sure that choices done previously automatically eliminates choices from the list(s) later in the prompt that are now incompatible. That's better UX. Use reactive interface: https://github.com/SBoudrias/Inquirer.js#reactive-interface
You could just make sure that choices done previously automatically eliminates choices from the list(s) later in the prompt that are now incompatible. That's better UX.
Sometimes it can be helpful to show an option but say why it cannot be use.
Use reactive interface
I'm using Inquirer via Yeoman, not sure it's possible.
Valid points :)
I'm also on a situation where I want to make use of validation on list. Maybe I just want to give an option to early abort the process, and providing a list of choices where one of them breaks the flow will save a potential user a lot of typing. I'm also see the same advantage on confirmation, which does not support validation neither.
Basically inquirer should provide ways to abort a set of prompts for any kind of question.
Regards
@danielo515 I'm open to receiving a PR to add validation to the list prompt.
Still, I think the best UX would be to find another way to do that. But I think it's fine if inquirer provide a consistent interface for all prompts type.
This is an old thread, but just chipping in to agree that validation and filter functionality on choice and list prompts would be really handy - as others have said above, it can be useful to show all options and return a custom response if the choice is invalid rather than hiding it altogether.
Don't remember if the option for disabled choices was present at the original date of this issue, but I would argue that with that option you no longer have any reason to support validation for list prompts.
To show options that appear selectable, then when the user selects a choice validate that choice to false is just confusing.
So you have 2 choices:
You can accomplist this by defining choices as a function, and removing the choices that are now invalid based on earlier answers. You have access to the answers object in the function.
You can accomplish this by defining choices as a function, and setting the disabled option of the choices that are now invalid. You have access to the answers object in the function.
Is there any real world use case to show invalid choices as valid choices?
Anyway for my plugin (https://github.com/mokkabonna/inquirer-autocomplete-prompt/pull/60#issuecomment-680989526) I will follow whatever api the list prompt implements as closely as possible.
@mokkabonna what about short circuit in case of one specific choice? How would you implement that?
I though some more about this and came across this comment in a issue debating this in the inquirer-autocomplete-prompt repo:
Yep, at least at first glance it feels like a valid use case, allowing use of the existing Inquirer feature in order to enable things like increasing efficiency by doing expensive extended validation only on the single option chosen. Seems like this would also enable other workflows too.
https://github.com/mokkabonna/inquirer-autocomplete-prompt/issues/59#issuecomment-406696798
That gave me a pause. For simple and quick/sync validation it still does not make much sense to me to support validation for a list of items.
BUT, consider if the validation is async and expensive(slow) it will take even longer to pre-validate the items in the list.
In this case you might want to display possible invalid choices, and validate them on selection. This will probably be a better user experience in some cases. It is still not optimal, but probably the better choice of the two bad options.
Leaning towards implementing it then. Any thoughts?
FYI: I have now implemented this and have a PR ready: https://github.com/mokkabonna/inquirer-autocomplete-prompt/pull/120
Oh great, thanks. I'm gonna try this out this weekend with my project, I'll let you know how it goes.
Most helpful comment
it make sense indeed but why not? If there're several lists I'd better show user all options but in case an incompatible combination I'll tell why it can't be used.
Please add support for
filterfor list prompts.Or at least update docs to say it's not supported.