// Container component - Pure Windjammer implementation
// NO `mut` keyword needed - compiler infers everything!
// Tests Vec support for children!
use super::traits::Renderable
pub struct Container {
children: Vec<string>,
max_width: string,
max_height: string,
padding: string,
background_color: string,
}
impl Container {
// Constructor - returns new Container
pub fn new() -> Container {
Container {
children: Vec::new(),
max_width: "".to_string(),
max_height: "".to_string(),
padding: "16px".to_string(),
background_color: "".to_string(),
}
}
// Add a single child (tests Vec::push)
pub fn child(self, child: string) -> Container {
self.children.push(child)
self
}
// Set multiple children (tests Vec assignment)
pub fn children(self, children: Vec<string>) -> Container {
self.children = children
self
}
// Builder pattern: consumes self, modifies, returns self
pub fn max_width(self, width: string) -> Container {
self.max_width = width
self
}
pub fn max_height(self, height: string) -> Container {
self.max_height = height
self
}
pub fn padding(self, padding: string) -> Container {
self.padding = padding
self
}
pub fn background_color(self, color: string) -> Container {
self.background_color = color
self
}
// TODO: Once external type bindings work, this will return VNode
// For now, return a string representation for testing
}
// Test the Container component with Vec<string> children!
impl Renderable for Container {
pub fn render(self) -> string {
let mut style = "margin: 0 auto; ".to_string()
if self.max_width != "" {
style = style + "max-width: " + self.max_width + "; "
}
if self.max_height != "" {
style = style + "max-height: " + self.max_height + "; "
}
if self.padding != "" {
style = style + "padding: " + self.padding + "; "
}
if self.background_color != "" {
style = style + "background-color: " + self.background_color + "; "
}
// Join children with newlines
let children_html = self.children.join("\n ")
format!("<div class='wj-container' style='{}'>\n {}\n</div>", style, children_html)
}
}
fn main() {
let container1 = Container::new()
.max_width("800px".to_string())
.padding("24px".to_string())
.child("<p>First child</p>".to_string())
.child("<p>Second child</p>".to_string())
let container2 = Container::new()
.max_width("1200px".to_string())
.max_height("600px".to_string())
.background_color("#f0f0f0".to_string())
.children(vec!["<h1>Title</h1>".to_string(), "<p>Content</p>".to_string()])
println!("Container 1:\n{}", container1.render())
println!("\nContainer 2:\n{}", container2.render())
}