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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
use crate::{AsClasses, Icon, WithBreakpoints};
use std::{fmt::Formatter, rc::Rc};
use yew::{prelude::*, virtual_dom::VChild};
#[derive(Clone, Debug, Properties, PartialEq)]
pub struct Props {
#[prop_or_default]
pub children: ChildrenWithProps<Tab>,
#[prop_or_default]
pub id: String,
#[prop_or_default]
pub r#box: bool,
#[prop_or_default]
pub vertical: bool,
#[prop_or_default]
pub filled: bool,
#[prop_or_default]
pub inset: Option<Inset>,
}
pub struct Tabs {
active: usize,
}
pub enum Msg {
Select(usize),
}
impl Component for Tabs {
type Message = Msg;
type Properties = Props;
fn create(_ctx: &Context<Self>) -> Self {
Self { active: 0 }
}
fn update(&mut self, _ctx: &Context<Self>, msg: Self::Message) -> bool {
match msg {
Msg::Select(idx) => {
if self.active != idx {
self.active = idx;
} else {
return false;
}
}
}
true
}
fn view(&self, ctx: &Context<Self>) -> Html {
let mut classes = Classes::from("pf-c-tabs");
if ctx.props().r#box {
classes.push("pf-m-box");
}
if ctx.props().vertical {
classes.push("pf-m-vertical");
}
if ctx.props().filled {
classes.push("pf-m-fill");
}
if let Some(inset) = &ctx.props().inset {
inset.extend(&mut classes);
}
let mut idx = 0;
let children = ctx
.props()
.children
.iter()
.map(|mut c| {
let props = Rc::make_mut(&mut c.props);
props.current = self.active == idx;
props.onselect = ctx.link().callback(move |_| Msg::Select(idx));
idx += 1;
c
})
.collect::<Vec<VChild<Tab>>>();
let active = children[self.active].props.children.clone();
html! (
<>
<div class={classes} id={ctx.props().id.clone()}>
<button
class="pf-c-tabs__scroll-button"
disabled=true
aria-hidden="true"
aria-label="Scroll left"
>
{ Icon::AngleLeft }
</button>
<ul class="pf-c-tabs__list">
{ for children.into_iter() }
</ul>
<button
class="pf-c-tabs__scroll-button"
disabled=true
aria-hidden="true"
aria-label="Scroll right"
>
{ Icon::AngleRight }
</button>
</div>
{ active }
</>
)
}
}
#[derive(Copy, Clone, Debug, PartialEq, Eq)]
pub enum Insets {
None,
Small,
Medium,
Large,
XLarge,
XXLarge,
}
impl std::fmt::Display for Insets {
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
match self {
Self::None => f.write_str("pf-m-inset-none"),
Self::Small => f.write_str("pf-m-inset-sm"),
Self::Medium => f.write_str("pf-m-inset-md"),
Self::Large => f.write_str("pf-m-inset-lg"),
Self::XLarge => f.write_str("pf-m-inset-xl"),
Self::XXLarge => f.write_str("pf-m-inset-2xl"),
}
}
}
#[derive(Clone, Debug, PartialEq)]
pub enum Inset {
Inset(WithBreakpoints<Insets>),
Page,
}
impl AsClasses for Inset {
fn extend(&self, classes: &mut Classes) {
match self {
Inset::Page => classes.push("pf-m-page-insets"),
Inset::Inset(insets) => {
insets.extend(classes);
}
}
}
}
#[derive(Clone, Debug, Properties, PartialEq)]
pub struct TabProps {
pub label: String,
#[prop_or_default]
pub icon: Option<Icon>,
#[prop_or_default]
pub(crate) onselect: Callback<()>,
#[prop_or_default]
pub(crate) current: bool,
#[prop_or_default]
pub children: Children,
}
#[derive(Clone, Copy, Debug)]
pub enum TabMsg {
Clicked,
}
pub struct Tab {}
impl Component for Tab {
type Message = TabMsg;
type Properties = TabProps;
fn create(_: &Context<Self>) -> Self {
Self {}
}
fn update(&mut self, ctx: &Context<Self>, msg: Self::Message) -> bool {
match msg {
TabMsg::Clicked => ctx.props().onselect.emit(()),
}
false
}
fn view(&self, ctx: &Context<Self>) -> Html {
let mut classes = Classes::from("pf-c-tabs__item");
if ctx.props().current {
classes.push("pf-m-current");
}
return html! {
<li class={classes}>
<button class="pf-c-tabs__link" onclick={ctx.link().callback(|_|TabMsg::Clicked)}>
if let Some(icon) = ctx.props().icon {
<span class="pf-c-tabs__item-icon" aria_hidden={true.to_string()}> { icon } </span>
}
<span class="pf-c-tabs__item-text"> { &ctx.props().label } </span>
</button>
</li>
};
}
}