public inbox for yew-devel@lists.proxmox.com
 help / color / mirror / Atom feed
From: Dominik Csapak <d.csapak@proxmox.com>
To: yew-devel@lists.proxmox.com
Subject: [yew-devel] [PATCH yew-widget-toolkit 1/2] fix #6382: widget: data table: prevent duplicate yew keys for rows
Date: Wed,  7 May 2025 16:37:57 +0200	[thread overview]
Message-ID: <20250507143800.3461963-2-d.csapak@proxmox.com> (raw)
In-Reply-To: <20250507143800.3461963-1-d.csapak@proxmox.com>

yews diff algorithm assumes that elements in a list have unique keys, so
it can properly diff the new and old list. Our Store/Datastore does not
guarantee that records have unique keys.

For the data table rows, we used the record keys as yew keys, so in case
we generated duplicate entries in our datastore, we had duplicate keys
for yew, which in turn can panic.

To prevent that for the data table, prefix the record key with the index
into the store, the combination of which must be unique in this list.

Only using the row would not be enough though, otherwise yew would reuse
items even if they're unrelated. e.g. when the whole store content
changes.

Signed-off-by: Dominik Csapak <d.csapak@proxmox.com>
---
 src/widget/data_table/row.rs | 9 +++++++--
 1 file changed, 7 insertions(+), 2 deletions(-)

diff --git a/src/widget/data_table/row.rs b/src/widget/data_table/row.rs
index 2770eb9..707ed31 100644
--- a/src/widget/data_table/row.rs
+++ b/src/widget/data_table/row.rs
@@ -80,7 +80,7 @@ impl<T: Clone + PartialEq + 'static> Component for PwtDataTableRow<T> {
         };
 
         let mut row = Container::from_tag("tr")
-            .key(props.record_key.clone())
+            .key(generate_key(&props))
             .attribute("role", "row")
             // aria-rowindex does not work, no firefox support?
             .attribute("aria-rowindex", (props.row_num + 1).to_string())
@@ -189,8 +189,13 @@ impl<T: Clone + PartialEq + 'static> Component for PwtDataTableRow<T> {
 
 impl<T: Clone + PartialEq + 'static> From<DataTableRow<T>> for VNode {
     fn from(val: DataTableRow<T>) -> Self {
-        let key = Some(val.record_key.clone());
+        let key = Some(generate_key(&val));
         let comp = VComp::new::<PwtDataTableRow<T>>(Rc::new(val), key);
         VNode::from(comp)
     }
 }
+
+// generate unique key from rownum + record key, since record key might not be unique
+fn generate_key<T: Clone + PartialEq>(row: &DataTableRow<T>) -> Key {
+    format!("{}-{}", row.row_num, row.record_key).into()
+}
-- 
2.39.5



_______________________________________________
yew-devel mailing list
yew-devel@lists.proxmox.com
https://lists.proxmox.com/cgi-bin/mailman/listinfo/yew-devel


  reply	other threads:[~2025-05-07 14:37 UTC|newest]

Thread overview: 8+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2025-05-07 14:37 [yew-devel] [PATCH yew-comp/yew-widget-toolkit 0/4] fix crash and improve task loading Dominik Csapak
2025-05-07 14:37 ` Dominik Csapak [this message]
2025-05-08  8:50   ` [yew-devel] [PATCH yew-widget-toolkit 1/2] fix #6382: widget: data table: prevent duplicate yew keys for rows Dietmar Maurer
2025-05-07 14:37 ` [yew-devel] [PATCH yew-widget-toolkit 2/2] widget: data table: add table scroll callback Dominik Csapak
2025-05-08  9:08   ` [yew-devel] applied: " Dietmar Maurer
2025-05-07 14:37 ` [yew-devel] [PATCH yew-comp 1/2] tasks: improve behavior of loading additional tasks Dominik Csapak
2025-05-07 14:38 ` [yew-devel] [PATCH yew-comp 2/2] tasks: don't add entries we already loaded Dominik Csapak
2025-05-08  9:32   ` Dietmar Maurer

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=20250507143800.3461963-2-d.csapak@proxmox.com \
    --to=d.csapak@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
Service provided by Proxmox Server Solutions GmbH | Privacy | Legal