Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions .changeset/select-key-disabled-rendering.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
"@clack/core": minor
"@clack/prompts": minor
---

Add support for disabled `selectKey` options: they are rendered dimmed, cannot be selected by keypress, and are ignored when set as the initial value.
14 changes: 10 additions & 4 deletions packages/core/src/prompts/select-key.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
import Prompt, { type PromptOptions } from './prompt.js';

export interface SelectKeyOptions<T extends { value: string }>
export interface SelectKeyOptions<T extends { value: string; disabled?: boolean }>
extends PromptOptions<T['value'], SelectKeyPrompt<T>> {
options: T[];
caseSensitive?: boolean;
}
export default class SelectKeyPrompt<T extends { value: string }> extends Prompt<T['value']> {
export default class SelectKeyPrompt<
T extends { value: string; disabled?: boolean },
> extends Prompt<T['value']> {
options: T[];
cursor = 0;

Expand All @@ -17,7 +19,8 @@ export default class SelectKeyPrompt<T extends { value: string }> extends Prompt
const keys = this.options.map(({ value: [initial] }) => {
return caseSensitive ? initial : initial?.toLowerCase();
});
this.cursor = Math.max(keys.indexOf(opts.initialValue), 0);
const cursor = Math.max(keys.indexOf(opts.initialValue), 0);
this.cursor = this.options[cursor]?.disabled ? 0 : cursor;

this.on('key', (key) => {
if (!key) {
Expand All @@ -28,7 +31,10 @@ export default class SelectKeyPrompt<T extends { value: string }> extends Prompt
return;
}

const value = this.options.find(({ value: [initial] }) => {
const value = this.options.find(({ value: [initial], disabled }) => {
if (disabled) {
return false;
}
return caseSensitive ? initial === casedKey : initial?.toLowerCase() === casedKey;
});
if (value) {
Expand Down
12 changes: 10 additions & 2 deletions packages/prompts/src/select-key.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,17 @@ export interface SelectKeyOptions<Value extends string> extends CommonOptions {
export const selectKey = <Value extends string>(opts: SelectKeyOptions<Value>) => {
const opt = (
option: Option<Value> | undefined,
state: 'inactive' | 'active' | 'selected' | 'cancelled' = 'inactive'
state: 'inactive' | 'active' | 'selected' | 'cancelled' | 'disabled' = 'inactive'
) => {
if (option === undefined) {
return '';
}
const label = option.label ?? String(option.value);
if (state === 'disabled') {
return `${styleText('dim', ` ${option.value} `)} ${styleText('gray', label)}${
option.hint ? ` ${styleText('dim', `(${option.hint})`)}` : ''
}`;
}
if (state === 'selected') {
return `${styleText('dim', label)}`;
}
Expand Down Expand Up @@ -75,7 +80,10 @@ export const selectKey = <Value extends string>(opts: SelectKeyOptions<Value>) =
.map((option, i) =>
wrapTextWithPrefix(
opts.output,
opt(option, i === this.cursor ? 'active' : 'inactive'),
opt(
option,
option.disabled ? 'disabled' : i === this.cursor ? 'active' : 'inactive'
),
defaultPrefix
)
)
Expand Down
80 changes: 80 additions & 0 deletions packages/prompts/test/__snapshots__/select-key.test.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,26 @@ exports[`text (isCI = false) > global withGuide: false removes guide 1`] = `
]
`;
exports[`text (isCI = false) > ignores a disabled initial value 1`] = `
[
"<cursor.hide>",
"│
◆ foo
│  d  Disabled
│  e  Enabled
└
",
"<cursor.show>",
"<cursor.backward count=999><cursor.up count=5>",
"<cursor.down count=1>",
"<erase.down>",
"◇ foo
│ Enabled",
"
",
]
`;
exports[`text (isCI = false) > input is case-insensitive by default 1`] = `
[
"<cursor.hide>",
Expand Down Expand Up @@ -248,6 +268,26 @@ exports[`text (isCI = false) > options are case-insensitive by default 1`] = `
]
`;
exports[`text (isCI = false) > renders disabled options dimmed 1`] = `
[
"<cursor.hide>",
"│
◆ foo
│  d  Disabled (unavailable)
│  e  Enabled
└
",
"<cursor.show>",
"<cursor.backward count=999><cursor.up count=5>",
"<cursor.down count=1>",
"<erase.down>",
"◇ foo
│ Enabled",
"
",
]
`;
exports[`text (isCI = false) > renders message with options 1`] = `
[
"<cursor.hide>",
Expand Down Expand Up @@ -387,6 +427,26 @@ exports[`text (isCI = true) > global withGuide: false removes guide 1`] = `
]
`;
exports[`text (isCI = true) > ignores a disabled initial value 1`] = `
[
"<cursor.hide>",
"│
◆ foo
│  d  Disabled
│  e  Enabled
└
",
"<cursor.show>",
"<cursor.backward count=999><cursor.up count=5>",
"<cursor.down count=1>",
"<erase.down>",
"◇ foo
│ Enabled",
"
",
]
`;
exports[`text (isCI = true) > input is case-insensitive by default 1`] = `
[
"<cursor.hide>",
Expand Down Expand Up @@ -554,6 +614,26 @@ exports[`text (isCI = true) > options are case-insensitive by default 1`] = `
]
`;
exports[`text (isCI = true) > renders disabled options dimmed 1`] = `
[
"<cursor.hide>",
"│
◆ foo
│  d  Disabled (unavailable)
│  e  Enabled
└
",
"<cursor.show>",
"<cursor.backward count=999><cursor.up count=5>",
"<cursor.down count=1>",
"<erase.down>",
"◇ foo
│ Enabled",
"
",
]
`;
exports[`text (isCI = true) > renders message with options 1`] = `
[
"<cursor.hide>",
Expand Down
70 changes: 70 additions & 0 deletions packages/prompts/test/select-key.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,76 @@ describe.each(['true', 'false'])('text (isCI = %s)', (isCI) => {
expect(value).toBe('b');
});

test('renders disabled options dimmed', async () => {
const result = prompts.selectKey({
message: 'foo',
options: [
{ label: 'Disabled', value: 'd', disabled: true, hint: 'unavailable' },
{ label: 'Enabled', value: 'e' },
],
input,
output,
});

input.emit('keypress', 'e', { name: 'e' });

await result;

expect(output.buffer).toMatchSnapshot();
});

test('does not select disabled options', async () => {
const result = prompts.selectKey({
message: 'foo',
options: [
{ label: 'Disabled', value: 'd', disabled: true },
{ label: 'Enabled', value: 'e' },
],
input,
output,
});

input.emit('keypress', 'd', { name: 'd' });
input.emit('keypress', 'e', { name: 'e' });

expect(await result).toBe('e');
});

test('skips disabled options sharing the same initial', async () => {
const result = prompts.selectKey({
message: 'foo',
options: [
{ label: 'delete', value: 'delete', disabled: true },
{ label: 'deploy', value: 'deploy' },
],
input,
output,
});

input.emit('keypress', 'd', { name: 'd' });

expect(await result).toBe('deploy');
});

test('ignores a disabled initial value', async () => {
const result = prompts.selectKey({
message: 'foo',
options: [
{ label: 'Disabled', value: 'd', disabled: true },
{ label: 'Enabled', value: 'e' },
],
initialValue: 'd',
input,
output,
});

input.emit('keypress', 'e', { name: 'e' });

await result;

expect(output.buffer).toMatchSnapshot();
});

test('can cancel by pressing escape', async () => {
const result = prompts.selectKey({
message: 'foo',
Expand Down
Loading