From: Shannon Sterz <s.sterz@proxmox.com>
To: yew-devel@lists.proxmox.com
Subject: [yew-devel] [RFC PATCH yew-widget-toolkit 1/1] store: add helper methods to extract a record via a selection
Date: Wed, 29 Oct 2025 14:30:01 +0100 [thread overview]
Message-ID: <20251029133002.236835-2-s.sterz@proxmox.com> (raw)
In-Reply-To: <20251029133002.236835-1-s.sterz@proxmox.com>
this adds two 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`.
Signed-off-by: Shannon Sterz <s.sterz@proxmox.com>
---
src/state/store.rs | 18 +++++++++++++++++-
1 file changed, 17 insertions(+), 1 deletion(-)
diff --git a/src/state/store.rs b/src/state/store.rs
index 42cb53b3..c075edd1 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,13 @@ 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()
+ }
+}
+
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 +565,15 @@ 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
+ }
}
#[doc(hidden)]
--
2.47.3
_______________________________________________
yew-devel mailing list
yew-devel@lists.proxmox.com
https://lists.proxmox.com/cgi-bin/mailman/listinfo/yew-devel
next prev parent reply other threads:[~2025-10-29 13:30 UTC|newest]
Thread overview: 4+ messages / expand[flat|nested] mbox.gz Atom feed top
2025-10-29 13:30 [yew-devel] [RFC yew-comp/yew-widget-toolkit 0/2] add helpers to get a record via a Selection from a Store Shannon Sterz
2025-10-29 13:30 ` Shannon Sterz [this message]
2025-10-29 13:30 ` [yew-devel] [RFC PATCH yew-comp 1/1] token panel: use new `selected_record` helper Shannon Sterz
2025-10-29 13:50 ` [yew-devel] [RFC yew-comp/yew-widget-toolkit 0/2] add helpers to get a record via a Selection from a Store Shannon Sterz
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20251029133002.236835-2-s.sterz@proxmox.com \
--to=s.sterz@proxmox.com \
--cc=yew-devel@lists.proxmox.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox