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,27 +14,8 @@ pub struct StyledAvatar<'l> {
pub user_index: usize,
}
impl<'l> Default for StyledAvatar<'l> {
fn default() -> Self {
Self {
avatar_url: None,
size: 32,
name: "",
on_click: None,
class_list: "",
user_index: 0,
}
}
}
impl<'l> ToNode for StyledAvatar<'l> {
#[inline(always)]
fn into_node(self) -> Node<Msg> {
render(self)
}
}
pub fn render(values: StyledAvatar) -> Node<Msg> {
impl<'l> StyledAvatar<'l> {
pub fn render(self) -> Node<Msg> {
let StyledAvatar {
avatar_url,
size,
@ -42,7 +23,7 @@ pub fn render(values: StyledAvatar) -> Node<Msg> {
on_click,
class_list,
user_index,
} = values;
} = self;
let index = user_index % 8;
@ -80,3 +61,24 @@ pub fn render(values: StyledAvatar) -> Node<Msg> {
}
}
}
}
impl<'l> Default for StyledAvatar<'l> {
fn default() -> Self {
Self {
avatar_url: None,
size: 32,
name: "",
on_click: None,
class_list: "",
user_index: 0,
}
}
}
impl<'l> ToNode for StyledAvatar<'l> {
#[inline(always)]
fn into_node(self) -> Node<Msg> {
self.render()
}
}

View File

@ -81,15 +81,9 @@ 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)
}
}
#[inline(always)]
pub fn render(values: StyledButton) -> Node<Msg> {
pub fn render(self) -> Node<Msg> {
let StyledButton {
text,
variant,
@ -101,25 +95,14 @@ pub fn render(values: StyledButton) -> Node<Msg> {
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 { "" }
);
} = self;
let handler = match on_click {
Some(h) if !disabled => vec![h],
_ => vec![],
};
let icon_node = icon.unwrap_or(Node::Empty);
let children_len = children.len();
let content = if children.is_empty() && text.is_none() {
Node::Empty
} else {
@ -129,11 +112,26 @@ pub fn render(values: StyledButton) -> Node<Msg> {
let button_id = button_id.map(|id| id.to_str()).unwrap_or_default();
seed::button![
C!["styledButton", class_list],
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_node,
icon.unwrap_or(Node::Empty),
content,
]
}
}
impl<'l> ToNode for StyledButton<'l> {
#[inline(always)]
fn into_node(self) -> Node<Msg> {
self.render()
}
}