Tree: clean-up: less pubs, impl Default if possible

This commit is contained in:
Cecile Tonglet 2020-09-14 21:32:08 +02:00
parent f5992a3a83
commit 3c5a8af353
2 changed files with 37 additions and 43 deletions

View file

@ -36,16 +36,8 @@ impl Component for App {
let root_id = tree let root_id = tree
.insert( .insert(
Node::new(NodeData { Node::new(NodeData {
icon: None,
icon_color: None,
icon_intent: None,
label: "".into(),
secondary_label: None,
is_selected: false,
is_expanded: false,
has_caret: true,
disabled: false,
data: 0, data: 0,
..Default::default()
}), }),
InsertBehavior::AsRoot, InsertBehavior::AsRoot,
) )
@ -54,15 +46,10 @@ impl Component for App {
.insert( .insert(
Node::new(NodeData { Node::new(NodeData {
icon: Some(IconName::FolderClose), icon: Some(IconName::FolderClose),
icon_color: None,
icon_intent: None,
label: "Directory 1".into(), label: "Directory 1".into(),
secondary_label: None,
is_selected: false,
is_expanded: false,
has_caret: true, has_caret: true,
disabled: false,
data: 1, data: 1,
..Default::default()
}), }),
InsertBehavior::UnderNode(&root_id), InsertBehavior::UnderNode(&root_id),
) )
@ -70,15 +57,9 @@ impl Component for App {
tree.insert( tree.insert(
Node::new(NodeData { Node::new(NodeData {
icon: Some(IconName::Document), icon: Some(IconName::Document),
icon_color: None,
icon_intent: None,
label: "File 1".into(), label: "File 1".into(),
secondary_label: None,
is_selected: false,
is_expanded: false,
has_caret: false,
disabled: false,
data: 2, data: 2,
..Default::default()
}), }),
InsertBehavior::UnderNode(&root_id), InsertBehavior::UnderNode(&root_id),
) )
@ -86,15 +67,11 @@ impl Component for App {
tree.insert( tree.insert(
Node::new(NodeData { Node::new(NodeData {
icon: Some(IconName::Tag), icon: Some(IconName::Tag),
icon_color: None,
icon_intent: Some(Intent::Primary), icon_intent: Some(Intent::Primary),
label: "File 2".into(), label: "File 2".into(),
secondary_label: Some(html!(<Icon icon=IconName::EyeOpen />)), secondary_label: Some(html!(<Icon icon=IconName::EyeOpen />)),
is_selected: false,
is_expanded: false,
has_caret: false,
disabled: false,
data: 3, data: 3,
..Default::default()
}), }),
InsertBehavior::UnderNode(&dir1), InsertBehavior::UnderNode(&dir1),
) )

View file

@ -68,6 +68,23 @@ pub struct NodeData<T> {
pub data: T, pub data: T,
} }
impl<T: Default> Default for NodeData<T> {
fn default() -> Self {
Self {
disabled: false,
has_caret: false,
icon: None,
icon_color: None,
icon_intent: None,
is_expanded: false,
is_selected: false,
label: Default::default(),
secondary_label: None,
data: Default::default(),
}
}
}
impl<T: Clone + PartialEq + 'static> Component for Tree<T> { impl<T: Clone + PartialEq + 'static> Component for Tree<T> {
type Message = (); type Message = ();
type Properties = Props<T>; type Properties = Props<T>;
@ -170,26 +187,26 @@ impl<T: Clone> Tree<T> {
} }
} }
pub struct TreeNode { struct TreeNode {
props: TreeNodeProps, props: TreeNodeProps,
} }
#[derive(Clone, PartialEq, Properties)] #[derive(Clone, PartialEq, Properties)]
pub struct TreeNodeProps { struct TreeNodeProps {
pub disabled: bool, disabled: bool,
pub has_caret: bool, has_caret: bool,
pub icon: Option<IconName>, icon: Option<IconName>,
pub icon_color: Option<String>, icon_color: Option<String>,
pub icon_intent: Option<Intent>, icon_intent: Option<Intent>,
pub is_expanded: bool, is_expanded: bool,
pub is_selected: bool, is_selected: bool,
pub label: yew::virtual_dom::VNode, label: yew::virtual_dom::VNode,
pub secondary_label: Option<yew::virtual_dom::VNode>, secondary_label: Option<yew::virtual_dom::VNode>,
pub on_collapse: Option<Callback<MouseEvent>>, on_collapse: Option<Callback<MouseEvent>>,
pub on_expand: Option<Callback<MouseEvent>>, on_expand: Option<Callback<MouseEvent>>,
pub onclick: Option<Callback<MouseEvent>>, onclick: Option<Callback<MouseEvent>>,
pub children: html::Children, children: html::Children,
pub depth: u32, depth: u32,
} }
impl Component for TreeNode { impl Component for TreeNode {