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, pub user_index: usize,
} }
impl<'l> Default for StyledAvatar<'l> { impl<'l> StyledAvatar<'l> {
fn default() -> Self { pub fn render(self) -> Node<Msg> {
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> {
let StyledAvatar { let StyledAvatar {
avatar_url, avatar_url,
size, size,
@ -42,7 +23,7 @@ pub fn render(values: StyledAvatar) -> Node<Msg> {
on_click, on_click,
class_list, class_list,
user_index, user_index,
} = values; } = self;
let index = user_index % 8; let index = user_index % 8;
@ -79,4 +60,25 @@ 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)] #[inline(always)]
fn into_node(self) -> Node<Msg> { pub fn render(self) -> Node<Msg> {
render(self)
}
}
#[inline(always)]
pub fn render(values: StyledButton) -> Node<Msg> {
let StyledButton { let StyledButton {
text, text,
variant, variant,
@ -101,25 +95,14 @@ pub fn render(values: StyledButton) -> Node<Msg> {
class_list, class_list,
button_type, button_type,
button_id, button_id,
} = values; } = self;
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 { let handler = match on_click {
Some(h) if !disabled => vec![h], Some(h) if !disabled => vec![h],
_ => vec![], _ => vec![],
}; };
let icon_node = icon.unwrap_or(Node::Empty); let children_len = children.len();
let content = if children.is_empty() && text.is_none() { let content = if children.is_empty() && text.is_none() {
Node::Empty Node::Empty
} else { } 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(); let button_id = button_id.map(|id| id.to_str()).unwrap_or_default();
seed::button![ 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], attrs![At::Id => button_id, At::Type => button_type],
IF![disabled => attrs![At::Disabled => true]], IF![disabled => attrs![At::Disabled => true]],
handler, handler,
icon_node, icon.unwrap_or(Node::Empty),
content, content,
] ]
}
}
impl<'l> ToNode for StyledButton<'l> {
#[inline(always)]
fn into_node(self) -> Node<Msg> {
self.render()
}
} }