## inquirer-plugin-lazy-list

This package is a plugin for [inquirer](https://github.com/SBoudrias/Inquirer.js).

By default an inquirer list of choices is assumed to be finite. This plugin allows choices to be added to a list in response to `onKeyDown` and `onKeyUp` events.

## Options

onChange(thisArg, type) => Promise<Array<{ value: string, name: string }> | void>

The LazyList component is just like inquirer's default List component, with the option to set an onChange handler. The onChange handler is called when an onKeyDown or onKeyUp event transpires. The onChange handler should return undefined or a promise that resolves to new choices to add to the list.


## Example

```js
const inquirer = require('inquirer')
const fetch = require('node-fetch')

inquirer.registerPrompt('lazy-list', require('inquirer-plugin-lazy-list'))

const pageSize = 5

const fetchMore = (n) => {
  let url = `https://jsonplaceholder.typicode.com/posts/${n}`
  return fetch(url)
    .then(resp => resp.json())
    .then(json => ([{ value: json.id, name: `${json.id}` }]))
}

inquirer.prompt({
  type: 'lazy-list',
  name: 'posts',
  message: 'Inbox',
  pageSize: pageSize,
  choices: new Array(5).fill(0).map((_, i) => ({ value: i, name: i })),
  onChange: (state, eventType) => {
    let index = state.selected
    let listLength = state.opt.choices.length
    let shouldFetchMore = (index + pageSize) > listLength
    if (
      eventType === 'onDownKey' &&
      shouldFetchMore
    ) {
      return fetchMore(state.opt.choices.length)
        .then(choices => {
          return state.opt.choices.choices.concat(choices)
        })
    }
  }
})

```