I want the user to be able to have a drop-down and use arrow keys to control their option with enter as the selecting. Kind of like this.
Pick A Color:
Red <
Green
Blue
I want the user to use arrow keys to navigate from red to blue, how do I do this?
There are a few popular prompt libraries you could try:
You should read each one's README to see which is easiest for you to setup. I typically use Inquirer but all three are great. With Inquirer, it would look something like this:
npm install @inquirer/select
import select from '@inquirer/select';
const answer = await select({
message: 'Pick a color:',
choices: [
{
value: 'red',
},
{
value: 'green',
},
{
value: 'blue',
},
],
});
You can read more about this specific select
prompt here: https://github.com/SBoudrias/Inquirer.js/tree/master/packages/select
If you get an error like this:
import select from "@inquirer/select";
^^^^^^
SyntaxError: Cannot use import statement outside a module
That's because the example I gave uses ESM-style (ES module) imports. You can just add "type": "module",
to your package.json
to fix it.