leptos_struct_table/
events.rs

1use leptos::ev::MouseEvent;
2use std::rc::Rc;
3
4/// The event provided to the `on_change` prop of the table component
5#[derive(Debug, Clone)]
6pub struct ChangeEvent<Row: Clone> {
7    /// The index of the table row that contains the cell that was changed. Starts at 0.
8    pub row_index: usize,
9    /// The index of the table column that contains the cell that was changed. Starts at 0.
10    pub col_index: usize,
11    /// The the row that was changed.
12    pub changed_row: Row,
13}
14
15/// The event provided to the `on_selection_change` prop of the table component
16#[derive(Debug, Clone)]
17pub struct SelectionChangeEvent<Row: Clone> {
18    /// `true` is the row was selected, `false` if it was de-selected.
19    pub selected: bool,
20    /// The index of the row that was de-/selected.
21    pub row_index: usize,
22    /// The row that was de-/selected.
23    pub row: Row,
24}
25
26/// Event emitted when a table head cell is clicked.
27#[derive(Debug)]
28pub struct TableHeadEvent {
29    /// The index of the column. Starts at 0 for the first column.
30    /// The order of the columns is the same as the order of the fields in the struct.
31    pub index: usize,
32    /// The mouse event that triggered the event.
33    pub mouse_event: MouseEvent,
34}
35
36macro_rules! impl_default_rc_fn {
37    (
38        $(#[$meta:meta])*
39        $name:ident<$($ty:ident),*>($($arg_name:ident: $arg_ty:ty),*)
40        $(-> $ret_ty:ty)?
41        $({ default $default_return:expr })?
42    ) => {
43        $(#[$meta])*
44        #[derive(Clone)]
45        pub struct $name<$($ty),*>(Rc<dyn Fn($($arg_ty),*) $(-> $ret_ty)?>);
46
47        impl<$($ty),*> Default for $name<$($ty),*> {
48            fn default() -> Self {
49                #[allow(unused_variables)]
50                Self(Rc::new(|$($arg_name: $arg_ty),*| {
51                    $($default_return)?
52                }))
53            }
54        }
55
56        impl<F, $($ty),*> From<F> for $name<$($ty),*>
57            where F: Fn($($arg_ty),*) $(-> $ret_ty)? + 'static
58        {
59            fn from(f: F) -> Self { Self(Rc::new(f)) }
60        }
61
62        impl<$($ty),*> $name<$($ty),*> {
63            pub fn run(&self, $($arg_name: $arg_ty),*) $(-> $ret_ty)? {
64                (self.0)($($arg_name),*)
65            }
66        }
67    }
68}
69
70impl_default_rc_fn!(
71    /// New type wrapper of a closure that takes a parameter `T`. This allows the event handler props
72    /// to be optional while being able to take a simple closure.
73    EventHandler<T>(event: T)
74);
75
76pub(crate) use impl_default_rc_fn;