helper functions and docs

This commit is contained in:
2024-06-05 09:57:51 -05:00
parent 9368b676d2
commit f19d53830c
3 changed files with 133 additions and 30 deletions
+14 -4
View File
@@ -90,6 +90,7 @@ struct FullFormBuilder<FD: FormToolData, FS: FormStyle> {
validation_cbs: Vec<Box<dyn ValidationCb>>,
views: Vec<View>,
}
/// The internal type for building forms
///
/// This allows us to build either the full form
@@ -109,12 +110,15 @@ enum FormBuilderInner<FD: FormToolData, FS: FormStyle> {
},
}
/// A builder for laying out forms.
///
/// This builder allows you to specify what component should make up the form.
pub struct FormBuilder<FD: FormToolData, FS: FormStyle> {
inner: FormBuilderInner<FD, FS>,
}
impl<FD: FormToolData, FS: FormStyle> FormBuilder<FD, FS> {
// TODO: remove the Default trait bound
/// Creates a new full builder.
fn new_full_builder(starting_data: FD, form_style: FS) -> FormBuilder<FD, FS> {
let fd = create_rw_signal(starting_data);
FormBuilder {
@@ -128,6 +132,7 @@ impl<FD: FormToolData, FS: FormStyle> FormBuilder<FD, FS> {
}
}
/// Creates a new builder that only collects the validation functions.
fn new_validation_builder() -> FormBuilder<FD, FS> {
FormBuilder {
inner: FormBuilderInner::ValidationBuilder {
@@ -225,9 +230,9 @@ impl<FD: FormToolData, FS: FormStyle> FormBuilder<FD, FS> {
let fd = builder.fd;
let value_getter = move || {
let getter = getter.clone();
// TODO: ideally, this should not be borrowed. If we get a clone when we call `.get` we should pass in the clone, not borrow this clone
// memoize so that updating one field doesn't cause a re-render for all fields
let field = create_memo(move |_| getter(fd.get()));
unparse_fn(&field.get())
unparse_fn(field.get())
};
let value_getter = value_getter.into_signal();
@@ -380,7 +385,7 @@ pub trait FormToolData: Default + Clone + 'static {
/// data should be parsed and checked.
fn build_form(fb: FormBuilder<Self, Self::Style>) -> FormBuilder<Self, Self::Style>;
/// Constructs a [`Form`] for this FormData type.
/// Constructs a [`Form`] for this [`FormToolData`] type.
///
/// The [`Form`] provides the way to render the form.
fn get_form(self, action: impl ToString, style: Self::Style) -> Form<Self> {
@@ -413,4 +418,9 @@ pub trait FormToolData: Default + Clone + 'static {
let builder = Self::build_form(builder);
builder.validator()
}
fn validate(&self) -> Result<(), String> {
let validator = Self::get_validator();
validator.validate(self)
}
}