From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from firstgate.proxmox.com (firstgate.proxmox.com [IPv6:2a01:7e0:0:424::9]) by lore.proxmox.com (Postfix) with ESMTPS id 4E2611FF165 for ; Thu, 9 Oct 2025 13:25:03 +0200 (CEST) Received: from firstgate.proxmox.com (localhost [127.0.0.1]) by firstgate.proxmox.com (Proxmox) with ESMTP id CE9621D141; Thu, 9 Oct 2025 13:25:09 +0200 (CEST) From: Hannes Laimer To: yew-devel@lists.proxmox.com Date: Thu, 9 Oct 2025 13:25:02 +0200 Message-ID: <20251009112502.312917-3-h.laimer@proxmox.com> X-Mailer: git-send-email 2.47.3 In-Reply-To: <20251009112502.312917-1-h.laimer@proxmox.com> References: <20251009112502.312917-1-h.laimer@proxmox.com> MIME-Version: 1.0 X-Bm-Milter-Handled: 55990f41-d878-4baa-be0a-ee34c49e34d2 X-Bm-Transport-Timestamp: 1760009073821 X-SPAM-LEVEL: Spam detection results: 0 AWL 0.044 Adjusted score from AWL reputation of From: address BAYES_00 -1.9 Bayes spam probability is 0 to 1% DMARC_MISSING 0.1 Missing DMARC policy KAM_DMARC_STATUS 0.01 Test Rule for DKIM or SPF Failure with Strict Alignment SPF_HELO_NONE 0.001 SPF: HELO does not publish an SPF Record SPF_PASS -0.001 SPF: sender matches SPF record Subject: [yew-devel] [PATCH yew-widget-toolkit v2 2/2] form: add schema() builder funtion to checkbox, number and field X-BeenThere: yew-devel@lists.proxmox.com X-Mailman-Version: 2.1.29 Precedence: list List-Id: Yew framework devel list at Proxmox List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Reply-To: Yew framework devel list at Proxmox Content-Type: text/plain; charset="us-ascii" Content-Transfer-Encoding: 7bit Errors-To: yew-devel-bounces@lists.proxmox.com Sender: "yew-devel" This will use the passed schema to populate properties of the form field depending on what type of schema is passed. The idea is to only have things like validation, help texts/description or selection items specified once. Namely in the Schema definition. These .schema() builder functions are guarded by the proxmox-schema feature. Signed-off-by: Hannes Laimer --- Cargo.toml | 5 +++ src/widget/form/checkbox.rs | 20 +++++++++++ src/widget/form/combobox.rs | 45 +++++++++++++++--------- src/widget/form/field.rs | 28 +++++++++++++++ src/widget/form/number.rs | 69 ++++++++++++++++++++++++++++++++++--- 5 files changed, 146 insertions(+), 21 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/checkbox.rs b/src/widget/form/checkbox.rs index 6b2cf4a..833cd44 100644 --- a/src/widget/form/checkbox.rs +++ b/src/widget/form/checkbox.rs @@ -4,6 +4,9 @@ use serde_json::Value; use yew::html::{IntoEventCallback, IntoPropValue}; use yew::prelude::*; +#[cfg(feature = "proxmox-schema")] +use proxmox_schema::Schema; + use pwt_macros::{builder, widget}; use super::{ @@ -107,6 +110,23 @@ impl Checkbox { yew::props!(Self {}) } + /// Builder style method to set the schema, will set tooltip and default value + #[cfg(feature = "proxmox-schema")] + pub fn schema(mut self, schema: &'static Schema) -> Self { + self.set_schema(schema); + self + } + + #[cfg(feature = "proxmox-schema")] + pub fn set_schema(&mut self, schema: &'static Schema) { + if let proxmox_schema::Schema::Boolean(s) = schema { + self.set_tip(s.description); + if let Some(value) = s.default { + self.set_default(value); + } + } + } + /// Builder style method to set the validate callback pub fn validate(mut self, validate: impl IntoValidateFn) -> Self { self.set_validate(validate); diff --git a/src/widget/form/combobox.rs b/src/widget/form/combobox.rs index db4505f..208df53 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}; @@ -172,21 +172,32 @@ 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 schema, will set validation, items and placeholder + #[cfg(feature = "proxmox-schema")] + pub fn schema(mut self, schema: &'static Schema) -> Self { + self.set_schema(schema); + self + } + + #[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 = 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, right: bool) -> Self { diff --git a/src/widget/form/field.rs b/src/widget/form/field.rs index 4a37d12..4b69a94 100644 --- a/src/widget/form/field.rs +++ b/src/widget/form/field.rs @@ -8,12 +8,16 @@ use web_sys::HtmlInputElement; use yew::html::{IntoEventCallback, IntoPropValue}; use yew::prelude::*; +#[cfg(feature = "proxmox-schema")] +use proxmox_schema::Schema; + use pwt_macros::{builder, widget}; use super::{ IntoValidateFn, ManagedField, ManagedFieldContext, ManagedFieldMaster, ManagedFieldState, ValidateFn, }; +use crate::props::FieldBuilder as _; use crate::props::{ ContainerBuilder, EventSubscriber, IntoVTag, WidgetBuilder, WidgetStyleBuilder, }; @@ -234,6 +238,30 @@ impl Field { self } + /// Builder style method to set the schema, will set the placeholder, tooltip and validation + #[cfg(feature = "proxmox-schema")] + pub fn schema(mut self, schema: &'static Schema) -> Self { + self.set_schema(schema); + self + } + + #[cfg(feature = "proxmox-schema")] + pub fn set_schema(&mut self, schema: &'static proxmox_schema::Schema) { + if let proxmox_schema::Schema::String(s) = schema { + // if s.format is a pattern we could include the regex, may be helpful occationally, + // but not sure. For things like IPs for example we really don't want that... + self.set_tip(s.description.to_string()); + if let Some(default) = s.default { + self.set_placeholder(default.to_string()); + } + } + + self.set_validate(move |value: &String| { + schema.parse_simple_value(value)?; + Ok(()) + }); + } + /// Builder style method to set the validate callback pub fn validate(mut self, validate: impl IntoValidateFn) -> Self { self.set_validate(validate); diff --git a/src/widget/form/number.rs b/src/widget/form/number.rs index ad1a3cd..33c5d93 100644 --- a/src/widget/form/number.rs +++ b/src/widget/form/number.rs @@ -10,14 +10,16 @@ use web_sys::HtmlInputElement; use yew::html::{IntoEventCallback, IntoPropValue}; use yew::prelude::*; -use pwt_macros::{builder, widget}; +#[cfg(feature = "proxmox-schema")] +use proxmox_schema::Schema; use super::{ IntoValidateFn, ManagedField, ManagedFieldContext, ManagedFieldMaster, ManagedFieldState, ValidateFn, }; -use crate::props::{ContainerBuilder, EventSubscriber, IntoVTag, WidgetBuilder}; +use crate::props::{ContainerBuilder, EventSubscriber, FieldBuilder as _, IntoVTag, WidgetBuilder}; use crate::widget::{Column, Container, Input, Tooltip}; +use pwt_macros::{builder, widget}; use crate::tr; @@ -397,6 +399,58 @@ impl Number { pub fn set_validate(&mut self, validate: impl IntoValidateFn) { self.validate = validate.into_validate_fn(); } + + /// Builder style method to set the schema, will set min, max, default and validation + #[cfg(feature = "proxmox-schema")] + pub fn schema(mut self, schema: &'static Schema) -> Self { + self.set_schema(schema); + self + } + + /// Method to set the validation schema, min, max, default and placeholder + #[cfg(feature = "proxmox-schema")] + pub fn set_schema(&mut self, schema: &'static Schema) { + if let proxmox_schema::Schema::Integer(s) = schema { + self.set_tip(s.description); + 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()); + } + } + + if let proxmox_schema::Schema::Number(s) = schema { + self.set_tip(s.description); + 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(()) + }); + } } pub enum Msg { @@ -711,8 +765,15 @@ impl ManagedField for NumberField { ), ); - if let Err(msg) = &validation_result { - input_container.set_tip(msg.clone()) + match validation_result { + Err(msg) => input_container.set_tip(msg.clone()), + Ok(_) => { + if let Some(tip) = &props.tip { + if !disabled { + input_container.set_tip(tip.clone()) + } + } + } } input_container.into() -- 2.47.3 _______________________________________________ yew-devel mailing list yew-devel@lists.proxmox.com https://lists.proxmox.com/cgi-bin/mailman/listinfo/yew-devel