Input prompt with max length for inquirer
npm install @matti-o7/inquirer-maxlength-input-prompt> 
> !test coverage
> 
> 
> !license
Extends the built-in input prompt type to allow a max input length to be
specified, and shows a character counter which updates as the user types.
!inquirer-maxlength-input-prompt
Install using npm or yarn:
``bash`
yarn add inquirer-maxlength-input-prompt
Register the prompt with inquirer:
`javascript
const inquirer = require('inquirer')
const MaxLengthInputPrompt = require('inquirer-maxlength-input-prompt')
// The name doesn't have to maxlength-input - it can be anything you like`
inquirer.registerPrompt('maxlength-input', MaxLengthInputPrompt)
When you use the prompt, you must provide a maxLength option:
`javascript`
inquirer.prompt([
{
type: 'maxlength-input',
name: 'title',
message: 'Enter a title',
maxLength: 20
}
]).then(answers => {
/ ...snip... /
})
In addition to the options that the built-in input type prompt
takes, this plugin also takes:
- maxLength (Number [_required_]): the maximum number of characters the user can enter
The following options behave slightly differently, and are demonstrated in:
- transformer (Function): you get the current answers hash as an extravalidate
(second) argument.
- (Function): for convenience, you get the raw user input as an extrafilter
(third) argument. This makes it easy to do checks against raw user input when you
have a parameter that adds prefixes/suffixes/otherwise modifies themaxLength
input.
- the validation against is performed before your own customvalidate
function is invoked
These additions are demonstrated in the example "Adding a prefix"
below.
`javascript
const inquirer = require('inquirer')
const MaxLengthInputPrompt = require('inquirer-maxlength-input-prompt')
inquirer.registerPrompt('maxlength-input', MaxLengthInputPrompt)
inquirer.prompt([
{
type: 'maxlength-input',
name: 'title',
message: 'Enter a title',
maxLength: 15
}
]).then(console.log)
`
This example shows one way to prefix the user's input with a value _and_ pretty
print it during prompting, using the filter and transformer optionsraw
respectively. In addition, this example also shows how the parameter thatvalidate
is passed to the function can be used - in this case, it is used to
ensure the user still enters data despite the presence of the prefix.
`javascript
const chalk = require('chalk')
const inquirer = require('inquirer')
const MaxLengthInputPrompt = require('inquirer-maxlength-input-prompt')
inquirer.registerPrompt('maxlength-input', MaxLengthInputPrompt)
inquirer.prompt([
{
type: 'list',
name: 'commitType',
message: 'What type of change are you committing?',
choices: ['feature', 'fix', 'docs', 'other']
},
{
type: 'maxlength-input',
name: 'commitHeader',
maxLength: 50,
message: 'Enter the commit subject',
validate(input, answers, raw) {
if (!raw) {
return 'Please enter a subject for your commit'
}
return true
},
filter(input, answers) {
// Return prefix + input
return ${answers.commitType}: ${input}
},
transformer(input, answers) {
// Pretty print prefix before input to assist user
return chalk.blue(${answers.commitType}: ) + input``
}
}
]).then(console.log)