From 88de865142d0df526dcb9685cb79d7dca799156a Mon Sep 17 00:00:00 2001 From: Andrea Zagli Date: Sat, 18 Feb 2023 10:20:36 +0100 Subject: [PATCH] Added methods get/set_label. Rendered html checkbox. --- examples/form.rs | 7 ++++ src/fields.rs | 92 ++++++++++++++++++++++++++++++++++++++++++++++-- 2 files changed, 96 insertions(+), 3 deletions(-) diff --git a/examples/form.rs b/examples/form.rs index ed10ff2..5e1bd8c 100644 --- a/examples/form.rs +++ b/examples/form.rs @@ -14,6 +14,8 @@ use actix_web::{ web, App, Error, HttpResponse, HttpServer, Responder, Result, }; +use zakform::fields::TField; + #[derive(Debug, Deserialize)] #[allow(unused)] struct Http { @@ -64,9 +66,14 @@ async fn index( f.add_field(Box::new(fi)); let mut fi = zakform::fields::FieldTextArea::new("textarea"); + fi.set_label("TEXT AREA"); fi.set_rows(15); f.add_field(Box::new(fi)); + let mut fi = zakform::fields::FieldCheck::new("checkbox"); + fi.set_label("the check text very very long"); + f.add_field(Box::new(fi)); + let fs = f.fields(); for fi in fs { s.push_str(fi.render().as_str()); diff --git a/src/fields.rs b/src/fields.rs index f048bbb..56fbf64 100644 --- a/src/fields.rs +++ b/src/fields.rs @@ -19,6 +19,10 @@ struct Field { pub trait TField { fn get_name(&self) -> String; + fn get_label(&self) -> String; + + fn set_label(&mut self, label: &str); + fn render(&self) -> String; fn any(&mut self) -> &mut dyn Any; @@ -35,6 +39,14 @@ impl TField for FieldText { String::from(&self.field.name) } + fn get_label(&self) -> String { + String::from(&self.field.label) + } + + fn set_label(&mut self, label: &str) { + self.field.label = String::from(label); + } + fn render(&self) -> String { let mut s = String::new(); @@ -79,7 +91,7 @@ _ => {}, self.maxlen = maxlen; } - pub fn maxlen(&self) -> i32 { + pub fn get_maxlen(&self) -> i32 { self.maxlen } } @@ -96,6 +108,14 @@ impl TField for FieldTextArea { String::from(&self.field.name) } + fn get_label(&self) -> String { + String::from(&self.field.label) + } + + fn set_label(&mut self, label: &str) { + self.field.label = String::from(label); + } + fn render(&self) -> String { let mut s = String::new(); @@ -144,7 +164,7 @@ _ => {}, self.maxlen = maxlen; } - pub fn maxlen(&self) -> i32 { + pub fn get_maxlen(&self) -> i32 { self.maxlen } @@ -152,7 +172,7 @@ _ => {}, self.rows = rows; } - pub fn rows(&self) -> i32 { + pub fn get_rows(&self) -> i32 { self.rows } } @@ -174,6 +194,14 @@ impl TField for FieldRadio { String::from(&self.field.name) } + fn get_label(&self) -> String { + String::from(&self.field.label) + } + + fn set_label(&mut self, label: &str) { + self.field.label = String::from(label); + } + fn render(&self) -> String { let mut s = String::new(); @@ -256,3 +284,61 @@ _ => {}, s } } + +#[derive(Default)] +pub struct FieldCheck { + field: Field, +} + +impl TField for FieldCheck { + fn get_name(&self) -> String { + String::from(&self.field.name) + } + + fn get_label(&self) -> String { + String::from(&self.field.label) + } + + fn set_label(&mut self, label: &str) { + self.field.label = String::from(label); + } + + fn render(&self) -> String { + let mut s = String::new(); + + let mut context = tera::Context::new(); + context.insert("name", &self.field.name); + context.insert("label", &self.field.label); + context.insert("disabled", &self.field.disabled); + context.insert("invisible", &self.field.invisible); + context.insert("help", &self.field.help); + context.insert("value", &self.field.value); + s.push_str(self.field.tmpl.render("field", &context).unwrap().as_str()); + + s + } + + fn any(&mut self) -> &mut dyn Any { + self + } +} + +impl FieldCheck { + pub fn new(name: &str) -> Self { + let mut f: FieldCheck = Default::default(); + + f.field.name = String::from(name); + f.field.label = String::from(name); + + match f.field.tmpl.add_raw_template("field", r#"
+ + {% if label %}{% endif %} + {% if help %}
{{ help }}
{% endif %} +
"#) { +Err(e) => { println!("{:?}", e) }, +_ => {}, +}; + + f + } +} -- 2.49.0