@@ -207,23 +207,15 @@ export class TreePattern<V> {
207
207
const manager = new KeyboardEventManager ( ) ;
208
208
const list = this . listBehavior ;
209
209
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 ( ) ) ;
227
219
228
220
if ( this . inputs . multi ( ) ) {
229
221
manager
@@ -260,6 +252,8 @@ export class TreePattern<V> {
260
252
manager
261
253
. on ( [ Modifier . Ctrl , Modifier . Meta ] , this . prevKey , ( ) => list . prev ( ) )
262
254
. 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 ( ) )
263
257
. on ( [ Modifier . Ctrl , Modifier . Meta ] , ' ' , ( ) => list . toggle ( ) )
264
258
. on ( [ Modifier . Ctrl , Modifier . Meta ] , 'Enter' , ( ) => list . toggle ( ) )
265
259
. on ( [ Modifier . Ctrl , Modifier . Meta ] , 'Home' , ( ) => list . first ( ) )
@@ -270,11 +264,6 @@ export class TreePattern<V> {
270
264
} ) ;
271
265
}
272
266
273
- manager
274
- . on ( this . expandKey , ( ) => this . expand ( ) )
275
- . on ( this . collapseKey , ( ) => this . collapse ( ) )
276
- . on ( Modifier . Shift , '*' , ( ) => this . expandSiblings ( ) ) ;
277
-
278
267
return manager ;
279
268
} ) ;
280
269
@@ -403,38 +392,38 @@ export class TreePattern<V> {
403
392
}
404
393
405
394
/** Expands a tree item. */
406
- expand ( item ?: TreeItemPattern < V > ) {
407
- item ?? = this . activeItem ( ) ;
395
+ expand ( opts ?: SelectOptions ) {
396
+ const item = this . activeItem ( ) ;
408
397
if ( ! item || ! this . listBehavior . isFocusable ( item ) ) return ;
409
398
410
399
if ( item . expandable ( ) && ! item . expanded ( ) ) {
411
400
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 ) ;
417
406
}
418
407
}
419
408
420
409
/** Expands all sibling tree items including itself. */
421
410
expandSiblings ( item ?: TreeItemPattern < V > ) {
422
411
item ??= this . activeItem ( ) ;
423
412
const siblings = item ?. parent ( ) ?. children ( ) ;
424
- siblings ?. forEach ( item => this . expand ( item ) ) ;
413
+ siblings ?. forEach ( item => item . expansion . open ( ) ) ;
425
414
}
426
415
427
416
/** Collapses a tree item. */
428
- collapse ( item ?: TreeItemPattern < V > ) {
429
- item ?? = this . activeItem ( ) ;
417
+ collapse ( opts ?: SelectOptions ) {
418
+ const item = this . activeItem ( ) ;
430
419
if ( ! item || ! this . listBehavior . isFocusable ( item ) ) return ;
431
420
432
421
if ( item . expandable ( ) && item . expanded ( ) ) {
433
422
item . expansion . close ( ) ;
434
423
} else if ( item . parent ( ) && item . parent ( ) !== this ) {
435
424
const parentItem = item . parent ( ) ;
436
425
if ( parentItem instanceof TreeItemPattern && this . listBehavior . isFocusable ( parentItem ) ) {
437
- this . listBehavior . goto ( parentItem ) ;
426
+ this . listBehavior . goto ( parentItem , opts ) ;
438
427
}
439
428
}
440
429
}
0 commit comments