Skip to content

Commit

Permalink
inquirer(feat): re-add checkbox prompt support for default prop
Browse files Browse the repository at this point in the history
---------

Co-authored-by: Simon Boudrias <admin@simonboudrias.com>
  • Loading branch information
mshima and SBoudrias authored Dec 19, 2024
1 parent e228166 commit 5001eaa
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 11 deletions.
29 changes: 29 additions & 0 deletions packages/inquirer/inquirer.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -519,6 +519,35 @@ describe('inquirer.prompt(...)', () => {
expect(answers).toEqual({ name1: 'bar', name: undefined });
});

it('should use `default` when passed along `choices`', async () => {
class FakeSelect {
constructor(question: QuestionMap['stubSelect']) {
expect(question.choices).toEqual([
{ name: 'A', value: 'A', checked: false },
{ name: 'B', value: 'B', checked: true },
]);
}

run() {
return Promise.resolve();
}

close() {}
}
inquirer.registerPrompt('stubSelect', FakeSelect);

const answers = await inquirer.prompt([
{
type: 'stubSelect',
name: 'name',
message: 'message',
choices: ['A', 'B'],
default: ['B'],
},
]);
expect(answers).toEqual({ name: undefined });
});

it('should expose the Reactive interface', async () => {
const spy = vi.fn();

Expand Down
6 changes: 5 additions & 1 deletion packages/inquirer/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,11 @@ type QuestionWithGetters<
>;

export type UnnamedDistinctQuestion<A extends Answers = object> =
| QuestionWithGetters<'checkbox', Parameters<typeof checkbox>[0], A>
| QuestionWithGetters<
'checkbox',
Parameters<typeof checkbox>[0] & { default: unknown[] },
A
>
| QuestionWithGetters<'confirm', Parameters<typeof confirm>[0], A>
| QuestionWithGetters<'editor', Parameters<typeof editor>[0], A>
| QuestionWithGetters<'expand', Parameters<typeof expand>[0], A>
Expand Down
31 changes: 21 additions & 10 deletions packages/inquirer/src/ui/prompt.ts
Original file line number Diff line number Diff line change
Expand Up @@ -266,17 +266,28 @@ export default class PromptsRunner<A extends Answers> {
let choices;
if (Array.isArray(resolvedChoices)) {
choices = resolvedChoices.map((choice: unknown) => {
if (typeof choice === 'string' || typeof choice === 'number') {
return { name: choice, value: choice };
} else if (
typeof choice === 'object' &&
choice != null &&
!('value' in choice) &&
'name' in choice
) {
return { ...choice, value: choice.name };
const choiceObj =
typeof choice !== 'object' || choice == null
? { name: choice, value: choice }
: {
...choice,
value:
'value' in choice
? choice.value
: 'name' in choice
? choice.name
: undefined,
};

if ('value' in choiceObj && Array.isArray(defaultValue)) {
// Add checked to question for backward compatibility. default was supported as alternative of per choice checked.
return {
checked: defaultValue.includes(choiceObj.value),
...choiceObj,
};
}
return choice;

return choiceObj;
});
}

Expand Down

0 comments on commit 5001eaa

Please sign in to comment.