generated from mitchell/rust_template
75 lines
1.9 KiB
Rust
75 lines
1.9 KiB
Rust
use super::ControlRenderData;
|
|
use crate::{form::FormToolData, form_builder::FormBuilder, styles::FormStyle};
|
|
use leptos::RwSignal;
|
|
use std::rc::Rc;
|
|
use web_sys::MouseEvent;
|
|
|
|
type ButtonAction<FD> = dyn Fn(MouseEvent, &mut FD);
|
|
|
|
#[derive(Clone)]
|
|
pub struct ButtonData<FD: FormToolData> {
|
|
pub(crate) text: String,
|
|
pub(crate) action: Option<Rc<ButtonAction<FD>>>,
|
|
}
|
|
impl<FD: FormToolData> Default for ButtonData<FD> {
|
|
fn default() -> Self {
|
|
ButtonData {
|
|
text: String::default(),
|
|
action: None,
|
|
}
|
|
}
|
|
}
|
|
|
|
impl<FD: FormToolData, FS: FormStyle> FormBuilder<FD, FS> {
|
|
pub fn button(
|
|
mut self,
|
|
builder: impl Fn(ButtonBuilder<FD, FS>) -> ButtonBuilder<FD, FS>,
|
|
) -> Self {
|
|
let button_builder = ButtonBuilder::new();
|
|
let control = builder(button_builder);
|
|
|
|
let render_data = ControlRenderData {
|
|
data: control.data,
|
|
styles: control.styles,
|
|
};
|
|
|
|
let render_fn = move |fs: &FS, fd: RwSignal<FD>| {
|
|
let view = fs.button(render_data, fd);
|
|
(view, None)
|
|
};
|
|
self.render_fns.push(Box::new(render_fn));
|
|
|
|
self
|
|
}
|
|
}
|
|
|
|
#[derive(Clone)]
|
|
pub struct ButtonBuilder<FD: FormToolData, FS: FormStyle> {
|
|
pub(crate) styles: Vec<FS::StylingAttributes>,
|
|
pub(crate) data: ButtonData<FD>,
|
|
}
|
|
|
|
impl<FD: FormToolData, FS: FormStyle> ButtonBuilder<FD, FS> {
|
|
fn new() -> Self {
|
|
ButtonBuilder {
|
|
styles: Vec::default(),
|
|
data: ButtonData::default(),
|
|
}
|
|
}
|
|
|
|
pub fn style(mut self, style: FS::StylingAttributes) -> Self {
|
|
self.styles.push(style);
|
|
self
|
|
}
|
|
|
|
pub fn text(mut self, text: impl ToString) -> Self {
|
|
self.data.text = text.to_string();
|
|
self
|
|
}
|
|
|
|
pub fn action(mut self, action: impl Fn(MouseEvent, &mut FD) + 'static) -> Self {
|
|
self.data.action = Some(Rc::new(action));
|
|
self
|
|
}
|
|
}
|