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
69
70
71
72
73
74
75
76
77
78
79
80
81
use yew::prelude::*;

use crate::vdom::{comp_with, tag};

/// Table component around html tables.
///
/// Example:
/// ```no_run
/// use yew_utils::components::table::Table;
/// use yew_utils::vdom::*;
/// use yew::prelude::*;
///
/// # #[function_component(Example)]
/// # fn example() -> Html {
/// let columns = Children::new(
///     ["col1", "col2"].map(|ea| text(ea).to_vnode()).to_vec(),
/// );
///
/// let data = 0..5;
/// let rows = data
///     .into_iter()
///     .map(|data| {
///         tr().key(data.to_string())
///             .append_all([
///                 td().text(data.to_string()),
///                 td().text(format!("{data} (col2)")),
///             ])
///             .to_vnode()
///     })
///     .collect::<Vec<_>>();
///
/// let table = Table::render(columns, yew::Children::new(rows));
/// # todo!();
/// # }
/// ```

pub struct Table;

impl Table {
    pub fn render(columns: Children, rows: Children) -> Html {
        comp_with::<Table>(TableProps {
            columns,
            children: rows,
        })
        .to_vnode()
    }
}

#[derive(PartialEq, Properties)]
pub struct TableProps {
    pub columns: Children,
    pub children: Children,
}

impl Component for Table {
    type Message = ();
    type Properties = TableProps;

    fn create(_ctx: &Context<Self>) -> Self {
        Self
    }

    fn view(&self, ctx: &Context<Self>) -> Html {
        let columns = &ctx.props().columns;
        let children = &ctx.props().children;
        tag("table")
            .class("mui-table")
            .append(
                tag("thead").append(
                    tag("tr").append_all(
                        columns
                            .iter()
                            .enumerate()
                            .map(|(i, node)| tag("th").append(node).key(i.to_string())),
                    ),
                ),
            )
            .append(tag("tbody").append_all(children.iter()))
            .to_vnode()
    }
}