public inbox for yew-devel@lists.proxmox.com
 help / color / mirror / Atom feed
From: Hannes Laimer <h.laimer@proxmox.com>
To: yew-devel@lists.proxmox.com
Subject: [yew-devel] [PATCH proxmox-yew-comp v3 1/2] schema_validation: use Schema to populate field properties of number, check- and combobox
Date: Thu,  9 Oct 2025 15:31:19 +0200	[thread overview]
Message-ID: <20251009133120.425255-3-h.laimer@proxmox.com> (raw)
In-Reply-To: <20251009133120.425255-1-h.laimer@proxmox.com>

The idea is to have things like enum/selection items, defaults or
property string validations only specified in one place.

Signed-off-by: Hannes Laimer <h.laimer@proxmox.com>
---
 src/schema_validation.rs | 99 +++++++++++++++++++++++++++++++++++++++-
 1 file changed, 98 insertions(+), 1 deletion(-)

diff --git a/src/schema_validation.rs b/src/schema_validation.rs
index af28705..fbdf96d 100644
--- a/src/schema_validation.rs
+++ b/src/schema_validation.rs
@@ -2,7 +2,13 @@ use std::cell::RefCell;
 use std::collections::HashMap;
 
 use proxmox_schema::Schema;
-use pwt::widget::form::{InputType, ValidateFn};
+use pwt::{
+    props::FieldBuilder,
+    state::Store,
+    widget::form::{InputType, NumberTypeInfo, ValidateFn},
+};
+use serde_json::Value;
+use yew::AttrValue;
 
 pub trait SchemaValidation {
     fn schema(mut self, schema: &'static Schema) -> Self
@@ -33,12 +39,24 @@ impl SchemaValidation for pwt::widget::form::Field {
                 self.max = s.maximum.map(|v| v as f64);
                 self.step = Some(1.0);
                 self.input_type = InputType::Number;
+                if let Some(value) = s.default {
+                    self.set_placeholder(value.to_string());
+                }
             }
             Schema::Number(s) => {
                 self.min = s.minimum;
                 self.max = s.maximum;
                 self.step = Some(1.0);
                 self.input_type = InputType::Number;
+                if let Some(value) = s.default {
+                    self.set_placeholder(value.to_string());
+                }
+            }
+            Schema::String(s) => {
+                self.set_tip(s.description.to_string());
+                if let Some(default) = s.default {
+                    self.set_placeholder(default.to_string());
+                }
             }
             _ => {}
         }
@@ -60,3 +78,82 @@ impl SchemaValidation for pwt::widget::form::Field {
         self.set_validate(validate);
     }
 }
+
+impl SchemaValidation for pwt::widget::form::Checkbox {
+    fn set_schema(&mut self, schema: &'static Schema) {
+        if let Schema::Boolean(s) = schema {
+            if let Some(value) = s.default {
+                self.set_default(value);
+            }
+        }
+    }
+}
+
+impl SchemaValidation for pwt::widget::form::Combobox {
+    fn set_schema(&mut self, schema: &'static Schema) {
+        if let 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, Store<AttrValue>)| {
+            schema.parse_simple_value(value)?;
+            Ok(())
+        });
+    }
+}
+
+// Generic implementation for Number<T>
+impl<T> SchemaValidation for pwt::widget::form::Number<T>
+where
+    T: NumberTypeInfo,
+{
+    fn set_schema(&mut self, schema: &'static Schema) {
+        match schema {
+            Schema::Integer(s) => {
+                if let Some(minimum) = s.minimum {
+                    if let Ok(min_value) = T::value_to_number(&Value::Number(minimum.into())) {
+                        self.min = Some(min_value);
+                    }
+                }
+                if let Some(maximum) = s.maximum {
+                    if let Ok(max_value) = T::value_to_number(&Value::Number(maximum.into())) {
+                        self.max = Some(max_value);
+                    }
+                }
+                if let Some(default) = s.default {
+                    self.set_placeholder(default.to_string());
+                }
+            }
+            Schema::Number(s) => {
+                if let Some(minimum) = s.minimum {
+                    if let Ok(min_value) = T::value_to_number(&Value::from(minimum)) {
+                        self.min = Some(min_value);
+                    }
+                }
+                if let Some(maximum) = s.maximum {
+                    if let Ok(max_value) = T::value_to_number(&Value::from(maximum)) {
+                        self.max = Some(max_value);
+                    }
+                }
+                if let Some(default) = s.default {
+                    self.set_placeholder(default.to_string());
+                }
+            }
+            _ => {}
+        }
+
+        self.set_validate(move |value: &T| {
+            let value_str = value.to_string();
+            schema.parse_simple_value(&value_str)?;
+            Ok(())
+        });
+    }
+}
-- 
2.47.3



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


  parent reply	other threads:[~2025-10-09 13:31 UTC|newest]

Thread overview: 6+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2025-10-09 13:31 [yew-devel] [PATCH proxmox{-yew-widget-toolkit, -yew-comp} v3 0/3] use Schema to populate field properties Hannes Laimer
2025-10-09 13:31 ` [yew-devel] [PATCH proxmox-yew-widget-toolkit v3 1/1] form: expose NumberTypeInfo Hannes Laimer
2025-10-10  8:18   ` [yew-devel] applied: " Dietmar Maurer
2025-10-09 13:31 ` Hannes Laimer [this message]
2025-10-10  8:32   ` [yew-devel] [PATCH proxmox-yew-comp v3 1/2] schema_validation: use Schema to populate field properties of number, check- and combobox Dietmar Maurer
2025-10-09 13:31 ` [yew-devel] [PATCH proxmox-yew-comp v3 2/2] schema_validation: add translated description as tooltip to field and checkbox Hannes Laimer

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=20251009133120.425255-3-h.laimer@proxmox.com \
    --to=h.laimer@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