From b0038bb24bb497562f1a16eb05fe806e7f58292d Mon Sep 17 00:00:00 2001 From: Jonathan Kelley Date: Fri, 19 Jan 2024 15:59:51 -0800 Subject: [PATCH] Tweak crm example --- examples/crm.rs | 69 +++++++++++++++++++++++-------------------------- 1 file changed, 32 insertions(+), 37 deletions(-) diff --git a/examples/crm.rs b/examples/crm.rs index e64599d51..2e9d4f6b1 100644 --- a/examples/crm.rs +++ b/examples/crm.rs @@ -1,35 +1,40 @@ //! Tiny CRM: A port of the Yew CRM example to Dioxus. use dioxus::prelude::*; +use dioxus_desktop::{LogicalSize, WindowBuilder}; use dioxus_router::prelude::*; fn main() { - launch(app); + LaunchBuilder::new() + .with_cfg(desktop!( + dioxus_desktop::Config::default() + .with_window(WindowBuilder::new().with_inner_size(LogicalSize::new(800, 600))) + )) + .launch(|| { + rsx! { + link { + rel: "stylesheet", + href: "https://unpkg.com/purecss@2.0.6/build/pure-min.css", + integrity: "sha384-Uu6IeWbM+gzNVXJcM9XV3SohHtmWE+3VGi496jvgX1jyvDTXfdK+rfZc8C1Aehk5", + crossorigin: "anonymous" + } + style { {r#" .red { background-color: rgb(202, 60, 60) !important; } "#} } + body { + padding: "20px", + h1 { "Dioxus CRM Example" } + Router:: {} + } + } + }); } /// We only have one list of clients for the whole app, so we can use a global signal. static CLIENTS: GlobalSignal> = Signal::global(|| Vec::new()); -fn app() -> Element { - rsx! { - link { - rel: "stylesheet", - href: "https://unpkg.com/purecss@2.0.6/build/pure-min.css", - integrity: "sha384-Uu6IeWbM+gzNVXJcM9XV3SohHtmWE+3VGi496jvgX1jyvDTXfdK+rfZc8C1Aehk5", - crossorigin: "anonymous" - } - - style { - " - .red {{ - background-color: rgb(202, 60, 60) !important; - }} - " - } - - h1 { "Dioxus CRM Example" } - - Router:: {} - } +#[derive(Clone, Debug, Default)] +pub struct Client { + pub first_name: String, + pub last_name: String, + pub description: String, } #[derive(Routable, Clone)] @@ -44,24 +49,13 @@ enum Route { Settings {}, } -#[derive(Clone, Debug, Default)] -pub struct Client { - pub first_name: String, - pub last_name: String, - pub description: String, -} - #[component] fn ClientList() -> Element { - let clients = use_hook(|| CLIENTS.signal()); - - println!("Clients: {:?}", clients.read()); - rsx! { h2 { "List of Clients" } Link { to: Route::ClientAdd {}, class: "pure-button pure-button-primary", "Add Client" } Link { to: Route::Settings {}, class: "pure-button", "Settings" } - for client in clients.read().iter() { + for client in CLIENTS.read().iter() { div { class: "client", style: "margin-bottom: 50px", p { "Name: {client.first_name} {client.last_name}" } p { "Description: {client.description}" } @@ -84,8 +78,6 @@ fn ClientAdd() -> Element { description: description(), }); - println!("Added client: {:?}", CLIENTS.read()); - // And then navigate back to the client list dioxus_router::router().push(Route::ClientList {}); }; @@ -102,7 +94,10 @@ fn ClientAdd() -> Element { placeholder: "First Name…", required: "", value: "{first_name}", - oninput: move |e| first_name.set(e.value()) + oninput: move |e| first_name.set(e.value()), + + // when the form mounts, focus the first name input + onmounted: move |e| {e.inner().set_focus(true);}, } }