Sfoglia il codice sorgente

fix: make tests pass

Jonathan Kelley 3 anni fa
parent
commit
75fa7b4

+ 92 - 0
examples/async.rs

@@ -0,0 +1,92 @@
+//! Render a bunch of doggos!
+//!
+
+use std::collections::HashMap;
+
+use dioxus::prelude::*;
+
+fn main() {
+    dioxus::desktop::launch(app);
+}
+
+#[derive(Debug, Clone, PartialEq, serde::Deserialize)]
+struct ListBreeds {
+    message: HashMap<String, Vec<String>>,
+}
+
+fn app(cx: Scope) -> Element {
+    let fut = use_future(&cx, || async move {
+        reqwest::get("https://dog.ceo/api/breeds/list/all")
+            .await
+            .unwrap()
+            .json::<ListBreeds>()
+            .await
+    });
+
+    let selected_breed = use_state(&cx, || None);
+
+    match fut.value() {
+        Some(Ok(breeds)) => cx.render(rsx! {
+            div {
+                h1 {"Select a dog breed!"}
+
+                div { display: "flex",
+                    ul { flex: "50%",
+                        breeds.message.keys().map(|breed| rsx!(
+                            li {
+                                button {
+                                    onclick: move |_| selected_breed.set(Some(breed.clone())),
+                                    "{breed}"
+                                }
+                            }
+                        ))
+                    }
+                    div { flex: "50%",
+                        match &*selected_breed {
+                            Some(breed) => rsx!( Breed { breed: breed.clone() } ),
+                            None => rsx!("No Breed selected"),
+                        }
+                    }
+                }
+            }
+        }),
+        Some(Err(e)) => cx.render(rsx! {
+            div { "Error fetching breeds" }
+        }),
+        None => cx.render(rsx! {
+            div { "Loading dogs..." }
+        }),
+    }
+}
+
+#[inline_props]
+fn Breed(cx: Scope, breed: String) -> Element {
+    #[derive(serde::Deserialize)]
+    struct DogApi {
+        message: String,
+    }
+
+    let endpoint = format!("https://dog.ceo/api/breed/{}/images/random", breed);
+
+    let fut = use_future(&cx, || async move {
+        reqwest::get(endpoint).await.unwrap().json::<DogApi>().await
+    });
+
+    cx.render(match fut.value() {
+        Some(Ok(resp)) => rsx! {
+            button {
+                onclick: move |_| fut.restart(),
+                "Click to fetch another doggo"
+            }
+            div {
+                img {
+                    max_width: "500px",
+                    max_height: "500px",
+                    src: "{resp.message}",
+                }
+            }
+        },
+        Some(Err(_)) => rsx! { div { "loading dogs failed" } },
+        None => rsx! { div { "loading dogs..." } },
+    })
+}

+ 1 - 1
packages/core/README.md

@@ -5,7 +5,7 @@ This is the core crate for the Dioxus Virtual DOM. This README will focus on the
 To build new apps with Dioxus or to extend the ecosystem with new hooks or components, use the higher-level `dioxus` crate with the appropriate feature flags.
 
 
-```rust
+```rust, ignore
 fn app(cx: Scope) -> Element {
     rsx!(cx, div { "hello world" })
 }

+ 3 - 3
packages/core/src/lib.rs

