-
-
Notifications
You must be signed in to change notification settings - Fork 1.4k
/
Copy pathlist.rs
81 lines (71 loc) · 2.03 KB
/
list.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
use implicit_clone::unsync::IArray;
use yew::prelude::*;
use yew::virtual_dom::VChild;
use crate::header::ListHeader;
use crate::item::ListItem;
use crate::{Hovered, WeakComponentLink};
pub enum Msg {
HeaderClick,
}
#[derive(Clone, PartialEq, Properties)]
pub struct Props {
#[prop_or_default]
pub header: IArray<VChild<ListHeader>>,
#[prop_or_default]
pub children: IArray<VChild<ListItem>>,
pub on_hover: Callback<Hovered>,
pub weak_link: WeakComponentLink<List>,
}
pub struct List {
inactive: bool,
}
impl Component for List {
type Message = Msg;
type Properties = Props;
fn create(ctx: &Context<Self>) -> Self {
ctx.props()
.weak_link
.borrow_mut()
.replace(ctx.link().clone());
Self { inactive: false }
}
fn update(&mut self, _ctx: &Context<Self>, msg: Self::Message) -> bool {
match msg {
Msg::HeaderClick => {
self.inactive = !self.inactive;
true
}
}
}
fn view(&self, ctx: &Context<Self>) -> Html {
let inactive = if self.inactive { "inactive" } else { "" };
let onmouseover = ctx.props().on_hover.reform(|e: MouseEvent| {
e.stop_propagation();
Hovered::List
});
html! {
<div class="list-container" {onmouseover}>
<div class={classes!("list", inactive)}>
{ &ctx.props().header }
<div class="items">
{ Self::view_items(&ctx.props().children) }
</div>
</div>
</div>
}
}
}
impl List {
fn view_items(children: &IArray<VChild<ListItem>>) -> Html {
children
.iter()
.filter(|c| !c.props.hide)
.enumerate()
.map(|(i, mut c)| {
let props = c.get_mut();
props.name = format!("#{} - {}", i + 1, props.name).into();
c
})
.collect::<Html>()
}
}