From 682066b5615e1eebf836f4491d0dfb0ac338f3b0 Mon Sep 17 00:00:00 2001 From: eraden Date: Sun, 18 Apr 2021 13:55:42 +0200 Subject: [PATCH] Rewrite some components render --- jirs-client/src/components/styled_avatar.rs | 98 +++++++++---------- jirs-client/src/components/styled_button.rs | 100 ++++++++++---------- 2 files changed, 99 insertions(+), 99 deletions(-) diff --git a/jirs-client/src/components/styled_avatar.rs b/jirs-client/src/components/styled_avatar.rs index b4840d98..a3e1889c 100644 --- a/jirs-client/src/components/styled_avatar.rs +++ b/jirs-client/src/components/styled_avatar.rs @@ -14,6 +14,55 @@ pub struct StyledAvatar<'l> { pub user_index: usize, } +impl<'l> StyledAvatar<'l> { + pub fn render(self) -> Node { + let StyledAvatar { + avatar_url, + size, + name, + on_click, + class_list, + user_index, + } = self; + + let index = user_index % 8; + + let shared_style = format!("width: {size}px; height: {size}px", size = size); + let letter = name + .chars() + .rev() + .last() + .map(|c| c.to_string()) + .unwrap_or_default(); + match avatar_url { + Some(url) => { + let style = format!( + "{shared}; background-image: url({url});", + shared = shared_style, + url = url + ); + div![ + C!["styledAvatar image", class_list], + attrs![At::Style => style, At::Title => name], + on_click + ] + } + _ => { + div![ + C!["styledAvatar letter", class_list], + attrs![ + At::Class => format!("avatarColor{}", index + 1), + At::Style => shared_style, + At::Title => name + ], + span![letter], + on_click, + ] + } + } + } +} + impl<'l> Default for StyledAvatar<'l> { fn default() -> Self { Self { @@ -30,53 +79,6 @@ impl<'l> Default for StyledAvatar<'l> { impl<'l> ToNode for StyledAvatar<'l> { #[inline(always)] fn into_node(self) -> Node { - render(self) - } -} - -pub fn render(values: StyledAvatar) -> Node { - let StyledAvatar { - avatar_url, - size, - name, - on_click, - class_list, - user_index, - } = values; - - let index = user_index % 8; - - let shared_style = format!("width: {size}px; height: {size}px", size = size); - let letter = name - .chars() - .rev() - .last() - .map(|c| c.to_string()) - .unwrap_or_default(); - match avatar_url { - Some(url) => { - let style = format!( - "{shared}; background-image: url({url});", - shared = shared_style, - url = url - ); - div![ - C!["styledAvatar image", class_list], - attrs![At::Style => style, At::Title => name], - on_click - ] - } - _ => { - div![ - C!["styledAvatar letter", class_list], - attrs![ - At::Class => format!("avatarColor{}", index + 1), - At::Style => shared_style, - At::Title => name - ], - span![letter], - on_click, - ] - } + self.render() } } diff --git a/jirs-client/src/components/styled_button.rs b/jirs-client/src/components/styled_button.rs index 47904261..611672bb 100644 --- a/jirs-client/src/components/styled_button.rs +++ b/jirs-client/src/components/styled_button.rs @@ -81,59 +81,57 @@ impl<'l> Default for StyledButton<'l> { } } -impl<'l> ToNode for StyledButton<'l> { +impl<'l> StyledButton<'l> { #[inline(always)] - fn into_node(self) -> Node { - render(self) + pub fn render(self) -> Node { + let StyledButton { + text, + variant, + disabled, + active, + icon, + on_click, + children, + class_list, + button_type, + button_id, + } = self; + + let handler = match on_click { + Some(h) if !disabled => vec![h], + _ => vec![], + }; + + let children_len = children.len(); + let content = if children.is_empty() && text.is_none() { + Node::Empty + } else { + span![C!["text"], text.unwrap_or_default(), children] + }; + + let button_id = button_id.map(|id| id.to_str()).unwrap_or_default(); + + seed::button![ + C![ + "styledButton", + class_list, + variant.to_str(), + IF![children_len > 0 && text.is_none() => "iconOnly"], + IF![active => "isActive"], + IF![icon.is_some() => "withIcon"], + ], + attrs![At::Id => button_id, At::Type => button_type], + IF![disabled => attrs![At::Disabled => true]], + handler, + icon.unwrap_or(Node::Empty), + content, + ] } } -#[inline(always)] -pub fn render(values: StyledButton) -> Node { - let StyledButton { - text, - variant, - disabled, - active, - icon, - on_click, - children, - class_list, - button_type, - button_id, - } = values; - let class_list = format!( - "{} {} {} {} {}", - class_list, - variant, - if children.is_empty() && text.is_none() { - "iconOnly" - } else { - "" - }, - if active { "isActive" } else { "" }, - if icon.is_some() { "withIcon" } else { "" } - ); - let handler = match on_click { - Some(h) if !disabled => vec![h], - _ => vec![], - }; - - let icon_node = icon.unwrap_or(Node::Empty); - let content = if children.is_empty() && text.is_none() { - Node::Empty - } else { - span![C!["text"], text.unwrap_or_default(), children] - }; - - let button_id = button_id.map(|id| id.to_str()).unwrap_or_default(); - - seed::button![ - C!["styledButton", class_list], - attrs![At::Id => button_id, At::Type => button_type], - IF![disabled => attrs![At::Disabled => true]], - handler, - icon_node, - content, - ] +impl<'l> ToNode for StyledButton<'l> { + #[inline(always)] + fn into_node(self) -> Node { + self.render() + } }