@@ -207,23 +207,15 @@ export class TreePattern<V> {
207207 const manager = new KeyboardEventManager ( ) ;
208208 const list = this . listBehavior ;
209209
210- if ( ! this . followFocus ( ) ) {
211- manager
212- . on ( this . prevKey , ( ) => list . prev ( ) )
213- . on ( this . nextKey , ( ) => list . next ( ) )
214- . on ( 'Home' , ( ) => list . first ( ) )
215- . on ( 'End' , ( ) => list . last ( ) )
216- . on ( this . typeaheadRegexp , e => list . search ( e . key ) ) ;
217- }
218-
219- if ( this . followFocus ( ) ) {
220- manager
221- . on ( this . prevKey , ( ) => list . prev ( { selectOne : true } ) )
222- . on ( this . nextKey , ( ) => list . next ( { selectOne : true } ) )
223- . on ( 'Home' , ( ) => list . first ( { selectOne : true } ) )
224- . on ( 'End' , ( ) => list . last ( { selectOne : true } ) )
225- . on ( this . typeaheadRegexp , e => list . search ( e . key , { selectOne : true } ) ) ;
226- }
210+ manager
211+ . on ( this . prevKey , ( ) => list . prev ( { selectOne : this . followFocus ( ) } ) )
212+ . on ( this . nextKey , ( ) => list . next ( { selectOne : this . followFocus ( ) } ) )
213+ . on ( 'Home' , ( ) => list . first ( { selectOne : this . followFocus ( ) } ) )
214+ . on ( 'End' , ( ) => list . last ( { selectOne : this . followFocus ( ) } ) )
215+ . on ( this . typeaheadRegexp , e => list . search ( e . key , { selectOne : this . followFocus ( ) } ) )
216+ . on ( this . expandKey , ( ) => this . expand ( { selectOne : this . followFocus ( ) } ) )
217+ . on ( this . collapseKey , ( ) => this . collapse ( { selectOne : this . followFocus ( ) } ) )
218+ . on ( Modifier . Shift , '*' , ( ) => this . expandSiblings ( ) ) ;
227219
228220 if ( this . inputs . multi ( ) ) {
229221 manager
@@ -260,6 +252,8 @@ export class TreePattern<V> {
260252 manager
261253 . on ( [ Modifier . Ctrl , Modifier . Meta ] , this . prevKey , ( ) => list . prev ( ) )
262254 . on ( [ Modifier . Ctrl , Modifier . Meta ] , this . nextKey , ( ) => list . next ( ) )
255+ . on ( [ Modifier . Ctrl , Modifier . Meta ] , this . expandKey , ( ) => this . expand ( ) )
256+ . on ( [ Modifier . Ctrl , Modifier . Meta ] , this . collapseKey , ( ) => this . collapse ( ) )
263257 . on ( [ Modifier . Ctrl , Modifier . Meta ] , ' ' , ( ) => list . toggle ( ) )
264258 . on ( [ Modifier . Ctrl , Modifier . Meta ] , 'Enter' , ( ) => list . toggle ( ) )
265259 . on ( [ Modifier . Ctrl , Modifier . Meta ] , 'Home' , ( ) => list . first ( ) )
@@ -270,11 +264,6 @@ export class TreePattern<V> {
270264 } ) ;
271265 }
272266
273- manager
274- . on ( this . expandKey , ( ) => this . expand ( ) )
275- . on ( this . collapseKey , ( ) => this . collapse ( ) )
276- . on ( Modifier . Shift , '*' , ( ) => this . expandSiblings ( ) ) ;
277-
278267 return manager ;
279268 } ) ;
280269
@@ -403,38 +392,38 @@ export class TreePattern<V> {
403392 }
404393
405394 /** Expands a tree item. */
406- expand ( item ?: TreeItemPattern < V > ) {
407- item ?? = this . activeItem ( ) ;
395+ expand ( opts ?: SelectOptions ) {
396+ const item = this . activeItem ( ) ;
408397 if ( ! item || ! this . listBehavior . isFocusable ( item ) ) return ;
409398
410399 if ( item . expandable ( ) && ! item . expanded ( ) ) {
411400 item . expansion . open ( ) ;
412- } else if ( item . expanded ( ) && item . children ( ) . length > 0 ) {
413- const firstChild = item . children ( ) [ 0 ] ;
414- if ( this . listBehavior . isFocusable ( firstChild ) ) {
415- this . listBehavior . goto ( firstChild ) ;
416- }
401+ } else if (
402+ item . expanded ( ) &&
403+ item . children ( ) . some ( item => this . listBehavior . isFocusable ( item ) )
404+ ) {
405+ this . listBehavior . next ( opts ) ;
417406 }
418407 }
419408
420409 /** Expands all sibling tree items including itself. */
421410 expandSiblings ( item ?: TreeItemPattern < V > ) {
422411 item ??= this . activeItem ( ) ;
423412 const siblings = item ?. parent ( ) ?. children ( ) ;
424- siblings ?. forEach ( item => this . expand ( item ) ) ;
413+ siblings ?. forEach ( item => item . expansion . open ( ) ) ;
425414 }
426415
427416 /** Collapses a tree item. */
428- collapse ( item ?: TreeItemPattern < V > ) {
429- item ?? = this . activeItem ( ) ;
417+ collapse ( opts ?: SelectOptions ) {
418+ const item = this . activeItem ( ) ;
430419 if ( ! item || ! this . listBehavior . isFocusable ( item ) ) return ;
431420
432421 if ( item . expandable ( ) && item . expanded ( ) ) {
433422 item . expansion . close ( ) ;
434423 } else if ( item . parent ( ) && item . parent ( ) !== this ) {
435424 const parentItem = item . parent ( ) ;
436425 if ( parentItem instanceof TreeItemPattern && this . listBehavior . isFocusable ( parentItem ) ) {
437- this . listBehavior . goto ( parentItem ) ;
426+ this . listBehavior . goto ( parentItem , opts ) ;
438427 }
439428 }
440429 }
0 commit comments