From 64d2631140081c8578002e8f431c471facc0cea1 Mon Sep 17 00:00:00 2001 From: Mitchell M Date: Wed, 12 Jun 2024 16:21:09 -0500 Subject: [PATCH] clean up style trait --- src/controls/group.rs | 5 ++++- src/controls/mod.rs | 1 + src/controls/select.rs | 2 -- src/form_builder.rs | 14 ++++++++++---- src/styles/grid_form.rs | 14 ++++++-------- src/styles/mod.rs | 4 ++-- 6 files changed, 23 insertions(+), 17 deletions(-) diff --git a/src/controls/group.rs b/src/controls/group.rs index 1379205..a2ad21e 100644 --- a/src/controls/group.rs +++ b/src/controls/group.rs @@ -18,7 +18,10 @@ impl FormBuilder { .map(|r_fn| r_fn(&fs, fd)) .unzip(); - let view = fs.group(views.collect_view(), group_builder.styles); + let view = fs.group(super::ControlRenderData { + data: Box::new(views.collect_view()), + styles: group_builder.styles, + }); let validation_cb = move || { let mut success = true; for validation in validation_cbs.iter().flatten() { diff --git a/src/controls/mod.rs b/src/controls/mod.rs index e8b4509..b672936 100644 --- a/src/controls/mod.rs +++ b/src/controls/mod.rs @@ -70,6 +70,7 @@ pub trait ValidatedControlData: ControlData {} /// The data needed to render a interactive control of type `C`. pub struct ControlRenderData { + // TODO: Does this need to be boxed? This isn't trait objected any more pub data: Box, pub styles: Vec, } diff --git a/src/controls/select.rs b/src/controls/select.rs index 09f7577..83a775e 100644 --- a/src/controls/select.rs +++ b/src/controls/select.rs @@ -2,8 +2,6 @@ use super::{ControlBuilder, ControlData, ControlRenderData, ValidatedControlData use crate::{form::FormToolData, form_builder::FormBuilder, styles::FormStyle}; use leptos::{Signal, View}; -// TODO: have an option to have a display string and a value string in the options field - #[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash, Default)] pub struct SelectData { pub(crate) name: String, diff --git a/src/form_builder.rs b/src/form_builder.rs index f649a27..0ad5841 100644 --- a/src/form_builder.rs +++ b/src/form_builder.rs @@ -1,7 +1,7 @@ use crate::{ controls::{ - BuiltControlData, BuiltVanityControlData, ControlBuilder, ControlData, FieldGetter, - FieldSetter, ParseFn, RenderFn, UnparseFn, ValidationCb, ValidationFn, + BuiltControlData, BuiltVanityControlData, ControlBuilder, ControlData, ControlRenderData, + FieldGetter, FieldSetter, ParseFn, RenderFn, UnparseFn, ValidationCb, ValidationFn, VanityControlBuilder, VanityControlData, }, form::{Form, FormToolData, FormValidator}, @@ -231,7 +231,10 @@ impl FormBuilder { .map(|r_fn| r_fn(&fs, fd)) .unzip(); - let elements = fs.form_frame(views.into_view(), self.styles); + let elements = fs.form_frame(ControlRenderData { + data: Box::new(views.into_view()), + styles: self.styles, + }); let on_submit = move |ev: SubmitEvent| { let mut failed = false; @@ -267,7 +270,10 @@ impl FormBuilder { .map(|r_fn| r_fn(&fs, fd)) .unzip(); - let elements = fs.form_frame(views.into_view(), self.styles); + let elements = fs.form_frame(ControlRenderData { + data: Box::new(views.into_view()), + styles: self.styles, + }); let on_submit = move |ev: SubmitEvent| { let mut failed = false; diff --git a/src/styles/grid_form.rs b/src/styles/grid_form.rs index 81ca54f..ab4ebae 100644 --- a/src/styles/grid_form.rs +++ b/src/styles/grid_form.rs @@ -21,8 +21,8 @@ pub struct GridFormStyle; impl FormStyle for GridFormStyle { type StylingAttributes = GridFormStylingAttributes; - fn form_frame(&self, children: View, _styles: Vec) -> View { - view! {
{children}
}.into_view() + fn form_frame(&self, form: ControlRenderData) -> View { + view! {
{*form.data}
}.into_view() } fn heading(&self, control: ControlRenderData) -> View { @@ -111,8 +111,7 @@ impl FormStyle for GridFormStyle { } fn submit(&self, control: ControlRenderData) -> View { - view! { } - .into_view() + view! { }.into_view() } fn text_area( @@ -383,10 +382,9 @@ impl FormStyle for GridFormStyle { .into_view() } - // TODO: change this and form frame to use ControlRenderData - fn group(&self, inner: View, styles: Vec) -> View { + fn group(&self, group: ControlRenderData) -> View { let mut width = 12; - for style in styles { + for style in group.styles { match style { GridFormStylingAttributes::Width(w) => width = w, } @@ -394,7 +392,7 @@ impl FormStyle for GridFormStyle { view! {
- {inner} + {*group.data}
} .into_view() diff --git a/src/styles/mod.rs b/src/styles/mod.rs index 0bd85c8..6e3bd47 100644 --- a/src/styles/mod.rs +++ b/src/styles/mod.rs @@ -23,7 +23,7 @@ pub trait FormStyle: 'static { /// /// Do NOT wrap it in an actual `form` element; any /// wrapping should be done with `div` or similar elements. - fn form_frame(&self, children: View, style: Vec) -> View; + fn form_frame(&self, form: ControlRenderData) -> View; fn heading(&self, control: ControlRenderData) -> View; fn hidden( &self, @@ -91,6 +91,6 @@ pub trait FormStyle: 'static { fn submit(&self, control: ControlRenderData) -> View; // TODO: test custom component fn custom_component(&self, view: View) -> View; - fn group(&self, inner: View, style: Vec) -> View; + fn group(&self, group: ControlRenderData) -> View; fn spacer(&self, control: ControlRenderData) -> View; }