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
62
63
64
65
66
67
#![allow(non_snake_case)]

use dioxus::events::MouseEvent;
use dioxus::prelude::*;
use std::fmt::Display;

#[derive(Props, PartialEq)]
pub struct DefaultTableCellProps<T: PartialEq> {
    #[props(default)]
    pub class: &'static str,

    pub value: T,

    pub precision: Option<usize>,
}

pub fn DefaultTableCellRenderer<T: Display + PartialEq>(
    cx: Scope<DefaultTableCellProps<T>>,
) -> Element {
    let text = match cx.props.precision {
        Some(precision) => format!("{:.precision$}", cx.props.value),
        None => format!("{}", cx.props.value),
    };

    cx.render(rsx! {
        td {
            class: "{cx.props.class}",
            "{text}"
        }
    })
}

pub struct TableHeadEvent<E> {
    pub event: E,
    pub column_index: usize,
    pub field: String,
}

#[derive(Props)]
pub struct DefaultTableHeaderProps<'a> {
    #[props(default)]
    pub class: &'a str,

    #[props(default)]
    pub onclick: EventHandler<'a, TableHeadEvent<MouseEvent>>,

    pub field: &'a str,
    pub column_index: usize,

    pub children: Element<'a>,
}

pub fn DefaultTableHeaderRenderer<'a>(cx: Scope<'a, DefaultTableHeaderProps<'a>>) -> Element<'a> {
    cx.render(rsx! {
        th {
            class: "{cx.props.class}",
            onclick: move |evt| {
                 cx.props.onclick.call(TableHeadEvent{
                    column_index: cx.props.column_index,
                    field: cx.props.field.to_owned(),
                    event: evt,
                 })
            },
            &cx.props.children
        }
    })
}