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