Various improvements #26

Merged
mitchell merged 5 commits from feature/separate_validation_builder into main 2024-06-12 21:46:28 +00:00
6 changed files with 23 additions and 17 deletions
Showing only changes of commit 64d2631140 - Show all commits

View File

@ -18,7 +18,10 @@ impl<FD: FormToolData, FS: FormStyle> FormBuilder<FD, FS> {
.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() {

View File

@ -70,6 +70,7 @@ pub trait ValidatedControlData: ControlData {}
/// The data needed to render a interactive control of type `C`.
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 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 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,

View File

@ -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<FD: FormToolData, FS: FormStyle> FormBuilder<FD, FS> {
.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<FD: FormToolData, FS: FormStyle> FormBuilder<FD, FS> {
.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;

View File

@ -21,8 +21,8 @@ pub struct GridFormStyle;
impl FormStyle for GridFormStyle {
type StylingAttributes = GridFormStylingAttributes;
fn form_frame(&self, children: View, _styles: Vec<Self::StylingAttributes>) -> View {
view! { <div class="form_grid">{children}</div> }.into_view()
fn form_frame(&self, form: ControlRenderData<Self, View>) -> View {
view! { <div class="form_grid">{*form.data}</div> }.into_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 {
view! { <input type="submit" value=control.data.text class="form_submit"/> }
.into_view()
view! { <input type="submit" value=control.data.text class="form_submit"/> }.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<Self::StylingAttributes>) -> View {
fn group(&self, group: ControlRenderData<Self, View>) -> 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! {
<div class="form_group form_grid" style:grid-column=format!("span {}", width)>
{inner}
{*group.data}
</div>
}
.into_view()

View File

@ -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<Self::StylingAttributes>) -> View;
fn form_frame(&self, form: ControlRenderData<Self, View>) -> View;
fn heading(&self, control: ControlRenderData<Self, HeadingData>) -> View;
fn hidden(
&self,
@ -91,6 +91,6 @@ pub trait FormStyle: 'static {
fn submit(&self, control: ControlRenderData<Self, SubmitData>) -> View;
// TODO: test custom component
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;
}