1- import { findNextCursor , findPrevCursor } from '../utils/cursor.js' ;
1+ import { findCursor } from '../utils/cursor.js' ;
22import Prompt , { type PromptOptions } from './prompt.js' ;
33
4- interface MultiSelectOptions < T extends { value : any ; disabled ?: boolean } >
4+ interface OptionLike {
5+ value : any ;
6+ disabled ?: boolean ;
7+ }
8+
9+ interface MultiSelectOptions < T extends OptionLike >
510 extends PromptOptions < T [ 'value' ] [ ] , MultiSelectPrompt < T > > {
611 options : T [ ] ;
712 initialValues ?: T [ 'value' ] [ ] ;
813 required ?: boolean ;
914 cursorAt ?: T [ 'value' ] ;
1015}
11- export default class MultiSelectPrompt < T extends { value : any ; disabled ?: boolean } > extends Prompt <
12- T [ 'value' ] [ ]
13- > {
16+ export default class MultiSelectPrompt < T extends OptionLike > extends Prompt < T [ 'value' ] [ ] > {
1417 options : T [ ] ;
1518 cursor = 0 ;
16- #enabledOptions: T [ ] = [ ] ;
1719
1820 private get _value ( ) : T [ 'value' ] {
1921 return this . options [ this . cursor ] . value ;
2022 }
2123
24+ private get _enabledOptions ( ) : T [ ] {
25+ return this . options . filter ( ( option ) => option . disabled !== true ) ;
26+ }
27+
2228 private toggleAll ( ) {
23- const allSelected = this . value !== undefined && this . value . length === this . #enabledOptions. length ;
24- this . value = allSelected ? [ ] : this . #enabledOptions. map ( ( v ) => v . value ) ;
29+ const enabledOptions = this . _enabledOptions ;
30+ const allSelected = this . value !== undefined && this . value . length === enabledOptions . length ;
31+ this . value = allSelected ? [ ] : enabledOptions . map ( ( v ) => v . value ) ;
2532 }
2633
2734 private toggleInvert ( ) {
2835 const value = this . value ;
2936 if ( ! value ) {
3037 return ;
3138 }
32- const notSelected = this . #enabledOptions . filter ( ( v ) => ! value . includes ( v . value ) ) ;
39+ const notSelected = this . _enabledOptions . filter ( ( v ) => ! value . includes ( v . value ) ) ;
3340 this . value = notSelected . map ( ( v ) => v . value ) ;
3441 }
3542
@@ -47,17 +54,12 @@ export default class MultiSelectPrompt<T extends { value: any; disabled?: boolea
4754 super ( opts , false ) ;
4855
4956 this . options = opts . options ;
50- this . #enabledOptions = this . options . filter ( ( option ) => ! option . disabled ) ;
51- if ( this . #enabledOptions. length === 0 ) return ;
5257 this . value = [ ...( opts . initialValues ?? [ ] ) ] ;
5358 const cursor = Math . max (
5459 this . options . findIndex ( ( { value } ) => value === opts . cursorAt ) ,
5560 0
5661 ) ;
57- this . cursor = this . options [ cursor ] . disabled ? findNextCursor < T > (
58- cursor ,
59- this . options
60- ) : cursor ;
62+ this . cursor = this . options [ cursor ] . disabled ? findCursor < T > ( cursor , 1 , this . options ) : cursor ;
6163 this . on ( 'key' , ( char ) => {
6264 if ( char === 'a' ) {
6365 this . toggleAll ( ) ;
@@ -71,11 +73,11 @@ export default class MultiSelectPrompt<T extends { value: any; disabled?: boolea
7173 switch ( key ) {
7274 case 'left' :
7375 case 'up' :
74- this . cursor = findPrevCursor < T > ( this . cursor , this . options ) ;
76+ this . cursor = findCursor < T > ( this . cursor , - 1 , this . options ) ;
7577 break ;
7678 case 'down' :
7779 case 'right' :
78- this . cursor = findNextCursor < T > ( this . cursor , this . options ) ;
80+ this . cursor = findCursor < T > ( this . cursor , 1 , this . options ) ;
7981 break ;
8082 case 'space' :
8183 this . toggleValue ( ) ;
0 commit comments