2022-07-07 08:50:36 +00:00
|
|
|
#![allow(non_snake_case)]
|
|
|
|
use dioxus::prelude::*;
|
|
|
|
|
|
|
|
fn main() {
|
2022-07-09 19:15:20 +00:00
|
|
|
dioxus_desktop::launch(App);
|
2022-07-07 08:50:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
fn App(cx: Scope) -> Element {
|
|
|
|
cx.render(rsx! {
|
|
|
|
// ANCHOR: OptionalProps_usage
|
|
|
|
Title {
|
|
|
|
title: "Some Title",
|
|
|
|
},
|
|
|
|
Title {
|
|
|
|
title: "Some Title",
|
|
|
|
subtitle: "Some Subtitle",
|
|
|
|
},
|
|
|
|
// Providing an Option explicitly won't compile though:
|
|
|
|
// Title {
|
|
|
|
// title: "Some Title",
|
|
|
|
// subtitle: None,
|
|
|
|
// },
|
|
|
|
// ANCHOR_END: OptionalProps_usage
|
|
|
|
|
|
|
|
// ANCHOR: ExplicitOption_usage
|
|
|
|
ExplicitOption {
|
|
|
|
title: "Some Title",
|
|
|
|
subtitle: None,
|
|
|
|
},
|
|
|
|
ExplicitOption {
|
|
|
|
title: "Some Title",
|
|
|
|
subtitle: Some("Some Title"),
|
|
|
|
},
|
|
|
|
// This won't compile:
|
|
|
|
// ExplicitOption {
|
|
|
|
// title: "Some Title",
|
|
|
|
// },
|
|
|
|
// ANCHOR_END: ExplicitOption_usage
|
|
|
|
|
|
|
|
// ANCHOR: DefaultComponent_usage
|
|
|
|
DefaultComponent {
|
|
|
|
number: 5,
|
|
|
|
},
|
|
|
|
DefaultComponent {},
|
|
|
|
// ANCHOR_END: DefaultComponent_usage
|
|
|
|
|
|
|
|
// ANCHOR: IntoComponent_usage
|
|
|
|
IntoComponent {
|
|
|
|
string: "some &str",
|
|
|
|
},
|
|
|
|
// ANCHOR_END: IntoComponent_usage
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
// ANCHOR: OptionalProps
|
|
|
|
#[derive(Props)]
|
|
|
|
struct OptionalProps<'a> {
|
|
|
|
title: &'a str,
|
|
|
|
subtitle: Option<&'a str>,
|
|
|
|
}
|
|
|
|
|
|
|
|
fn Title<'a>(cx: Scope<'a, OptionalProps>) -> Element<'a> {
|
2022-09-13 23:22:27 +00:00
|
|
|
cx.render(rsx!(h1{
|
2022-07-07 08:50:36 +00:00
|
|
|
"{cx.props.title}: ",
|
2022-09-13 23:22:27 +00:00
|
|
|
cx.props.subtitle.unwrap_or("No subtitle provided"),
|
|
|
|
}))
|
2022-07-07 08:50:36 +00:00
|
|
|
}
|
|
|
|
// ANCHOR_END: OptionalProps
|
|
|
|
|
|
|
|
// ANCHOR: ExplicitOption
|
|
|
|
#[derive(Props)]
|
|
|
|
struct ExplicitOptionProps<'a> {
|
|
|
|
title: &'a str,
|
|
|
|
#[props(!optional)]
|
|
|
|
subtitle: Option<&'a str>,
|
|
|
|
}
|
|
|
|
|
|
|
|
fn ExplicitOption<'a>(cx: Scope<'a, ExplicitOptionProps>) -> Element<'a> {
|
2022-09-13 23:22:27 +00:00
|
|
|
cx.render(rsx!(h1 {
|
2022-07-07 08:50:36 +00:00
|
|
|
"{cx.props.title}: ",
|
2022-09-13 23:22:27 +00:00
|
|
|
cx.props.subtitle.unwrap_or("No subtitle provided"),
|
|
|
|
}))
|
2022-07-07 08:50:36 +00:00
|
|
|
}
|
|
|
|
// ANCHOR_END: ExplicitOption
|
|
|
|
|
|
|
|
// ANCHOR: DefaultComponent
|
|
|
|
#[derive(PartialEq, Props)]
|
|
|
|
struct DefaultProps {
|
|
|
|
// default to 42 when not provided
|
|
|
|
#[props(default = 42)]
|
|
|
|
number: i64,
|
|
|
|
}
|
|
|
|
|
|
|
|
fn DefaultComponent(cx: Scope<DefaultProps>) -> Element {
|
2022-09-13 23:22:27 +00:00
|
|
|
cx.render(rsx!(h1 { "{cx.props.number}" }))
|
2022-07-07 08:50:36 +00:00
|
|
|
}
|
|
|
|
// ANCHOR_END: DefaultComponent
|
|
|
|
|
|
|
|
// ANCHOR: IntoComponent
|
|
|
|
#[derive(PartialEq, Props)]
|
|
|
|
struct IntoProps {
|
|
|
|
#[props(into)]
|
|
|
|
string: String,
|
|
|
|
}
|
|
|
|
|
|
|
|
fn IntoComponent(cx: Scope<IntoProps>) -> Element {
|
2022-09-13 23:22:27 +00:00
|
|
|
cx.render(rsx!(h1 { "{cx.props.string}" }))
|
2022-07-07 08:50:36 +00:00
|
|
|
}
|
|
|
|
// ANCHOR_END: IntoComponent
|