all lists on lists.proxmox.com
 help / color / mirror / Atom feed
From: Hannes Laimer <h.laimer@proxmox.com>
To: pdm-devel@lists.proxmox.com
Subject: [pdm-devel] [RFC pwt] combobox: add .schema() to builder
Date: Tue, 30 Sep 2025 14:59:58 +0200	[thread overview]
Message-ID: <20250930125958.309839-1-h.laimer@proxmox.com> (raw)

... guarded by the `proxmox-schema` feature.

Signed-off-by: Hannes Laimer <h.laimer@proxmox.com>
---

Very much RFC! Is there a reason to not have this on most elements? This
patch is exemplary, we just pull enum items and default value out of the
schema for the comobox. We could do the same for descriptions or regex
formats. 

 Cargo.toml                  |  5 ++++
 src/widget/form/combobox.rs | 46 +++++++++++++++++++++++--------------
 2 files changed, 34 insertions(+), 17 deletions(-)

diff --git a/Cargo.toml b/Cargo.toml
index 86f73cc..b2d2f1c 100644
--- a/Cargo.toml
+++ b/Cargo.toml
@@ -71,4 +71,9 @@ url = "2.1"
 percent-encoding = "2.1"
 gettext = "0.4"
 
+proxmox-schema = { version = "5.0.0", optional = true }
 pwt-macros = { version = "0.5", path = "pwt-macros" }
+
+[features]
+default = ["proxmox-schema"]
+proxmox-schema = ["dep:proxmox-schema"]
diff --git a/src/widget/form/combobox.rs b/src/widget/form/combobox.rs
index fc402ba..167fdb4 100644
--- a/src/widget/form/combobox.rs
+++ b/src/widget/form/combobox.rs
@@ -7,8 +7,8 @@ use yew::html::{IntoEventCallback, IntoPropValue};
 use yew::prelude::*;
 use yew::virtual_dom::Key;
 
-// #[cfg(feature = "proxmox-schema")]
-// use proxmox_schema::Schema;
+#[cfg(feature = "proxmox-schema")]
+use proxmox_schema::Schema;
 
 use crate::prelude::*;
 use crate::props::{IntoOptionalRenderFn, IntoTextFilterFn, RenderFn, TextFilterFn};
@@ -164,21 +164,33 @@ impl Combobox {
         self.validate = validate.into_validate_fn();
     }
 
-    // /// Builder style method to set the validation schema
-    // #[cfg(feature = "proxmox-schema")]
-    // pub fn schema(mut self, schema: &'static Schema) -> Self {
-    //     self.set_schema(schema);
-    //     self
-    // }
-
-    // /// Method to set the validation schema
-    // #[cfg(feature = "proxmox-schema")]
-    // pub fn set_schema(&mut self, schema: &'static Schema) {
-    //     self.set_validate(move |(value, _store): &(String, _)| {
-    //         schema.parse_simple_value(value)?;
-    //         Ok(())
-    //     });
-    // }
+    /// Builder style method to set the validation schema, items and placeholder
+    #[cfg(feature = "proxmox-schema")]
+    pub fn schema(mut self, schema: &'static Schema) -> Self {
+        self.set_schema(schema);
+        self
+    }
+
+    /// Method to set the validation schema, items and placeholder
+    #[cfg(feature = "proxmox-schema")]
+    pub fn set_schema(&mut self, schema: &'static Schema) {
+        if let proxmox_schema::Schema::String(s) = schema {
+            if let Some(proxmox_schema::ApiStringFormat::Enum(e)) = s.format {
+                let items: Vec<AttrValue> = e
+                    .iter()
+                    .map(|entry| entry.value.to_string().into())
+                    .collect();
+                self.set_items(items.into());
+            }
+            if let Some(value) = s.default {
+                self.set_placeholder(value);
+            }
+        }
+        self.set_validate(move |(value, _store): &(String, _)| {
+            schema.parse_simple_value(value)?;
+            Ok(())
+        });
+    }
 
     /// Builder style method to add an trigger
     pub fn with_trigger(mut self, trigger: impl Into<Trigger>, right: bool) -> Self {
-- 
2.47.3



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


                 reply	other threads:[~2025-09-30 13:00 UTC|newest]

Thread overview: [no followups] expand[flat|nested]  mbox.gz  Atom feed

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=20250930125958.309839-1-h.laimer@proxmox.com \
    --to=h.laimer@proxmox.com \
    --cc=pdm-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 an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.
Service provided by Proxmox Server Solutions GmbH | Privacy | Legal