From edc9c4d371af4dc9bc820d6438f7b8fda847c29e Mon Sep 17 00:00:00 2001 From: Mitchell M Date: Fri, 7 Jun 2024 14:42:47 -0500 Subject: [PATCH] form now has a style for the entire form --- grid_form.scss | 10 ++++++++-- src/controls/group.rs | 2 +- src/form_builder.rs | 13 +++++++++++-- src/styles/grid_form.rs | 18 +++++++++++++----- src/styles/mod.rs | 4 ++-- 5 files changed, 35 insertions(+), 12 deletions(-) diff --git a/grid_form.scss b/grid_form.scss index 7af6f06..f4baf6f 100644 --- a/grid_form.scss +++ b/grid_form.scss @@ -30,7 +30,6 @@ background-color: #0295f744; border-radius: 25px; padding: 20px; - grid-column: 1 / -1; } .form_label { @@ -87,12 +86,19 @@ color: #fff; font-weight: bold; cursor: pointer; - margin: 0 auto; + margin: 0 auto auto auto; padding: 0.75rem 1.25rem; font-size: 1rem; appearance: none; min-height: 40px; } + +.form_submit { + @extend .form_button; + @extend .col-span-full; + margin: 0 auto; +} + .form_submit:hover { background-color: #005fb3; } diff --git a/src/controls/group.rs b/src/controls/group.rs index ef7a886..1832f41 100644 --- a/src/controls/group.rs +++ b/src/controls/group.rs @@ -19,7 +19,7 @@ impl FormBuilder { .map(|r_fn| r_fn(&fs, fd)) .unzip(); - let view = fs.group(views.collect_view()); + let view = fs.group(views.collect_view(), group_builder.styles); let validation_cb = move || { let mut success = true; for validation in validation_cbs.iter().flatten() { diff --git a/src/form_builder.rs b/src/form_builder.rs index 763fac9..10b952c 100644 --- a/src/form_builder.rs +++ b/src/form_builder.rs @@ -28,6 +28,8 @@ pub struct FormBuilder { pub(crate) validations: Vec>>, /// The list of functions that will render the form. pub(crate) render_fns: Vec>>, + /// The list of styling attributes applied on the form level + pub(crate) styles: Vec, } impl FormBuilder { @@ -39,6 +41,7 @@ impl FormBuilder { fs: form_style, validations: Vec::new(), render_fns: Vec::new(), + styles: Vec::new(), } } @@ -48,9 +51,15 @@ impl FormBuilder { fs, validations: Vec::new(), render_fns: Vec::new(), + styles: Vec::new(), } } + pub fn style(mut self, style: FS::StylingAttributes) -> Self { + self.styles.push(style); + self + } + pub(crate) fn new_vanity( mut self, builder: impl Fn(VanityControlBuilder) -> VanityControlBuilder, @@ -235,7 +244,7 @@ impl FormBuilder { .map(|r_fn| r_fn(&self.fs, self.fd)) .unzip(); - let elements = self.fs.form_frame(views.into_view()); + let elements = self.fs.form_frame(views.into_view(), self.styles); let on_submit = move |ev: SubmitEvent| { let mut failed = false; @@ -269,7 +278,7 @@ impl FormBuilder { .map(|r_fn| r_fn(&self.fs, self.fd)) .unzip(); - let elements = self.fs.form_frame(views.into_view()); + let elements = self.fs.form_frame(views.into_view(), 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 5949138..3cefd8c 100644 --- a/src/styles/grid_form.rs +++ b/src/styles/grid_form.rs @@ -18,7 +18,7 @@ pub struct GridFormStyle; impl FormStyle for GridFormStyle { type StylingAttributes = GridFormStylingAttributes; - fn form_frame(&self, children: View) -> View { + fn form_frame(&self, children: View, _styles: Vec) -> View { view! {
{children}
}.into_view() } @@ -113,7 +113,7 @@ impl FormStyle for GridFormStyle { fn submit(&self, control: ControlRenderData) -> View { view! { - + } .into_view() } @@ -372,16 +372,24 @@ impl FormStyle for GridFormStyle { }; view! { - } .into_view() } - fn group(&self, inner: View) -> View { + // TODO: change this and form frame to use ControlRenderData + fn group(&self, inner: View, styles: Vec) -> View { + let mut width = 12; + for style in styles { + match style { + GridFormStylingAttributes::Width(w) => width = w, + } + } + view! { -
+
{inner}
} diff --git a/src/styles/mod.rs b/src/styles/mod.rs index 7d7d9a7..0b0e574 100644 --- a/src/styles/mod.rs +++ b/src/styles/mod.rs @@ -22,7 +22,7 @@ pub trait FormStyle: Default + '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) -> View; + fn form_frame(&self, children: View, styles: Vec) -> View; fn heading(&self, control: ControlRenderData) -> View; fn hidden( &self, @@ -87,5 +87,5 @@ pub trait FormStyle: Default + 'static { // TODO: test custom component fn custom_component(&self, view: View) -> View; // TODO: test group - fn group(&self, inner: View) -> View; + fn group(&self, inner: View, styles: Vec) -> View; }