leptos_bootstrap/v5/
table.rs

1use leptos::prelude::*;
2use std::fmt;
3
4pub enum TableKind {
5    Default,
6    Primary,
7    Secondary,
8    Success,
9    Danger,
10    Warning,
11    Info,
12    Light,
13    Dark,
14}
15
16impl fmt::Display for TableKind {
17    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
18        let s = match self {
19            Self::Default => "",
20            Self::Primary => "table-primary",
21            Self::Secondary => "table-secondary",
22            Self::Success => "table-success",
23            Self::Danger => "table-danger",
24            Self::Warning => "table-warning",
25            Self::Info => "table-info",
26            Self::Light => "table-light",
27            Self::Dark => "table-dark",
28        };
29        write!(f, "{}", s)
30    }
31}
32
33pub enum TableBorderKind {
34    Default,
35    Bordered,
36    Borderless,
37}
38
39impl fmt::Display for TableBorderKind {
40    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
41        let s = match self {
42            Self::Default => "",
43            Self::Bordered => "table-bordered",
44            Self::Borderless => "table-bordeless",
45        };
46        write!(f, "{}", s)
47    }
48}
49
50pub enum TableStripeKind {
51    Default,
52    Rows,
53    Cols,
54    RowsAndCols,
55}
56
57impl fmt::Display for TableStripeKind {
58    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
59        let s = match self {
60            Self::Default => "",
61            Self::Rows => "table-striped",
62            Self::Cols => "table-striped-columns",
63            Self::RowsAndCols => "table-striped table-striped-columns",
64        };
65        write!(f, "{}", s)
66    }
67}
68
69#[component]
70pub fn Table<'a>(
71    #[prop(default = TableKind::Default)] kind: TableKind,
72    #[prop(default = TableBorderKind::Default)] border: TableBorderKind,
73    #[prop(default = TableStripeKind::Default)] stripe: TableStripeKind,
74    #[prop(default = false)] sm: bool,
75    #[prop(default = false)] hover: bool,
76    #[prop(optional, into)] class: &'a str,
77    children: Children,
78) -> impl IntoView {
79    let mut class = format!("table {} {} {} {}", kind, border, stripe, class);
80    if hover {
81        class.push_str(" table-hover");
82    }
83    if sm {
84        class.push_str(" table-sm");
85    }
86    view! { <table class=class>{children()}</table> }
87}
88
89#[component]
90pub fn TableHead<'a>(#[prop(optional, into)] class: &'a str, children: Children) -> impl IntoView {
91    view! { <thead class=class>{children()}</thead> }
92}
93
94#[component]
95pub fn TableRow<'a>(#[prop(optional, into)] class: &'a str, children: Children) -> impl IntoView {
96    view! { <tr class=class>{children()}</tr> }
97}
98
99#[component]
100pub fn TableHeadCol<'a>(
101    #[prop(optional, into)] class: &'a str,
102    children: Children,
103) -> impl IntoView {
104    view! { <th class=class>{children()}</th> }
105}
106
107#[component]
108pub fn TableCol<'a>(#[prop(optional, into)] class: &'a str, children: Children) -> impl IntoView {
109    view! { <td class=class>{children()}</td> }
110}