2024-02-14 20:33:07 +00:00
|
|
|
//! Optional props
|
2022-01-10 07:57:03 +00:00
|
|
|
//!
|
2024-02-14 20:33:07 +00:00
|
|
|
//! This example demonstrates how to use optional props in your components. The `Button` component has several props,
|
|
|
|
//! and we use a variety of attributes to set them.
|
2022-01-10 07:57:03 +00:00
|
|
|
|
|
|
|
use dioxus::prelude::*;
|
|
|
|
|
|
|
|
fn main() {
|
2024-01-20 08:11:55 +00:00
|
|
|
launch(app);
|
2022-01-10 07:57:03 +00:00
|
|
|
}
|
|
|
|
|
2024-01-14 04:51:37 +00:00
|
|
|
fn app() -> Element {
|
2024-01-16 19:18:46 +00:00
|
|
|
rsx! {
|
2024-02-14 20:33:07 +00:00
|
|
|
// We can set some of the props, and the rest will be filled with their default values
|
|
|
|
// By default `c` can take a `None` value, but `d` is required to wrap a `Some` value
|
2023-11-29 17:38:28 +00:00
|
|
|
Button {
|
|
|
|
a: "asd".to_string(),
|
2024-02-14 20:33:07 +00:00
|
|
|
// b can be omitted, and it will be filled with its default value
|
2023-11-29 17:38:28 +00:00
|
|
|
c: "asd".to_string(),
|
|
|
|
d: Some("asd".to_string()),
|
|
|
|
e: Some("asd".to_string()),
|
|
|
|
}
|
2024-02-14 20:33:07 +00:00
|
|
|
|
2022-01-10 07:57:03 +00:00
|
|
|
Button {
|
|
|
|
a: "asd".to_string(),
|
2023-11-29 17:39:38 +00:00
|
|
|
b: "asd".to_string(),
|
2024-02-14 20:33:07 +00:00
|
|
|
|
|
|
|
// We can omit the `Some` on `c` since Dioxus automatically transforms Option<T> into optional
|
2022-03-16 06:36:39 +00:00
|
|
|
c: "asd".to_string(),
|
|
|
|
d: Some("asd".to_string()),
|
2022-01-16 20:27:41 +00:00
|
|
|
e: "asd".to_string(),
|
2022-01-10 07:57:03 +00:00
|
|
|
}
|
2024-02-14 20:33:07 +00:00
|
|
|
|
2024-07-24 00:49:33 +00:00
|
|
|
// `b` and `e` are omitted
|
2023-11-29 17:39:38 +00:00
|
|
|
Button {
|
|
|
|
a: "asd".to_string(),
|
|
|
|
c: "asd".to_string(),
|
|
|
|
d: Some("asd".to_string()),
|
|
|
|
}
|
2024-01-14 05:12:21 +00:00
|
|
|
}
|
2022-01-10 07:57:03 +00:00
|
|
|
}
|
|
|
|
|
2024-01-16 01:04:39 +00:00
|
|
|
#[derive(Props, PartialEq, Clone)]
|
2022-01-10 07:57:03 +00:00
|
|
|
struct ButtonProps {
|
|
|
|
a: String,
|
|
|
|
|
|
|
|
#[props(default)]
|
2022-03-16 06:36:39 +00:00
|
|
|
b: String,
|
2022-01-10 07:57:03 +00:00
|
|
|
|
|
|
|
c: Option<String>,
|
|
|
|
|
2022-03-16 06:36:39 +00:00
|
|
|
#[props(!optional)]
|
2022-01-10 07:57:03 +00:00
|
|
|
d: Option<String>,
|
2022-01-16 20:27:41 +00:00
|
|
|
|
|
|
|
#[props(optional)]
|
2022-03-16 06:36:39 +00:00
|
|
|
e: SthElse<String>,
|
2022-01-10 07:57:03 +00:00
|
|
|
}
|
|
|
|
|
2024-01-18 11:03:17 +00:00
|
|
|
type SthElse<T> = Option<T>;
|
|
|
|
|
2024-02-14 21:50:10 +00:00
|
|
|
#[allow(non_snake_case)]
|
2024-01-16 01:04:39 +00:00
|
|
|
fn Button(props: ButtonProps) -> Element {
|
2024-01-16 19:18:46 +00:00
|
|
|
rsx! {
|
2022-01-16 20:27:41 +00:00
|
|
|
button {
|
2024-01-16 01:04:39 +00:00
|
|
|
"{props.a} | "
|
|
|
|
"{props.b:?} | "
|
|
|
|
"{props.c:?} | "
|
|
|
|
"{props.d:?} | "
|
|
|
|
"{props.e:?}"
|
2022-01-16 20:27:41 +00:00
|
|
|
}
|
2024-01-14 05:12:21 +00:00
|
|
|
}
|
2022-01-10 07:57:03 +00:00
|
|
|
}
|