Skip to content

Commit 64f3726

Browse files
authored
fix: TextInput initial value scroll offset (#105)
* fix: TextInput initial value scroll offset * update other tests
1 parent e8911cb commit 64f3726

File tree

1 file changed

+30
-7
lines changed

1 file changed

+30
-7
lines changed

packages/iocraft/src/components/text_input.rs

Lines changed: 30 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -307,7 +307,7 @@ pub fn TextInput(mut hooks: Hooks, props: &mut TextInputProps) -> impl Into<AnyE
307307
}
308308

309309
// Update the offset if the cursor is out of bounds.
310-
{
310+
if width > 0 && height > 0 {
311311
if cursor_row >= scroll_offset_row.get() + height {
312312
scroll_offset_row.set(cursor_row - height + 1);
313313
} else if cursor_row < scroll_offset_row.get() {
@@ -488,10 +488,15 @@ mod tests {
488488
use macro_rules_attribute::apply;
489489
use smol_macros::test;
490490

491+
#[derive(Default, Props)]
492+
struct MyComponentProps {
493+
initial_value: String,
494+
}
495+
491496
#[component]
492-
fn MyComponent(mut hooks: Hooks) -> impl Into<AnyElement<'static>> {
497+
fn MyComponent(mut hooks: Hooks, props: &MyComponentProps) -> impl Into<AnyElement<'static>> {
493498
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());
495500

496501
if value.read().contains("!") {
497502
system.exit();
@@ -546,7 +551,25 @@ mod tests {
546551
.map(|c| c.to_string())
547552
.collect::<Vec<_>>()
548553
.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"];
550573
assert_eq!(actual, expected);
551574
}
552575

@@ -576,7 +599,7 @@ mod tests {
576599
.map(|c| c.to_string())
577600
.collect::<Vec<_>>()
578601
.await;
579-
let expected = vec!["\n", " \n", " xxxxxxxx! \n"];
602+
let expected = vec![" \n", " xxxxxxxx! \n"];
580603
assert_eq!(actual, expected);
581604
}
582605

@@ -596,7 +619,7 @@ mod tests {
596619
.map(|c| c.to_string())
597620
.collect::<Vec<_>>()
598621
.await;
599-
let expected = vec!["\n", " \n", " 一二! \n"];
622+
let expected = vec![" \n", " 一二! \n"];
600623
assert_eq!(actual, expected);
601624
}
602625

@@ -620,7 +643,7 @@ mod tests {
620643
.map(|c| c.to_string())
621644
.collect::<Vec<_>>()
622645
.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"];
624647
assert_eq!(actual, expected);
625648
}
626649

0 commit comments

Comments
 (0)