1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
//! Component model and traits
use crate::vdom::VNode;
/// Trait for components
///
/// Note: The `render()` method should be implemented as a regular method,
/// not as part of this trait. The `#[component]` macro will ensure the
/// struct implements this trait.
pub trait Component: Send + Sync {
/// Initialize the component
fn init(&mut self) {}
/// Update the component with new props
fn update(&mut self) {}
/// Cleanup when component is unmounted
fn cleanup(&mut self) {}
/// Render the component to a virtual DOM node
/// This is provided as a trait method for type safety
fn render(&self) -> VNode;
}
/// Props trait for component properties
pub trait ComponentProps: Clone + Send + Sync {}
/// Implement ComponentProps for unit (components with no props)
impl ComponentProps for () {}
#[cfg(test)]
mod tests {
use super::*;
use crate::vdom::VText;
struct TestComponent;
impl Component for TestComponent {
fn render(&self) -> VNode {
VNode::Text(VText {
content: "Hello, World!".to_string(),
})
}
}
#[test]
fn test_component_render() {
let component = TestComponent;
let vnode = component.render();
assert!(matches!(vnode, VNode::Text(_)));
}
}