Skip to content

Commit

Permalink
hide form when name exists
Browse files Browse the repository at this point in the history
  • Loading branch information
randallard committed Nov 28, 2024
1 parent c3f4df7 commit da65106
Showing 1 changed file with 22 additions and 15 deletions.
37 changes: 22 additions & 15 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,19 @@ use web_sys::MouseEvent;
fn App() -> impl IntoView {
let (name, set_name) = signal(String::new());
let (greeting, set_greeting) = signal(String::new());
let (show_form, set_show_form) = signal(true);

let handle_submit = move |_: MouseEvent| {
if !name.get().is_empty() {
set_greeting.set(format!("Hello, {}!", name.get()));
set_show_form.set(false);
}
};

let handle_keypress = move |ev: web_sys::KeyboardEvent| {
if ev.key() == "Enter" && !name.get().is_empty() {
set_greeting.set(format!("Hello, {}!", name.get()));
set_show_form.set(false);
}
};

Expand All @@ -28,21 +31,25 @@ fn App() -> impl IntoView {
greeting.get()
}}
</h1>
<input
type="text"
class="px-4 py-2 rounded bg-slate-800 border border-slate-700"
on:input=move |ev| {
set_name.set(event_target_value(&ev));
}
on:keypress=handle_keypress
prop:value=name
/>
<button
class="px-4 py-2 bg-blue-600 rounded hover:bg-blue-700"
on:click=handle_submit
>
"Hello"
</button>
{move || show_form.get().then(|| view! {
<>
<input
type="text"
class="px-4 py-2 rounded bg-slate-800 border border-slate-700"
on:input=move |ev| {
set_name.set(event_target_value(&ev));
}
on:keypress=handle_keypress
prop:value=name
/>
<button
class="px-4 py-2 bg-blue-600 rounded hover:bg-blue-700"
on:click=handle_submit
>
"Hello"
</button>
</>
})}
</div>
}
}
Expand Down

0 comments on commit da65106

Please sign in to comment.