Commit 10d33e7
Move compoent code to macro and test
Changed files (3)
Diff
@@ -13,7 +13,7 @@ manually when you want to render a component and use `inner_html("#the-id", content)` to update the DOM. ## Dependencies-```+```bash cargo add kaja-web cargo add kaja-html-macro cargo add kaja-callback-macro@@ -25,7 +25,7 @@ cargo add js-sys ``` ## Example:-```rust+```rust,ignore use kaja_web::prelude::*; use std::sync::RwLock; @@ -48,7 +48,7 @@ fn update_counter_display(app_data: &AppData) { <button onclick="incrementClickCounter()">Click me</button> }}; - try_inner_html(".main-content", content);+ try_inner_html(".main-content", &content); } fn update_count(app_data: &AppData) {@@ -56,7 +56,7 @@ fn update_count(app_data: &AppData) { $(app_data.current_count) }}; - try_inner_html(".click-count", count);+ try_inner_html(".click-count", &count); } fn main() {@@ -92,7 +92,7 @@ fn update_component() { <button onclick="foo({ test_parameter: 'test' })">Click me</button> }}; - inner_html(".main-content", content);+ inner_html(".main-content", &content); } #[callback(foo)]@@ -100,7 +100,7 @@ pub fn try_handle_component_update_message(event: &web_sys::MessageEvent) -> boo /// Register callbacks that can be used in JS to run Rust functions in the WASM /// module. This needs to run when the WASM bundle is loaded. ///-/// ```rust+/// ```rust,ignore /// #[wasm_bindgen(start)] /// pub fn init() { /// init_callbacks();@@ -112,13 +112,13 @@ pub fn try_handle_component_update_message(event: &web_sys::MessageEvent) -> boo /// `testCallback`. Note that camel case is used in JS and regular snake case /// is used in Rust for the same function. ///-/// ```rust+/// ```rust,ignore /// #[callback(testCallback)] /// pub fn test_callback() { /// } /// ``` ///-/// ```rust+/// ```rust,ignore /// let html = html!{{ /// <button onclick="testCallback()">Test</button> /// }};@@ -245,7 +245,7 @@ pub fn inner_text(selector: &str, text: &str) -> Result<()> { /// <input type="text" id="test" value="test value" /> /// ``` ///-/// ```rust+/// ```rust,ignore /// let value = get_value("#test"); /// assert_eq!(value, Some("test value")); /// ```@@ -339,7 +339,7 @@ pub fn set_session_cookie_value(name: &str, value: &str) -> Result<()> { /// to HTML elements without generating an HTML string. /// /// Example:-/// ```rust+/// ```rust,ignore /// use web_sys::HtmlImageElement; /// use gloo_console::log; ///@@ -1,6 +1,8 @@ #[path = "common.rs"] mod common; +use std::borrow;+ use kaja_web::prelude::*; use kaja_web_component::component; use wasm_bindgen_test::*;@@ -9,19 +11,40 @@ use wasm_bindgen_test::*; fn test_new_component() { common::setup_test(); - Test2RefContainer::create_component("test-get-set-id");-- let id = "test-get-set-id";-+ let id = "test-id-for-component"; let content = html! {{- <test-component2 id="$id" test_attribute="initial-value-for-attribute"></test-component1>+ <test-get-set-id id="$id" test_attribute="initial-value-for-attribute"></test-get-set-id> }}; inner_html("#test-div", &content).unwrap();- let component = Test2Component::get_component(id); // ref++ let component = Test2Component::get_component(id).expect("Component not found in global store");+ let attr = component.get_attribute("number").unwrap();+ assert_eq!(attr, "42");++ {+ component.set_attribute("number", "100");+ let attr = component.get_attribute("number").unwrap();+ assert_eq!(attr, "100");++ let test2_inner = component.get_attributes();+ let test2 = test2_inner.borrow();+ log!("test2 number:", test2.number);+ }++ component.set_attribute("number", "SOMETHING_NOT_i32"); // NAN++ {+ let attr = component.get_attribute("number").unwrap();+ assert_eq!(attr, "100");++ let test2_inner = component.get_attributes();+ let test2 = test2_inner.borrow();+ log!("test2 number:", test2.number);+ } } -#[derive(Default)]+#[component("test-get-set-id")] struct Test2 { test: bool, number: i32,@@ -30,6 +53,12 @@ struct Test2 { impl Component for Test2Component { fn connected(&self) { log!("Connected:", self.get_id());+ log!("Set number");++ let id = self.get_id();+ let element = get_element_by_id(&id).unwrap();+ let res = element.set_attribute("number", "42");+ assert!(res.is_ok()); } fn disconnected(&self) {@@ -47,98 +76,3 @@ impl Component for Test2Component { ); } }--thread_local! {- static TEST2_COMPONENTS: RefCell<HashMap<String, std::rc::Rc<std::cell::RefCell<Test2RefContainer>>>> =- RefCell::new(HashMap::new());-}--impl Test2 {- fn set_test_attribute(&mut self, name: &str, value: &str) {- let _ = self.try_set_test_attribute(name, value);- }-- fn try_set_test_attribute(&mut self, name: &str, value: &str) -> Result<()> {- if name == "test" {- self.test = value.parse().map_err(|_| Error)?;- } else if name == "number" {- self.number = value.parse().map_err(|_| Error)?;- } else {- error!("No field", name, "in Test2 can't set it to", value);- return Err(Error);- }-- Ok(())- }-}--#[derive(Default)]-struct Test2RefContainer {- id: String,- attribute_values: Rc<RefCell<Test2>>,-}--impl Test2RefContainer {- fn create_component(id: &str) {- let handle = Rc::new(RefCell::new(Test2RefContainer {- id: id.to_string(),- attribute_values: std::rc::Rc::new(std::cell::RefCell::new(Test2::default())),- }));-- Self::register_component(handle.clone());- let component = Test2Component::get_component(&id);-- if component.is_none() {- error!("Failed to register compoent.");- return;- }-- let component_impl = component.unwrap();- component_impl.connected();- }-- fn register_component(component: Rc<RefCell<Test2RefContainer>>) {- let id = component.borrow().id.clone();-- TEST2_COMPONENTS.with(|components| {- components.borrow_mut().insert(id, component);- });- }-}--#[derive(Default)]-struct Test2Component {- handle: Rc<RefCell<Test2RefContainer>>,-}--impl Test2Component {- fn get_component(id: &str) -> Option<Test2Component> {- let handle = TEST2_COMPONENTS.with(|components| components.borrow().get(id).cloned());-- if !handle.is_some() {- return None;- }-- Some(Test2Component {- handle: handle.unwrap(),- })- }-- fn internal_observed_attributes() -> &'static [&'static str] {- &[]- }-- fn internal_attributes_update(&self, name: &str, old_value: &str, new_value: &str) {- let handle = self.handle.borrow();- let mut attr = handle.attribute_values.borrow_mut();- attr.set_test_attribute(name, new_value);- }-- fn get_attributes(&self) -> Rc<RefCell<Test2>> {- self.handle.borrow().attribute_values.clone()- }-- fn get_id(&self) -> String {- self.handle.borrow().id.clone()- }-}