leptos_struct_table/components/
row.rs

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