syntax.rs 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. use std::marker::PhantomData;
  2. use dioxus::component::Scope;
  3. use dioxus::events::on::MouseEvent;
  4. use dioxus::nodes::{IntoVNode, IntoVNodeList};
  5. use dioxus_core as dioxus;
  6. use dioxus_core::prelude::*;
  7. use dioxus_core_macro::*;
  8. use dioxus_html as dioxus_elements;
  9. fn main() {}
  10. fn html_usage() {
  11. let mo = move |_| {};
  12. let r = rsx! {
  13. div {
  14. onclick: move |_| {}
  15. onmouseover: {mo}
  16. "type": "bar",
  17. "hello world"
  18. }
  19. };
  20. let items = ["bob", "bill", "jack"];
  21. let f = items
  22. .iter()
  23. .filter(|f| f.starts_with('b'))
  24. .map(|f| rsx!("hello {f}"));
  25. let p = rsx!(div { {f} });
  26. }
  27. static App2: FC<()> = |(cx, _)| cx.render(rsx!("hello world!"));
  28. static App: FC<()> = |(cx, props)| {
  29. let name = cx.use_state(|| 0);
  30. cx.render(rsx!(div {
  31. h1 {}
  32. h2 {}
  33. }))
  34. };
  35. pub trait UseState<'a, T: 'static> {
  36. fn use_state(self, f: impl FnOnce() -> T) -> &'a T;
  37. }
  38. impl<'a, T: 'static> UseState<'a, T> for Context<'a> {
  39. fn use_state(self, f: impl FnOnce() -> T) -> &'a T {
  40. todo!()
  41. }
  42. }
  43. fn App3((cx, props): Scope<()>) -> Element {
  44. let p = rsx! {
  45. Child {
  46. bame: 10,
  47. }
  48. };
  49. cx.render(rsx!(Child {
  50. bame: 102,
  51. ..ChildProps { bame: 10 }
  52. }))
  53. }
  54. #[derive(Props, PartialEq, Debug)]
  55. struct ChildProps {
  56. bame: i32, // children: Children<'a>,
  57. }
  58. fn Child<'a>((cx, props): Scope<'a, ChildProps>) -> Element<'a> {
  59. cx.render(rsx!(div {
  60. // {props.children}
  61. }))
  62. }
  63. // Some(LazyNodes::new(|f| {
  64. // //
  65. // // let r = f.fragment_from_iter(&props.children);
  66. // r
  67. // // todo!()
  68. // }))
  69. // todo!()
  70. // rsx!({ Some(p) })
  71. // todo!()
  72. pub struct Children<'a> {
  73. children: VNode<'static>,
  74. _p: PhantomData<&'a ()>,
  75. }
  76. impl<'a> Children<'a> {
  77. pub fn new(children: VNode<'a>) -> Self {
  78. Self {
  79. children: unsafe { std::mem::transmute(children) },
  80. _p: PhantomData,
  81. }
  82. }
  83. }
  84. impl<'a> IntoVNodeList<'a> for &Children<'a> {
  85. fn into_vnode_list(self, cx: NodeFactory<'a>) -> &'a [VNode<'a>] {
  86. todo!()
  87. }
  88. }
  89. static Bapp: FC<()> = |(cx, props)| {
  90. let name = cx.use_state(|| 0);
  91. cx.render(rsx!(
  92. div {
  93. div {
  94. }
  95. div {
  96. }
  97. }
  98. ))
  99. };