diff --git a/examples/counter_without_macros/Cargo.toml b/examples/counter_without_macros/Cargo.toml new file mode 100644 index 000000000..1ef80f3ba --- /dev/null +++ b/examples/counter_without_macros/Cargo.toml @@ -0,0 +1,13 @@ +[package] +name = "counter_without_macros" +version = "0.1.0" +edition = "2021" + +[dependencies] +leptos = { path = "../../leptos", features = ["stable"] } +console_log = "0.2" +log = "0.4" +console_error_panic_hook = "0.1.7" + +[dev-dependencies] +wasm-bindgen-test = "0.3.0" diff --git a/examples/counter_without_macros/README.md b/examples/counter_without_macros/README.md new file mode 100644 index 000000000..c233421dd --- /dev/null +++ b/examples/counter_without_macros/README.md @@ -0,0 +1,5 @@ +# Leptos Counter Example + +This example is the same like the `counter` but it's written without using macros and can be build with stable Rust. + +To run it, just issue the `trunk serve --open` command in the example root. This will build the app, run it, and open a new browser to serve it. diff --git a/examples/counter_without_macros/index.html b/examples/counter_without_macros/index.html new file mode 100644 index 000000000..75fa1f12a --- /dev/null +++ b/examples/counter_without_macros/index.html @@ -0,0 +1,8 @@ + + +
+ + + + + \ No newline at end of file diff --git a/examples/counter_without_macros/public/favicon.ico b/examples/counter_without_macros/public/favicon.ico new file mode 100644 index 000000000..2ba8527cb Binary files /dev/null and b/examples/counter_without_macros/public/favicon.ico differ diff --git a/examples/counter_without_macros/src/lib.rs b/examples/counter_without_macros/src/lib.rs new file mode 100644 index 000000000..669731e3a --- /dev/null +++ b/examples/counter_without_macros/src/lib.rs @@ -0,0 +1,44 @@ +use leptos::{ev, *}; + +pub struct Props { + /// The starting value for the counter + pub initial_value: i32, + /// The change that should be applied each time the button is clicked. + pub step: i32, +} + +/// A simple counter view. +pub fn view(cx: Scope, props: Props) -> impl IntoView { + let Props { + initial_value, + step, + } = props; + let (value, set_value) = create_signal(cx, initial_value); + + div(cx) + .child(( + cx, + button(cx) + .on(ev::click, move |_| set_value.update(|value| *value = 0)) + .child((cx, "Clear")), + )) + .child(( + cx, + button(cx) + .on(ev::click, move |_| set_value.update(|value| *value -= step)) + .child((cx, "-1")), + )) + .child(( + cx, + span(cx) + .child((cx, "Value: ")) + .child((cx, move || value.get())) + .child((cx, "!")), + )) + .child(( + cx, + button(cx) + .on(ev::click, move |_| set_value.update(|value| *value += step)) + .child((cx, "+1")), + )) +} diff --git a/examples/counter_without_macros/src/main.rs b/examples/counter_without_macros/src/main.rs new file mode 100644 index 000000000..21f07ddb0 --- /dev/null +++ b/examples/counter_without_macros/src/main.rs @@ -0,0 +1,16 @@ +use counter_without_macros as counter; +use leptos::*; + +pub fn main() { + _ = console_log::init_with_level(log::Level::Debug); + console_error_panic_hook::set_once(); + mount_to_body(|cx| { + counter::view( + cx, + counter::Props { + initial_value: 0, + step: 1, + }, + ) + }) +} diff --git a/examples/counter_without_macros/tests/mod.rs b/examples/counter_without_macros/tests/mod.rs new file mode 100644 index 000000000..7d9512dad --- /dev/null +++ b/examples/counter_without_macros/tests/mod.rs @@ -0,0 +1,58 @@ +use wasm_bindgen_test::*; + +wasm_bindgen_test_configure!(run_in_browser); +use counter_without_macros as counter; +use leptos::*; +use web_sys::HtmlElement; + +#[wasm_bindgen_test] +fn inc() { + mount_to_body(|cx| { + counter::view( + cx, + counter::Props { + initial_value: 0, + step: 1, + }, + ) + }); + + let document = leptos::document(); + let div = document.query_selector("div").unwrap().unwrap(); + let clear = div + .first_child() + .unwrap() + .dyn_into::