Skip to content

Latest commit

 

History

History
43 lines (36 loc) · 2.31 KB

select.md

File metadata and controls

43 lines (36 loc) · 2.31 KB

Select plugin

Preview

Features

  • Ask the user to select items from a list
  • radio option to allow only one selected item
  • default selected items

Example

const sh = require('kool-shell')

sh.select('What do you want for dinner ?', [
    'A double cheese',
    { value: 'An omelette du fromage', selected: true }
    'Some vegetables'
  ])
  .then(result => {
    if (result.length > 1) console.log('Are you sure to eat all of this?')
  })

Usage

sh.select(label, [items], [options])

Ask label to the user.
Display a list of items. With the keyboard, the user needs to choose item in this list.
Return a promise that will be resolved when the user submit his selection by typing the Enter key. The resolved value is an array of the selected items.

  • label (String): A statement or query to write to the console, prepended to the list.
  • items (Array): List of item the user choose from.
    • an item can be a simple string
    • you can also make an object with these parameters:
      • value (String): the display value of this item
      • selected (Boolean, default false): if true the item will be selected by default
      • onChosen (Function) A function that will be called when the user has submit his selection. It's like the onSubmit option but for individual choice. It can be a promise for asynchronous actions like another sh.select input.
  • options (Object)
    • radio (Boolean, default false): Allow only one selected item, like the <radio> input in HTML
    • instructions (String) Change the instruction of the input (useful if you don't want the message in English)
    • checked (String) The checked symbol added before a checked item
    • unchecked (String) The unchecked symbol added before an unchecked item
    • onSubmit: (Function): A function that will be called when the user has answered. You can return a simple value or a promise if you want to do asynchronous actions. The input will resolve with the return value from the onSubmit function. If you reject your onSubmit promise, sh.select will be rejected as well. The onSubmit option can be useful to avoid too much nested promises or to process the user answer before resolving sh.select.