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
use crate::icon::{Icon, IconKind};
use dioxus::prelude::*;
/// Componente `Textarea` multilinea con supporto ad autosize reattivo, conteggio caratteri,
/// cancellazione rapida (clearable) e integrazione con il tema Shadow.
#[component]
pub fn Textarea(
/// Signal reattivo per il contenuto testuale multilinea.
value: Signal<String>,
/// Etichetta descrittiva posizionata sopra al campo.
#[props(default)]
label: Option<String>,
/// Testo segnaposto informativo visibile quando il campo รจ vuoto.
#[props(default)]
placeholder: Option<String>,
/// Testo di aiuto o istruzione secondaria sotto al campo.
#[props(default)]
help_text: Option<String>,
/// Messaggio di errore che evidenzia il bordo in rosso fenice e mostra il messaggio.
#[props(default)]
error: Option<String>,
/// Disabilita il campo impedendo l'interazione.
#[props(default)]
disabled: bool,
/// Rende il campo in sola lettura.
#[props(default)]
readonly: bool,
/// Numero minimo di righe visualizzate (default: 3).
#[props(default = 3)]
min_rows: usize,
/// Numero massimo di righe per espansione autosize (default: None, illimitato).
#[props(default)]
max_rows: Option<usize>,
/// Abilita l'espansione verticale automatica in proporzione alle righe digitate.
#[props(default)]
autosize: bool,
/// Mostra il conteggio dei caratteri in basso a destra.
#[props(default)]
show_count: bool,
/// Limite massimo di caratteri consentiti.
#[props(default)]
max_length: Option<usize>,
/// Mostra un pulsante rapido (X) per cancellare il contenuto.
#[props(default)]
clearable: bool,
/// Permette di ridimensionare manualmente la larghezza in basso a destra (resize orizzontale o both).
#[props(default)]
resize_width: bool,
/// Permette di ridimensionare manualmente l'altezza in basso a destra (resize verticale o both).
#[props(default)]
resize_height: bool,
/// Alone ottico perimetrale coordinato con il tema attivo.
#[props(default)]
glow: Option<bool>,
/// Classi CSS o SCSS addizionali.
#[props(default)]
class: String,
/// Callback invocata ad ogni variazione del testo.
#[props(default)]
on_change: Option<EventHandler<String>>,
) -> Element {
#[cfg(feature = "theme")]
let theme_val = crate::theme::use_theme()();
#[cfg(feature = "theme")]
let radius_class = theme_val.radius_scale.md();
#[cfg(feature = "theme")]
let base_override = theme_val.resolve_component_base("textarea").unwrap_or("");
#[cfg(feature = "theme")]
let class_modifiers = theme_val.resolve_class_modifiers("textarea", &class);
#[cfg(feature = "theme")]
let glow_class = theme_val.resolve_elevation_class("textarea", glow);
#[cfg(not(feature = "theme"))]
let base_override = "";
#[cfg(not(feature = "theme"))]
let class_modifiers = String::new();
#[cfg(not(feature = "theme"))]
let radius_class = "";
#[cfg(not(feature = "theme"))]
let glow_class = if glow.unwrap_or(false) {
"sd-textarea--glow".to_string()
} else {
String::new()
};
let text_val = value();
let char_count = text_val.chars().count();
// Calcolo righe dinamiche per autosize
let rows_to_display = if autosize {
let lines = if text_val.is_empty() {
1
} else {
text_val.lines().count().max(1)
};
let mut r = lines.max(min_rows);
if let Some(max_r) = max_rows {
r = r.min(max_r);
}
r
} else {
min_rows
};
let resize_style = match (resize_width, resize_height) {
(true, true) => "resize: both;",
(true, false) => "resize: horizontal;",
(false, true) => "resize: vertical;",
(false, false) => "resize: none;",
};
let has_clear_btn = clearable && !text_val.is_empty() && !disabled && !readonly;
let autosize_class = if autosize { "sd-textarea--autosize" } else { "" };
let error_class = if error.is_some() { "sd-textarea--error" } else { "" };
let disabled_class = if disabled { "sd-textarea--disabled" } else { "" };
let clear_padding_class = if has_clear_btn { "sd-textarea--has-clear" } else { "" };
let is_at_limit = if let Some(max) = max_length {
char_count >= max
} else {
false
};
rsx! {
div {
class: "sd-textarea-wrapper {class_modifiers} {class}",
if let Some(lbl) = label {
label { class: "sd-textarea-label", "{lbl}" }
}
div {
class: "sd-textarea-container",
textarea {
class: "sd-textarea {autosize_class} {error_class} {disabled_class} {clear_padding_class} {base_override} {radius_class} {glow_class}",
style: "{resize_style}",
placeholder: placeholder.unwrap_or_default(),
disabled,
readonly,
rows: "{rows_to_display}",
maxlength: max_length.map(|m| m.to_string()),
value: "{text_val}",
oninput: move |e| {
let new_val = e.value();
*value.write() = new_val.clone();
if let Some(h) = &on_change {
h.call(new_val);
}
},
}
if has_clear_btn {
button {
r#type: "button",
class: "sd-textarea-clear",
title: "Cancella testo",
onclick: move |e| {
e.stop_propagation();
*value.write() = String::new();
if let Some(h) = &on_change {
h.call(String::new());
}
},
Icon { kind: IconKind::X, size: 12 }
}
}
}
if error.is_some() || help_text.is_some() || show_count {
div {
class: "sd-textarea-footer",
if let Some(err) = error {
span { class: "sd-textarea-error", "{err}" }
} else if let Some(help) = help_text {
span { class: "sd-textarea-help", "{help}" }
} else {
span {}
}
if show_count {
span {
class: if is_at_limit { "sd-textarea-counter sd-textarea-counter--limit" } else { "sd-textarea-counter" },
if let Some(max) = max_length {
"{char_count} / {max}"
} else {
"{char_count} caratteri"
}
}
}
}
}
}
}
}
/// Variante con espansione verticale dinamica proporzionale al contenuto abilitata per impostazione predefinita.
#[component]
pub fn AutoResizeTextarea(
/// Signal reattivo per il contenuto testuale multilinea.
value: Signal<String>,
/// Etichetta descrittiva superiore.
#[props(default)]
label: Option<String>,
/// Testo segnaposto informativo visibile a campo vuoto.
#[props(default)]
placeholder: Option<String>,
/// Testo di aiuto o istruzione secondaria sotto al campo.
#[props(default)]
help_text: Option<String>,
/// Messaggio di errore.
#[props(default)]
error: Option<String>,
/// Disabilita il campo.
#[props(default)]
disabled: bool,
/// Rende il campo in sola lettura.
#[props(default)]
readonly: bool,
/// Numero minimo di righe visualizzate (default: 2).
#[props(default = 2)]
min_rows: usize,
/// Numero massimo di righe per espansione autosize (default: None).
#[props(default)]
max_rows: Option<usize>,
/// Mostra il conteggio dei caratteri in basso a destra.
#[props(default)]
show_count: bool,
/// Limite massimo di caratteri consentiti.
#[props(default)]
max_length: Option<usize>,
/// Mostra un pulsante rapido per cancellare il testo.
#[props(default)]
clearable: bool,
/// Permette di ridimensionare manualmente la larghezza in basso a destra (resize orizzontale o both).
#[props(default)]
resize_width: bool,
/// Permette di ridimensionare manualmente l'altezza in basso a destra (resize verticale o both).
#[props(default)]
resize_height: bool,
/// Alone ottico perimetrale coordinato con il tema attivo.
#[props(default)]
glow: Option<bool>,
/// Classi CSS o SCSS addizionali.
#[props(default)]
class: String,
/// Callback invocata ad ogni variazione del testo.
#[props(default)]
on_change: Option<EventHandler<String>>,
) -> Element {
rsx! {
Textarea {
value,
label,
placeholder,
help_text,
error,
disabled,
readonly,
min_rows,
max_rows,
autosize: true,
show_count,
max_length,
clearable,
resize_width,
resize_height,
glow,
class,
on_change,
}
}
}
#[cfg(test)]
mod tests {
#[test]
fn test_autosize_row_calculation() {
let text = "riga 1\nriga 2\nriga 3\nriga 4\n";
// lines() esclude l'eventuale newline finale vuoto per evitare spazio morto
let lines = text.lines().count().max(1);
assert_eq!(lines, 4);
let min_rows = 2;
let max_rows = 3;
let calculated = lines.max(min_rows).min(max_rows);
assert_eq!(calculated, 3);
}
#[test]
fn test_char_count() {
let text = "ombra fenice";
assert_eq!(text.chars().count(), 12);
}
}