@@ -44,13 +44,16 @@ function normalisedValue<T>(multiple: boolean, values: T[] | undefined): T | T[]
4444 return values [ 0 ] ;
4545}
4646
47- interface AutocompleteOptions < T extends OptionLike > extends PromptOptions < AutocompletePrompt < T > > {
47+ interface AutocompleteOptions < T extends OptionLike >
48+ extends PromptOptions < T [ 'value' ] | T [ 'value' ] [ ] , AutocompletePrompt < T > > {
4849 options : T [ ] ;
4950 filter ?: FilterFunction < T > ;
5051 multiple ?: boolean ;
5152}
5253
53- export default class AutocompletePrompt < T extends OptionLike > extends Prompt {
54+ export default class AutocompletePrompt < T extends OptionLike > extends Prompt <
55+ T [ 'value' ] | T [ 'value' ] [ ]
56+ > {
5457 options : T [ ] ;
5558 filteredOptions : T [ ] ;
5659 multiple : boolean ;
@@ -59,30 +62,27 @@ export default class AutocompletePrompt<T extends OptionLike> extends Prompt {
5962
6063 focusedValue : T [ 'value' ] | undefined ;
6164 #cursor = 0 ;
62- #lastValue: T [ 'value' ] | undefined ;
65+ #lastUserInput = '' ;
6366 #filterFn: FilterFunction < T > ;
6467
6568 get cursor ( ) : number {
6669 return this . #cursor;
6770 }
6871
69- get valueWithCursor ( ) {
70- if ( ! this . value ) {
72+ get userInputWithCursor ( ) {
73+ if ( ! this . userInput ) {
7174 return color . inverse ( color . hidden ( '_' ) ) ;
7275 }
73- if ( this . _cursor >= this . value . length ) {
74- return `${ this . value } █` ;
76+ if ( this . _cursor >= this . userInput . length ) {
77+ return `${ this . userInput } █` ;
7578 }
76- const s1 = this . value . slice ( 0 , this . _cursor ) ;
77- const [ s2 , ...s3 ] = this . value . slice ( this . _cursor ) ;
79+ const s1 = this . userInput . slice ( 0 , this . _cursor ) ;
80+ const [ s2 , ...s3 ] = this . userInput . slice ( this . _cursor ) ;
7881 return `${ s1 } ${ color . inverse ( s2 ) } ${ s3 . join ( '' ) } ` ;
7982 }
8083
8184 constructor ( opts : AutocompleteOptions < T > ) {
82- super ( {
83- ...opts ,
84- initialValue : undefined ,
85- } ) ;
85+ super ( opts ) ;
8686
8787 this . options = opts . options ;
8888 this . filteredOptions = [ ...this . options ] ;
@@ -102,17 +102,17 @@ export default class AutocompletePrompt<T extends OptionLike> extends Prompt {
102102 }
103103
104104 if ( initialValues ) {
105- this . selectedValues = initialValues ;
106105 for ( const selectedValue of initialValues ) {
107106 const selectedIndex = this . options . findIndex ( ( opt ) => opt . value === selectedValue ) ;
108107 if ( selectedIndex !== - 1 ) {
109108 this . toggleSelected ( selectedValue ) ;
110109 this . #cursor = selectedIndex ;
111- this . focusedValue = this . options [ this . #cursor] ?. value ;
112110 }
113111 }
114112 }
115113
114+ this . focusedValue = this . options [ this . #cursor] ?. value ;
115+
116116 this . on ( 'finalize' , ( ) => {
117117 if ( ! this . value ) {
118118 this . value = normalisedValue ( this . multiple , initialValues ) ;
@@ -124,7 +124,7 @@ export default class AutocompletePrompt<T extends OptionLike> extends Prompt {
124124 } ) ;
125125
126126 this . on ( 'key' , ( char , key ) => this . #onKey( char , key ) ) ;
127- this . on ( 'value ' , ( value ) => this . #onValueChanged ( value ) ) ;
127+ this . on ( 'userInput ' , ( value ) => this . #onUserInputChanged ( value ) ) ;
128128 }
129129
130130 protected override _isActionKey ( char : string | undefined , key : Key ) : boolean {
@@ -187,9 +187,9 @@ export default class AutocompletePrompt<T extends OptionLike> extends Prompt {
187187 }
188188 }
189189
190- #onValueChanged ( value : string | undefined ) : void {
191- if ( value !== this . #lastValue ) {
192- this . #lastValue = value ;
190+ #onUserInputChanged ( value : string ) : void {
191+ if ( value !== this . #lastUserInput ) {
192+ this . #lastUserInput = value ;
193193
194194 if ( value ) {
195195 this . filteredOptions = this . options . filter ( ( opt ) => this . #filterFn( value , opt ) ) ;
0 commit comments