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