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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
use crate::Icon;
use yew::prelude::*;
#[derive(Clone, PartialEq, Properties)]
pub struct Props {
pub children: Children,
#[prop_or_default]
pub title: Option<Html>,
#[prop_or_default]
pub footer: Option<Html>,
#[prop_or_default]
pub compact: bool,
#[prop_or_default]
pub flat: bool,
#[prop_or_default]
pub hoverable: bool,
#[prop_or_default]
pub selectable: bool,
#[prop_or_default]
pub selected: bool,
#[prop_or_default]
pub onclick: Callback<yew::MouseEvent>,
#[prop_or_default]
pub expandable: bool,
#[prop_or_default]
pub large: bool,
}
pub struct Card {
props: Props,
link: ComponentLink<Self>,
expanded: bool,
}
#[derive(Clone, Copy, Debug)]
pub enum Msg {
Toggle,
}
impl Component for Card {
type Message = Msg;
type Properties = Props;
fn create(props: Self::Properties, link: ComponentLink<Self>) -> Self {
Self {
props,
link,
expanded: false,
}
}
fn update(&mut self, msg: Self::Message) -> bool {
match msg {
Msg::Toggle => {
self.expanded = !self.expanded;
}
}
true
}
fn change(&mut self, props: Self::Properties) -> bool {
if self.props != props {
self.props = props;
true
} else {
false
}
}
fn view(&self) -> Html {
let mut classes = Classes::from("pf-c-card");
if self.props.compact {
classes.push("pf-m-compact");
}
if self.props.expandable && self.expanded {
classes.push("pf-m-expanded");
}
if self.props.large {
classes.push("pf-m-display-lg");
}
if self.props.flat {
classes.push("pf-m-flat");
}
if self.props.hoverable {
classes.push("pf-m-hoverable");
}
if self.props.selectable {
classes.push("pf-m-selectable");
}
if self.props.selected {
classes.push("pf-m-selected");
}
html! {
<div
class=classes
onclick=&self.props.onclick
>
{ self.header() }
{
if self.expanded || !self.props.expandable {
self.body()
} else {
html!{}
}
}
{ self.footer() }
</div>
}
}
}
impl Card {
fn body(&self) -> Html {
html! {
{for self.props.children.iter().map(|child|{
html_nested!{
<div class="pf-c-card__body">
{ child }
</div>
}
})}
}
}
fn header(&self) -> Html {
if self.props.expandable {
html! {
<div class="pf-c-card__header">
<div class="pf-c-card__header-toggle">
<button
class="pf-c-button pf-m-plain"
type="button"
aria-label="Details"
onclick=self.link.callback(|_|Msg::Toggle)
>
<span class="pf-c-card__header-toggle-icon"> { Icon::AngleRight } </span>
</button>
</div>
{ self.title() }
</div>
}
} else {
self.title()
}
}
fn title(&self) -> Html {
match &self.props.title {
Some(t) => html! {
<div class="pf-c-card__title">
{ t.clone() }
</div>
},
None => html! {},
}
}
fn footer(&self) -> Html {
match &self.props.footer {
Some(f) => html! {
<div class="pf-c-card__footer">
{ f.clone() }
</div>
},
None => html! {},
}
}
}