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
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
use dioxus::prelude::*;
/// Cascader option
#[derive(Clone, PartialEq)]
pub struct CascaderOption {
pub value: String,
pub label: String,
pub children: Vec<CascaderOption>,
pub disabled: bool,
}
impl CascaderOption {
pub fn new(value: &str, label: &str) -> Self {
Self {
value: value.to_string(),
label: label.to_string(),
children: vec![],
disabled: false,
}
}
pub fn child(mut self, option: CascaderOption) -> Self {
self.children.push(option);
self
}
pub fn disabled(mut self, disabled: bool) -> Self {
self.disabled = disabled;
self
}
}
/// Cascader props
#[derive(Props, Clone, PartialEq)]
pub struct CascaderProps {
#[props(default)]
pub options: Vec<CascaderOption>,
#[props(default)]
pub model_value: Vec<String>,
#[props(default = "Select".to_string())]
pub placeholder: String,
#[props(default = false)]
pub disabled: bool,
#[props(default = false)]
pub clearable: bool,
#[props(default = false)]
pub filterable: bool,
#[props(default = true)]
pub show_all_levels: bool,
#[props(default = " / ".to_string())]
pub separator: String,
#[props(default)]
pub on_change: Option<EventHandler<Vec<String>>>,
#[props(default)]
pub class: Option<String>,
#[props(default)]
pub style: Option<String>,
}
/// Cascader node render data: (value, label, disabled, has_children, is_active)
type NodeRender = (String, String, bool, bool, bool);
/// Cascader component for multi-level selection
#[component]
pub fn Cascader(props: CascaderProps) -> Element {
let mut class_names = vec!["el-cascader".to_string()];
if props.disabled {
class_names.push("is-disabled".to_string());
}
if let Some(ref c) = props.class {
class_names.push(c.clone());
}
// Build display text from model_value
let display_text = if props.model_value.is_empty() {
String::new()
} else {
let labels = resolve_path_labels(&props.options, &props.model_value);
if props.show_all_levels {
labels.join(&props.separator)
} else {
labels.last().cloned().unwrap_or_default()
}
};
// Pre-compute level 1 nodes: (value, label, disabled, has_children, is_active)
let level1_nodes: Vec<NodeRender> = props
.options
.iter()
.map(|opt| {
let is_active = props.model_value.first().map_or(false, |v| v == &opt.value);
(opt.value.clone(), opt.label.clone(), opt.disabled, !opt.children.is_empty(), is_active)
})
.collect();
// Pre-compute level 2 nodes: (value, label, disabled, has_children, is_active, l1_value)
let level2_nodes: Vec<(String, String, bool, bool, bool, String)> = if props.model_value.len() >= 1 {
let l1 = &props.model_value[0];
props
.options
.iter()
.find(|o| &o.value == l1)
.map(|parent| {
let l1_val = l1.clone();
parent
.children
.iter()
.map(|opt| {
let is_active = props.model_value.get(1).map_or(false, |v| v == &opt.value);
(opt.value.clone(), opt.label.clone(), opt.disabled, !opt.children.is_empty(), is_active, l1_val.clone())
})
.collect()
})
.unwrap_or_default()
} else {
vec![]
};
// Pre-compute level 3 nodes: (value, label, disabled, has_children, is_active, l1_value, l2_value)
let level3_nodes: Vec<(String, String, bool, bool, bool, String, String)> = if props.model_value.len() >= 2 {
let l1 = &props.model_value[0];
let l2 = &props.model_value[1];
props
.options
.iter()
.find(|o| &o.value == l1)
.and_then(|p1| p1.children.iter().find(|o| &o.value == l2))
.map(|p2| {
let l1_val = l1.clone();
let l2_val = l2.clone();
p2
.children
.iter()
.map(|opt| {
let is_active = props.model_value.get(2).map_or(false, |v| v == &opt.value);
(opt.value.clone(), opt.label.clone(), opt.disabled, !opt.children.is_empty(), is_active, l1_val.clone(), l2_val.clone())
})
.collect()
})
.unwrap_or_default()
} else {
vec![]
};
let has_value = !props.model_value.is_empty();
let placeholder = props.placeholder.clone();
let show_clear = props.clearable && has_value && !props.disabled;
let show_l1 = !level1_nodes.is_empty();
let show_l2 = !level2_nodes.is_empty();
let show_l3 = !level3_nodes.is_empty();
let on_change = props.on_change;
rsx! {
div {
class: "{class_names.join(\" \")}",
style: props.style.clone().unwrap_or_default(),
div {
class: "el-cascader__wrapper",
div {
class: "el-input el-input--suffix el-cascader__input",
input {
class: "el-input__inner",
r#type: "text",
placeholder: "{placeholder}",
readonly: true,
disabled: props.disabled,
value: "{display_text}",
}
if show_clear {
span {
class: "el-input__suffix el-cascader__clear-icon",
onclick: move |_| {
if let Some(handler) = on_change.as_ref() {
handler.call(vec![]);
}
},
i { class: "el-icon-circle-close" }
}
} else {
span {
class: "el-input__suffix el-cascader__arrow-icon",
i { class: "el-icon-arrow-down" }
}
}
}
}
if has_value {
div {
class: "el-cascader__dropdown",
style: "position: absolute; z-index: 2000; margin-top: 4px;",
div {
class: "el-cascader-panel",
style: "display: flex;",
if show_l1 {
div {
class: "el-cascader-menu",
for (value, label, disabled, has_children, is_active) in level1_nodes.into_iter() {
div {
class: if is_active {
"el-cascader-node is-active"
} else if disabled {
"el-cascader-node is-disabled"
} else {
"el-cascader-node"
},
onclick: move |_| {
if !disabled {
if let Some(handler) = on_change.as_ref() {
handler.call(vec![value.clone()]);
}
}
},
span {
class: "el-cascader-node__label",
"{label}"
}
if has_children {
i { class: "el-cascader-node__postfix el-icon-arrow-right" }
}
}
}
}
}
if show_l2 {
div {
class: "el-cascader-menu",
for (value, label, disabled, has_children, is_active, l1_val) in level2_nodes.into_iter() {
div {
class: if is_active {
"el-cascader-node is-active"
} else if disabled {
"el-cascader-node is-disabled"
} else {
"el-cascader-node"
},
onclick: move |_| {
if !disabled {
if let Some(handler) = on_change.as_ref() {
handler.call(vec![l1_val.clone(), value.clone()]);
}
}
},
span {
class: "el-cascader-node__label",
"{label}"
}
if has_children {
i { class: "el-cascader-node__postfix el-icon-arrow-right" }
}
}
}
}
}
if show_l3 {
div {
class: "el-cascader-menu",
for (value, label, disabled, _has_children, is_active, l1_val, l2_val) in level3_nodes.into_iter() {
div {
class: if is_active {
"el-cascader-node is-active"
} else if disabled {
"el-cascader-node is-disabled"
} else {
"el-cascader-node"
},
onclick: move |_| {
if !disabled {
if let Some(handler) = on_change.as_ref() {
handler.call(vec![l1_val.clone(), l2_val.clone(), value.clone()]);
}
}
},
span {
class: "el-cascader-node__label",
"{label}"
}
}
}
}
}
}
}
}
}
}
}
/// Resolve labels for a path of values
fn resolve_path_labels(options: &[CascaderOption], path: &[String]) -> Vec<String> {
let mut labels = vec![];
let mut current_options = options;
for value in path {
if let Some(opt) = current_options.iter().find(|o| &o.value == value) {
labels.push(opt.label.clone());
current_options = &opt.children;
} else {
break;
}
}
labels
}