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. dioxus_desktop::launch(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. type SthElse<T> = Option<T>;
  32. #[derive(Props, PartialEq, Clone)]
  33. struct ButtonProps {
  34. a: String,
  35. #[props(default)]
  36. b: String,
  37. c: Option<String>,
  38. #[props(!optional)]
  39. d: Option<String>,
  40. #[props(optional)]
  41. e: SthElse<String>,
  42. }
  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. }