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
51/// New type wrapper of a closure that takes a parameter `T`. This allows the event handler props
52/// to be optional while being able to take a simple closure.
53#[derive(Clone)]
54pub struct EventHandler<T>(Arc<dyn Fn(T) + Send + Sync>);
55
56impl<T> Default for EventHandler<T> {
57    fn default() -> Self {
58        #[allow(unused_variables)]
59        Self(Arc::new(|event: T| {}))
60    }
61}
62
63impl<F, T> From<F> for EventHandler<T>
64where
65    F: Fn(T) + Send + Sync + 'static,
66{
67    fn from(f: F) -> Self {
68        Self(Arc::new(f))
69    }
70}
71
72impl<T> EventHandler<T> {
73    #[inline]
74    pub fn run(&self, event: T) {
75        (self.0)(event)
76    }
77}