File tree 3 files changed +55
-11
lines changed
3 files changed +55
-11
lines changed Original file line number Diff line number Diff line change @@ -519,6 +519,35 @@ describe('inquirer.prompt(...)', () => {
519
519
expect ( answers ) . toEqual ( { name1 : 'bar' , name : undefined } ) ;
520
520
} ) ;
521
521
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
+
522
551
it ( 'should expose the Reactive interface' , async ( ) => {
523
552
const spy = vi . fn ( ) ;
524
553
Original file line number Diff line number Diff line change @@ -78,7 +78,11 @@ type QuestionWithGetters<
78
78
> ;
79
79
80
80
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
+ >
82
86
| QuestionWithGetters < 'confirm' , Parameters < typeof confirm > [ 0 ] , A >
83
87
| QuestionWithGetters < 'editor' , Parameters < typeof editor > [ 0 ] , A >
84
88
| QuestionWithGetters < 'expand' , Parameters < typeof expand > [ 0 ] , A >
Original file line number Diff line number Diff line change @@ -266,17 +266,28 @@ export default class PromptsRunner<A extends Answers> {
266
266
let choices ;
267
267
if ( Array . isArray ( resolvedChoices ) ) {
268
268
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
+ } ;
278
288
}
279
- return choice ;
289
+
290
+ return choiceObj ;
280
291
} ) ;
281
292
}
282
293
You can’t perform that action at this time.
0 commit comments