@inquirer/input
Advanced tools
@@ -16,4 +16,6 @@ import { type Theme } from '@inquirer/core'; | ||
| theme?: PartialDeep<Theme<InputTheme>>; | ||
| pattern?: RegExp; | ||
| patternError?: string; | ||
| }; | ||
| declare const _default: import("@inquirer/type").Prompt<string, InputConfig>; | ||
| export default _default; |
@@ -8,3 +8,3 @@ "use strict"; | ||
| exports.default = (0, core_1.createPrompt)((config, done) => { | ||
| const { required, validate = () => true, prefill = 'tab' } = config; | ||
| const { prefill = 'tab' } = config; | ||
| const theme = (0, core_1.makeTheme)(inputTheme, config.theme); | ||
@@ -16,2 +16,15 @@ const [status, setStatus] = (0, core_1.useState)('idle'); | ||
| const prefix = (0, core_1.usePrefix)({ status, theme }); | ||
| async function validate(value) { | ||
| const { required, pattern, patternError = 'Invalid input' } = config; | ||
| if (required && !value) { | ||
| return 'You must provide a value'; | ||
| } | ||
| if (pattern && !pattern.test(value)) { | ||
| return patternError; | ||
| } | ||
| if (typeof config.validate === 'function') { | ||
| return (await config.validate(value)) || 'You must provide a valid value'; | ||
| } | ||
| return true; | ||
| } | ||
| (0, core_1.useKeypress)(async (key, rl) => { | ||
@@ -25,3 +38,3 @@ // Ignore keypress while our prompt is doing other processing. | ||
| setStatus('loading'); | ||
| const isValid = required && !answer ? 'You must provide a value' : await validate(answer); | ||
| const isValid = await validate(answer); | ||
| if (isValid === true) { | ||
@@ -41,3 +54,3 @@ setValue(answer); | ||
| } | ||
| setError(isValid || 'You must provide a valid value'); | ||
| setError(isValid); | ||
| setStatus('idle'); | ||
@@ -44,0 +57,0 @@ } |
@@ -16,4 +16,6 @@ import { type Theme } from '@inquirer/core'; | ||
| theme?: PartialDeep<Theme<InputTheme>>; | ||
| pattern?: RegExp; | ||
| patternError?: string; | ||
| }; | ||
| declare const _default: import("@inquirer/type").Prompt<string, InputConfig>; | ||
| export default _default; |
+16
-3
@@ -6,3 +6,3 @@ import { createPrompt, useState, useKeypress, useEffect, usePrefix, isBackspaceKey, isEnterKey, isTabKey, makeTheme, } from '@inquirer/core'; | ||
| export default createPrompt((config, done) => { | ||
| const { required, validate = () => true, prefill = 'tab' } = config; | ||
| const { prefill = 'tab' } = config; | ||
| const theme = makeTheme(inputTheme, config.theme); | ||
@@ -14,2 +14,15 @@ const [status, setStatus] = useState('idle'); | ||
| const prefix = usePrefix({ status, theme }); | ||
| async function validate(value) { | ||
| const { required, pattern, patternError = 'Invalid input' } = config; | ||
| if (required && !value) { | ||
| return 'You must provide a value'; | ||
| } | ||
| if (pattern && !pattern.test(value)) { | ||
| return patternError; | ||
| } | ||
| if (typeof config.validate === 'function') { | ||
| return (await config.validate(value)) || 'You must provide a valid value'; | ||
| } | ||
| return true; | ||
| } | ||
| useKeypress(async (key, rl) => { | ||
@@ -23,3 +36,3 @@ // Ignore keypress while our prompt is doing other processing. | ||
| setStatus('loading'); | ||
| const isValid = required && !answer ? 'You must provide a value' : await validate(answer); | ||
| const isValid = await validate(answer); | ||
| if (isValid === true) { | ||
@@ -39,3 +52,3 @@ setValue(answer); | ||
| } | ||
| setError(isValid || 'You must provide a valid value'); | ||
| setError(isValid); | ||
| setStatus('idle'); | ||
@@ -42,0 +55,0 @@ } |
+7
-6
| { | ||
| "name": "@inquirer/input", | ||
| "version": "4.2.5", | ||
| "version": "4.3.0", | ||
| "description": "Inquirer input text prompt", | ||
@@ -77,9 +77,10 @@ "keywords": [ | ||
| "dependencies": { | ||
| "@inquirer/core": "^10.3.0", | ||
| "@inquirer/type": "^3.0.9" | ||
| "@inquirer/core": "^10.3.1", | ||
| "@inquirer/type": "^3.0.10" | ||
| }, | ||
| "devDependencies": { | ||
| "@arethetypeswrong/cli": "^0.18.2", | ||
| "@inquirer/testing": "^2.1.51", | ||
| "tshy": "^3.0.2" | ||
| "@inquirer/testing": "^2.1.52", | ||
| "@repo/tsconfig": "0.0.0", | ||
| "tshy": "^3.0.3" | ||
| }, | ||
@@ -109,3 +110,3 @@ "engines": { | ||
| }, | ||
| "gitHead": "87cb01e67a25983bdaf0d74a7685915c0afb5f23" | ||
| "gitHead": "6881993e517e76fa891b72e1f5086fd11f7676ac" | ||
| } |
+11
-19
@@ -7,12 +7,2 @@ # `@inquirer/input` | ||
| # Special Thanks | ||
| <div align="center" markdown="1"> | ||
| [](https://graphite.dev/?utm_source=npmjs&utm_medium=repo&utm_campaign=inquirerjs)<br> | ||
| ### [Graphite is the AI developer productivity platform helping teams on GitHub ship higher quality software, faster](https://graphite.dev/?utm_source=npmjs&utm_medium=repo&utm_campaign=inquirerjs) | ||
| </div> | ||
| # Installation | ||
@@ -74,11 +64,13 @@ | ||
| | Property | Type | Required | Description | | ||
| | ----------- | ----------------------------------------------------------- | -------- | ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | | ||
| | message | `string` | yes | The question to ask | | ||
| | default | `string` | no | Default value if no answer is provided; see the prefill option below for governing it's behaviour. | | ||
| | prefill | `'tab' \| 'editable'` | no | Defaults to `'tab'`. If set to `'tab'`, pressing `backspace` will clear the default and pressing `tab` will inline the value for edits; If set to `'editable'`, the default value will already be inlined to edit. | | ||
| | required | `boolean` | no | Defaults to `false`. If set to true, `undefined` (empty) will not be accepted for this. | | ||
| | 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, modify the answer in your code if needed. | | ||
| | validate | `string => boolean \| string \| Promise<boolean \| string>` | 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. | | ||
| | theme | [See Theming](#Theming) | no | Customize look of the prompt. | | ||
| | Property | Type | Required | Description | | ||
| | ------------ | ----------------------------------------------------------- | -------- | ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | | ||
| | message | `string` | yes | The question to ask | | ||
| | default | `string` | no | Default value if no answer is provided; see the prefill option below for governing it's behaviour. | | ||
| | prefill | `'tab' \| 'editable'` | no | Defaults to `'tab'`. If set to `'tab'`, pressing `backspace` will clear the default and pressing `tab` will inline the value for edits; If set to `'editable'`, the default value will already be inlined to edit. | | ||
| | required | `boolean` | no | Defaults to `false`. If set to true, `undefined` (empty) will not be accepted for this. | | ||
| | 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, modify the answer in your code if needed. | | ||
| | validate | `string => boolean \| string \| Promise<boolean \| string>` | 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. | | ||
| | pattern | `RegExp` | no | Regular expression to validate the input against. If the input doesn't match the pattern, validation will fail with the error message specified in `patternError`. | | ||
| | patternError | `string` | no | Error message to display when the input doesn't match the `pattern`. Defaults to `'Invalid input'`. | | ||
| | theme | [See Theming](#Theming) | no | Customize look of the prompt. | | ||
@@ -85,0 +77,0 @@ ## Theming |
17048
6.37%236
14.56%4
33.33%102
-7.27%Updated
Updated