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
use yew::prelude::*;

#[derive(Clone, Debug, Properties, PartialEq)]
pub struct TableProps {
    #[prop_or_default]
    pub children: Children,
    #[prop_or_default]
    pub classes: Classes,
    /// Add borders to all the cells.
    #[prop_or_default]
    pub bordered: bool,
    /// Add stripes to the table.
    #[prop_or_default]
    pub striped: bool,
    /// Make the cells narrower.
    #[prop_or_default]
    pub narrow: bool,
    /// Add a hover effect on each row.
    #[prop_or_default]
    pub hoverable: bool,
    /// Make the table fullwidth.
    #[prop_or_default]
    pub fullwidth: bool,
    /// Make the table scrollable, wrapping the table in a `div.table-container`.
    #[prop_or_default]
    pub scrollable: bool,
}

/// An HTML table component.
///
/// [https://bulma.io/documentation/elements/table/](https://bulma.io/documentation/elements/table/)
#[function_component(Table)]
pub fn table(props: &TableProps) -> Html {
    let class = classes!(
        "table",
        props.classes.clone(),
        props.bordered.then_some("is-bordered"),
        props.striped.then_some("is-striped"),
        props.narrow.then_some("is-narrow"),
        props.hoverable.then_some("is-hoverable"),
        props.fullwidth.then_some("is-fullwidth"),
    );
    if props.scrollable {
        html! {
            <div class="table-container">
                <table {class}>
                    {props.children.clone()}
                </table>
            </div>
        }
    } else {
        html! {
            <table {class}>
                {props.children.clone()}
            </table>
        }
    }
}