leptos_struct_table/
selection.rs

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
use leptos::prelude::*;
use std::collections::HashSet;

/// Type of selection together with the `RwSignal` to hold the selection
#[derive(Clone, Copy, PartialEq, Eq, Debug, Default)]
pub enum Selection {
    /// No selection possible (the default).
    #[default]
    None,

    /// Allow only one row to be selected at a time. `None` if no rows are selected.
    /// `Some(<row index>)` if a row is selected.
    Single(RwSignal<Option<usize>>),

    /// Allow multiple rows to be selected at a time. Each entry in the `Vec`
    /// is the index of a selected row.
    Multiple(RwSignal<HashSet<usize>>),
}

impl Selection {
    /// Clear the selection
    pub fn clear(&self) {
        match self {
            Selection::None => {}
            Selection::Single(selected_index) => {
                selected_index.set(None);
            }
            Selection::Multiple(selected_indices) => {
                selected_indices.write().clear();
            }
        }
    }
}