]> git.proxmox.com Git - ui/proxmox-yew-widget-toolkit.git/commitdiff
grid_picker: remove manual selection, instead call on_select manually
authorDominik Csapak <d.csapak@proxmox.com>
Mon, 16 Oct 2023 10:34:07 +0000 (12:34 +0200)
committerDominik Csapak <d.csapak@proxmox.com>
Mon, 16 Oct 2023 10:34:07 +0000 (12:34 +0200)
the selection itself is handled by the DataTable, but we want to
manually call 'on_select' every time, otherwise we don't close the
picker when selecting an already selected element

Signed-off-by: Dominik Csapak <d.csapak@proxmox.com>
src/widget/grid_picker.rs

index accbed88b9feaa962e30ac234ecf0284b3538345..8fd8240f11e25cbb95abfe78ebfaf1f965e62f5b 100644 (file)
@@ -10,7 +10,7 @@ use yew::virtual_dom::{Key, VComp, VNode};
 
 use crate::prelude::*;
 use crate::props::{FilterFn, IntoTextFilterFn, TextFilterFn};
-use crate::state::{DataStore, Selection, SelectionObserver};
+use crate::state::{DataStore, Selection};
 use crate::widget::data_table::{DataTable, DataTableMouseEvent};
 use crate::widget::{Column, Input, Row};
 
@@ -113,7 +113,6 @@ pub enum Msg {
 pub struct PwtGridPicker<S> {
     filter: String,
     store: S,
-    _selection_observer: Option<SelectionObserver>,
     _phantom: PhantomData<S>,
 }
 
@@ -151,22 +150,7 @@ impl<S: DataStore + 'static> Component for PwtGridPicker<S> {
     fn create(ctx: &Context<Self>) -> Self {
         let props = ctx.props();
 
-        let _selection_observer = match &props.selection {
-            Some(selection) => Some(selection.add_listener({
-                let on_select = props.on_select.clone();
-                move |selection: Selection| {
-                    if let Some(on_select) = &on_select {
-                        if let Some(key) = selection.selected_key() {
-                            on_select.emit(key);
-                        }
-                    }
-                }
-            })),
-            None => None,
-        };
-
         let mut me = Self {
-            _selection_observer,
             _phantom: PhantomData::<S>,
             filter: String::new(),
             store: props.table.get_store(),
@@ -190,6 +174,7 @@ impl<S: DataStore + 'static> Component for PwtGridPicker<S> {
     fn view(&self, ctx: &Context<Self>) -> Html {
         let props = ctx.props();
 
+        let on_select = ctx.props().on_select.clone();
         let table: Html = props
             .table
             .clone()
@@ -199,10 +184,10 @@ impl<S: DataStore + 'static> Component for PwtGridPicker<S> {
             .hover(true)
             .header_focusable(false)
             .selection(props.selection.clone())
-            .on_row_click(|event: &mut DataTableMouseEvent| {
+            .on_row_click(move |event: &mut DataTableMouseEvent| {
                 let key = event.record_key.clone();
-                if let Some(selection) = &event.selection {
-                    selection.select(key);
+                if let Some(on_select) = &on_select {
+                    on_select.emit(key);
                 }
             })
             .into();