From 933a9c7316a2a95519ea526bbd9a1a48e30e63f5 Mon Sep 17 00:00:00 2001 From: luoxiao Date: Sat, 10 Jun 2023 12:40:52 +0800 Subject: [PATCH] =?UTF-8?q?=E2=9C=A8=20feat:=20use=20if=20component?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- examples/basic/src/app.rs | 3 ++ examples/basic/src/demo_checkbox.rs | 20 ------- examples/basic/src/demo_slider.rs | 13 ----- examples/basic/src/main.rs | 2 - examples/basic/src/pages/checkbox/mod.rs | 14 +++++ examples/basic/src/pages/components.rs | 1 + examples/basic/src/pages/home.rs | 6 --- examples/basic/src/pages/mod.rs | 2 + src/card/mod.rs | 66 ++++++++++++++++-------- src/checkbox/mod.rs | 34 +++++------- src/components/if_comp.rs | 2 +- src/components/mod.rs | 2 +- src/modal/mod.rs | 51 +++++++++--------- 13 files changed, 105 insertions(+), 111 deletions(-) delete mode 100644 examples/basic/src/demo_checkbox.rs delete mode 100644 examples/basic/src/demo_slider.rs create mode 100644 examples/basic/src/pages/checkbox/mod.rs diff --git a/examples/basic/src/app.rs b/examples/basic/src/app.rs index 4ffb903..ce29b04 100644 --- a/examples/basic/src/app.rs +++ b/examples/basic/src/app.rs @@ -43,6 +43,9 @@ pub fn App(cx: Scope) -> impl IntoView { } /> + + } /> diff --git a/examples/basic/src/demo_checkbox.rs b/examples/basic/src/demo_checkbox.rs deleted file mode 100644 index 02aa3e3..0000000 --- a/examples/basic/src/demo_checkbox.rs +++ /dev/null @@ -1,20 +0,0 @@ -use leptos::*; -use melt_ui::*; - -#[component] -pub fn DemoCheckout(cx: Scope) -> impl IntoView { - let (checked, set_checked) = create_signal(cx, false); - view! {cx, -
- - "A" - - - "B" - - - "Click" - -
- } -} \ No newline at end of file diff --git a/examples/basic/src/demo_slider.rs b/examples/basic/src/demo_slider.rs deleted file mode 100644 index 77e3730..0000000 --- a/examples/basic/src/demo_slider.rs +++ /dev/null @@ -1,13 +0,0 @@ -use leptos::*; -use melt_ui::*; - -#[component] -pub fn DemoSlider(cx: Scope) -> impl IntoView { - let (value, set_value) = create_signal(cx, 0.0); - let on_value = SignalSetter::map(cx, move |value| { - set_value.set(value); - }); - view! { cx, - - } -} diff --git a/examples/basic/src/main.rs b/examples/basic/src/main.rs index b0082f8..38ab1fd 100644 --- a/examples/basic/src/main.rs +++ b/examples/basic/src/main.rs @@ -1,6 +1,4 @@ mod app; -mod demo_checkbox; -mod demo_slider; mod pages; use app::*; diff --git a/examples/basic/src/pages/checkbox/mod.rs b/examples/basic/src/pages/checkbox/mod.rs new file mode 100644 index 0000000..7d63d87 --- /dev/null +++ b/examples/basic/src/pages/checkbox/mod.rs @@ -0,0 +1,14 @@ +use leptos::*; +use melt_ui::*; + +#[component] +pub fn CheckboxPage(cx: Scope) -> impl IntoView { + let checked = create_rw_signal(cx, false); + view! {cx, +
+ + "Click" + +
+ } +} diff --git a/examples/basic/src/pages/components.rs b/examples/basic/src/pages/components.rs index 25b161f..4e7db06 100644 --- a/examples/basic/src/pages/components.rs +++ b/examples/basic/src/pages/components.rs @@ -41,6 +41,7 @@ pub fn ComponentsPage(cx: Scope) -> impl IntoView { +
diff --git a/examples/basic/src/pages/home.rs b/examples/basic/src/pages/home.rs index adb3aa4..8d41b0c 100644 --- a/examples/basic/src/pages/home.rs +++ b/examples/basic/src/pages/home.rs @@ -1,5 +1,3 @@ -use crate::demo_checkbox::*; -use crate::demo_slider::*; use leptos::*; use leptos_router::use_navigate; use melt_ui::*; @@ -38,9 +36,5 @@ pub fn Home(cx: Scope) -> impl IntoView { -
- -
- } } diff --git a/examples/basic/src/pages/mod.rs b/examples/basic/src/pages/mod.rs index 032ea51..54f95c8 100644 --- a/examples/basic/src/pages/mod.rs +++ b/examples/basic/src/pages/mod.rs @@ -1,4 +1,5 @@ mod button; +mod checkbox; mod components; mod home; mod image; @@ -11,6 +12,7 @@ mod slider; mod tabbar; pub use button::*; +pub use checkbox::*; pub use components::*; pub use home::*; pub use image::*; diff --git a/src/card/mod.rs b/src/card/mod.rs index 7c90f84..6f03842 100644 --- a/src/card/mod.rs +++ b/src/card/mod.rs @@ -1,51 +1,75 @@ +use std::rc::Rc; + use crate::{components::*, utils::mount_style::mount_style}; use leptos::*; use stylers::style_sheet_str; +#[slot] +pub struct CardHeader { + children: ChildrenFn, +} + +#[slot] +pub struct CardHeaderExtra { + children: ChildrenFn, +} + +#[slot] +pub struct CardFooter { + #[prop(default = leptos::MaybeSignal::Static(true), into)] + if_: MaybeSignal, + children: ChildrenFn, +} + #[component] pub fn Card( cx: Scope, - #[prop(optional)] title: MaybeSignal, - #[prop(default = None)] header: Option, - #[prop(default = None)] header_extra: Option, + #[prop(optional, into)] title: MaybeSignal<&'static str>, + #[prop(optional)] card_header: Option, + #[prop(optional)] card_header_extra: Option, children: Children, - #[prop(default = None)] footer: Option, + #[prop(optional)] card_footer: Option, ) -> impl IntoView { let class_name = mount_style("card", || style_sheet_str!("./src/card/card.css")); + + let is_header = card_header.is_some(); + let header = card_header.map_or(None, |v| Some(Rc::new(v))); + let header_extra = card_header_extra.map_or(None, |v| Some(Rc::new(v))); + // let footer = card_footer.map_or(None, |v| Some(Rc::new(v))); + view! { cx, class=class_name,
- { - if header.is_some() || title.get().is_empty() { - view! { - cx, class=class_name, + +
- + { title.get() } - { header(cx) } + { (header.children)(cx) }
- +
- { header_extra(cx) } + { (header_extra.children)(cx) }
- }.into() - } else { - None - } - } +
+
{ children(cx) }
- - + + + + + +
} diff --git a/src/checkbox/mod.rs b/src/checkbox/mod.rs index 27fa241..c43ad87 100644 --- a/src/checkbox/mod.rs +++ b/src/checkbox/mod.rs @@ -1,13 +1,12 @@ -use crate::{theme::use_theme, utils::mount_style::mount_style, Theme}; +use crate::{theme::use_theme, utils::mount_style::mount_style, Theme, components::*}; use leptos::*; -use stylers::style_sheet_str; use leptos_icons::*; +use stylers::style_sheet_str; #[component] pub fn Checkbox( cx: Scope, - #[prop(optional, into)] checked: MaybeSignal, - #[prop(optional, into)] on_checked: Option>, + #[prop(into)] checked: RwSignal, children: Children, ) -> impl IntoView { let theme = use_theme(cx, Theme::light); @@ -22,29 +21,20 @@ pub fn Checkbox( css_vars.push_str(&format!("--background-color-checked: {bg_color};")); css_vars }); - let on_click = move |_| { - if let Some(on_checked) = on_checked { - on_checked.set(!checked.get()); - } - }; + view! {cx, class=class_name, -
+
- { - move || { - if checked.get() { - view! {cx, - - }.into() - } else { - None - } - } - } + + + + +
- {children(cx)} + { children(cx) }
} diff --git a/src/components/if_comp.rs b/src/components/if_comp.rs index 94d8c78..ff6d007 100644 --- a/src/components/if_comp.rs +++ b/src/components/if_comp.rs @@ -15,7 +15,7 @@ pub struct ElseIf { #[component] pub fn If( cx: Scope, - cond: MaybeSignal, + #[prop(into)] cond: MaybeSignal, then: Then, #[prop(default=vec![])] else_if: Vec, #[prop(optional)] fallback: Option, diff --git a/src/components/mod.rs b/src/components/mod.rs index 4f92fcf..64ae4a0 100644 --- a/src/components/mod.rs +++ b/src/components/mod.rs @@ -2,8 +2,8 @@ mod if_comp; mod option_comp; pub use if_comp::*; -use leptos::*; pub use option_comp::*; +use leptos::*; #[slot] pub struct Fallback { diff --git a/src/modal/mod.rs b/src/modal/mod.rs index b3917c5..a60c106 100644 --- a/src/modal/mod.rs +++ b/src/modal/mod.rs @@ -1,48 +1,49 @@ use crate::card::*; +use crate::components::OptionComp; use crate::teleport::*; use crate::utils::mount_style::mount_style; use leptos::*; use stylers::style_sheet_str; use leptos_icons::*; +#[slot] +pub struct ModalFooter { + children: ChildrenFn, +} + #[component] pub fn Modal( cx: Scope, - #[prop(optional, into)] title: Option>, - children: Children, - #[prop(optional)] footer: Option, #[prop(into)] show: RwSignal, + #[prop(optional, into)] title: MaybeSignal<&'static str>, + children: Children, + #[prop(optional)] modal_footer: Option, ) -> impl IntoView { let class_name = mount_style("modal", || style_sheet_str!("./src/modal/modal.css")); - let header = move |cx| { - view! { - cx, class=class_name, - <> - - {title} - - - } - }; - let header_extra = move |cx| { - view! { - cx, - <> - - - - - } - }; view! { cx, class=class_name,
- - {children(cx)} + + + + { title.get() } + + + + + + + + { children(cx) } + + + { (footer.children)(cx) } + +