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
4 changes: 4 additions & 0 deletions packages/checkbox/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,10 @@ type Theme = {
highlight: (text: string) => string;
key: (text: string) => string;
disabledChoice: (text: string) => string;
renderSelectedChoices: <T>(
selectedChoices: ReadonlyArray<Choice<T>>,
allChoices: ReadonlyArray<Choice<T> | Separator>,
) => string;
};
icon: {
checked: string;
Expand Down
60 changes: 60 additions & 0 deletions packages/checkbox/checkbox.test.mts
Original file line number Diff line number Diff line change
Expand Up @@ -685,4 +685,64 @@ describe('checkbox prompt', () => {
events.keypress('enter');
await expect(answer).resolves.toEqual([1]);
});

it('renderSelectedChoices', async () => {
const { answer, events, getScreen } = await render(checkbox, {
message: 'Select your favourite number.',
choices: numberedChoices,
theme: {
style: {
renderSelectedChoices(selected: { value: number }[]) {
if (selected.length > 1) {
return `You have selected ${selected[0].value} and ${selected.length - 1} more.`;
}
return `You have selected ${selected
.slice(0, 1)
.map((c) => c.value)
.join(', ')}.`;
},
},
},
});

events.keypress('space');
events.keypress('down');
events.keypress('space');
events.keypress('down');
events.keypress('space');
events.keypress('enter');

await answer;
expect(getScreen()).toMatchInlineSnapshot(
'"? Select your favourite number. You have selected 1 and 2 more."',
);
});

it('renderSelectedChoices - using allChoices parameter', async () => {
const { answer, events, getScreen } = await render(checkbox, {
message: 'Select your favourite number.',
choices: numberedChoices,
theme: {
style: {
renderSelectedChoices(
selected: { value: number }[],
all: ({ value: number } | Separator)[],
) {
return `You have selected ${selected.length} out of ${all.length} options.`;
},
},
},
});

events.keypress('space');
events.keypress('down');
events.keypress('down');
events.keypress('space');
events.keypress('enter');

await answer;
expect(getScreen()).toMatchInlineSnapshot(
'"? Select your favourite number. You have selected 2 out of 12 options."',
);
});
});
16 changes: 12 additions & 4 deletions packages/checkbox/src/index.mts
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,10 @@ type CheckboxTheme = {
};
style: {
disabledChoice: (text: string) => string;
renderSelectedChoices: <T>(
selectedChoices: ReadonlyArray<Choice<T>>,
allChoices: ReadonlyArray<Choice<T> | Separator>,
) => string;
};
};

Expand All @@ -38,6 +42,8 @@ const checkboxTheme: CheckboxTheme = {
},
style: {
disabledChoice: (text: string) => chalk.dim(`- ${text}`),
renderSelectedChoices: (selectedChoices) =>
selectedChoices.map((choice) => choice.name || choice.value).join(', '),
},
};

Expand Down Expand Up @@ -193,10 +199,12 @@ export default createPrompt(
});

if (status === 'done') {
const selection = items
.filter(isChecked)
.map((choice) => choice.name || choice.value);
return `${prefix} ${message} ${theme.style.answer(selection.join(', '))}`;
const selection = items.filter(isChecked);
const answer = theme.style.answer(
theme.style.renderSelectedChoices(selection, items),
);

return `${prefix} ${message} ${answer}`;
}

let helpTip = '';
Expand Down