Skip to content

Commit 5001eaa

Browse files
mshimaSBoudrias
andauthored
inquirer(feat): re-add checkbox prompt support for default prop
--------- Co-authored-by: Simon Boudrias <[email protected]>
1 parent e228166 commit 5001eaa

File tree

3 files changed

+55
-11
lines changed

3 files changed

+55
-11
lines changed

packages/inquirer/inquirer.test.ts

+29
Original file line numberDiff line numberDiff line change
@@ -519,6 +519,35 @@ describe('inquirer.prompt(...)', () => {
519519
expect(answers).toEqual({ name1: 'bar', name: undefined });
520520
});
521521

522+
it('should use `default` when passed along `choices`', async () => {
523+
class FakeSelect {
524+
constructor(question: QuestionMap['stubSelect']) {
525+
expect(question.choices).toEqual([
526+
{ name: 'A', value: 'A', checked: false },
527+
{ name: 'B', value: 'B', checked: true },
528+
]);
529+
}
530+
531+
run() {
532+
return Promise.resolve();
533+
}
534+
535+
close() {}
536+
}
537+
inquirer.registerPrompt('stubSelect', FakeSelect);
538+
539+
const answers = await inquirer.prompt([
540+
{
541+
type: 'stubSelect',
542+
name: 'name',
543+
message: 'message',
544+
choices: ['A', 'B'],
545+
default: ['B'],
546+
},
547+
]);
548+
expect(answers).toEqual({ name: undefined });
549+
});
550+
522551
it('should expose the Reactive interface', async () => {
523552
const spy = vi.fn();
524553

packages/inquirer/src/types.ts

+5-1
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,11 @@ type QuestionWithGetters<
7878
>;
7979

8080
export type UnnamedDistinctQuestion<A extends Answers = object> =
81-
| QuestionWithGetters<'checkbox', Parameters<typeof checkbox>[0], A>
81+
| QuestionWithGetters<
82+
'checkbox',
83+
Parameters<typeof checkbox>[0] & { default: unknown[] },
84+
A
85+
>
8286
| QuestionWithGetters<'confirm', Parameters<typeof confirm>[0], A>
8387
| QuestionWithGetters<'editor', Parameters<typeof editor>[0], A>
8488
| QuestionWithGetters<'expand', Parameters<typeof expand>[0], A>

packages/inquirer/src/ui/prompt.ts

+21-10
Original file line numberDiff line numberDiff line change
@@ -266,17 +266,28 @@ export default class PromptsRunner<A extends Answers> {
266266
let choices;
267267
if (Array.isArray(resolvedChoices)) {
268268
choices = resolvedChoices.map((choice: unknown) => {
269-
if (typeof choice === 'string' || typeof choice === 'number') {
270-
return { name: choice, value: choice };
271-
} else if (
272-
typeof choice === 'object' &&
273-
choice != null &&
274-
!('value' in choice) &&
275-
'name' in choice
276-
) {
277-
return { ...choice, value: choice.name };
269+
const choiceObj =
270+
typeof choice !== 'object' || choice == null
271+
? { name: choice, value: choice }
272+
: {
273+
...choice,
274+
value:
275+
'value' in choice
276+
? choice.value
277+
: 'name' in choice
278+
? choice.name
279+
: undefined,
280+
};
281+
282+
if ('value' in choiceObj && Array.isArray(defaultValue)) {
283+
// Add checked to question for backward compatibility. default was supported as alternative of per choice checked.
284+
return {
285+
checked: defaultValue.includes(choiceObj.value),
286+
...choiceObj,
287+
};
278288
}
279-
return choice;
289+
290+
return choiceObj;
280291
});
281292
}
282293

0 commit comments

Comments
 (0)