@inquirer/checkbox
Advanced tools
Comparing version
"use strict"; | ||
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { | ||
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); } | ||
return new (P || (P = Promise))(function (resolve, reject) { | ||
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } | ||
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } | ||
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); } | ||
step((generator = generator.apply(thisArg, _arguments || [])).next()); | ||
}); | ||
}; | ||
var __importDefault = (this && this.__importDefault) || function (mod) { | ||
@@ -41,3 +50,3 @@ return (mod && mod.__esModule) ? mod : { "default": mod }; | ||
exports.default = (0, core_1.createPrompt)((config, done) => { | ||
const { prefix = (0, core_1.usePrefix)(), instructions, pageSize, loop = true, choices } = config; | ||
const { prefix = (0, core_1.usePrefix)(), instructions, pageSize, loop = true, choices, required, validate = () => true, } = config; | ||
const [status, setStatus] = (0, core_1.useState)('pending'); | ||
@@ -56,20 +65,32 @@ const [items, setItems] = (0, core_1.useState)(choices.map((choice) => (Object.assign({}, choice)))); | ||
const [showHelpTip, setShowHelpTip] = (0, core_1.useState)(true); | ||
(0, core_1.useKeypress)((key) => { | ||
const [errorMsg, setError] = (0, core_1.useState)(undefined); | ||
(0, core_1.useKeypress)((key) => __awaiter(void 0, void 0, void 0, function* () { | ||
if ((0, core_1.isEnterKey)(key)) { | ||
setStatus('done'); | ||
done(items.filter(isChecked).map((choice) => choice.value)); | ||
const selection = items.filter(isChecked); | ||
const isValid = yield validate([...selection]); | ||
if (required && !items.some(isChecked)) { | ||
setError('At least one choice must be selected'); | ||
} | ||
else if (isValid === true) { | ||
setStatus('done'); | ||
done(selection.map((choice) => choice.value)); | ||
} | ||
else { | ||
setError(isValid || 'You must select a valid value'); | ||
} | ||
} | ||
else if ((0, core_1.isUpKey)(key) || (0, core_1.isDownKey)(key)) { | ||
if (!loop && active === bounds.first && (0, core_1.isUpKey)(key)) | ||
return; | ||
if (!loop && active === bounds.last && (0, core_1.isDownKey)(key)) | ||
return; | ||
const offset = (0, core_1.isUpKey)(key) ? -1 : 1; | ||
let next = active; | ||
do { | ||
next = (next + offset + items.length) % items.length; | ||
} while (!isSelectable(items[next])); | ||
setActive(next); | ||
if (loop || | ||
((0, core_1.isUpKey)(key) && active !== bounds.first) || | ||
((0, core_1.isDownKey)(key) && active !== bounds.last)) { | ||
const offset = (0, core_1.isUpKey)(key) ? -1 : 1; | ||
let next = active; | ||
do { | ||
next = (next + offset + items.length) % items.length; | ||
} while (!isSelectable(items[next])); | ||
setActive(next); | ||
} | ||
} | ||
else if ((0, core_1.isSpaceKey)(key)) { | ||
setError(undefined); | ||
setShowHelpTip(false); | ||
@@ -89,8 +110,8 @@ setItems(items.map((choice, i) => (i === active ? toggle(choice) : choice))); | ||
const item = items[position]; | ||
if (item == null || !isSelectable(item)) | ||
return; | ||
setActive(position); | ||
setItems(items.map((choice, i) => (i === position ? toggle(choice) : choice))); | ||
if (item != null && isSelectable(item)) { | ||
setActive(position); | ||
setItems(items.map((choice, i) => (i === position ? toggle(choice) : choice))); | ||
} | ||
} | ||
}); | ||
})); | ||
const message = chalk_1.default.bold(config.message); | ||
@@ -125,3 +146,7 @@ const page = (0, core_1.usePagination)({ | ||
} | ||
return `${prefix} ${message}${helpTip}\n${page}${ansi_escapes_1.default.cursorHide}`; | ||
let error = ''; | ||
if (errorMsg) { | ||
error = chalk_1.default.red(`> ${errorMsg}`); | ||
} | ||
return `${prefix} ${message}${helpTip}\n${page}\n${error}${ansi_escapes_1.default.cursorHide}`; | ||
}); |
"use strict"; | ||
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { | ||
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); } | ||
return new (P || (P = Promise))(function (resolve, reject) { | ||
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } | ||
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } | ||
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); } | ||
step((generator = generator.apply(thisArg, _arguments || [])).next()); | ||
}); | ||
}; | ||
var __importDefault = (this && this.__importDefault) || function (mod) { | ||
@@ -41,3 +50,3 @@ return (mod && mod.__esModule) ? mod : { "default": mod }; | ||
exports.default = (0, core_1.createPrompt)((config, done) => { | ||
const { prefix = (0, core_1.usePrefix)(), instructions, pageSize, loop = true, choices } = config; | ||
const { prefix = (0, core_1.usePrefix)(), instructions, pageSize, loop = true, choices, required, validate = () => true, } = config; | ||
const [status, setStatus] = (0, core_1.useState)('pending'); | ||
@@ -56,20 +65,32 @@ const [items, setItems] = (0, core_1.useState)(choices.map((choice) => (Object.assign({}, choice)))); | ||
const [showHelpTip, setShowHelpTip] = (0, core_1.useState)(true); | ||
(0, core_1.useKeypress)((key) => { | ||
const [errorMsg, setError] = (0, core_1.useState)(undefined); | ||
(0, core_1.useKeypress)((key) => __awaiter(void 0, void 0, void 0, function* () { | ||
if ((0, core_1.isEnterKey)(key)) { | ||
setStatus('done'); | ||
done(items.filter(isChecked).map((choice) => choice.value)); | ||
const selection = items.filter(isChecked); | ||
const isValid = yield validate([...selection]); | ||
if (required && !items.some(isChecked)) { | ||
setError('At least one choice must be selected'); | ||
} | ||
else if (isValid === true) { | ||
setStatus('done'); | ||
done(selection.map((choice) => choice.value)); | ||
} | ||
else { | ||
setError(isValid || 'You must select a valid value'); | ||
} | ||
} | ||
else if ((0, core_1.isUpKey)(key) || (0, core_1.isDownKey)(key)) { | ||
if (!loop && active === bounds.first && (0, core_1.isUpKey)(key)) | ||
return; | ||
if (!loop && active === bounds.last && (0, core_1.isDownKey)(key)) | ||
return; | ||
const offset = (0, core_1.isUpKey)(key) ? -1 : 1; | ||
let next = active; | ||
do { | ||
next = (next + offset + items.length) % items.length; | ||
} while (!isSelectable(items[next])); | ||
setActive(next); | ||
if (loop || | ||
((0, core_1.isUpKey)(key) && active !== bounds.first) || | ||
((0, core_1.isDownKey)(key) && active !== bounds.last)) { | ||
const offset = (0, core_1.isUpKey)(key) ? -1 : 1; | ||
let next = active; | ||
do { | ||
next = (next + offset + items.length) % items.length; | ||
} while (!isSelectable(items[next])); | ||
setActive(next); | ||
} | ||
} | ||
else if ((0, core_1.isSpaceKey)(key)) { | ||
setError(undefined); | ||
setShowHelpTip(false); | ||
@@ -89,8 +110,8 @@ setItems(items.map((choice, i) => (i === active ? toggle(choice) : choice))); | ||
const item = items[position]; | ||
if (item == null || !isSelectable(item)) | ||
return; | ||
setActive(position); | ||
setItems(items.map((choice, i) => (i === position ? toggle(choice) : choice))); | ||
if (item != null && isSelectable(item)) { | ||
setActive(position); | ||
setItems(items.map((choice, i) => (i === position ? toggle(choice) : choice))); | ||
} | ||
} | ||
}); | ||
})); | ||
const message = chalk_1.default.bold(config.message); | ||
@@ -125,3 +146,7 @@ const page = (0, core_1.usePagination)({ | ||
} | ||
return `${prefix} ${message}${helpTip}\n${page}${ansi_escapes_1.default.cursorHide}`; | ||
let error = ''; | ||
if (errorMsg) { | ||
error = chalk_1.default.red(`> ${errorMsg}`); | ||
} | ||
return `${prefix} ${message}${helpTip}\n${page}\n${error}${ansi_escapes_1.default.cursorHide}`; | ||
}); |
@@ -9,2 +9,3 @@ import { Separator } from '@inquirer/core'; | ||
}; | ||
type Item<Value> = Separator | Choice<Value>; | ||
declare const _default: <Value extends unknown>(config: { | ||
@@ -17,4 +18,6 @@ message: string | Promise<string> | (() => Promise<string>); | ||
loop?: boolean | undefined; | ||
required?: boolean | undefined; | ||
validate?: ((items: readonly Item<Value>[]) => string | boolean | Promise<string | boolean>) | undefined; | ||
}, context?: import("@inquirer/type").Context | undefined) => import("@inquirer/type").CancelablePromise<Value[]>; | ||
export default _default; | ||
export { Separator }; |
{ | ||
"name": "@inquirer/checkbox", | ||
"version": "1.4.0", | ||
"version": "1.5.0", | ||
"engines": { | ||
@@ -60,3 +60,3 @@ "node": ">=14.18.0" | ||
"dependencies": { | ||
"@inquirer/core": "^5.1.0", | ||
"@inquirer/core": "^5.1.1", | ||
"@inquirer/type": "^1.1.5", | ||
@@ -68,3 +68,3 @@ "ansi-escapes": "^4.3.2", | ||
"devDependencies": { | ||
"@inquirer/testing": "^2.1.8" | ||
"@inquirer/testing": "^2.1.9" | ||
}, | ||
@@ -91,3 +91,3 @@ "scripts": { | ||
}, | ||
"gitHead": "c88aaca660e58aa0fb079fe656c1004855e029da" | ||
"gitHead": "a318aec57d33b53131c0b03cc8dd6ab4efae3e50" | ||
} |
@@ -38,6 +38,10 @@ # `@inquirer/checkbox` | ||
| Property | Type | Required | Description | | ||
| -------- | ------------------------------------------------------------------------------------------------------- | -------- | ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | | ||
| message | `string` | yes | The question to ask | | ||
| choices | `Array<{ value: string, name?: string, disabled?: boolean \| string, checked?: boolean } \| Separator>` | yes | List of the available choices. The `value` will be returned as the answer, and used as display if no `name` is defined. Choices who're `disabled` will be displayed, but not selectable. | | ||
| Property | Type | Required | Description | | ||
| -------- | ------------------------------------------------------------------------------------------------------- | -------- | ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | | ||
| message | `string` | yes | The question to ask | | ||
| choices | `Array<{ value: string, name?: string, disabled?: boolean \| string, checked?: boolean } \| Separator>` | yes | List of the available choices. The `value` will be returned as the answer, and used as display if no `name` is defined. Choices who're `disabled` will be displayed, but not selectable. | | ||
| pageSize | `number` | no | By default, lists of choice longer than 7 will be paginated. Use this option to control how many choices will appear on the screen at once. | | ||
| loop | `boolean` | no | Defaults to `true`. When set to `false`, the cursor will be constrained to the top and bottom of the choice list without looping. | | ||
| required | `boolean` | no | When set to `true`, ensures at least one choice must be selected. | | ||
| validate | `string => boolean \| string \| Promise<string \| boolean>` | no | On submit, validate the choices. 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. | | ||
@@ -44,0 +48,0 @@ The `Separator` object can be used to render non-selectable lines in the choice list. By default it'll render a line, but you can provide the text as argument (`new Separator('-- Dependencies --')`). This option is often used to add labels to groups within long list of options. |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
27308
23.18%450
18.11%53
8.16%Updated