leptos_struct_table/
events.rs

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