@inquirer/input
Advanced tools
Comparing version 0.0.7-alpha.0 to 0.0.9-alpha.0
79
index.js
@@ -1,33 +0,64 @@ | ||
const { createPrompt } = require('@inquirer/core'); | ||
const { createPrompt, useState, useKeypress } = require('@inquirer/core/hooks'); | ||
const { usePrefix } = require('@inquirer/core/lib/prefix'); | ||
const { isEnterKey, isBackspaceKey } = require('@inquirer/core/lib/key'); | ||
const chalk = require('chalk'); | ||
module.exports = createPrompt( | ||
{ | ||
canRemoveDefault: true, | ||
onKeypress: (value, key, { canRemoveDefault }, setState) => { | ||
const newState = { canRemoveDefault: !value }; | ||
module.exports = createPrompt((config, done) => { | ||
const [status, setStatus] = useState('pending'); | ||
const [defaultValue, setDefaultValue] = useState(config.default); | ||
const [errorMsg, setError] = useState(); | ||
const [value, setValue] = useState(''); | ||
// Allow user to remove the default value by pressing backspace | ||
if (canRemoveDefault && key.name === 'backspace') { | ||
newState.default = undefined; | ||
} | ||
const isLoading = status === 'loading'; | ||
const prefix = usePrefix(isLoading); | ||
setState(newState); | ||
useKeypress(async (key, rl) => { | ||
// Ignore keypress while our prompt is doing other processing. | ||
if (status !== 'pending') { | ||
return; | ||
} | ||
}, | ||
state => { | ||
const { prefix, value = '', status } = state; | ||
const message = chalk.bold(state.message); | ||
let formattedValue = value; | ||
if (status === 'done') { | ||
formattedValue = chalk.cyan(value || state.default || ''); | ||
} | ||
let defaultValue = ''; | ||
if (state.default && status !== 'done' && !value) { | ||
defaultValue = chalk.dim(` (${state.default})`); | ||
if (isEnterKey(key)) { | ||
const answer = value || defaultValue || ''; | ||
setStatus('loading'); | ||
const isValid = await config.validate(answer); | ||
if (isValid === true) { | ||
setValue(answer); | ||
setStatus('done'); | ||
done(answer); | ||
} else { | ||
// TODO: Can we keep the value after validation failure? | ||
// `rl.line = value` works but it looses the cursor position. | ||
setValue(''); | ||
setError(isValid || 'You must provide a valid value'); | ||
setStatus('pending'); | ||
} | ||
} else if (isBackspaceKey(key) && !value) { | ||
setDefaultValue(undefined); | ||
} else { | ||
setValue(rl.line); | ||
setError(undefined); | ||
} | ||
}); | ||
return `${prefix} ${message}${defaultValue} ${formattedValue}`; | ||
const message = chalk.bold(config.message); | ||
let formattedValue = value; | ||
if (typeof config.transformer === 'function') { | ||
formattedValue = config.transformer(value, { isFinal: status === 'done' }); | ||
} | ||
); | ||
if (status === 'done') { | ||
formattedValue = chalk.cyan(formattedValue); | ||
} | ||
let defaultStr = ''; | ||
if (defaultValue && status !== 'done' && !value) { | ||
defaultStr = chalk.dim(` (${defaultValue})`); | ||
} | ||
let error = ''; | ||
if (errorMsg) { | ||
error = chalk.red(`> ${errorMsg}`); | ||
} | ||
return [`${prefix} ${message}${defaultStr} ${formattedValue}`, error]; | ||
}); |
{ | ||
"name": "@inquirer/input", | ||
"version": "0.0.7-alpha.0", | ||
"version": "0.0.9-alpha.0", | ||
"description": "Inquirer input text prompt", | ||
@@ -16,4 +16,4 @@ "main": "index.js", | ||
"dependencies": { | ||
"@inquirer/core": "^0.0.7-alpha.0", | ||
"chalk": "^2.4.1" | ||
"@inquirer/core": "^0.0.9-alpha.0", | ||
"chalk": "^3.0.0" | ||
}, | ||
@@ -23,3 +23,3 @@ "publishConfig": { | ||
}, | ||
"gitHead": "54285c75bc7986e0e2c7bcd2bc8ae4ddd6f05929" | ||
"gitHead": "65a4d598658e1b7dfd45ec88620962c989c94c5d" | ||
} |
@@ -5,2 +5,4 @@ # `@inquirer/input` | ||
![Input prompt](https://cdn.rawgit.com/SBoudrias/Inquirer.js/28ae8337ba51d93e359ef4f7ee24e79b69898962/assets/screenshots/input.svg) | ||
# Installation | ||
@@ -10,2 +12,4 @@ | ||
npm install @inquirer/input | ||
yarn add @inquirer/input | ||
``` | ||
@@ -16,3 +20,3 @@ | ||
```js | ||
import input from '@inquirer/input/'; | ||
import input from '@inquirer/input'; | ||
@@ -24,8 +28,7 @@ const answer = await input({ message: 'Enter your name' }); | ||
| Property | Type | Required | Description | | ||
| ----------- | -------------------------------------------------------- | -------- | ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | | ||
| message | `string` | yes | The question to ask | | ||
| default | `string` | no | Default value if no answer is provided (clear it by pressing backspace) | | ||
| transformer | `(string, { isFinal: boolean }) => string` | no | Transform/Format the raw value entered by the user. Once the prompt is completed, `isFinal` will be `true`. This function is purely visual; to modify the answer, use the `filter` option. | | ||
| filter | `string => string \| Promise<string>` | no | Transform the answer | | ||
| Property | Type | Required | Description | | ||
| ----------- | ----------------------------------------------------------- | -------- | ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | | ||
| message | `string` | yes | The question to ask | | ||
| default | `string` | no | Default value if no answer is provided (clear it by pressing backspace) | | ||
| transformer | `(string, { isFinal: boolean }) => string` | no | Transform/Format the raw value entered by the user. Once the prompt is completed, `isFinal` will be `true`. This function is purely visual; to modify the answer, use the `filter` option. | | ||
| validate | `string => boolean \| string \| Promise<string \| boolean>` | no | On submit, validate the filtered answered content. When returning a string, it'll be used as the error message displayed to the user. Note: returning a rejected promise, we'll assume a code error happened and crash. | | ||
@@ -35,3 +38,3 @@ | ||
Copyright (c) 2018 Simon Boudrias (twitter: [@vaxilart](https://twitter.com/Vaxilart)) | ||
Copyright (c) 2019 Simon Boudrias (twitter: [@vaxilart](https://twitter.com/Vaxilart)) | ||
Licensed under the MIT license. |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
36
7312
5
97
+ Added@inquirer/core@0.0.9-alpha.0(transitive)
+ Addedansi-styles@4.3.0(transitive)
+ Addedchalk@3.0.0(transitive)
+ Addedcolor-convert@2.0.1(transitive)
+ Addedcolor-name@1.1.4(transitive)
+ Addedhas-flag@4.0.0(transitive)
+ Addedsupports-color@7.2.0(transitive)
- Removed@inquirer/core@0.0.7-alpha.0(transitive)
- Removedansi-regex@4.1.1(transitive)
- Removedansi-styles@3.2.1(transitive)
- Removedchalk@2.4.2(transitive)
- Removedcolor-convert@1.9.3(transitive)
- Removedcolor-name@1.1.3(transitive)
- Removedescape-string-regexp@1.0.5(transitive)
- Removedhas-flag@3.0.0(transitive)
- Removedstrip-ansi@5.2.0(transitive)
- Removedsupports-color@5.5.0(transitive)
Updatedchalk@^3.0.0