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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
use crate::{Renderable};
use crate::node::{Node, NodeContainer};
use std::borrow::BorrowMut;
use crate::{DefaultModifiers};
use crate::components::{Appendable, ChildContainer};



#[derive(Debug, Clone)]
pub struct Form {
    children: Vec<Box<dyn Renderable>>,
    node: Node,
    pub action: String,
    pub is_async: bool
}

impl NodeContainer for Form {
    fn get_node(&mut self) -> &mut Node {
        self.node.borrow_mut()
    }
}

impl DefaultModifiers<Form> for Form {}

impl Form {
    pub fn new(name: &str, action: &str) -> Self {
        Form {
            children: vec![],
            node: Node::default(),
            action: action.to_string(),
            is_async: false
        }
            .set_attr("id", name)
            .set_attr("method", "POST")
    }
    pub fn async_form(&mut self) -> Self {
        self.is_async = true;
        self.clone()
    }
}

impl ChildContainer for Form {
    fn get_children(&mut self) -> &mut Vec<Box<dyn Renderable>> {
        return self.children.borrow_mut();
    }
}
impl Appendable for Form {}

impl Renderable for Form {
    fn render(&self) -> Node {

        let mut form = self.clone()
            .add_class("form")
            .set_attr("action", &self.action)
            .tag("form");

        if self.is_async {
            form.set_attr("data-async", "data-async");
        }

        let mut node = form.node;

        self.children.iter()
            .for_each(|child|
                node.children.push(child.render()));
        node
    }
}