Inquirer.js icon indicating copy to clipboard operation
Inquirer.js copied to clipboard

Can I configure questions according to the input ?

Open thidasapankaja opened this issue 6 years ago • 2 comments

Let's say I have multiple paths in a survey. I ask a question and the user can enter 1-5 numbers (1,2,3,4,5). Then he/she should be seen a specific set of questions according to the number he/she entered in the previous question.

I'm wondering if there's a way to do such a thing?

thidasapankaja avatar Jun 20 '19 02:06 thidasapankaja

All control flow can be done in your own JavaScript code. You can then just rely on Inquirer to display the relevant questions.

const { firstAnswer } = await inquirer.prompt(...);

if (firstAnswer === 'foo') {
	const answers = await inquirer.prompt(...);
} else {
	const answers = await inquirer.prompt(...);
}

Inquirer scope doesn't focus on wrapping user flows in extra APIs.

SBoudrias avatar Jun 20 '19 04:06 SBoudrias

Could you use when for this purpose? Especially if you have a list question to get the user's choice of 1-5.

questionOne: {
    type: "list",
    name: "question1",
    message: "Please select option 1-5.",
    choices: [1, 2, 3, 4, 5]
},
questionTwo: {
    type: "input",
    name: "question2",
    message: "after selecting choice 1, please write your answer to question 2.",
    when: (answers) => {
        if (answers.userChoice === 1) {
            return true;
        }
    }
}

when can take in the answers hash and needs to return a boolean. If true, the question will display.

GAFelton avatar Oct 17 '20 00:10 GAFelton