This repository has been archived on 2024-08-06. You can view files and clone it, but cannot push or open issues or pull requests.
leptos_form_tool/src/lib.rs
2024-03-15 21:12:19 -05:00

60 lines
1.9 KiB
Rust

use controls::{
heading::HeadingData, select::SelectData, submit::SubmitData, text_area::TextAreaData,
text_input::TextInputData,
};
use leptos::*;
use std::marker::PhantomData;
mod controls;
mod provider_impl;
pub trait FormStyleProvider {
type StylingAttributes;
fn heading(&self, data: HeadingData, attributes: Vec<Self::StylingAttributes>) -> View;
fn text_input(&self, data: TextInputData, attributes: Vec<Self::StylingAttributes>) -> View;
fn select(&self, data: SelectData, attributes: Vec<Self::StylingAttributes>) -> View;
fn submit(&self, data: SubmitData, attributes: Vec<Self::StylingAttributes>) -> View;
fn text_area(&self, data: TextAreaData, attributes: Vec<Self::StylingAttributes>) -> View;
fn custom_component(&self, component: View) -> View;
// TODO: add group
}
pub trait ControlData<FS: FormStyleProvider>: 'static {
fn build_control(self, fs: &FS, attributes: Vec<FS::StylingAttributes>) -> View;
}
pub struct FormBuilder<FD: FormData, FS: FormStyleProvider> {
_fd: PhantomData<FD>,
fs: FS,
controls: Vec<(Box<dyn ControlData<FS>>, Vec<FS::StylingAttributes>)>,
}
pub trait FormData {}
pub struct ControlBuilder<FD: FormData, FS: FormStyleProvider, C: ControlData<FS>> {
fb: FormBuilder<FD, FS>,
parse_fn: Option<Box<dyn Fn(&str, &mut FD)>>,
style_attributes: Vec<FS::StylingAttributes>,
data: C,
}
impl<FD: FormData, FS: FormStyleProvider, C: ControlData<FS>> ControlBuilder<FD, FS, C> {
pub fn parse_fn(mut self, parse_fn: Box<dyn Fn(&str, &mut FD)>) -> Self {
self.parse_fn = Some(parse_fn);
self
}
pub fn style(mut self, attribute: FS::StylingAttributes) -> Self {
self.style_attributes.push(attribute);
self
}
pub fn end(mut self) -> FormBuilder<FD, FS> {
self.fb
.controls
.push((Box::new(self.data), self.style_attributes));
self.fb
}
}