forked from yewstack/yew
-
Notifications
You must be signed in to change notification settings - Fork 0
/
list.rs
132 lines (115 loc) · 3.44 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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
use std::rc::Rc;
use yew::html::{ChildrenRenderer, NodeRef};
use yew::prelude::*;
use yew::virtual_dom::{VChild, VComp};
use crate::header::{ListHeader, Props as HeaderProps};
use crate::item::{ListItem, Props as ItemProps};
use crate::{Hovered, WeakComponentLink};
#[derive(Clone, PartialEq)]
pub enum Variants {
Item(Rc<<ListItem as Component>::Properties>),
Header(Rc<<ListHeader as Component>::Properties>),
}
impl From<ItemProps> for Variants {
fn from(props: ItemProps) -> Self {
Variants::Item(Rc::new(props))
}
}
impl From<HeaderProps> for Variants {
fn from(props: HeaderProps) -> Self {
Variants::Header(Rc::new(props))
}
}
#[derive(PartialEq, Clone)]
pub struct ListVariant {
props: Variants,
}
impl<CHILD> From<VChild<CHILD>> for ListVariant
where
CHILD: Component,
CHILD::Properties: Into<Variants> + Clone,
{
fn from(vchild: VChild<CHILD>) -> Self {
Self {
props: (*vchild.props).clone().into(),
}
}
}
impl From<ListVariant> for Html {
fn from(variant: ListVariant) -> Html {
match variant.props {
Variants::Header(props) => {
VComp::new::<ListHeader>(props, None).into()
}
Variants::Item(props) => VComp::new::<ListItem>(props, None).into(),
}
}
}
pub enum Msg {
HeaderClick,
}
#[derive(Clone, PartialEq, Properties)]
pub struct Props {
pub children: ChildrenRenderer<ListVariant>,
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)}>
{ Self::view_header(&ctx.props().children) }
<div class="items">
{ Self::view_items(&ctx.props().children) }
</div>
</div>
</div>
}
}
}
impl List {
fn view_header(children: &ChildrenRenderer<ListVariant>) -> Html {
html! { for children.iter().filter(|c| matches!(c.props, Variants::Header(_))) }
}
fn view_items(children: &ChildrenRenderer<ListVariant>) -> Html {
children
.iter()
.filter(|c| matches!(&c.props, Variants::Item(props) if !props.hide))
.enumerate()
.map(|(i, mut c)| {
if let Variants::Item(props) = c.props {
let mut props = (*props).clone();
props.name = format!("#{} - {}", i + 1, props.name);
c.props = Variants::Item(Rc::new(props));
}
c
})
.collect::<Html>()
}
}