clean up style trait

This commit is contained in:
Mitchell Marino 2024-06-12 16:21:09 -05:00
parent 115ff1abde
commit 64d2631140
6 changed files with 23 additions and 17 deletions

View File

@ -18,7 +18,10 @@ impl<FD: FormToolData, FS: FormStyle> FormBuilder<FD, FS> {
.map(|r_fn| r_fn(&fs, fd)) .map(|r_fn| r_fn(&fs, fd))
.unzip(); .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 validation_cb = move || {
let mut success = true; let mut success = true;
for validation in validation_cbs.iter().flatten() { for validation in validation_cbs.iter().flatten() {

View File

@ -70,6 +70,7 @@ pub trait ValidatedControlData: ControlData {}
/// The data needed to render a interactive control of type `C`. /// The data needed to render a interactive control of type `C`.
pub struct ControlRenderData<FS: FormStyle + ?Sized, C: ?Sized> { pub struct ControlRenderData<FS: FormStyle + ?Sized, C: ?Sized> {
// TODO: Does this need to be boxed? This isn't trait objected any more
pub data: Box<C>, pub data: Box<C>,
pub styles: Vec<FS::StylingAttributes>, pub styles: Vec<FS::StylingAttributes>,
} }

View File

@ -2,8 +2,6 @@ use super::{ControlBuilder, ControlData, ControlRenderData, ValidatedControlData
use crate::{form::FormToolData, form_builder::FormBuilder, styles::FormStyle}; use crate::{form::FormToolData, form_builder::FormBuilder, styles::FormStyle};
use leptos::{Signal, View}; 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)] #[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash, Default)]
pub struct SelectData { pub struct SelectData {
pub(crate) name: String, pub(crate) name: String,

View File

@ -1,7 +1,7 @@
use crate::{ use crate::{
controls::{ controls::{
BuiltControlData, BuiltVanityControlData, ControlBuilder, ControlData, FieldGetter, BuiltControlData, BuiltVanityControlData, ControlBuilder, ControlData, ControlRenderData,
FieldSetter, ParseFn, RenderFn, UnparseFn, ValidationCb, ValidationFn, FieldGetter, FieldSetter, ParseFn, RenderFn, UnparseFn, ValidationCb, ValidationFn,
VanityControlBuilder, VanityControlData, VanityControlBuilder, VanityControlData,
}, },
form::{Form, FormToolData, FormValidator}, form::{Form, FormToolData, FormValidator},
@ -231,7 +231,10 @@ impl<FD: FormToolData, FS: FormStyle> FormBuilder<FD, FS> {
.map(|r_fn| r_fn(&fs, fd)) .map(|r_fn| r_fn(&fs, fd))
.unzip(); .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 on_submit = move |ev: SubmitEvent| {
let mut failed = false; let mut failed = false;
@ -267,7 +270,10 @@ impl<FD: FormToolData, FS: FormStyle> FormBuilder<FD, FS> {
.map(|r_fn| r_fn(&fs, fd)) .map(|r_fn| r_fn(&fs, fd))
.unzip(); .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 on_submit = move |ev: SubmitEvent| {
let mut failed = false; let mut failed = false;

View File

@ -21,8 +21,8 @@ pub struct GridFormStyle;
impl FormStyle for GridFormStyle { impl FormStyle for GridFormStyle {
type StylingAttributes = GridFormStylingAttributes; type StylingAttributes = GridFormStylingAttributes;
fn form_frame(&self, children: View, _styles: Vec<Self::StylingAttributes>) -> View { fn form_frame(&self, form: ControlRenderData<Self, View>) -> View {
view! { <div class="form_grid">{children}</div> }.into_view() view! { <div class="form_grid">{*form.data}</div> }.into_view()
} }
fn heading(&self, control: ControlRenderData<Self, HeadingData>) -> View { fn heading(&self, control: ControlRenderData<Self, HeadingData>) -> View {
@ -111,8 +111,7 @@ impl FormStyle for GridFormStyle {
} }
fn submit(&self, control: ControlRenderData<Self, SubmitData>) -> View { fn submit(&self, control: ControlRenderData<Self, SubmitData>) -> View {
view! { <input type="submit" value=control.data.text class="form_submit"/> } view! { <input type="submit" value=control.data.text class="form_submit"/> }.into_view()
.into_view()
} }
fn text_area( fn text_area(
@ -383,10 +382,9 @@ impl FormStyle for GridFormStyle {
.into_view() .into_view()
} }
// TODO: change this and form frame to use ControlRenderData fn group(&self, group: ControlRenderData<Self, View>) -> View {
fn group(&self, inner: View, styles: Vec<Self::StylingAttributes>) -> View {
let mut width = 12; let mut width = 12;
for style in styles { for style in group.styles {
match style { match style {
GridFormStylingAttributes::Width(w) => width = w, GridFormStylingAttributes::Width(w) => width = w,
} }
@ -394,7 +392,7 @@ impl FormStyle for GridFormStyle {
view! { view! {
<div class="form_group form_grid" style:grid-column=format!("span {}", width)> <div class="form_group form_grid" style:grid-column=format!("span {}", width)>
{inner} {*group.data}
</div> </div>
} }
.into_view() .into_view()

View File

@ -23,7 +23,7 @@ pub trait FormStyle: 'static {
/// ///
/// Do NOT wrap it in an actual `form` element; any /// Do NOT wrap it in an actual `form` element; any
/// wrapping should be done with `div` or similar elements. /// wrapping should be done with `div` or similar elements.
fn form_frame(&self, children: View, style: Vec<Self::StylingAttributes>) -> View; fn form_frame(&self, form: ControlRenderData<Self, View>) -> View;
fn heading(&self, control: ControlRenderData<Self, HeadingData>) -> View; fn heading(&self, control: ControlRenderData<Self, HeadingData>) -> View;
fn hidden( fn hidden(
&self, &self,
@ -91,6 +91,6 @@ pub trait FormStyle: 'static {
fn submit(&self, control: ControlRenderData<Self, SubmitData>) -> View; fn submit(&self, control: ControlRenderData<Self, SubmitData>) -> View;
// TODO: test custom component // TODO: test custom component
fn custom_component(&self, view: View) -> View; fn custom_component(&self, view: View) -> View;
fn group(&self, inner: View, style: Vec<Self::StylingAttributes>) -> View; fn group(&self, group: ControlRenderData<Self, View>) -> View;
fn spacer(&self, control: ControlRenderData<Self, SpacerData>) -> View; fn spacer(&self, control: ControlRenderData<Self, SpacerData>) -> View;
} }