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)]
#![doc(cfg(feature = "chrono"))]

use chrono::{NaiveDate, NaiveDateTime, NaiveTime};
use leptos::*;
use paste::paste;

macro_rules! date_cell_renderer {
    (
        $(#[$outer:meta])*
        $date_type:ident
    ) => {
        paste! {
            $(#[$outer])*
            ///
            /// This is only available when the **crate feature `chrono`** is enabled
            #[component]
            pub fn [<Default $date_type TableCellRenderer>]<F> (

                /// The class attribute for the cell element. Generated by the classes provider.
                class: String,
                /// The value to display.
                #[prop(into)] value: MaybeSignal<$date_type>,
                /// 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 format string to use for formatting the date. Provided by the `#[table(format(string="..."))]` attribute of the field.
                /// See [`chrono::format::strftime`] for more information.
                #[prop(optional)] format_string: Option<String>,
            ) -> impl IntoView
            where
                F: Fn($date_type) + 'static,
            {
                let text = match format_string {
                    Some(format_string) => create_memo( move |_| value().format(&format_string).to_string()),
                    None => create_memo( move |_| value().to_string()),
                };

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

date_cell_renderer!(
    /// The default cell renderer for [`chrono::NaiveDate`].
    NaiveDate
);

date_cell_renderer!(
    /// The default cell renderer for [`chrono::NaiveDateTime`].
    NaiveDateTime
);

date_cell_renderer!(
    /// The default cell renderer for [`chrono::NaiveTime`].
    NaiveTime
);