@@ -29,7 +29,7 @@ pub(crate) mod innerlude {
     ///
     /// ## Rust-Style
     ///
-    /// ```rust
+    /// ```rust, ignore
     /// fn example(cx: Scope<Props>) -> Element {
     ///     // ...
     /// }
@@ -39,7 +39,7 @@ pub(crate) mod innerlude {
     /// )
     /// ```
     /// ## React-Style
-    /// ```rust
+    /// ```rust, ignore
     /// fn Example(cx: Scope<Props>) -> Element {
     ///     // ...
     /// }
@@ -52,7 +52,7 @@ pub(crate) mod innerlude {
     /// ## As a closure
     /// This particular type alias lets you even use static closures for pure/static components:
     ///
-    /// ```rust
+    /// ```rust, ignore
     /// static Example: Component<Props> = |cx| {
     ///     // ...
     /// };

+ 9 - 9
packages/core/src/virtual_dom.rs

@@ -185,7 +185,7 @@ impl VirtualDom {
     /// This is useful when the VirtualDom must be driven from outside a thread and it doesn't make sense to wait for the
     /// VirtualDom to be created just to retrieve its channel receiver.
     ///
-    /// ```rust
+    /// ```rust, ignore
     /// let channel = futures_channel::mpsc::unbounded();
     /// let dom = VirtualDom::new_with_scheduler(Example, (), channel);
     /// ```
@@ -228,7 +228,7 @@ impl VirtualDom {
     ///
     /// # Example
     ///
-    /// ```rust
+    /// ```rust, ignore
     /// let mut dom = VirtualDom::new(example);
     /// dom.rebuild();
     ///
@@ -547,7 +547,7 @@ impl VirtualDom {
     ///
     /// Useful when needing to render nodes from outside the VirtualDom, such as in a test.
     ///
-    /// ```rust
+    /// ```rust, ignore
     /// fn Base(cx: Scope) -> Element {
     ///     rsx!(cx, div {})
     /// }
@@ -567,7 +567,7 @@ impl VirtualDom {
     ///
     /// Useful when needing to render nodes from outside the VirtualDom, such as in a test.
     ///
-    /// ```rust
+    /// ```rust, ignore
     /// fn Base(cx: Scope) -> Element {
     ///     rsx!(cx, div {})
     /// }
@@ -589,7 +589,7 @@ impl VirtualDom {
     /// Useful when needing to render nodes from outside the VirtualDom, such as in a test.
     ///
     ///
-    /// ```rust
+    /// ```rust, ignore
     /// fn Base(cx: Scope) -> Element {
     ///     rsx!(cx, div {})
     /// }
@@ -612,7 +612,7 @@ impl VirtualDom {
     /// Useful when needing to diff two rsx! calls from outside the VirtualDom, such as in a test.
     ///
     ///
-    /// ```rust
+    /// ```rust, ignore
     /// fn Base(cx: Scope) -> Element {
     ///     rsx!(cx, div {})
     /// }
@@ -756,7 +756,7 @@ pub enum SchedulerMsg {
 /// `Send` will not be lifted. The entire `UserEvent` must also be `Send + Sync` due to its use in the scheduler channel.
 ///
 /// # Example
-/// ```rust
+/// ```rust, ignore
 /// fn App(cx: Scope) -> Element {
 ///     rsx!(cx, div {
 ///         onclick: move |_| println!("Clicked!")
@@ -957,7 +957,7 @@ pub fn Fragment<'a>(cx: Scope<'a, FragmentProps<'a>>) -> Element {
 /// ## Example
 ///
 /// For props that are 'static:
-/// ```rust, ignore ignore
+/// ```rust, ignore
 /// #[derive(Props, PartialEq)]
 /// struct MyProps {
 ///     data: String
@@ -966,7 +966,7 @@ pub fn Fragment<'a>(cx: Scope<'a, FragmentProps<'a>>) -> Element {
 ///
 /// For props that borrow:
 ///
-/// ```rust, ignore ignore
+/// ```rust, ignore
 /// #[derive(Props)]
 /// struct MyProps<'a >{
 ///     data: &'a str

+ 3 - 4
packages/core/tests/miri_stress.rs

@@ -9,13 +9,12 @@ Specifically:
 - [ ] Async isn't busted
 */
 
-use dioxus::{prelude::*, DomEdit, SchedulerMsg, ScopeId};
+use dioxus::{prelude::*, SchedulerMsg, ScopeId};
 use dioxus_core as dioxus;
 use dioxus_core_macro::*;
 use dioxus_html as dioxus_elements;
 
 mod test_logging;
-use DomEdit::*;
 
 const IS_LOGGING_ENABLED: bool = false;
 
@@ -97,7 +96,7 @@ fn memo_works_properly() {
         let name = cx.use_hook(|_| String::from("asd"));
 
         cx.render(rsx!(
-            div { "Hello, world!" }
+            div { "Hello, world! {name}" }
             child(na: "asdfg".to_string())
         ))
     }
@@ -149,7 +148,7 @@ fn free_works_on_root_props() {
 
     impl Drop for Custom {
         fn drop(&mut self) {
-            dbg!("dropped!");
+            dbg!("dropped! {}", &self.val);
         }
     }
 

+ 2 - 2
packages/hooks/src/use_shared_state.rs

@@ -39,14 +39,14 @@ impl<T> ProvidedStateInner<T> {
 ///
 /// ## Provider
 ///
-/// ```rust
+/// ```rust, ignore
 ///
 ///
 /// ```
 ///
 /// ## Consumer
 ///
-/// ```rust
+/// ```rust, ignore
 ///
 ///
 /// ```

+ 3 - 3
packages/html/src/events.rs

@@ -122,7 +122,7 @@ pub mod on {
         /// The MouseEvent interface represents events that occur due to the user interacting with a pointing device (such as a mouse).
         ///
         /// ## Trait implementation:
-        /// ```rust
+        /// ```rust, ignore
         ///     fn alt_key(&self) -> bool;
         ///     fn button(&self) -> i16;
         ///     fn buttons(&self) -> u16;
@@ -416,7 +416,7 @@ pub mod on {
         ///
         /// # Example
         ///
-        /// ```rust
+        /// ```rust, ignore
         /// match event.key().as_str() {
         ///     "Esc" | "Escape" => {}
         ///     "ArrowDown" => {}
@@ -435,7 +435,7 @@ pub mod on {
         ///
         /// ## Example
         ///
-        /// ```rust
+        /// ```rust, ignore
         /// use dioxus::KeyCode;
         /// match event.key_code() {
         ///     KeyCode::Escape => {}

+ 1 - 1
packages/router/src/link.rs

@@ -13,7 +13,7 @@ pub struct LinkProps<'a, R: Routable> {
     ///
     /// You can either put it your own inline method or just autoderive the route using `derive(Routable)`
     ///
-    /// ```rust
+    /// ```rust, ignore
     ///
     /// Link { to: Route::Home, href: |_| "home".to_string() }
     ///

+ 2 - 2
packages/web/src/lib.rs

@@ -79,7 +79,7 @@ mod ric_raf;
 ///
 /// # Example
 ///
-/// ```rust
+/// ```rust, ignore
 /// fn main() {
 ///     dioxus_web::launch(App);
 /// }
@@ -98,7 +98,7 @@ pub fn launch(root_component: Component) {
 ///
 /// # Example
 ///
-/// ```rust
+/// ```rust, ignore
 /// fn main() {
 ///     dioxus_web::launch_with_props(App, RootProps { name: String::from("joe") });
 /// }