summaryrefslogtreecommitdiff
path: root/src/view/input_objects/mod.rs
diff options
context:
space:
mode:
authorjelemux <jeremias.weber@protonmail.com>2020-11-24 21:58:51 +0100
committerjelemux <jeremias.weber@protonmail.com>2020-11-24 21:58:51 +0100
commit5a03734b6767fed04c0913384584d8f59dc597ea (patch)
tree62ef0a0afc0ab56dd36da1a662db768fd8c22eda /src/view/input_objects/mod.rs
parent49588f22f7d20193f899226107c9e323a82c6951 (diff)
downloadwasm-card-5a03734b6767fed04c0913384584d8f59dc597ea.tar.gz
wasm-card-5a03734b6767fed04c0913384584d8f59dc597ea.tar.bz2
add traits for viewmodel and view
Diffstat (limited to 'src/view/input_objects/mod.rs')
-rw-r--r--src/view/input_objects/mod.rs106
1 files changed, 106 insertions, 0 deletions
diff --git a/src/view/input_objects/mod.rs b/src/view/input_objects/mod.rs
new file mode 100644
index 0000000..7c0fdff
--- /dev/null
+++ b/src/view/input_objects/mod.rs
@@ -0,0 +1,106 @@
+use vcard::properties;
+use yew::prelude::*;
+use super::VCardPropertyInputComponent;
+
+pub mod address;
+pub mod birthday;
+pub mod name;
+pub mod photo;
+pub mod telephone;
+pub mod utility;
+
+pub trait VCardPropertyInputObject<P: properties::Property, C: VCardPropertyInputComponent<P, Self>>
+ where Self: Sized
+{
+ fn new() -> Self;
+ fn get_input_fields(&self, link: &ComponentLink<C>) -> Vec<VCardPropertyInputField>;
+ fn render(&self, link: &ComponentLink<C>) -> Html {
+ html!{
+ <div class="columns is-mobile is-multiline">
+ {
+ for self.get_input_fields(link).iter().map(|field|
+ field.render()
+ )
+ }
+ </div>
+ }
+ }
+ fn to_vcard_property(&self) -> Result<P, VCardPropertyInputError>;
+}
+
+#[derive(Debug)]
+pub struct VCardPropertyInputError {
+ msg: String,
+}
+
+pub enum VCardPropertyInputField {
+ Text {
+ label: String,
+ id: Option<String>,
+ placeholder: Option<String>,
+ oninput: Callback<InputData>,
+ value: String,
+ },
+ CheckBox {
+ label: String,
+ id: Option<String>,
+ onclick: Callback<MouseEvent>,
+ value: bool,
+ },
+}
+
+impl VCardPropertyInputField {
+ pub fn render(&self) -> Html {
+ match self {
+ Self::Text {
+ label,
+ id,
+ placeholder,
+ oninput,
+ value: _,
+ } => Self::text_field_input(label, id, placeholder, oninput),
+ Self::CheckBox {
+ label,
+ id,
+ onclick,
+ value,
+ } => Self::checkbox_field_input(label, id, value, onclick),
+ }
+ }
+ fn text_field_input(label: &str, id: &Option<String>, placeholder: &Option<String>, oninput: &Callback<InputData>) -> Html {
+ html!{
+ <div class="field column
+ is-one-fifth-widescreen
+ is-one-quarter-desktop
+ is-one-third-tablet
+ is-half-mobile" >
+ <label class="label">{ label }</label>
+ <div class="control">
+ <input id=id.as_ref().unwrap_or(&"".to_string())
+ type="text"
+ placeholder=placeholder.as_ref().unwrap_or(&"".to_string())
+ oninput=oninput
+ />
+ </div>
+ </div>
+ }
+ }
+ fn checkbox_field_input(label: &str, id: &Option<String>, checked: &bool, onclick: &Callback<MouseEvent>) -> Html {
+ html!{
+ <div class="field column
+ is-one-fifth-widescreen
+ is-one-quarter-desktop
+ is-one-third-tablet
+ is-half-mobile" >
+ <label class="checkbox">
+ <input id=id.as_ref().unwrap_or(&"".to_string())
+ type="checkbox"
+ checked=*checked
+ onclick=onclick
+ />
+ { label }
+ </label>
+ </div>
+ }
+ }
+} \ No newline at end of file