leptos_struct_table/components/
row.rs

1use crate::table_row::TableRow;
2use crate::EventHandler;
3use leptos::prelude::*;
4
5/// The default table row renderer. Uses the `<tr>` element. Please note that this
6/// is **NOT** a `#[component]`.
7#[allow(unused_variables)]
8pub fn DefaultTableRowRenderer<Row>(
9    // The class attribute for the row element. Generated by the classes provider.
10    class: Signal<String>,
11    // The row to render.
12    row: RwSignal<Row>,
13    // The index of the row. Starts at 0 for the first body row.
14    index: usize,
15    // The selected state of the row. True, when the row is selected.
16    selected: Signal<bool>,
17    // Event handler callback when this row is selected
18    on_select: EventHandler<web_sys::MouseEvent>,
19) -> impl IntoView
20where
21    Row: TableRow + 'static,
22{
23    view! {
24        <tr class=class on:click=move |mouse_event| on_select.run(mouse_event)>
25            {TableRow::render_row(row, index)}
26        </tr>
27    }
28}
29
30/// The default row placeholder renderer which is just a div that is set to the
31/// appropriate height. This is used in place of rows that are not shown
32/// before and after the currently visible rows.
33pub fn DefaultRowPlaceholderRenderer(height: Signal<f64>) -> impl IntoView {
34    view! { <tr style:height=move || format!("{}px", height.get()) style="display: block"></tr> }
35}
36
37/// The default error row renderer which just displays the error message when
38/// a row fails to load, i.e. when [`TableDataProvider::get_rows`] returns an `Err(..)`.
39#[allow(unused_variables)]
40pub fn DefaultErrorRowRenderer(err: String, index: usize, col_count: usize) -> impl IntoView {
41    view! { <tr><td colspan=col_count>{err}</td></tr> }
42}
43
44/// The default loading row renderer which just displays a loading indicator.
45#[allow(unused_variables, unstable_name_collisions)]
46pub fn DefaultLoadingRowRenderer(
47    class: Signal<String>,
48    get_cell_class: Callback<(usize,), String>,
49    get_inner_cell_class: Callback<(usize,), String>,
50    index: usize,
51    col_count: usize,
52) -> impl IntoView {
53    view! {
54        <tr class=class>
55            {
56                (0..col_count).map(|col_index| view! {
57                    <td class=get_cell_class.run((col_index,))>
58                        <div class=get_inner_cell_class.run((col_index,))></div>
59                        " "
60                    </td>
61                }).collect_view()
62            }
63        </tr>
64    }
65}