leptos_struct_table/selection.rs
1use leptos::*;
2use std::collections::HashSet;
3
4/// Type of selection together with the `RwSignal` to hold the selection
5#[derive(Clone, Copy, PartialEq, Eq, Debug, Default)]
6pub enum Selection {
7 /// No selection possible (the default).
8 #[default]
9 None,
10
11 /// Allow only one row to be selected at a time. `None` if no rows are selected.
12 /// `Some(<row index>)` if a row is selected.
13 Single(RwSignal<Option<usize>>),
14
15 /// Allow multiple rows to be selected at a time. Each entry in the `Vec`
16 /// is the index of a selected row.
17 Multiple(RwSignal<HashSet<usize>>),
18}
19
20impl Selection {
21 /// Clear the selection
22 pub fn clear(&self) {
23 match self {
24 Selection::None => {}
25 Selection::Single(selected_index) => {
26 selected_index.set(None);
27 }
28 Selection::Multiple(selected_indices) => {
29 selected_indices.update(|selected_indices| {
30 selected_indices.clear();
31 });
32 }
33 }
34 }
35}