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
use crate::{BackdropDispatcher};
use yew::prelude::*;
#[derive(Copy, Clone, Debug, PartialEq, Eq)]
pub enum ModalVariant {
None,
Small,
Medium,
Large,
}
impl ModalVariant {
pub fn as_classes(&self) -> Vec<&str> {
match self {
ModalVariant::None => vec![],
ModalVariant::Small => vec!["pf-m-sm"],
ModalVariant::Medium => vec!["pf-m-md"],
ModalVariant::Large => vec!["pf-m-lg"],
}
}
}
impl Default for ModalVariant {
fn default() -> Self {
ModalVariant::None
}
}
#[derive(Clone, PartialEq, Properties)]
pub struct Props {
#[prop_or_default]
pub title: String,
#[prop_or_default]
pub description: String,
#[prop_or_default]
pub variant: ModalVariant,
#[prop_or_default]
pub children: Children,
#[prop_or_default]
pub footer: Option<Html>,
#[prop_or_default]
pub onclose: Option<Callback<()>>,
}
pub enum Msg {
Close,
}
#[derive(Clone)]
pub struct Modal {
props: Props,
link: ComponentLink<Self>,
}
impl Component for Modal {
type Message = Msg;
type Properties = Props;
fn create(props: Self::Properties, link: ComponentLink<Self>) -> Self {
Self { props, link }
}
fn update(&mut self, msg: Self::Message) -> ShouldRender {
match msg {
Msg::Close => {
if let Some(onclose) = &self.props.onclose {
onclose.emit(());
} else {
BackdropDispatcher::default().close();
}
}
}
true
}
fn change(&mut self, props: Self::Properties) -> ShouldRender {
if self.props != props {
self.props = props;
true
} else {
false
}
}
fn view(&self) -> Html {
let mut classes = self.props.variant.as_classes();
classes.push("pf-c-modal-box");
return html! {
<div class=classes
role="dialog"
aria-modal="true"
aria-labelledby="modal-title"
aria-describedby="modal-description">
<button
class="pf-c-button pf-m-plain"
type="button"
aria-label="Close dialog"
onclick=self.link.callback(|_|Msg::Close)
>
<i class="fas fa-times" aria-hidden="true"></i>
</button>
<header class="pf-c-modal-box__header">
<h1
class="pf-c-modal-box__title"
id="modal-title-modal-with-form"
>{ &self.props.title }</h1>
</header>
<div class="pf-c-modal-box__body">
<p>{ &self.props.description }</p>
</div>
{ for self.props.children.iter().map(|c|{
{html!{
<div class="pf-c-modal-box__body">{c}</div>
}}
}) }
{ if let Some(footer) = &self.props.footer {
{html!{
<footer class="pf-c-modal-box__footer">
{ footer.clone() }
</footer>
}}
} else {
html!{}
}}
</div>
};
}
}