dioxus_table/
cell_renderers.rs

1#![allow(non_snake_case)]
2
3use dioxus::events::MouseEvent;
4use dioxus::prelude::*;
5use std::fmt::Display;
6
7#[derive(Props, PartialEq)]
8pub struct DefaultTableCellProps<T: PartialEq> {
9    #[props(default)]
10    pub class: &'static str,
11
12    pub value: T,
13
14    pub precision: Option<usize>,
15}
16
17pub fn DefaultTableCellRenderer<T: Display + PartialEq>(
18    cx: Scope<DefaultTableCellProps<T>>,
19) -> Element {
20    let text = match cx.props.precision {
21        Some(precision) => format!("{:.precision$}", cx.props.value),
22        None => format!("{}", cx.props.value),
23    };
24
25    cx.render(rsx! {
26        td {
27            class: "{cx.props.class}",
28            "{text}"
29        }
30    })
31}
32
33pub struct TableHeadEvent<E> {
34    pub event: E,
35    pub column_index: usize,
36    pub field: String,
37}
38
39#[derive(Props)]
40pub struct DefaultTableHeaderProps<'a> {
41    #[props(default)]
42    pub class: &'a str,
43
44    #[props(default)]
45    pub onclick: EventHandler<'a, TableHeadEvent<MouseEvent>>,
46
47    pub field: &'a str,
48    pub column_index: usize,
49
50    pub children: Element<'a>,
51}
52
53pub fn DefaultTableHeaderRenderer<'a>(cx: Scope<'a, DefaultTableHeaderProps<'a>>) -> Element<'a> {
54    cx.render(rsx! {
55        th {
56            class: "{cx.props.class}",
57            onclick: move |evt| {
58                 cx.props.onclick.call(TableHeadEvent{
59                    column_index: cx.props.column_index,
60                    field: cx.props.field.to_owned(),
61                    event: evt,
62                 })
63            },
64            &cx.props.children
65        }
66    })
67}