leptos_struct_table/components/
row.rs

1use crate::table_row::TableRow;
2use crate::{ChangeEvent, EventHandler};
3use leptos::*;
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: 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    // Event handler callback for changes
20    on_change: EventHandler<ChangeEvent<Row>>,
21) -> impl IntoView
22where
23    Row: TableRow + Clone + 'static,
24{
25    view! {
26        <tr class=class on:click=move |mouse_event| on_select.run(mouse_event)>
27            {row.render_row(index, on_change)}
28        </tr>
29    }
30}
31
32/// The default row placeholder renderer which is just a div that is set to the
33/// appropriate height. This is used in place of rows that are not shown
34/// before and after the currently visible rows.
35pub fn DefaultRowPlaceholderRenderer(height: Signal<f64>) -> impl IntoView {
36    view! { <tr style:height=move || format!("{}px", height.get()) style="display: block"></tr> }
37}
38
39/// The default error row renderer which just displays the error message when
40/// a row fails to load, i.e. when [`TableDataProvider::get_rows`] returns an `Err(..)`.
41#[allow(unused_variables)]
42pub fn DefaultErrorRowRenderer(err: String, index: usize, col_count: usize) -> impl IntoView {
43    view! { <tr><td colspan=col_count>{err}</td></tr> }
44}
45
46/// The default loading row renderer which just displays a loading indicator.
47#[allow(unused_variables, unstable_name_collisions)]
48pub fn DefaultLoadingRowRenderer(
49    class: Signal<String>,
50    get_cell_class: Callback<usize, String>,
51    get_inner_cell_class: Callback<usize, String>,
52    index: usize,
53    col_count: usize,
54) -> impl IntoView {
55    view! {
56        <tr class=class>
57            {
58                (0..col_count).map(|col_index| view! {
59                    <td class=get_cell_class.call(col_index)>
60                        <div class=get_inner_cell_class.call(col_index)></div>
61                        " "
62                    </td>
63                }).collect_view()
64            }
65        </tr>
66    }
67}