diff --git a/src/controls/button.rs b/src/controls/button.rs index cc317fa..bac3328 100644 --- a/src/controls/button.rs +++ b/src/controls/button.rs @@ -1,4 +1,4 @@ -use super::{BuilderFn, ControlRenderData, ShowWhenFn}; +use super::{BuilderCxFn, BuilderFn, ControlRenderData, ShowWhenFn}; use crate::{form::FormToolData, form_builder::FormBuilder, styles::FormStyle}; use leptos::view; use leptos::RwSignal; @@ -31,10 +31,19 @@ impl Clone for ButtonData { } impl FormBuilder { - pub fn button(mut self, builder: impl BuilderFn, FD::Context>) -> Self { + pub fn button(self, builder: impl BuilderFn>) -> Self { + let button_builder = ButtonBuilder::new(); + let control = builder(button_builder); + self.button_helper(control) + } + + pub fn button_cx(self, builder: impl BuilderCxFn, FD::Context>) -> Self { let button_builder = ButtonBuilder::new(); let control = builder(button_builder, self.cx.clone()); + self.button_helper(control) + } + fn button_helper(mut self, control: ButtonBuilder) -> Self { let render_data = ControlRenderData { data: control.data, styles: control.styles, diff --git a/src/controls/checkbox.rs b/src/controls/checkbox.rs index 8e97e69..f7b91f6 100644 --- a/src/controls/checkbox.rs +++ b/src/controls/checkbox.rs @@ -1,4 +1,4 @@ -use super::{BuilderFn, ControlBuilder, ControlData, ControlRenderData}; +use super::{BuilderCxFn, BuilderFn, ControlBuilder, ControlData, ControlRenderData}; use crate::{form::FormToolData, form_builder::FormBuilder, styles::FormStyle}; use leptos::{Signal, View}; use std::rc::Rc; @@ -26,10 +26,17 @@ impl ControlData for CheckboxData { impl FormBuilder { pub fn checkbox( self, - builder: impl BuilderFn, FD::Context>, + builder: impl BuilderFn>, ) -> Self { self.new_control(builder) } + + pub fn checkbox_cx( + self, + builder: impl BuilderCxFn, FD::Context>, + ) -> Self { + self.new_control_cx(builder) + } } impl ControlBuilder { diff --git a/src/controls/custom.rs b/src/controls/custom.rs index 6555cba..44f922e 100644 --- a/src/controls/custom.rs +++ b/src/controls/custom.rs @@ -1,11 +1,11 @@ -use super::{BuilderFn, ControlBuilder, ControlData}; +use super::{BuilderCxFn, BuilderFn, ControlBuilder, ControlData}; use crate::{FormBuilder, FormToolData}; impl FormBuilder { pub fn custom( mut self, control_data: CC, - builder: impl Fn(ControlBuilder) -> ControlBuilder, + builder: impl BuilderFn>, ) -> Self { let control_builder = ControlBuilder::new(control_data); let control = builder(control_builder); @@ -13,10 +13,28 @@ impl FormBuilder { self } + pub fn custom_cx( + mut self, + control_data: CC, + builder: impl BuilderCxFn, FD::Context>, + ) -> Self { + let control_builder = ControlBuilder::new(control_data); + let control = builder(control_builder, self.cx.clone()); + self.add_control(control); + self + } + pub fn custom_default( self, - builder: impl BuilderFn, FD::Context>, + builder: impl BuilderFn>, ) -> Self { self.new_control(builder) } + + pub fn custom_default_cx( + self, + builder: impl BuilderCxFn, FD::Context>, + ) -> Self { + self.new_control_cx(builder) + } } diff --git a/src/controls/heading.rs b/src/controls/heading.rs index 680cf61..1586339 100644 --- a/src/controls/heading.rs +++ b/src/controls/heading.rs @@ -1,4 +1,4 @@ -use super::{BuilderFn, ControlRenderData, VanityControlBuilder, VanityControlData}; +use super::{BuilderCxFn, BuilderFn, ControlRenderData, VanityControlBuilder, VanityControlData}; use crate::{form::FormToolData, form_builder::FormBuilder, styles::FormStyle}; use leptos::View; use std::rc::Rc; @@ -19,12 +19,16 @@ impl VanityControlData for HeadingData { } impl FormBuilder { - pub fn heading( - self, - builder: impl BuilderFn, FD::Context>, - ) -> Self { + pub fn heading(self, builder: impl BuilderFn>) -> Self { self.new_vanity(builder) } + + pub fn heading_cx( + self, + builder: impl BuilderCxFn, FD::Context>, + ) -> Self { + self.new_vanity_cx(builder) + } } impl VanityControlBuilder { diff --git a/src/controls/hidden.rs b/src/controls/hidden.rs index ec774f2..49139f3 100644 --- a/src/controls/hidden.rs +++ b/src/controls/hidden.rs @@ -1,5 +1,6 @@ use super::{ - BuilderFn, ControlRenderData, GetterVanityControlData, VanityControlBuilder, VanityControlData, + BuilderCxFn, BuilderFn, ControlRenderData, GetterVanityControlData, VanityControlBuilder, + VanityControlData, }; use crate::{form::FormToolData, form_builder::FormBuilder, styles::FormStyle}; use leptos::{Signal, View}; @@ -22,12 +23,16 @@ impl VanityControlData for HiddenData { impl GetterVanityControlData for HiddenData {} impl FormBuilder { - pub fn hidden( - self, - builder: impl BuilderFn, FD::Context>, - ) -> Self { + pub fn hidden(self, builder: impl BuilderFn>) -> Self { self.new_vanity(builder) } + + pub fn hidden_cx( + self, + builder: impl BuilderCxFn, FD::Context>, + ) -> Self { + self.new_vanity_cx(builder) + } } impl VanityControlBuilder { diff --git a/src/controls/mod.rs b/src/controls/mod.rs index 544cb3c..0e04c0f 100644 --- a/src/controls/mod.rs +++ b/src/controls/mod.rs @@ -18,7 +18,8 @@ pub mod submit; pub mod text_area; pub mod text_input; -pub trait BuilderFn: Fn(B, Rc) -> B {} +pub trait BuilderFn: Fn(B) -> B {} +pub trait BuilderCxFn: Fn(B, Rc) -> B {} pub trait ValidationFn: Fn(&FD) -> Result<(), String> + 'static {} pub trait ValidationCb: Fn() -> bool + 'static {} pub trait ParseFn: Fn(CR) -> Result + 'static {} @@ -32,7 +33,8 @@ pub trait RenderFn: } // implement the traits for all valid types -impl BuilderFn for T where T: Fn(B, Rc) -> B {} +impl BuilderFn for T where T: Fn(B) -> B {} +impl BuilderCxFn for T where T: Fn(B, Rc) -> B {} impl ValidationFn for T where T: Fn(&FDT) -> Result<(), String> + 'static {} impl ValidationCb for T where T: Fn() -> bool + 'static {} impl ParseFn for F where F: Fn(CR) -> Result + 'static {} diff --git a/src/controls/output.rs b/src/controls/output.rs index 7962e16..c8f001c 100644 --- a/src/controls/output.rs +++ b/src/controls/output.rs @@ -1,5 +1,6 @@ use super::{ - BuilderFn, ControlRenderData, GetterVanityControlData, VanityControlBuilder, VanityControlData, + BuilderCxFn, BuilderFn, ControlRenderData, GetterVanityControlData, VanityControlBuilder, + VanityControlData, }; use crate::{form::FormToolData, form_builder::FormBuilder, styles::FormStyle}; use leptos::{Signal, View}; @@ -20,10 +21,14 @@ impl VanityControlData for OutputData { impl GetterVanityControlData for OutputData {} impl FormBuilder { - pub fn output( - self, - builder: impl BuilderFn, FD::Context>, - ) -> Self { + pub fn output(self, builder: impl BuilderFn>) -> Self { self.new_vanity(builder) } + + pub fn output_cx( + self, + builder: impl BuilderCxFn, FD::Context>, + ) -> Self { + self.new_vanity_cx(builder) + } } diff --git a/src/controls/radio_buttons.rs b/src/controls/radio_buttons.rs index fcc7d05..b2b0611 100644 --- a/src/controls/radio_buttons.rs +++ b/src/controls/radio_buttons.rs @@ -1,4 +1,6 @@ -use super::{BuilderFn, ControlBuilder, ControlData, ControlRenderData, ValidatedControlData}; +use super::{ + BuilderCxFn, BuilderFn, ControlBuilder, ControlData, ControlRenderData, ValidatedControlData, +}; use crate::{form::FormToolData, form_builder::FormBuilder, styles::FormStyle}; use leptos::{Signal, View}; use std::rc::Rc; @@ -28,10 +30,17 @@ impl ValidatedControlData for RadioButtonsData {} impl FormBuilder { pub fn radio_buttons( self, - builder: impl BuilderFn, FD::Context>, + builder: impl BuilderFn>, ) -> Self { self.new_control(builder) } + + pub fn radio_buttons_cx( + self, + builder: impl BuilderCxFn, FD::Context>, + ) -> Self { + self.new_control_cx(builder) + } } impl ControlBuilder { diff --git a/src/controls/select.rs b/src/controls/select.rs index 43e437c..ee68c62 100644 --- a/src/controls/select.rs +++ b/src/controls/select.rs @@ -1,4 +1,6 @@ -use super::{BuilderFn, ControlBuilder, ControlData, ControlRenderData, ValidatedControlData}; +use super::{ + BuilderCxFn, BuilderFn, ControlBuilder, ControlData, ControlRenderData, ValidatedControlData, +}; use crate::{form::FormToolData, form_builder::FormBuilder, styles::FormStyle}; use leptos::{Signal, View}; use std::rc::Rc; @@ -31,10 +33,16 @@ impl ValidatedControlData for SelectData {} impl FormBuilder { pub fn select( self, - builder: impl BuilderFn, FD::Context>, + builder: impl BuilderFn>, ) -> Self { self.new_control(builder) } + pub fn select_cx( + self, + builder: impl BuilderCxFn, FD::Context>, + ) -> Self { + self.new_control_cx(builder) + } } impl ControlBuilder { diff --git a/src/controls/slider.rs b/src/controls/slider.rs index 4a79b57..3b3ad95 100644 --- a/src/controls/slider.rs +++ b/src/controls/slider.rs @@ -1,4 +1,4 @@ -use super::{BuilderFn, ControlBuilder, ControlData, ControlRenderData}; +use super::{BuilderCxFn, BuilderFn, ControlBuilder, ControlData, ControlRenderData}; use crate::{form::FormToolData, form_builder::FormBuilder, styles::FormStyle}; use leptos::{Signal, View}; use std::{ops::RangeInclusive, rc::Rc}; @@ -39,10 +39,17 @@ impl ControlData for SliderData { impl FormBuilder { pub fn slider( self, - builder: impl BuilderFn, FD::Context>, + builder: impl BuilderFn>, ) -> Self { self.new_control(builder) } + + pub fn slider_cx( + self, + builder: impl BuilderCxFn, FD::Context>, + ) -> Self { + self.new_control_cx(builder) + } } impl ControlBuilder { diff --git a/src/controls/spacer.rs b/src/controls/spacer.rs index b5b800d..fe7d50a 100644 --- a/src/controls/spacer.rs +++ b/src/controls/spacer.rs @@ -1,4 +1,4 @@ -use super::{BuilderFn, ControlRenderData, VanityControlBuilder, VanityControlData}; +use super::{BuilderCxFn, BuilderFn, ControlRenderData, VanityControlBuilder, VanityControlData}; use crate::{form::FormToolData, form_builder::FormBuilder, styles::FormStyle}; use leptos::{prelude::Signal, View}; use std::rc::Rc; @@ -19,12 +19,16 @@ impl VanityControlData for SpacerData { } impl FormBuilder { - pub fn spacer( - self, - builder: impl BuilderFn, FD::Context>, - ) -> Self { + pub fn spacer(self, builder: impl BuilderFn>) -> Self { self.new_vanity(builder) } + + pub fn spacer_cx( + self, + builder: impl BuilderCxFn, FD::Context>, + ) -> Self { + self.new_vanity_cx(builder) + } } impl VanityControlBuilder { diff --git a/src/controls/stepper.rs b/src/controls/stepper.rs index 401bd5a..aa83c56 100644 --- a/src/controls/stepper.rs +++ b/src/controls/stepper.rs @@ -1,4 +1,6 @@ -use super::{BuilderFn, ControlBuilder, ControlData, ControlRenderData, ValidatedControlData}; +use super::{ + BuilderCxFn, BuilderFn, ControlBuilder, ControlData, ControlRenderData, ValidatedControlData, +}; use crate::{form::FormToolData, form_builder::FormBuilder, styles::FormStyle}; use leptos::{Signal, View}; use std::{ops::RangeInclusive, rc::Rc}; @@ -30,10 +32,17 @@ impl ValidatedControlData for StepperData {} impl FormBuilder { pub fn stepper( self, - builder: impl BuilderFn, FD::Context>, + builder: impl BuilderFn>, ) -> Self { self.new_control(builder) } + + pub fn stepper_cx( + self, + builder: impl BuilderCxFn, FD::Context>, + ) -> Self { + self.new_control_cx(builder) + } } impl ControlBuilder { diff --git a/src/controls/submit.rs b/src/controls/submit.rs index f429de8..78b3c0a 100644 --- a/src/controls/submit.rs +++ b/src/controls/submit.rs @@ -1,4 +1,4 @@ -use super::{BuilderFn, ControlRenderData, VanityControlBuilder, VanityControlData}; +use super::{BuilderCxFn, BuilderFn, ControlRenderData, VanityControlBuilder, VanityControlData}; use crate::{form::FormToolData, form_builder::FormBuilder, styles::FormStyle}; use leptos::{prelude::Signal, View}; use std::rc::Rc; @@ -19,12 +19,16 @@ impl VanityControlData for SubmitData { } impl FormBuilder { - pub fn submit( - self, - builder: impl BuilderFn, FD::Context>, - ) -> Self { + pub fn submit(self, builder: impl BuilderFn>) -> Self { self.new_vanity(builder) } + + pub fn submit_cx( + self, + builder: impl BuilderCxFn, FD::Context>, + ) -> Self { + self.new_vanity_cx(builder) + } } impl VanityControlBuilder { diff --git a/src/controls/text_area.rs b/src/controls/text_area.rs index 34784ec..bd0376b 100644 --- a/src/controls/text_area.rs +++ b/src/controls/text_area.rs @@ -1,4 +1,6 @@ -use super::{BuilderFn, ControlBuilder, ControlData, ControlRenderData, ValidatedControlData}; +use super::{ + BuilderCxFn, BuilderFn, ControlBuilder, ControlData, ControlRenderData, ValidatedControlData, +}; use crate::{form::FormToolData, form_builder::FormBuilder, styles::FormStyle}; use leptos::{Signal, View}; use std::rc::Rc; @@ -27,10 +29,17 @@ impl ValidatedControlData for TextAreaData {} impl FormBuilder { pub fn text_area( self, - builder: impl BuilderFn, FD::Context>, + builder: impl BuilderFn>, ) -> Self { self.new_control(builder) } + + pub fn text_area_cx( + self, + builder: impl BuilderCxFn, FD::Context>, + ) -> Self { + self.new_control_cx(builder) + } } impl ControlBuilder { diff --git a/src/controls/text_input.rs b/src/controls/text_input.rs index ea8ae34..bfc0c8e 100644 --- a/src/controls/text_input.rs +++ b/src/controls/text_input.rs @@ -1,8 +1,9 @@ -use std::rc::Rc; - -use super::{BuilderFn, ControlBuilder, ControlData, ControlRenderData, ValidatedControlData}; +use super::{ + BuilderCxFn, BuilderFn, ControlBuilder, ControlData, ControlRenderData, ValidatedControlData, +}; use crate::{form::FormToolData, form_builder::FormBuilder, styles::FormStyle}; use leptos::{Signal, View}; +use std::rc::Rc; #[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)] pub struct TextInputData { @@ -43,10 +44,17 @@ impl ValidatedControlData for TextInputData {} impl FormBuilder { pub fn text_input( self, - builder: impl BuilderFn, FD::Context>, + builder: impl BuilderFn>, ) -> Self { self.new_control(builder) } + + pub fn text_input_cx( + self, + builder: impl BuilderCxFn, FD::Context>, + ) -> Self { + self.new_control_cx(builder) + } } impl ControlBuilder { diff --git a/src/form_builder.rs b/src/form_builder.rs index 3ce29d2..a22934b 100644 --- a/src/form_builder.rs +++ b/src/form_builder.rs @@ -1,7 +1,7 @@ use crate::{ controls::{ - BuilderFn, BuiltControlData, BuiltVanityControlData, ControlBuilder, ControlData, - ControlRenderData, FieldSetter, ParseFn, RenderFn, ValidationCb, ValidationFn, + BuilderCxFn, BuilderFn, BuiltControlData, BuiltVanityControlData, ControlBuilder, + ControlData, ControlRenderData, FieldSetter, ParseFn, RenderFn, ValidationCb, ValidationFn, VanityControlBuilder, VanityControlData, }, form::{Form, FormToolData, FormValidator}, @@ -63,7 +63,17 @@ impl FormBuilder { pub(crate) fn new_vanity( mut self, - builder: impl BuilderFn, FD::Context>, + builder: impl BuilderFn>, + ) -> Self { + let vanity_builder = VanityControlBuilder::new(C::default()); + let control = builder(vanity_builder); + self.add_vanity(control); + self + } + + pub(crate) fn new_vanity_cx( + mut self, + builder: impl BuilderCxFn, FD::Context>, ) -> Self { let vanity_builder = VanityControlBuilder::new(C::default()); let control = builder(vanity_builder, self.cx.clone()); @@ -73,7 +83,17 @@ impl FormBuilder { pub(crate) fn new_control( mut self, - builder: impl BuilderFn, FD::Context>, + builder: impl BuilderFn>, + ) -> Self { + let control_builder = ControlBuilder::new(C::default()); + let control = builder(control_builder); + self.add_control(control); + self + } + + pub(crate) fn new_control_cx( + mut self, + builder: impl BuilderCxFn, FD::Context>, ) -> Self { let control_builder = ControlBuilder::new(C::default()); let control = builder(control_builder, self.cx.clone());