1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
use super::{BuilderCxFn, BuilderFn, ControlRenderData, VanityControlBuilder, VanityControlData};
use crate::{form::FormToolData, form_builder::FormBuilder, styles::FormStyle};
use leptos::{prelude::Signal, RwSignal, View};
use std::rc::Rc;

/// Data used for the spacer control.
#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash, Default)]
pub struct SpacerData {
    pub height: Option<String>,
}

impl<FD: FormToolData> VanityControlData<FD> for SpacerData {
    fn render_control<FS: FormStyle>(
        fs: &FS,
        _fd: RwSignal<FD>,
        control: Rc<ControlRenderData<FS, Self>>,
        _value_getter: Option<Signal<String>>,
    ) -> View {
        fs.spacer(control)
    }
}

impl<FD: FormToolData> FormBuilder<FD> {
    /// Builds a spacer and adds it to the form.
    pub fn spacer(self, builder: impl BuilderFn<VanityControlBuilder<FD, SpacerData>>) -> Self {
        self.new_vanity(builder)
    }

    /// Builds a spacer using the form's context and adds it to the form.
    pub fn spacer_cx(
        self,
        builder: impl BuilderCxFn<VanityControlBuilder<FD, SpacerData>, FD::Context>,
    ) -> Self {
        self.new_vanity_cx(builder)
    }
}

impl<FD: FormToolData> VanityControlBuilder<FD, SpacerData> {
    /// Sets the height of the spacer.
    ///
    /// This is a string to allow different units like "10px" or "1.25em".
    ///
    /// This may or may not be respected based on the Style implementation.
    pub fn height(mut self, height: impl ToString) -> Self {
        self.data.height = Some(height.to_string());
        self
    }
}