Skip to content

Commit 37d3eea

Browse files
committed
feat: select prompt disabled option
1 parent 5529c89 commit 37d3eea

File tree

1 file changed

+26
-0
lines changed

1 file changed

+26
-0
lines changed

packages/prompts/src/index.ts

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -228,6 +228,12 @@ export type Option<Value> = Value extends Primitive
228228
* By default, no `hint` is displayed.
229229
*/
230230
hint?: string;
231+
/**
232+
* If true, this option will be disabled.
233+
*
234+
* By default, no option is disabled.
235+
*/
236+
disabled?: boolean;
231237
}
232238
: {
233239
/**
@@ -245,6 +251,12 @@ export type Option<Value> = Value extends Primitive
245251
* By default, no `hint` is displayed.
246252
*/
247253
hint?: string;
254+
/**
255+
* If true, this option will be disabled.
256+
*
257+
* By default, no option is disabled.
258+
*/
259+
disabled?: boolean;
248260
};
249261

250262
export interface SelectOptions<Value> {
@@ -274,6 +286,11 @@ export const select = <Value>(opts: SelectOptions<Value>) => {
274286
return new SelectPrompt({
275287
options: opts.options,
276288
initialValue: opts.initialValue,
289+
validate(value) {
290+
if (this.options.find((o) => o.value === value)?.disabled) {
291+
return 'Selected option is disabled.';
292+
}
293+
},
277294
render() {
278295
const title = `${color.gray(S_BAR)}\n${symbol(this.state)} ${opts.message}\n`;
279296

@@ -285,6 +302,15 @@ export const select = <Value>(opts: SelectOptions<Value>) => {
285302
this.options[this.cursor],
286303
'cancelled'
287304
)}\n${color.gray(S_BAR)}`;
305+
case 'error':
306+
return `${title}${color.yellow(S_BAR)} ${limitOptions({
307+
cursor: this.cursor,
308+
options: this.options,
309+
maxItems: opts.maxItems,
310+
style: (item, active) => opt(item, active ? 'active' : 'inactive'),
311+
}).join(
312+
`\n${color.yellow(S_BAR)} `
313+
)}\n${color.yellow(S_BAR_END)} ${color.yellow(this.error)}\n`;
288314
default: {
289315
return `${title}${color.cyan(S_BAR)} ${limitOptions({
290316
cursor: this.cursor,

0 commit comments

Comments
 (0)