Skip to content

Commit 3b29ca9

Browse files
committed
fix: update
1 parent 7d14250 commit 3b29ca9

4 files changed

Lines changed: 48 additions & 13 deletions

File tree

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
---
2-
"@clack/core": patch
3-
"@clack/prompts": patch
2+
"@clack/core": minor
3+
"@clack/prompts": minor
44
---
55

6-
Render disabled `selectKey` options dimmed and skip them for the initial cursor.
6+
Add support for disabled `selectKey` options: they are rendered dimmed, cannot be selected by keypress, and are ignored when set as the initial value.

‎packages/core/src/prompts/select-key.ts‎

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
import { findCursor } from '../utils/cursor.js';
21
import Prompt, { type PromptOptions } from './prompt.js';
32

43
export interface SelectKeyOptions<T extends { value: string; disabled?: boolean }>
@@ -21,7 +20,7 @@ export default class SelectKeyPrompt<
2120
return caseSensitive ? initial : initial?.toLowerCase();
2221
});
2322
const cursor = Math.max(keys.indexOf(opts.initialValue), 0);
24-
this.cursor = this.options[cursor]?.disabled ? findCursor<T>(cursor, 1, this.options) : cursor;
23+
this.cursor = this.options[cursor]?.disabled ? 0 : cursor;
2524

2625
this.on('key', (key) => {
2726
if (!key) {
@@ -32,7 +31,10 @@ export default class SelectKeyPrompt<
3231
return;
3332
}
3433

35-
const value = this.options.find(({ value: [initial] }) => {
34+
const value = this.options.find(({ value: [initial], disabled }) => {
35+
if (disabled) {
36+
return false;
37+
}
3638
return caseSensitive ? initial === casedKey : initial?.toLowerCase() === casedKey;
3739
});
3840
if (value) {

‎packages/prompts/test/__snapshots__/select-key.test.ts.snap‎

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -81,13 +81,13 @@ exports[`text (isCI = false) > global withGuide: false removes guide 1`] = `
8181
]
8282
`;
8383
84-
exports[`text (isCI = false) > initial cursor skips disabled options 1`] = `
84+
exports[`text (isCI = false) > ignores a disabled initial value 1`] = `
8585
[
8686
"<cursor.hide>",
8787
"│
8888
◆ foo
8989
│  d  Disabled
90-
[36m│[39m [46m[90m e [39m[49m Enabled
90+
[36m│[39m [90m[47m[7m e [27m[49m[39m Enabled
9191
└
9292
",
9393
"<cursor.show>",
@@ -274,7 +274,7 @@ exports[`text (isCI = false) > renders disabled options dimmed 1`] = `
274274
"│
275275
◆ foo
276276
│  d  Disabled (unavailable)
277-
[36m│[39m [46m[90m e [39m[49m Enabled
277+
[36m│[39m [90m[47m[7m e [27m[49m[39m Enabled
278278
└
279279
",
280280
"<cursor.show>",
@@ -427,13 +427,13 @@ exports[`text (isCI = true) > global withGuide: false removes guide 1`] = `
427427
]
428428
`;
429429
430-
exports[`text (isCI = true) > initial cursor skips disabled options 1`] = `
430+
exports[`text (isCI = true) > ignores a disabled initial value 1`] = `
431431
[
432432
"<cursor.hide>",
433433
"│
434434
◆ foo
435435
│  d  Disabled
436-
[36m│[39m [46m[90m e [39m[49m Enabled
436+
[36m│[39m [90m[47m[7m e [27m[49m[39m Enabled
437437
└
438438
",
439439
"<cursor.show>",
@@ -620,7 +620,7 @@ exports[`text (isCI = true) > renders disabled options dimmed 1`] = `
620620
"│
621621
◆ foo
622622
│  d  Disabled (unavailable)
623-
[36m│[39m [46m[90m e [39m[49m Enabled
623+
[36m│[39m [90m[47m[7m e [27m[49m[39m Enabled
624624
└
625625
",
626626
"<cursor.show>",

‎packages/prompts/test/select-key.test.ts‎

Lines changed: 34 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,40 @@ describe.each(['true', 'false'])('text (isCI = %s)', (isCI) => {
8383
expect(output.buffer).toMatchSnapshot();
8484
});
8585

86-
test('initial cursor skips disabled options', async () => {
86+
test('does not select disabled options', async () => {
87+
const result = prompts.selectKey({
88+
message: 'foo',
89+
options: [
90+
{ label: 'Disabled', value: 'd', disabled: true },
91+
{ label: 'Enabled', value: 'e' },
92+
],
93+
input,
94+
output,
95+
});
96+
97+
input.emit('keypress', 'd', { name: 'd' });
98+
input.emit('keypress', 'e', { name: 'e' });
99+
100+
expect(await result).toBe('e');
101+
});
102+
103+
test('skips disabled options sharing the same initial', async () => {
104+
const result = prompts.selectKey({
105+
message: 'foo',
106+
options: [
107+
{ label: 'delete', value: 'delete', disabled: true },
108+
{ label: 'deploy', value: 'deploy' },
109+
],
110+
input,
111+
output,
112+
});
113+
114+
input.emit('keypress', 'd', { name: 'd' });
115+
116+
expect(await result).toBe('deploy');
117+
});
118+
119+
test('ignores a disabled initial value', async () => {
87120
const result = prompts.selectKey({
88121
message: 'foo',
89122
options: [

0 commit comments

Comments
 (0)