Skip to content

Commit 46dc0a4

Browse files
authored
fix: add hints for selected options in multiselect prompts & tests (bombshell-dev#279)
1 parent 0146d83 commit 46dc0a4

File tree

4 files changed

+107
-2
lines changed

4 files changed

+107
-2
lines changed

.changeset/afraid-socks-deny.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@clack/prompts": patch
3+
---
4+
5+
Fixes multiselect only shows hints on the first item in the options list. Now correctly shows hints for all selected options with hint property.

packages/prompts/src/__snapshots__/index.test.ts.snap

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -955,6 +955,42 @@ exports[`prompts (isCI = false) > multiselect > renders validation errors 1`] =
955955
]
956956
`;
957957

958+
exports[`prompts (isCI = false) > multiselect > shows hints for all selected options 1`] = `
959+
[
960+
"[?25l",
961+
"│
962+
◆ foo
963+
│ ◼ opt0 (Hint 0)
964+
│ ◼ opt1 (Hint 1)
965+
│ ◻ opt2
966+
└
967+
",
968+
"",
969+
"",
970+
"",
971+
"│ ◼ opt0 (Hint 0)
972+
│ ◼ opt1 (Hint 1)
973+
│ ◻ opt2
974+
└
975+
",
976+
"",
977+
"",
978+
"",
979+
"│ ◼ opt1 (Hint 1)
980+
│ ◻ opt2 (Hint 2)
981+
└
982+
",
983+
"",
984+
"",
985+
"",
986+
"◇ foo
987+
│ opt0, opt1",
988+
"
989+
",
990+
"[?25h",
991+
]
992+
`;
993+
958994
exports[`prompts (isCI = false) > multiselect > sliding window loops downwards 1`] = `
959995
[
960996
"[?25l",
@@ -2693,6 +2729,42 @@ exports[`prompts (isCI = true) > multiselect > renders validation errors 1`] = `
26932729
]
26942730
`;
26952731

2732+
exports[`prompts (isCI = true) > multiselect > shows hints for all selected options 1`] = `
2733+
[
2734+
"[?25l",
2735+
"│
2736+
◆ foo
2737+
│ ◼ opt0 (Hint 0)
2738+
│ ◼ opt1 (Hint 1)
2739+
│ ◻ opt2
2740+
└
2741+
",
2742+
"",
2743+
"",
2744+
"",
2745+
"│ ◼ opt0 (Hint 0)
2746+
│ ◼ opt1 (Hint 1)
2747+
│ ◻ opt2
2748+
└
2749+
",
2750+
"",
2751+
"",
2752+
"",
2753+
"│ ◼ opt1 (Hint 1)
2754+
│ ◻ opt2 (Hint 2)
2755+
└
2756+
",
2757+
"",
2758+
"",
2759+
"",
2760+
"◇ foo
2761+
│ opt0, opt1",
2762+
"
2763+
",
2764+
"[?25h",
2765+
]
2766+
`;
2767+
26962768
exports[`prompts (isCI = true) > multiselect > sliding window loops downwards 1`] = `
26972769
[
26982770
"[?25l",

packages/prompts/src/index.test.ts

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -790,6 +790,30 @@ describe.each(['true', 'false'])('prompts (isCI = %s)', (isCI) => {
790790
expect(output.buffer).toMatchSnapshot();
791791
});
792792

793+
test('shows hints for all selected options', async () => {
794+
const result = prompts.multiselect({
795+
message: 'foo',
796+
options: [
797+
{ value: 'opt0', hint: 'Hint 0' },
798+
{ value: 'opt1', hint: 'Hint 1' },
799+
{ value: 'opt2', hint: 'Hint 2' },
800+
],
801+
initialValues: ['opt0', 'opt1'],
802+
input,
803+
output,
804+
});
805+
806+
// Check that both selected options show their hints
807+
input.emit('keypress', '', { name: 'down' });
808+
input.emit('keypress', '', { name: 'down' });
809+
input.emit('keypress', '', { name: 'return' });
810+
811+
const value = await result;
812+
813+
expect(value).toEqual(['opt0', 'opt1']);
814+
expect(output.buffer).toMatchSnapshot();
815+
});
816+
793817
test('renders multiple cancelled values', async () => {
794818
const result = prompts.multiselect({
795819
message: 'foo',

packages/prompts/src/index.ts

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -386,7 +386,9 @@ export const multiselect = <Value>(opts: MultiSelectOptions<Value>) => {
386386
}`;
387387
}
388388
if (state === 'selected') {
389-
return `${color.green(S_CHECKBOX_SELECTED)} ${color.dim(label)}`;
389+
return `${color.green(S_CHECKBOX_SELECTED)} ${color.dim(label)} ${
390+
option.hint ? color.dim(`(${option.hint})`) : ''
391+
}`;
390392
}
391393
if (state === 'cancelled') {
392394
return `${color.strikethrough(color.dim(label))}`;
@@ -522,7 +524,9 @@ export const groupMultiselect = <Value>(opts: GroupMultiSelectOptions<Value>) =>
522524
}
523525
if (state === 'selected') {
524526
const selectedCheckbox = isItem || selectableGroups ? color.green(S_CHECKBOX_SELECTED) : '';
525-
return `${color.dim(prefix)}${selectedCheckbox} ${color.dim(label)}`;
527+
return `${color.dim(prefix)}${selectedCheckbox} ${color.dim(label)} ${
528+
option.hint ? color.dim(`(${option.hint})`) : ''
529+
}`;
526530
}
527531
if (state === 'cancelled') {
528532
return `${color.strikethrough(color.dim(label))}`;

0 commit comments

Comments
 (0)