1
0

optional_props.rs 918 B

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. #![allow(non_snake_case)]
  2. //! Example: README.md showcase
  3. //!
  4. //! The example from the README.md.
  5. use dioxus::prelude::*;
  6. fn main() {
  7. dioxus_desktop::launch(app);
  8. }
  9. fn app(cx: Scope) -> Element {
  10. cx.render(rsx! {
  11. Button {
  12. a: "asd".to_string(),
  13. c: "asd".to_string(),
  14. d: Some("asd".to_string()),
  15. e: "asd".to_string(),
  16. }
  17. })
  18. }
  19. type SthElse<T> = Option<T>;
  20. #[derive(Props, PartialEq)]
  21. struct ButtonProps {
  22. a: String,
  23. #[props(default)]
  24. b: String,
  25. c: Option<String>,
  26. #[props(!optional)]
  27. d: Option<String>,
  28. #[props(optional)]
  29. e: SthElse<String>,
  30. }
  31. fn Button(cx: Scope<ButtonProps>) -> Element {
  32. cx.render(rsx! {
  33. button {
  34. "{cx.props.a} | "
  35. "{cx.props.b:?} | "
  36. "{cx.props.c:?} | "
  37. "{cx.props.d:?} | "
  38. "{cx.props.e:?}"
  39. }
  40. })
  41. }