[yew-devel] [PATCH yew-widget-toolkit v2 1/1] store: add helper methods to extract a record via a selection

Shannon Sterz s.sterz at proxmox.com
Thu Nov 6 15:47:39 CET 2025


this adds helpers to the `Store` (and its `StoreState`) that have
similar objectives:

- `selected_record(Selection) -> Option<T>`: returns a clone of the
record that has the same `Key` as the currently selected item in the
`Selection`. this requires the type `T` to implement `Clone`.
- `lookup_with_selection(Selection) -> Option<&T>`: requires the user
to acquire a read guard on the store first and will then return a
reference to the record of the store that matches the currently
selected item in the `Selection`.
- each also has variation that is prefixed with `multi_` and returns a
vector of all entries that have a key selected by a `Selection` in
multi-select mode

Signed-off-by: Shannon Sterz <s.sterz at proxmox.com>
---
 src/state/store.rs | 49 +++++++++++++++++++++++++++++++++++++++++++++-
 1 file changed, 48 insertions(+), 1 deletion(-)

diff --git a/src/state/store.rs b/src/state/store.rs
index b424f379..1a5ec470 100644
--- a/src/state/store.rs
+++ b/src/state/store.rs
@@ -14,7 +14,7 @@ use yew::virtual_dom::Key;
 use crate::props::{
     ExtractKeyFn, ExtractPrimaryKey, FilterFn, IntoFilterFn, IntoSorterFn, SorterFn,
 };
-use crate::state::{optional_rc_ptr_eq, DataNode, DataNodeDerefGuard, DataStore};
+use crate::state::{optional_rc_ptr_eq, DataNode, DataNodeDerefGuard, DataStore, Selection};
 
 /// Hook to use a [Store] with functional components.
 ///
@@ -102,6 +102,29 @@ impl<T: ExtractPrimaryKey + 'static> FromIterator<T> for Store<T> {
     }
 }
 
+impl<T: Clone> Store<T> {
+    /// Returns a clone of the record that is currently selected by `selection`.
+    pub fn selected_record(&self, selection: &Selection) -> Option<T> {
+        self.read().lookup_with_selection(selection).cloned()
+    }
+
+    /// Returns a vector of clones of all records that are selected by the `selection` in
+    /// multi-select mode.
+    ///
+    /// # Note
+    ///
+    /// If a `Key` returned by `Selection::selected_keys` is not in the store, it will be filtered
+    /// from the returned vector. The vector will still contain a record for every other `Key` that
+    /// a record can be found for.
+    pub fn multi_selected_records(&self, selection: &Selection) -> Vec<T> {
+        self.read()
+            .lookup_with_multi_selection(selection)
+            .into_iter()
+            .cloned()
+            .collect()
+    }
+}
+
 impl<T: 'static> Store<T> {
     /// Creates a new instance with the specifies extract key function.
     pub fn with_extract_key(extract_key: impl Into<ExtractKeyFn<T>>) -> Self {
@@ -558,6 +581,30 @@ impl<T: 'static> StoreState<T> {
     pub fn lookup_record_mut(&mut self, key: &Key) -> Option<&mut T> {
         self.record_pos(key).map(|n| &mut self.data[n])
     }
+
+    /// Find the record that has the same key as is currently selected by `selection`.
+    pub fn lookup_with_selection(&self, selection: &Selection) -> Option<&T> {
+        if let Some(ref key) = selection.selected_key() {
+            return self.lookup_record(key);
+        }
+
+        None
+    }
+
+    /// Get all records that are selected by the `selection` in multi-select mode.
+    ///
+    /// # Note
+    ///
+    /// If a `Key` returned by `Selection::selected_keys` is not in the store, it will be filtered
+    /// from the returned vector. The vector will still contain a record for every other `Key` that
+    /// a record can be found for.
+    pub fn lookup_with_multi_selection(&self, selection: &Selection) -> Vec<&T> {
+        selection
+            .selected_keys()
+            .iter()
+            .filter_map(|k| self.lookup_record(k))
+            .collect()
+    }
 }
 
 #[doc(hidden)]
-- 
2.47.3





More information about the yew-devel mailing list