@@ -12,6 +12,7 @@ const createStdin = () => {
12
12
stdin . setRawMode = spy ( ) ;
13
13
stdin . setEncoding = ( ) => { } ;
14
14
stdin . resume = ( ) => { } ;
15
+ stdin . pause = ( ) => { } ;
15
16
16
17
return stdin ;
17
18
} ;
@@ -23,6 +24,7 @@ interface TestProps {
23
24
disabled ?: boolean ;
24
25
focusNext ?: boolean ;
25
26
focusPrevious ?: boolean ;
27
+ unmountChildren ?: boolean ;
26
28
}
27
29
28
30
const Test : FC < TestProps > = ( {
@@ -31,7 +33,8 @@ const Test: FC<TestProps> = ({
31
33
autoFocus = false ,
32
34
disabled = false ,
33
35
focusNext = false ,
34
- focusPrevious = false
36
+ focusPrevious = false ,
37
+ unmountChildren = false
35
38
} ) => {
36
39
const focusManager = useFocusManager ( ) ;
37
40
@@ -55,6 +58,10 @@ const Test: FC<TestProps> = ({
55
58
}
56
59
} , [ focusPrevious ] ) ;
57
60
61
+ if ( unmountChildren ) {
62
+ return null ;
63
+ }
64
+
58
65
return (
59
66
< Box flexDirection = "column" >
60
67
{ showFirst && < Item label = "First" autoFocus = { autoFocus } /> }
@@ -385,3 +392,35 @@ test('manually focus previous component', async t => {
385
392
[ 'First' , 'Second' , 'Third ✔' ] . join ( '\n' )
386
393
) ;
387
394
} ) ;
395
+
396
+ test ( 'doesnt crash when focusing next on unmounted children' , async t => {
397
+ const stdout = createStdout ( ) ;
398
+ const stdin = createStdin ( ) ;
399
+ const { rerender} = render ( < Test autoFocus /> , {
400
+ stdout,
401
+ stdin,
402
+ debug : true
403
+ } ) ;
404
+
405
+ await delay ( 100 ) ;
406
+ rerender ( < Test focusNext unmountChildren /> ) ;
407
+ await delay ( 100 ) ;
408
+
409
+ t . is ( stdout . write . lastCall . args [ 0 ] , '' ) ;
410
+ } ) ;
411
+
412
+ test ( 'doesnt crash when focusing previous on unmounted children' , async t => {
413
+ const stdout = createStdout ( ) ;
414
+ const stdin = createStdin ( ) ;
415
+ const { rerender} = render ( < Test autoFocus /> , {
416
+ stdout,
417
+ stdin,
418
+ debug : true
419
+ } ) ;
420
+
421
+ await delay ( 100 ) ;
422
+ rerender ( < Test focusPrevious unmountChildren /> ) ;
423
+ await delay ( 100 ) ;
424
+
425
+ t . is ( stdout . write . lastCall . args [ 0 ] , '' ) ;
426
+ } ) ;
0 commit comments