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! {
87        <table class=class>
88            {children()}
89        </table>
90    }
91}
92
93#[component]
94pub fn TableHead<'a>(#[prop(optional, into)] class: &'a str, children: Children) -> impl IntoView {
95    view! {
96        <thead class=class>
97            {children()}
98        </thead>
99    }
100}
101
102#[component]
103pub fn TableRow<'a>(#[prop(optional, into)] class: &'a str, children: Children) -> impl IntoView {
104    view! {
105        <tr class=class>
106            {children()}
107        </tr>
108    }
109}
110
111#[component]
112pub fn TableHeadCol<'a>(
113    #[prop(optional, into)] class: &'a str,
114    children: Children,
115) -> impl IntoView {
116    view! {
117        <th class=class>
118            {children()}
119        </th>
120    }
121}
122
123#[component]
124pub fn TableCol<'a>(#[prop(optional, into)] class: &'a str, children: Children) -> impl IntoView {
125    view! {
126        <td class=class>
127            {children()}
128        </td>
129    }
130}