From 1dc676cc370b97ee017c31af1f356793164a001c Mon Sep 17 00:00:00 2001 From: Mitchell M Date: Mon, 10 Jun 2024 13:54:37 -0500 Subject: [PATCH] add spacer --- src/controls/custom.rs | 22 ++++++++++++++++++++++ src/controls/mod.rs | 13 +++++++++++-- src/controls/spacer.rs | 37 +++++++++++++++++++++++++++++++++++++ src/controls/submit.rs | 4 ++-- src/styles/grid_form.rs | 18 ++++++++++++++++-- src/styles/mod.rs | 6 ++++-- 6 files changed, 92 insertions(+), 8 deletions(-) create mode 100644 src/controls/custom.rs create mode 100644 src/controls/spacer.rs diff --git a/src/controls/custom.rs b/src/controls/custom.rs new file mode 100644 index 0000000..fc867b9 --- /dev/null +++ b/src/controls/custom.rs @@ -0,0 +1,22 @@ +use super::{ControlBuilder, ControlData}; +use crate::{styles::FormStyle, FormBuilder, FormToolData}; + +impl FormBuilder { + pub fn custom( + mut self, + control_data: CC, + builder: impl Fn(ControlBuilder) -> ControlBuilder, + ) -> Self { + let control_builder = ControlBuilder::new(control_data); + let control = builder(control_builder); + self.add_control(control); + self + } + + pub fn custom_default( + self, + builder: impl Fn(ControlBuilder) -> ControlBuilder, + ) -> Self { + self.new_control(builder) + } +} diff --git a/src/controls/mod.rs b/src/controls/mod.rs index cacf492..751cf82 100644 --- a/src/controls/mod.rs +++ b/src/controls/mod.rs @@ -4,6 +4,7 @@ use std::{fmt::Display, rc::Rc, str::FromStr}; pub mod button; pub mod checkbox; +pub mod custom; pub mod group; pub mod heading; pub mod hidden; @@ -11,6 +12,7 @@ pub mod output; pub mod radio_buttons; pub mod select; pub mod slider; +pub mod spacer; pub mod stepper; pub mod submit; pub mod text_area; @@ -52,6 +54,7 @@ pub trait GetterVanityControlData: VanityControlData {} /// A trait for the data needed to render an interactive control. pub trait ControlData: 'static { + /// This is the data type returned by this control. type ReturnType: Clone; /// Builds the control, returning the [`View`] that was built. @@ -103,6 +106,12 @@ impl VanityControlBuilder getter: self.getter, } } + + /// Adds a styling attribute to this control. + pub fn style(mut self, attribute: FS::StylingAttributes) -> Self { + self.style_attributes.push(attribute); + self + } } impl VanityControlBuilder { @@ -159,7 +168,7 @@ pub struct ControlBuilder pub(crate) unparse_fn: Option>>, pub(crate) validation_fn: Option>>, pub(crate) style_attributes: Vec, - pub(crate) data: C, + pub data: C, } impl ControlBuilder { @@ -312,7 +321,7 @@ where /// The parse and unparse functions define how to turn what the user /// types in the form into what is stored in the form data struct and /// vice versa. - pub fn parse_trimmed_string(mut self) -> Self { + pub fn parse_trimmed(mut self) -> Self { self.parse_fn = Some(Box::new(|control_return_value| { control_return_value .trim() diff --git a/src/controls/spacer.rs b/src/controls/spacer.rs new file mode 100644 index 0000000..e65e864 --- /dev/null +++ b/src/controls/spacer.rs @@ -0,0 +1,37 @@ +use leptos::{prelude::Signal, View}; + +use super::{ControlRenderData, VanityControlBuilder, VanityControlData}; +use crate::{form::FormToolData, form_builder::FormBuilder, styles::FormStyle}; + +#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash, Default)] +pub struct SpacerData { + pub(crate) height: Option, +} + +impl VanityControlData for SpacerData { + fn build_control( + fs: &FS, + control: ControlRenderData, + _value_getter: Option>, + ) -> View { + fs.spacer(control) + } +} + +impl FormBuilder { + pub fn spacer( + self, + builder: impl Fn( + VanityControlBuilder, + ) -> VanityControlBuilder, + ) -> Self { + self.new_vanity(builder) + } +} + +impl VanityControlBuilder { + pub fn height(mut self, height: impl ToString) -> Self { + self.data.height = Some(height.to_string()); + self + } +} diff --git a/src/controls/submit.rs b/src/controls/submit.rs index 35ee7bc..487799e 100644 --- a/src/controls/submit.rs +++ b/src/controls/submit.rs @@ -1,4 +1,4 @@ -use leptos::View; +use leptos::{prelude::Signal, View}; use super::{ControlRenderData, VanityControlBuilder, VanityControlData}; use crate::{form::FormToolData, form_builder::FormBuilder, styles::FormStyle}; @@ -12,7 +12,7 @@ impl VanityControlData for SubmitData { fn build_control( fs: &FS, control: ControlRenderData, - _value_getter: Option>, + _value_getter: Option>, ) -> View { fs.submit(control) } diff --git a/src/styles/grid_form.rs b/src/styles/grid_form.rs index 3cefd8c..dc6cf9e 100644 --- a/src/styles/grid_form.rs +++ b/src/styles/grid_form.rs @@ -1,8 +1,8 @@ use super::FormStyle; use crate::controls::{ checkbox::CheckboxData, heading::HeadingData, hidden::HiddenData, output::OutputData, - select::SelectData, submit::SubmitData, text_area::TextAreaData, text_input::TextInputData, - ControlData, ControlRenderData, + select::SelectData, spacer::SpacerData, submit::SubmitData, text_area::TextAreaData, + text_input::TextInputData, ControlData, ControlRenderData, }; use leptos::*; use std::rc::Rc; @@ -395,4 +395,18 @@ impl FormStyle for GridFormStyle { } .into_view() } + + fn spacer(&self, control: ControlRenderData) -> View { + let mut width = 12; + for style in control.style { + match style { + GridFormStylingAttributes::Width(w) => width = w, + } + } + + view! { +
+ } + .into_view() + } } diff --git a/src/styles/mod.rs b/src/styles/mod.rs index 25896f1..36383f6 100644 --- a/src/styles/mod.rs +++ b/src/styles/mod.rs @@ -1,10 +1,11 @@ mod grid_form; + use crate::{ controls::{ button::ButtonData, checkbox::CheckboxData, heading::HeadingData, hidden::HiddenData, output::OutputData, radio_buttons::RadioButtonsData, select::SelectData, - slider::SliderData, stepper::StepperData, submit::SubmitData, text_area::TextAreaData, - text_input::TextInputData, ControlData, ControlRenderData, + slider::SliderData, spacer::SpacerData, stepper::StepperData, submit::SubmitData, + text_area::TextAreaData, text_input::TextInputData, ControlData, ControlRenderData, }, FormToolData, }; @@ -87,4 +88,5 @@ pub trait FormStyle: Default + 'static { // TODO: test custom component fn custom_component(&self, view: View) -> View; fn group(&self, inner: View, style: Vec) -> View; + fn spacer(&self, control: ControlRenderData) -> View; }