Rewrite some components render

This commit is contained in:
eraden 2021-04-18 13:55:42 +02:00
parent a5594f6844
commit 682066b561
2 changed files with 99 additions and 99 deletions

View File

@ -14,6 +14,55 @@ pub struct StyledAvatar<'l> {
pub user_index: usize,
}
impl<'l> StyledAvatar<'l> {
pub fn render(self) -> Node<Msg> {
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<Msg> {
render(self)
}
}
pub fn render(values: StyledAvatar) -> Node<Msg> {
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()
}
}

View File

@ -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<Msg> {
render(self)
pub fn render(self) -> Node<Msg> {
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<Msg> {
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<Msg> {
self.render()
}
}