rumtk_web/components/Form/
form_utils.rs1use rumtk_core::cache::{new_cache, LazyRUMCache};
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 type FormElements = Vec<RUMString>;
31pub type FormCache = LazyRUMCache<RUMString, FormElements>;
32pub type FormElementBuilder =
33 fn(element: &str, data: &str, props: InputProps, css: &str) -> RUMString;
34pub type FormBuilderFunction = fn(builder: FormElementBuilder) -> FormElements;
35
36static mut form_cache: FormCache = new_cache();
37
38fn new_form_entry(name: &RUMString) -> FormElements {
39 vec![]
40}
41
42fn build_form_element(element: &str, data: &str, props: InputProps, css: &str) -> RUMString {
43 rumtk_web_render_component!(|| -> HTMLResult { form_element(element, data, props, css) })
44}
45
46pub fn register_form_elements(name: &str, element_builder: FormBuilderFunction) -> &FormElements {
47 let key = RUMString::from(name);
48 rumtk_cache_fetch!(&mut form_cache, &key, new_form_entry);
49 let data = element_builder(build_form_element);
50 rumtk_cache_push!(&mut form_cache, &key, &data)
51}
52
53pub fn get_form(name: &str) -> &FormElements {
54 rumtk_cache_fetch!(&mut form_cache, &RUMString::from(name), new_form_entry)
55}