optional_props.rs 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  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. launch_desktop(app);
  8. }
  9. fn app() -> Element {
  10. rsx! {
  11. Button {
  12. a: "asd".to_string(),
  13. c: "asd".to_string(),
  14. d: Some("asd".to_string()),
  15. e: Some("asd".to_string()),
  16. }
  17. Button {
  18. a: "asd".to_string(),
  19. b: "asd".to_string(),
  20. c: "asd".to_string(),
  21. d: Some("asd".to_string()),
  22. e: "asd".to_string(),
  23. }
  24. Button {
  25. a: "asd".to_string(),
  26. c: "asd".to_string(),
  27. d: Some("asd".to_string()),
  28. }
  29. }
  30. }
  31. #[derive(Props, PartialEq, Clone)]
  32. struct ButtonProps {
  33. a: String,
  34. #[props(default)]
  35. b: String,
  36. c: Option<String>,
  37. #[props(!optional)]
  38. d: Option<String>,
  39. #[props(optional)]
  40. e: SthElse<String>,
  41. }
  42. type SthElse<T> = Option<T>;
  43. fn Button(props: ButtonProps) -> Element {
  44. rsx! {
  45. button {
  46. "{props.a} | "
  47. "{props.b:?} | "
  48. "{props.c:?} | "
  49. "{props.d:?} | "
  50. "{props.e:?}"
  51. }
  52. }
  53. }