@@ -307,7 +307,7 @@ pub fn TextInput(mut hooks: Hooks, props: &mut TextInputProps) -> impl Into<AnyE
307
307
}
308
308
309
309
// Update the offset if the cursor is out of bounds.
310
- {
310
+ if width > 0 && height > 0 {
311
311
if cursor_row >= scroll_offset_row. get ( ) + height {
312
312
scroll_offset_row. set ( cursor_row - height + 1 ) ;
313
313
} else if cursor_row < scroll_offset_row. get ( ) {
@@ -488,10 +488,15 @@ mod tests {
488
488
use macro_rules_attribute:: apply;
489
489
use smol_macros:: test;
490
490
491
+ #[ derive( Default , Props ) ]
492
+ struct MyComponentProps {
493
+ initial_value : String ,
494
+ }
495
+
491
496
#[ component]
492
- fn MyComponent ( mut hooks : Hooks ) -> impl Into < AnyElement < ' static > > {
497
+ fn MyComponent ( mut hooks : Hooks , props : & MyComponentProps ) -> impl Into < AnyElement < ' static > > {
493
498
let mut system = hooks. use_context_mut :: < SystemContext > ( ) ;
494
- let mut value = hooks. use_state ( || "" . to_string ( ) ) ;
499
+ let mut value = hooks. use_state ( || props . initial_value . clone ( ) ) ;
495
500
496
501
if value. read ( ) . contains ( "!" ) {
497
502
system. exit ( ) ;
@@ -546,7 +551,25 @@ mod tests {
546
551
. map ( |c| c. to_string ( ) )
547
552
. collect :: < Vec < _ > > ( )
548
553
. await ;
549
- let expected = vec ! [ "\n " , " \n " , " foo! \n " ] ;
554
+ let expected = vec ! [ " \n " , " foo! \n " ] ;
555
+ assert_eq ! ( actual, expected) ;
556
+ }
557
+
558
+ #[ apply( test!) ]
559
+ async fn test_text_input_initial_value ( ) {
560
+ let actual = element ! {
561
+ MyComponent ( initial_value: "foo" )
562
+ }
563
+ . mock_terminal_render_loop ( MockTerminalConfig :: with_events ( futures:: stream:: iter (
564
+ vec ! [
565
+ TerminalEvent :: Key ( KeyEvent :: new( KeyEventKind :: Press , KeyCode :: Char ( '!' ) ) ) ,
566
+ TerminalEvent :: Key ( KeyEvent :: new( KeyEventKind :: Release , KeyCode :: Char ( '!' ) ) ) ,
567
+ ] ,
568
+ ) ) )
569
+ . map ( |c| c. to_string ( ) )
570
+ . collect :: < Vec < _ > > ( )
571
+ . await ;
572
+ let expected = vec ! [ " foo \n " , " foo! \n " ] ;
550
573
assert_eq ! ( actual, expected) ;
551
574
}
552
575
@@ -576,7 +599,7 @@ mod tests {
576
599
. map ( |c| c. to_string ( ) )
577
600
. collect :: < Vec < _ > > ( )
578
601
. await ;
579
- let expected = vec ! [ "\n " , " \n ", " xxxxxxxx! \n " ] ;
602
+ let expected = vec ! [ " \n " , " xxxxxxxx! \n " ] ;
580
603
assert_eq ! ( actual, expected) ;
581
604
}
582
605
@@ -596,7 +619,7 @@ mod tests {
596
619
. map ( |c| c. to_string ( ) )
597
620
. collect :: < Vec < _ > > ( )
598
621
. await ;
599
- let expected = vec ! [ "\n " , " \n ", " 一二! \n " ] ;
622
+ let expected = vec ! [ " \n " , " 一二! \n " ] ;
600
623
assert_eq ! ( actual, expected) ;
601
624
}
602
625
@@ -620,7 +643,7 @@ mod tests {
620
643
. map ( |c| c. to_string ( ) )
621
644
. collect :: < Vec < _ > > ( )
622
645
. await ;
623
- let expected = vec ! [ "\n \n \n " , " \n \n \n ", " foo\n ! \n \n " ] ;
646
+ let expected = vec ! [ " \n \n \n " , " foo\n ! \n \n " ] ;
624
647
assert_eq ! ( actual, expected) ;
625
648
}
626
649
0 commit comments