From 5b9f62a7b12bc67d982e53d8f73824822c199401 Mon Sep 17 00:00:00 2001 From: jelemux Date: Tue, 10 Nov 2020 23:43:24 +0100 Subject: seperate component for name + switch to bulma.css --- src/name.rs | 161 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 161 insertions(+) create mode 100644 src/name.rs (limited to 'src/name.rs') diff --git a/src/name.rs b/src/name.rs new file mode 100644 index 0000000..bf91186 --- /dev/null +++ b/src/name.rs @@ -0,0 +1,161 @@ +use yew::prelude::*; + +#[derive(Clone)] +pub struct Name { + pub prefix: String, + pub first_name: String, + pub middle_name: String, + pub last_name: String, + pub suffix: String, +} + +impl Name { + pub fn new() -> Self { + Self { + prefix: String::new(), + first_name: String::new(), + middle_name: String::new(), + last_name: String::new(), + suffix: String::new(), + } + } + pub fn formatted_name(&self) -> String { + let mut formatted_name = String::new(); + + if !self.prefix.is_empty() { + formatted_name.push_str(&self.prefix); + } + if !self.first_name.is_empty() { + formatted_name.push_str(" "); + formatted_name.push_str(&self.first_name); + } + if !self.middle_name.is_empty() { + formatted_name.push_str(" "); + formatted_name.push_str(&self.middle_name); + } + if !self.last_name.is_empty() { + formatted_name.push_str(" "); + formatted_name.push_str(&self.last_name); + } + if !self.suffix.is_empty() { + formatted_name.push_str(", "); + formatted_name.push_str(&self.suffix); + } + + formatted_name + } +} + +pub struct NameView { + link: ComponentLink, + value: Name, + oninput: Callback, + //errors: Vec, +} + +pub enum Msg { + UpdatePrefix(String), + UpdateFirstName(String), + UpdateMiddleName(String), + UpdateLastName(String), + UpdateSuffix(String), +} + +#[derive(Clone, PartialEq, Properties)] +pub struct Props { + pub oninput: Callback, + //pub errors: Vec, +} + +impl Component for NameView { + type Message = Msg; + type Properties = Props; + fn create(props: ::Properties, link: yew::html::Scope) -> Self { + Self { + link, + value: Name::new(), + oninput: props.oninput, + } + } + fn update(&mut self, msg: ::Message) -> bool { + match msg { + Msg::UpdatePrefix(p) => self.value.prefix = p, + Msg::UpdateFirstName(f) => self.value.first_name = f, + Msg::UpdateMiddleName(m) => self.value.middle_name = m, + Msg::UpdateLastName(l) => self.value.last_name = l, + Msg::UpdateSuffix(s) => self.value.suffix = s, + }; + self.oninput.emit(self.value.clone()); + true + } + fn change(&mut self, props: ::Properties) -> bool { + self.oninput = props.oninput; + true + } + fn view(&self) -> yew::virtual_dom::VNode { + html!{ + <> +

{ "Name" }

+ +
+ +
+ +
+ +
+
+ +
+ +
+ +
+
+ +
+ +
+ +
+
+ +
+ +
+ +
+
+ +
+ +
+ +
+
+ +
+ + } + } +} \ No newline at end of file -- cgit v1.2.3