Skip to content

Commit 4a2a150

Browse files
matthykSBoudrias
andauthored
add renderSelectedChoices style option (#1360)
* add renderSelectedChoices style option * Update packages/checkbox/src/index.mts Co-authored-by: Simon Boudrias <[email protected]> * fix formatting --------- Co-authored-by: Simon Boudrias <[email protected]>
1 parent 44016a4 commit 4a2a150

File tree

3 files changed

+76
-4
lines changed

3 files changed

+76
-4
lines changed

packages/checkbox/README.md

+4
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,10 @@ type Theme = {
6767
highlight: (text: string) => string;
6868
key: (text: string) => string;
6969
disabledChoice: (text: string) => string;
70+
renderSelectedChoices: <T>(
71+
selectedChoices: ReadonlyArray<Choice<T>>,
72+
allChoices: ReadonlyArray<Choice<T> | Separator>,
73+
) => string;
7074
};
7175
icon: {
7276
checked: string;

packages/checkbox/checkbox.test.mts

+60
Original file line numberDiff line numberDiff line change
@@ -685,4 +685,64 @@ describe('checkbox prompt', () => {
685685
events.keypress('enter');
686686
await expect(answer).resolves.toEqual([1]);
687687
});
688+
689+
it('renderSelectedChoices', async () => {
690+
const { answer, events, getScreen } = await render(checkbox, {
691+
message: 'Select your favourite number.',
692+
choices: numberedChoices,
693+
theme: {
694+
style: {
695+
renderSelectedChoices(selected: { value: number }[]) {
696+
if (selected.length > 1) {
697+
return `You have selected ${selected[0].value} and ${selected.length - 1} more.`;
698+
}
699+
return `You have selected ${selected
700+
.slice(0, 1)
701+
.map((c) => c.value)
702+
.join(', ')}.`;
703+
},
704+
},
705+
},
706+
});
707+
708+
events.keypress('space');
709+
events.keypress('down');
710+
events.keypress('space');
711+
events.keypress('down');
712+
events.keypress('space');
713+
events.keypress('enter');
714+
715+
await answer;
716+
expect(getScreen()).toMatchInlineSnapshot(
717+
'"? Select your favourite number. You have selected 1 and 2 more."',
718+
);
719+
});
720+
721+
it('renderSelectedChoices - using allChoices parameter', async () => {
722+
const { answer, events, getScreen } = await render(checkbox, {
723+
message: 'Select your favourite number.',
724+
choices: numberedChoices,
725+
theme: {
726+
style: {
727+
renderSelectedChoices(
728+
selected: { value: number }[],
729+
all: ({ value: number } | Separator)[],
730+
) {
731+
return `You have selected ${selected.length} out of ${all.length} options.`;
732+
},
733+
},
734+
},
735+
});
736+
737+
events.keypress('space');
738+
events.keypress('down');
739+
events.keypress('down');
740+
events.keypress('space');
741+
events.keypress('enter');
742+
743+
await answer;
744+
expect(getScreen()).toMatchInlineSnapshot(
745+
'"? Select your favourite number. You have selected 2 out of 12 options."',
746+
);
747+
});
688748
});

packages/checkbox/src/index.mts

+12-4
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,10 @@ type CheckboxTheme = {
2727
};
2828
style: {
2929
disabledChoice: (text: string) => string;
30+
renderSelectedChoices: <T>(
31+
selectedChoices: ReadonlyArray<Choice<T>>,
32+
allChoices: ReadonlyArray<Choice<T> | Separator>,
33+
) => string;
3034
};
3135
};
3236

@@ -38,6 +42,8 @@ const checkboxTheme: CheckboxTheme = {
3842
},
3943
style: {
4044
disabledChoice: (text: string) => chalk.dim(`- ${text}`),
45+
renderSelectedChoices: (selectedChoices) =>
46+
selectedChoices.map((choice) => choice.name || choice.value).join(', '),
4147
},
4248
};
4349

@@ -193,10 +199,12 @@ export default createPrompt(
193199
});
194200

195201
if (status === 'done') {
196-
const selection = items
197-
.filter(isChecked)
198-
.map((choice) => choice.name || choice.value);
199-
return `${prefix} ${message} ${theme.style.answer(selection.join(', '))}`;
202+
const selection = items.filter(isChecked);
203+
const answer = theme.style.answer(
204+
theme.style.renderSelectedChoices(selection, items),
205+
);
206+
207+
return `${prefix} ${message} ${answer}`;
200208
}
201209

202210
let helpTip = '';

0 commit comments

Comments
 (0)