rumtk_web/components/form/
mod.rs1use rumtk_core::cache::{new_cache, LazyRUMCache, LazyRUMCacheValue};
22use rumtk_core::strings::RUMString;
23use rumtk_core::{rumtk_cache_fetch, rumtk_cache_push};
24
25use crate::components::form::form_element::form_element;
26use crate::components::form::props::InputProps;
27use crate::rumtk_web_render_component;
28use crate::utils::HTMLResult;
29
30pub mod form;
31pub mod form_element;
32pub mod props;
33
34pub type FormElements = Vec<RUMString>;
35pub type FormCache = LazyRUMCache<RUMString, FormElements>;
36pub type FormElementBuilder =
37 fn(element: &str, data: &str, props: InputProps, css: &str) -> RUMString;
38pub type FormBuilderFunction = fn(builder: FormElementBuilder) -> FormElements;
39pub type FormCacheItem = LazyRUMCacheValue<FormElements>;
40
41static mut FORM_CACHE: FormCache = new_cache();
42static DEFAULT_FORMELEMENTS: FormElements = vec![];
43
44fn new_form_entry(_name: &RUMString) -> FormElements {
45 DEFAULT_FORMELEMENTS.clone()
46}
47
48fn build_form_element(element: &str, data: &str, props: InputProps, css: &str) -> RUMString {
49 rumtk_web_render_component!(|| -> HTMLResult { form_element(element, data, props, css) })
50}
51
52pub fn register_form_elements(name: &str, element_builder: FormBuilderFunction) -> FormCacheItem {
53 let key = RUMString::from(name);
54 let _ = rumtk_cache_fetch!(&raw mut FORM_CACHE, &key, new_form_entry);
55 let data = element_builder(build_form_element);
56 rumtk_cache_push!(&raw mut FORM_CACHE, &key, &data)
57}
58
59pub fn get_form(name: &str) -> FormCacheItem {
60 rumtk_cache_fetch!(&raw mut FORM_CACHE, &RUMString::from(name), new_form_entry)
61}
62
63#[macro_export]
67macro_rules! rumtk_web_add_form {
68 ( $name:expr, $build_fxn:expr ) => {{
69 use $crate::components::form::register_form_elements;
70
71 register_form_elements($name, $build_fxn)
72 }};
73}
74
75#[macro_export]
80macro_rules! rumtk_web_get_form {
81 ( $name:expr ) => {{
82 use $crate::components::form::get_form;
83
84 get_form($name)
85 }};
86}