1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
#![allow(unused_variables)]

#[cfg(feature = "chrono")]
mod chrono;
#[cfg(feature = "chrono")]
pub use self::chrono::*;

use core::fmt::Display;
use leptos::*;

/// The default cell renderer. Uses the `<td>` element.
#[component]
pub fn DefaultTableCellRenderer<T, F>(
    /// The class attribute for the cell element. Generated by the classes provider.
    class: String,
    /// The value to display.
    #[prop(into)]
    value: MaybeSignal<T>,
    /// Event handler called when the cell is changed. In this default renderer this will never happen.
    on_change: F,
    /// The index of the column. Starts at 0.
    index: usize,
) -> impl IntoView
where
    T: IntoView + Clone + 'static,
    F: Fn(T) + 'static,
{
    view! {
        <td class=class>{value}</td>
    }
}

/// The default number cell renderer. Uses the `<td>` element.
#[component]
pub fn DefaultNumberTableCellRenderer<T, F>(
    /// The class attribute for the cell element. Generated by the classes provider.
    class: String,
    /// The value to display.
    #[prop(into)]
    value: MaybeSignal<T>,
    /// Event handler called when the cell is changed. In this default renderer this will never happen.
    on_change: F,
    /// The index of the column. Starts at 0.
    index: usize,
    /// The number of digits to display after the decimal point. Provided by the `#[table(format(precision=X))]` attribute of the field.
    #[prop(optional)]
    precision: Option<usize>,
) -> impl IntoView
where
    T: Display + Clone + 'static,
    F: Fn(T) + 'static,
{
    let text = create_memo(move |_| match precision {
        Some(precision) => format!("{:.precision$}", value()),
        None => format!("{}", value()),
    });

    view! {
        <td class=class>{text}</td>
    }
}