Skip to main content

eui_core/layout_tree/
fields.rs

1//! The form controls (a themed surface expands each into its recipe parts):
2//! Input/Textarea, Select (the combobox, its popup a LAYER), DatePicker and
3//! DateRangePicker (one calendar layer), TimePicker (a list layer),
4//! NumberField (steppers), ChipInput (chips + suggestions layer), FileUpload
5//! (drop zone + files), Checkbox/Switch, Radio, Slider and the Form that
6//! collects them. A field's failed checks (after a refused submit) show
7//! under it as `<field id>.error` and put its parts in the `invalid` state.
8
9use serde_json::{json, Map, Value};
10
11use super::{bool_prop, js, str_prop, Builder, LNode, LayerPlacement, NodeKind};
12use crate::overlay::{OverlayAlign, OverlaySide};
13use crate::theme::RecipeQuery;
14use crate::types::UiNode;
15
16/// Calendar arithmetic for the pickers (proleptic Gregorian, no clock).
17pub mod dates {
18    pub fn is_leap(y: i32) -> bool {
19        (y % 4 == 0 && y % 100 != 0) || y % 400 == 0
20    }
21
22    pub fn days_in_month(y: i32, m: u32) -> u32 {
23        match m {
24            1 | 3 | 5 | 7 | 8 | 10 | 12 => 31,
25            4 | 6 | 9 | 11 => 30,
26            _ => {
27                if is_leap(y) {
28                    29
29                } else {
30                    28
31                }
32            }
33        }
34    }
35
36    /// 0 = Sunday … 6 = Saturday (Sakamoto).
37    pub fn weekday(y: i32, m: u32, d: u32) -> u32 {
38        const T: [i32; 12] = [0, 3, 2, 5, 0, 3, 5, 1, 4, 6, 2, 4];
39        let y = if m < 3 { y - 1 } else { y };
40        ((y + y / 4 - y / 100 + y / 400 + T[(m - 1) as usize] + d as i32).rem_euclid(7)) as u32
41    }
42
43    /// `yyyy-mm-dd` → (y, m, d).
44    pub fn parse(s: &str) -> Option<(i32, u32, u32)> {
45        let b = s.get(0..10)?;
46        let mut it = b.split('-');
47        let y = it.next()?.parse().ok()?;
48        let m: u32 = it.next()?.parse().ok()?;
49        let d: u32 = it.next()?.parse().ok()?;
50        ((1..=12).contains(&m) && d >= 1 && d <= days_in_month(y, m)).then_some((y, m, d))
51    }
52
53    pub fn format(y: i32, m: u32, d: u32) -> String {
54        format!("{y:04}-{m:02}-{d:02}")
55    }
56
57    /// The month after/before.
58    pub fn shift_month(y: i32, m: u32, delta: i32) -> (i32, u32) {
59        let total = y * 12 + (m as i32 - 1) + delta;
60        (total.div_euclid(12), (total.rem_euclid(12) + 1) as u32)
61    }
62
63    /// The day `n` days after (y, m, d).
64    pub fn add_days(y: i32, m: u32, d: u32, n: i32) -> (i32, u32, u32) {
65        let (mut y, mut m, mut d) = (y, m, d as i32 + n);
66        while d < 1 {
67            let (py, pm) = shift_month(y, m, -1);
68            y = py;
69            m = pm;
70            d += days_in_month(y, m) as i32;
71        }
72        while d > days_in_month(y, m) as i32 {
73            d -= days_in_month(y, m) as i32;
74            let (ny, nm) = shift_month(y, m, 1);
75            y = ny;
76            m = nm;
77        }
78        (y, m, d as u32)
79    }
80
81    pub const MONTHS: [&str; 12] = ["January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"];
82    pub const WEEKDAYS_SHORT: [&str; 7] = ["Su", "Mo", "Tu", "We", "Th", "Fr", "Sa"];
83
84    /// `Oct 7, 2026` (the `en` medium date; natives format in the surface
85    /// locale from the ISO value).
86    pub fn label(s: &str) -> String {
87        match parse(s) {
88            Some((y, m, d)) => format!("{} {d}, {y}", &MONTHS[(m - 1) as usize][..3]),
89            None => s.to_string(),
90        }
91    }
92}
93
94/// `HH:mm` → minutes since midnight.
95pub fn parse_time(s: &str) -> Option<u32> {
96    let (h, m) = s.split_once(':')?;
97    let h: u32 = h.parse().ok()?;
98    let m: u32 = m.get(0..2)?.parse().ok()?;
99    (h < 24 && m < 60).then_some(h * 60 + m)
100}
101
102pub fn format_time(minutes: u32) -> String {
103    format!("{:02}:{:02}", minutes / 60, minutes % 60)
104}
105
106/// Round a plain non-negative decimal string (`123.4567`, no exponent) to
107/// `p` fraction digits, ties AWAY from zero (a next digit ≥ 5 rounds up).
108fn round_decimal(s: &str, p: usize) -> String {
109    let (int, frac) = s.split_once('.').unwrap_or((s, ""));
110    let mut digits: Vec<u8> = int.bytes().chain(frac.bytes().chain(std::iter::repeat(b'0')).take(p)).collect();
111    if frac.as_bytes().get(p).copied().unwrap_or(b'0') >= b'5' {
112        let mut k = digits.len();
113        loop {
114            if k == 0 {
115                digits.insert(0, b'1');
116                break;
117            }
118            k -= 1;
119            if digits[k] == b'9' {
120                digits[k] = b'0';
121            } else {
122                digits[k] += 1;
123                break;
124            }
125        }
126    }
127    let split = digits.len() - p;
128    let mut out = String::from_utf8_lossy(&digits[..split]).into_owned();
129    if out.is_empty() {
130        out.push('0');
131    }
132    if p > 0 {
133        out.push('.');
134        out.push_str(&String::from_utf8_lossy(&digits[split..]));
135    }
136    out
137}
138
139/// JavaScript's `String(n)` ([`crate::json::number_to_string`]).
140pub fn js_number_string(n: f64) -> String {
141    crate::json::number_to_string(n)
142}
143
144/// The reference's `decimalsOf(step)`: the digits after the `.` of
145/// `String(step)`.
146pub fn decimals_of(n: f64) -> u32 {
147    let s = js_number_string(n);
148    s.find('.').map(|i| (s.len() - i - 1) as u32).unwrap_or(0)
149}
150
151/// JavaScript's `n.toFixed(p)`: the EXACT binary value rounded half away
152/// from zero (`(2.5).toFixed(0)` = `3`, `(1.005).toFixed(2)` = `1.00`).
153pub fn to_fixed(n: f64, p: u32) -> String {
154    if !n.is_finite() || n.abs() >= 1e21 {
155        return js_number_string(n);
156    }
157    let p = p.min(100) as usize;
158    // Every f64 has at most 1074 fraction digits: this is the exact value.
159    let exact = format!("{:.1100}", n.abs());
160    let body = round_decimal(&exact, p);
161    // `(-0.04).toFixed(1)` = "-0.0"; `(-0).toFixed(1)` = "0.0".
162    if n < 0.0 {
163        format!("-{body}")
164    } else {
165        body
166    }
167}
168
169/// A NumberField's value as it shows: `Intl.NumberFormat("en", {minimum +
170/// maximumFractionDigits: precision})`: the SHORTEST round-trip decimal
171/// rounded half away from zero (`1.005` → `1.01`, `2.5` → `3`), grouped.
172pub fn format_number_value(n: f64, precision: u32) -> String {
173    if !n.is_finite() {
174        return js_number_string(n);
175    }
176    let shortest = format!("{}", n.abs());
177    let rounded = round_decimal(&shortest, precision.min(100) as usize);
178    let (int, frac) = rounded.split_once('.').unwrap_or((&rounded, ""));
179    let mut out = String::new();
180    if n.is_sign_negative() {
181        out.push('-');
182    }
183    let len = int.len();
184    for (i, c) in int.chars().enumerate() {
185        if i > 0 && (len - i) % 3 == 0 {
186            out.push(',');
187        }
188        out.push(c);
189    }
190    if !frac.is_empty() {
191        out.push('.');
192        out.push_str(frac);
193    }
194    out
195}
196
197/// A NumberField's step (`step || 1`) and precision (`precision`, else the
198/// step's decimals), as the reference reads them.
199pub fn number_step_precision(props: &crate::types::Props) -> (f64, u32) {
200    let step = props.get("step").map(crate::json::to_number).filter(|s| *s != 0.0 && s.is_finite()).unwrap_or(1.0);
201    let precision = match props.get("precision") {
202        Some(p) => crate::json::to_number(p).max(0.0).floor().min(100.0) as u32,
203        None => decimals_of(step),
204    };
205    (step, precision)
206}
207
208/// A numeric NumberField prop the reference's way (`num`): numbers and
209/// numeric strings; absent, null or `""` = none.
210pub fn number_prop(props: &crate::types::Props, key: &str) -> Option<f64> {
211    match props.get(key)? {
212        Value::Number(n) => n.as_f64(),
213        Value::String(s) if !s.trim().is_empty() => s.trim().parse::<f64>().ok().filter(|v| v.is_finite()).or(Some(0.0)),
214        Value::Null => None,
215        Value::String(_) => None,
216        _ => Some(0.0),
217    }
218}
219
220/// The reference's `clamp`: into `[min, max]`, then `Number(v.toFixed(p))`.
221pub fn clamp_number_value(v: f64, min: Option<f64>, max: Option<f64>, precision: u32) -> f64 {
222    let mut v = v;
223    if let Some(m) = min {
224        v = v.max(m);
225    }
226    if let Some(m) = max {
227        v = v.min(m);
228    }
229    to_fixed(v, precision).parse().unwrap_or(v)
230}
231
232/// A byte count as the web shows it (`Intl` `style: "unit"`, short): 1024
233/// steps, at most one fraction digit, through [`Formatter::bytes`]
234/// (`47.1 kB` in the English fallback).
235///
236/// [`Formatter::bytes`]: crate::format::Formatter::bytes
237pub fn format_bytes_with(formatter: &dyn crate::format::Formatter, n: f64) -> String {
238    use crate::format::ByteUnit;
239    const UNITS: [ByteUnit; 4] = [ByteUnit::Byte, ByteUnit::Kilobyte, ByteUnit::Megabyte, ByteUnit::Gigabyte];
240    let mut v = n.max(0.0);
241    let mut i = 0;
242    while v >= 1024.0 && i < UNITS.len() - 1 {
243        v /= 1024.0;
244        i += 1;
245    }
246    formatter.bytes((v * 10.0).round() / 10.0, UNITS[i])
247}
248
249/// [`format_bytes_with`] through the English formatter.
250pub fn format_bytes(n: f64) -> String {
251    format_bytes_with(&crate::format::EnglishFormatter, n)
252}
253
254impl Builder<'_, '_> {
255    /// The column frame every labelled field shares: label first.
256    fn field_open(&mut self, index: u32) -> LNode {
257        self.style_default(index, &[("display", json!("flex")), ("flexDirection", json!("column")), ("gap", json!("$spacing.xs"))]);
258        self.nodes[index as usize].kind = NodeKind::Container;
259        let owner = self.nodes[index as usize].clone();
260        if let Some(label) = str_prop(&owner.props, "label") {
261            self.text_part(index, &owner, "label", label, "label", "");
262        }
263        owner
264    }
265
266    /// The failed checks of a field (after a refused submit) plus an
267    /// authored `error`.
268    fn field_errors(&self, owner: &LNode) -> Vec<String> {
269        let mut out: Vec<String> = self.ctx.local.errors.get(&owner.id).map(|e| e.messages.clone()).unwrap_or_default();
270        if let Some(e) = str_prop(&owner.props, "error") {
271            out.push(e.to_string());
272        }
273        out
274    }
275
276    /// Description and error lines under the control; `control` (the part
277    /// that IS the control) gets the `invalid` state.
278    fn field_close(&mut self, owner: &LNode, control: Option<u32>) {
279        if let Some(d) = str_prop(&owner.props, "description") {
280            self.text_part(owner.index, owner, "description", d, "muted", "");
281        }
282        let errors = self.field_errors(owner);
283        if !errors.is_empty() {
284            let e = self.text_part(owner.index, owner, "error", &errors.join("\n"), "caption", "");
285            self.add_state(e, "invalid");
286            if let Some(c) = control {
287                self.add_state(c, "invalid");
288            }
289        }
290    }
291
292    /// Props for a field's control part: the owner's minus the frame's.
293    fn control_props(owner: &LNode) -> Value {
294        let mut props = owner.props.clone();
295        for k in ["label", "description", "style", "checks", "error"] {
296            props.shift_remove(k);
297        }
298        Value::Object(props)
299    }
300
301    /// Input / Textarea: label, the host-measured field, description, error.
302    pub(crate) fn field(&mut self, index: u32) {
303        let owner = self.field_open(index);
304        let f = self.part(&owner, "field", &owner.component, NodeKind::Leaf, json!({"alignSelf": "stretch"}), Self::control_props(&owner));
305        self.nodes[f as usize].pressable = !bool_prop(&owner.props, "disabled");
306        self.field_close(&owner, Some(f));
307    }
308
309    /// A trigger part that opens the owner's popup layer.
310    fn trigger(&mut self, owner: &LNode, text: &str, placeholder: bool, glyph: &str) -> u32 {
311        let mut props = Self::control_props(owner);
312        props["text"] = json!(text);
313        props["placeholder"] = json!(placeholder);
314        props["icon"] = json!(super::builtin_icon(glyph).unwrap_or("ui-selector"));
315        let t = self.part(owner, "trigger", &owner.component, NodeKind::Leaf, json!({"alignSelf": "stretch"}), props);
316        let disabled = bool_prop(&owner.props, "disabled");
317        self.nodes[t as usize].pressable = !disabled;
318        self.nodes[t as usize].trigger_for = Some(owner.id.clone());
319        if self.is_open(&owner.id, &Map::new(), false) {
320            self.add_state(t, "open");
321        }
322        t
323    }
324
325    /// The anchored popup of a field: its layer root (`part`), placed under
326    /// the trigger, at least as wide.
327    fn popup(&mut self, owner: &LNode, part: &str, anchor: u32, style: Value) -> (u32, u32) {
328        let (layer, root) = self.layer_root(owner, part, style);
329        let _ = anchor;
330        (layer, root)
331    }
332
333    /// Select = the combobox: trigger; open → a layer with an optional
334    /// search field, the (filtered) options and `empty`.
335    pub(crate) fn select(&mut self, index: u32) {
336        let owner = self.field_open(index);
337        // VAPP-103: only OBJECT options paint (`options: [null]` skips the entry).
338        let options: Vec<Value> = owner.props.get("options").and_then(Value::as_array).map(|a| a.iter().filter(|o| o.is_object()).cloned().collect()).unwrap_or_default();
339        let multiple = bool_prop(&owner.props, "multiple");
340        let value = owner.props.get("value").map(js).unwrap_or_default();
341        let label_of = |v: &str| options.iter().find(|o| o.get("value").map(js).as_deref() == Some(v)).and_then(|o| o.get("label")).map(js);
342        // VAPP-146: "" is an ordinary single value: an option holding it (an
343        // unseeded value reads as "") is the one chosen.
344        let chosen: Vec<String> = if multiple { value.split(',').filter(|s| !s.is_empty()).map(str::to_string).collect() } else { vec![value.clone()].into_iter().filter(|v| !v.is_empty() || label_of(v).is_some()).collect() };
345        let shown: Vec<String> = chosen.iter().map(|v| label_of(v).unwrap_or_else(|| v.clone())).collect();
346        let placeholder = str_prop(&owner.props, "placeholder").map(str::to_string).unwrap_or_else(|| self.string("choose"));
347        let (text, is_placeholder) = if shown.is_empty() { (placeholder, true) } else { (shown.join(", "), false) };
348        let trigger = self.trigger(&owner, &text, is_placeholder, "Select.trigger");
349        self.field_close(&owner, Some(trigger));
350        if !self.is_open(&owner.id, &Map::new(), false) {
351            return;
352        }
353        let (layer, root) = self.popup(&owner, "content", trigger, json!({"display": "flex", "flexDirection": "column", "gap": "$spacing.xxs", "padding": "$spacing.xs", "borderRadius": "$radius.md", "maxHeight": 320}));
354        let query = self.ctx.local.query.get(&owner.id).cloned().unwrap_or_default();
355        if bool_prop(&owner.props, "searchable") {
356            let search = self.part_in(root, &owner, "search", "Select", NodeKind::Leaf, json!({"alignSelf": "stretch", "flexShrink": 0}), json!({"placeholder": self.string("search"), "value": query, "icon": super::builtin_icon("Select.search")}), "");
357            self.nodes[search as usize].pressable = true;
358        }
359        let list = self.part_in(root, &owner, "list", "Box", NodeKind::Container, json!({"display": "flex", "flexDirection": "column", "flexShrink": 1, "minHeight": 0, "overflowY": "scroll"}), json!({}), "");
360        self.nodes[list as usize].part_query = None;
361        let needle = query.to_lowercase();
362        let mut shown_any = false;
363        for (i, option) in options.iter().enumerate() {
364            let label = option.get("label").map(js).unwrap_or_default();
365            if !needle.is_empty() && !label.to_lowercase().contains(&needle) {
366                continue;
367            }
368            shown_any = true;
369            let v = option.get("value").map(js).unwrap_or_default();
370            let selected = chosen.contains(&v);
371            let disabled = option.get("disabled").and_then(Value::as_bool).unwrap_or(false);
372            let mut props = json!({"text": label, "value": v, "lines": 1, "selected": selected, "disabled": disabled});
373            if let Some(icon) = option.get("icon") {
374                props["icon"] = icon.clone();
375            }
376            if selected {
377                props["check"] = json!(super::builtin_icon("Select.check"));
378            }
379            let item = self.part_in(list, &owner, "item", "Text", NodeKind::Leaf, json!({"display": "flex", "flexDirection": "row", "alignItems": "center"}), props, &format!(".{i}"));
380            self.nodes[item as usize].pressable = !disabled;
381            if selected {
382                self.add_state(item, "selected");
383            }
384            if disabled {
385                self.add_state(item, "disabled");
386            }
387        }
388        if !shown_any {
389            let empty = owner.props.get("emptyText").map(js).unwrap_or_else(|| self.string("noResults"));
390            self.text_part(list, &owner, "empty", &empty, "muted", "");
391        }
392        self.push_layer(layer, root, &owner, LayerPlacement::Anchored { anchor: trigger, side: OverlaySide::Bottom, align: OverlayAlign::Start }, false, true, true, Some(list));
393    }
394
395    /// A `yyyy-mm-dd` as the surface formatter's medium date (the raw
396    /// value when unreadable).
397    pub(crate) fn date_label(&self, iso: &str) -> String {
398        match crate::format::format_date_value(self.ctx.formatter, &json!(iso), &crate::format::DateOptions::default()) {
399            s if s.is_empty() => iso.to_string(),
400            s => s,
401        }
402    }
403
404    /// A `yyyy-mm-dd` through a TR35 pattern of the surface formatter.
405    pub(crate) fn date_pattern(&self, iso: &str, pattern: &str) -> String {
406        crate::format::format_date_value(self.ctx.formatter, &json!(iso), &crate::format::DateOptions { format: Some(pattern.to_string()), ..Default::default() })
407    }
408
409    /// DatePicker / DateRangePicker: trigger; open → one calendar layer:
410    /// header (previous, the month title, next), the weekday row, six weeks
411    /// of `day` cells from the week start (`firstDayOfWeek`, else the
412    /// locale's).
413    pub(crate) fn date_picker(&mut self, index: u32) {
414        let owner = self.field_open(index);
415        let range = owner.component == "DateRangePicker";
416        let (start, end) = if range {
417            (owner.props.get("start").and_then(Value::as_str).unwrap_or("").to_string(), owner.props.get("end").and_then(Value::as_str).unwrap_or("").to_string())
418        } else {
419            (owner.props.get("value").and_then(Value::as_str).unwrap_or("").to_string(), String::new())
420        };
421        let placeholder = str_prop(&owner.props, "placeholder").map(str::to_string).unwrap_or_else(|| self.string("choose"));
422        let text = match (range, start.is_empty()) {
423            (_, true) => placeholder.clone(),
424            (false, false) => self.date_label(&start),
425            (true, false) => format!("{} – {}", self.date_label(&start), if end.is_empty() { "…".to_string() } else { self.date_label(&end) }),
426        };
427        let glyph = format!("{}.trigger", owner.component);
428        let trigger = self.trigger(&owner, &text, start.is_empty(), &glyph);
429        self.field_close(&owner, Some(trigger));
430        if !self.is_open(&owner.id, &Map::new(), false) {
431            return;
432        }
433        let today = self.today();
434        let (y, m) = self.ctx.local.month.get(&owner.id).copied().or_else(|| dates::parse(&start).map(|(y, m, _)| (y, m))).or_else(|| today.as_deref().and_then(dates::parse).map(|(y, m, _)| (y, m))).or_else(|| owner.props.get("min").and_then(Value::as_str).and_then(dates::parse).map(|(y, m, _)| (y, m))).unwrap_or((2026, 1));
435        let first_dow = owner.props.get("firstDayOfWeek").and_then(Value::as_u64).map(|d| (d % 7) as u32).unwrap_or_else(|| crate::locale::week_start(self.ctx.locale) as u32);
436        let (layer, root) = self.popup(&owner, "calendar", trigger, json!({"display": "flex", "flexDirection": "column", "gap": "$spacing.xs", "padding": "$spacing.sm", "borderRadius": "$radius.md"}));
437        let header = self.part_in(root, &owner, "header", "Box", NodeKind::Container, json!({"display": "flex", "flexDirection": "row", "alignItems": "center", "justifyContent": "space-between", "gap": "$spacing.xs"}), json!({}), "");
438        self.nodes[header as usize].part_query = None;
439        let button = |b: &mut Self, parent: u32, part: &str, glyph: &str, label_id: &str| {
440            let label = b.string(label_id);
441            let n = b.part_in(parent, &owner, part, "Button", NodeKind::Leaf, json!({"flexShrink": 0}), json!({"icon": super::builtin_icon(glyph), "label": label, "variant": "ghost", "size": "icon"}), "");
442            b.nodes[n as usize].pressable = true;
443            b.nodes[n as usize].own_query = Some(RecipeQuery::new("Button", "root", b.ctx.recipes.native_recipe_props("Button", &b.nodes[n as usize].props.clone()), Vec::new()));
444            n
445        };
446        button(self, header, "previous", &format!("{}.previousMonth", "DatePicker"), "previousMonth");
447        // Round 2: month and weekday names through the surface formatter.
448        let title = self.date_pattern(&dates::format(y, m, 1), "MMMM yyyy");
449        let t = self.part_in(header, &owner, "title", "Text", NodeKind::Leaf, json!({"flexGrow": 1, "textAlign": "center"}), json!({"text": title, "variant": "label", "month": m, "year": y}), "");
450        self.nodes[t as usize].live = Some("polite".into());
451        button(self, header, "next", "DatePicker.nextMonth", "nextMonth");
452        let week_style = json!({"display": "flex", "flexDirection": "row", "gap": "$spacing.xxs"});
453        let cell_style = json!({"flexGrow": 1, "flexBasis": 0, "minWidth": 32, "height": 32, "textAlign": "center"});
454        let weekdays = self.part_in(root, &owner, "weekdays", "Box", NodeKind::Container, week_style.clone(), json!({}), "");
455        self.nodes[weekdays as usize].part_query = None;
456        for c in 0..7u32 {
457            let dow = (first_dow + c) % 7;
458            // 2026-01-04 is a Sunday.
459            let name = self.date_pattern(&dates::format(2026, 1, 4 + dow), "EEE");
460            let w = self.part_in(weekdays, &owner, "weekday", "Text", NodeKind::Leaf, cell_style.clone(), json!({"text": name, "variant": "caption", "weekday": dow}), &format!(".{c}"));
461            self.nodes[w as usize].part_query = None;
462        }
463        let lead = (dates::weekday(y, m, 1) + 7 - first_dow) % 7;
464        let (sy, sm, sd) = dates::add_days(y, m, 1, -(lead as i32));
465        let min = owner.props.get("min").and_then(Value::as_str).unwrap_or("").to_string();
466        let max = owner.props.get("max").and_then(Value::as_str).unwrap_or("").to_string();
467        let anchor = self.ctx.local.range_anchor.get(&owner.id).cloned();
468        let (lo, hi) = match (&anchor, range) {
469            (Some(a), true) => (a.clone(), String::new()),
470            _ => (start.clone(), end.clone()),
471        };
472        for r in 0..6 {
473            let week = self.part_in(root, &owner, "week", "Box", NodeKind::Container, week_style.clone(), json!({}), &format!(".{r}"));
474            self.nodes[week as usize].part_query = None;
475            for c in 0..7 {
476                let (dy, dm, dd) = dates::add_days(sy, sm, sd, r * 7 + c);
477                let date = dates::format(dy, dm, dd);
478                let outside = dm != m;
479                let disabled = (!min.is_empty() && date < min) || (!max.is_empty() && date > max);
480                let is_start = !lo.is_empty() && date == lo;
481                let is_end = range && !hi.is_empty() && date == hi;
482                let in_range = range && !lo.is_empty() && !hi.is_empty() && date > lo && date < hi;
483                let selected = is_start || is_end;
484                let props = json!({"text": dd.to_string(), "date": date, "outside": outside, "disabled": disabled, "selected": selected, "inRange": in_range, "rangeStart": range && is_start, "rangeEnd": is_end, "today": today.as_deref() == Some(date.as_str())});
485                let day = self.part_in(week, &owner, "day", "Text", NodeKind::Leaf, cell_style.clone(), props, &format!(".{r}.{c}"));
486                // The cell's name = the full localized date (the reference's
487                // `{weekday: long, year, month: long, day}`).
488                let label = crate::format::format_date_value(self.ctx.formatter, &json!(date), &crate::format::DateOptions { style: Some(crate::format::DateStyle::Full), ..Default::default() });
489                self.default_a11y(day, "label", json!(label));
490                self.nodes[day as usize].pressable = !disabled;
491                if selected {
492                    self.add_state(day, "selected");
493                }
494                if disabled {
495                    self.add_state(day, "disabled");
496                }
497                if in_range {
498                    self.query_prop(day, "inRange", Value::Bool(true));
499                }
500            }
501        }
502        self.push_layer(layer, root, &owner, LayerPlacement::Anchored { anchor: trigger, side: OverlaySide::Bottom, align: OverlayAlign::Start }, false, true, false, None);
503    }
504
505    /// The host's notion of today (`SurfaceSettings.today`), if set.
506    fn today(&self) -> Option<String> {
507        self.ctx.today.clone()
508    }
509
510    /// TimePicker: trigger; open → a scrolling list layer every `step`
511    /// minutes from `min` to `max`.
512    pub(crate) fn time_picker(&mut self, index: u32) {
513        let owner = self.field_open(index);
514        let value = owner.props.get("value").and_then(Value::as_str).unwrap_or("").to_string();
515        let placeholder = str_prop(&owner.props, "placeholder").map(str::to_string).unwrap_or_else(|| "--:--".to_string());
516        let trigger = self.trigger(&owner, if value.is_empty() { &placeholder } else { &value }, value.is_empty(), "TimePicker.trigger");
517        self.field_close(&owner, Some(trigger));
518        if !self.is_open(&owner.id, &Map::new(), false) {
519            return;
520        }
521        let step = owner.props.get("step").and_then(Value::as_f64).unwrap_or(15.0).max(1.0) as u32;
522        let min = owner.props.get("min").and_then(Value::as_str).and_then(parse_time).unwrap_or(0);
523        let max = owner.props.get("max").and_then(Value::as_str).and_then(parse_time).unwrap_or(23 * 60 + 59);
524        let (layer, root) = self.popup(&owner, "list", trigger, json!({"display": "flex", "flexDirection": "column", "padding": "$spacing.xs", "borderRadius": "$radius.md", "maxHeight": 240, "overflowY": "scroll"}));
525        let mut t = min;
526        let mut i = 0;
527        while t <= max && i < 24 * 60 {
528            let label = format_time(t);
529            let selected = label == value;
530            let item = self.part_in(root, &owner, "item", "Text", NodeKind::Leaf, json!({}), json!({"text": label, "value": label, "lines": 1, "selected": selected}), &format!(".{i}"));
531            self.nodes[item as usize].pressable = true;
532            if selected {
533                self.add_state(item, "selected");
534            }
535            t += step;
536            i += 1;
537        }
538        self.push_layer(layer, root, &owner, LayerPlacement::Anchored { anchor: trigger, side: OverlaySide::Bottom, align: OverlayAlign::Start }, false, true, true, Some(root));
539    }
540
541    /// NumberField: the bordered `field` box holds decrement, the host text
542    /// `input`, the unit and increment.
543    pub(crate) fn number_field(&mut self, index: u32) {
544        let owner = self.field_open(index);
545        let value = number_prop(&owner.props, "value");
546        let min = number_prop(&owner.props, "min");
547        let max = number_prop(&owner.props, "max");
548        let (_, precision) = number_step_precision(&owner.props);
549        let disabled = bool_prop(&owner.props, "disabled");
550        let field = self.part(&owner, "field", "Box", NodeKind::Container, json!({"display": "flex", "flexDirection": "row", "alignItems": "center", "alignSelf": "stretch", "gap": "$spacing.xxs"}), json!({}));
551        // The reference's `part('field', disabled && 'disabled')`: the whole
552        // frame dims (its recipe's `disabled` opacity), not only the value.
553        if disabled {
554            self.add_state(field, "disabled");
555        }
556        let stepper = |b: &mut Self, part: &str, at_limit: bool| {
557            let label = b.string(part);
558            let glyph = format!("NumberField.{part}");
559            let n = b.part_in(field, &owner, part, "Button", NodeKind::Leaf, json!({"flexShrink": 0}), json!({"icon": super::builtin_icon(&glyph), "label": label, "variant": "ghost", "size": "icon", "disabled": disabled || at_limit}), "");
560            b.nodes[n as usize].pressable = !(disabled || at_limit);
561            if disabled || at_limit {
562                b.add_state(n, "disabled");
563            }
564            n
565        };
566        stepper(self, "decrement", value.zip(min).is_some_and(|(v, m)| v <= m));
567        let text = value.map(|v| self.ctx.formatter.number(v, crate::format::NumberOptions { decimals: Some(precision), grouping: true })).unwrap_or_default();
568        let mut props = Self::control_props(&owner);
569        props["text"] = json!(text);
570        let input = self.part_in(field, &owner, "input", "NumberField", NodeKind::Leaf, json!({"flexGrow": 1, "flexBasis": 0, "minWidth": 40}), props, "");
571        self.nodes[input as usize].pressable = !disabled;
572        if let Some(unit) = str_prop(&owner.props, "unit") {
573            self.text_part(field, &owner, "unit", unit, "muted", "");
574        }
575        stepper(self, "increment", value.zip(max).is_some_and(|(v, m)| v >= m));
576        self.field_close(&owner, Some(field));
577    }
578
579    /// ChipInput: the `field` box wraps the chips (label + remove) and the
580    /// host text `input`; typed text offers matching suggestions in a layer.
581    pub(crate) fn chip_input(&mut self, index: u32) {
582        let owner = self.field_open(index);
583        let values: Vec<String> = owner.props.get("values").and_then(Value::as_array).map(|a| a.iter().map(js).collect()).unwrap_or_default();
584        let disabled = bool_prop(&owner.props, "disabled");
585        let field = self.part(&owner, "field", "Box", NodeKind::Container, json!({"display": "flex", "flexDirection": "row", "flexWrap": "wrap", "alignItems": "center", "alignSelf": "stretch", "gap": "$spacing.xs"}), json!({}));
586        if disabled {
587            self.add_state(field, "disabled");
588        }
589        for (i, v) in values.iter().enumerate() {
590            let suffix = format!(".{i}");
591            let chip = self.part_in(field, &owner, "chip", "Box", NodeKind::Container, json!({"display": "flex", "flexDirection": "row", "alignItems": "center", "gap": "$spacing.xxs", "flexShrink": 0}), json!({"value": v}), &suffix);
592            let _ = chip;
593            self.part_in(chip, &owner, "chipLabel", "Text", NodeKind::Leaf, json!({}), json!({"text": v, "lines": 1}), &suffix);
594            if !disabled {
595                let label = self.string_with("removeItem", &[("name", v)]);
596                let r = self.part_in(chip, &owner, "remove", "Icon", NodeKind::Leaf, json!({"flexShrink": 0}), json!({"name": super::builtin_icon("ChipInput.remove"), "size": "sm", "label": label, "value": v}), &suffix);
597                self.nodes[r as usize].pressable = true;
598            }
599        }
600        let query = self.ctx.local.query.get(&owner.id).cloned().unwrap_or_default();
601        let at_max = owner.props.get("max").and_then(Value::as_f64).is_some_and(|m| values.len() as f64 >= m);
602        let placeholder = if values.is_empty() { str_prop(&owner.props, "placeholder").unwrap_or("").to_string() } else { String::new() };
603        let input = self.part_in(field, &owner, "input", "ChipInput", NodeKind::Leaf, json!({"flexGrow": 1, "flexBasis": 80, "minWidth": 80}), json!({"text": query, "placeholder": placeholder, "disabled": disabled || at_max}), "");
604        self.nodes[input as usize].pressable = !disabled;
605        self.field_close(&owner, Some(field));
606        let needle = query.trim().to_lowercase();
607        if needle.is_empty() || disabled || at_max {
608            return;
609        }
610        let suggestions: Vec<Value> = owner
611            .props
612            .get("suggestions")
613            .and_then(Value::as_array)
614            .map(|a| a.iter().filter(|o| o.get("label").map(js).is_some_and(|l| l.to_lowercase().contains(&needle)) && !values.contains(&o.get("value").map(js).unwrap_or_default())).cloned().collect())
615            .unwrap_or_default();
616        if suggestions.is_empty() {
617            return;
618        }
619        let (layer, root) = self.popup(&owner, "suggestions", field, json!({"display": "flex", "flexDirection": "column", "padding": "$spacing.xs", "borderRadius": "$radius.md", "maxHeight": 240, "overflowY": "scroll"}));
620        for (i, o) in suggestions.iter().enumerate() {
621            let item = self.part_in(root, &owner, "suggestion", "Text", NodeKind::Leaf, json!({}), json!({"text": o.get("label").map(js).unwrap_or_default(), "value": o.get("value").map(js).unwrap_or_default(), "lines": 1}), &format!(".{i}"));
622            self.nodes[item as usize].pressable = true;
623        }
624        self.push_layer(layer, root, &owner, LayerPlacement::Anchored { anchor: field, side: OverlaySide::Bottom, align: OverlayAlign::Start }, false, true, true, Some(root));
625    }
626
627    /// FileUpload: the dashed `dropzone` (icon, hint, browse) and one row
628    /// per attached file (name, size, remove).
629    pub(crate) fn file_upload(&mut self, index: u32) {
630        let owner = self.field_open(index);
631        let disabled = bool_prop(&owner.props, "disabled");
632        let zone = self.part(&owner, "dropzone", "Box", NodeKind::Container, json!({"display": "flex", "flexDirection": "column", "alignItems": "center", "gap": "$spacing.xs", "padding": "$spacing.md", "alignSelf": "stretch"}), json!({"accept": owner.props.get("accept").cloned().unwrap_or(Value::Null), "multiple": bool_prop(&owner.props, "multiple")}));
633        self.nodes[zone as usize].pressable = !disabled;
634        self.icon_part(zone, &owner, "icon", "FileUpload.icon", "");
635        let title = self.string("dropFiles");
636        let t = self.text_part(zone, &owner, "title", &title, "label", "");
637        // The title's weight (the web's `.xui-FileUpload-title`); a
638        // `FileUpload/title` recipe overrides it.
639        self.style_default(t, &[("fontWeight", json!("$type.weight.medium"))]);
640        if let Some(hint) = str_prop(&owner.props, "hint") {
641            self.text_part(zone, &owner, "hint", hint, "muted", "");
642        }
643        let browse_label = self.string("browse");
644        // EXP-1563: Browse carries no `disabled` of its own (inert through
645        // `pressable` alone): the disabled dropzone already dims it once,
646        // web's look; the Button's own disabled state on top drew it at a
647        // quarter on the natives.
648        let browse = self.part_in(zone, &owner, "browse", "Button", NodeKind::Leaf, json!({"flexShrink": 0}), json!({"label": browse_label, "variant": "outline", "size": "sm"}), "");
649        self.nodes[browse as usize].pressable = !disabled;
650        self.nodes[browse as usize].own_query = Some(RecipeQuery::new("Button", "root", self.ctx.recipes.native_recipe_props("Button", &self.nodes[browse as usize].props.clone()), Vec::new()));
651        let files = owner.props.get("files").and_then(Value::as_array).cloned().unwrap_or_default();
652        for (i, f) in files.iter().enumerate() {
653            let suffix = format!(".{i}");
654            let name = f.get("name").map(js).unwrap_or_default();
655            let row = self.part_in(owner.index, &owner, "file", "Box", NodeKind::Container, json!({"display": "flex", "flexDirection": "row", "alignItems": "center", "gap": "$spacing.sm"}), json!({"name": name}), &suffix);
656            self.icon_part(row, &owner, "fileIcon", "FileUpload.file", &suffix);
657            self.part_in(row, &owner, "fileName", "Text", NodeKind::Leaf, json!({"flexGrow": 1, "flexShrink": 1, "minWidth": 0}), json!({"text": name, "lines": 1}), &suffix);
658            if let Some(size) = f.get("size").and_then(Value::as_f64) {
659                let meta = format_bytes_with(self.ctx.formatter, size);
660                self.text_part(row, &owner, "fileMeta", &meta, "muted", &suffix);
661            }
662            if !disabled {
663                let label = self.string_with("removeItem", &[("name", &name)]);
664                let r = self.part_in(row, &owner, "remove", "Icon", NodeKind::Leaf, json!({"flexShrink": 0}), json!({"name": super::builtin_icon("FileUpload.remove"), "size": "sm", "label": label, "file": name}), &suffix);
665                self.nodes[r as usize].pressable = true;
666            }
667        }
668        self.field_close(&owner, Some(zone));
669    }
670
671    /// Checkbox / Switch: the control box beside label + description.
672    pub(crate) fn check(&mut self, index: u32) {
673        let owner = self.nodes[index as usize].clone();
674        self.style_default(index, &[("display", json!("flex")), ("flexDirection", json!("row")), ("alignItems", json!("center")), ("gap", json!("$spacing.sm"))]);
675        self.nodes[index as usize].kind = NodeKind::Container;
676        self.nodes[index as usize].pressable = !bool_prop(&owner.props, "disabled");
677        let control_part = if owner.component == "Switch" { "track" } else { "box" };
678        let checked = bool_prop(&owner.props, "checked");
679        let invalid = !self.field_errors(&owner).is_empty();
680        let mut control_props = json!({"checked": checked, "disabled": bool_prop(&owner.props, "disabled")});
681        if owner.component == "Switch" {
682            control_props["size"] = json!(str_prop(&owner.props, "size").unwrap_or("default"));
683        }
684        let control = |b: &mut Self, parent: u32| {
685            let c = b.part_in(parent, &owner, control_part, &owner.component, NodeKind::Leaf, json!({"flexShrink": 0}), control_props.clone(), "");
686            if checked {
687                b.add_state(c, "checked");
688            }
689            if invalid {
690                b.add_state(c, "invalid");
691            }
692            c
693        };
694        let text = |b: &mut Self, parent: u32| {
695            let has_desc = str_prop(&owner.props, "description").is_some();
696            if has_desc {
697                let col = b.part_in(parent, &owner, "body", "Box", NodeKind::Container, json!({"display": "flex", "flexDirection": "column", "gap": "$spacing.xxs", "flexGrow": 1, "minWidth": 0}), json!({}), "");
698                if let Some(label) = str_prop(&owner.props, "label") {
699                    b.text_part(col, &owner, "label", label, "body", "");
700                }
701                if let Some(d) = str_prop(&owner.props, "description") {
702                    b.text_part(col, &owner, "description", d, "muted", "");
703                }
704            } else if let Some(label) = str_prop(&owner.props, "label") {
705                let l = b.text_part(parent, &owner, "label", label, "body", "");
706                b.nodes[l as usize].base_style.insert("flexGrow".into(), json!(1));
707                b.nodes[l as usize].base_style.insert("minWidth".into(), json!(0));
708            }
709        };
710        if owner.component == "Switch" {
711            text(self, index);
712            control(self, index);
713        } else {
714            control(self, index);
715            text(self, index);
716        }
717        let errors = self.field_errors(&owner);
718        if !errors.is_empty() {
719            let wrap_note = errors.join("\n");
720            let e = self.text_part(index, &owner, "error", &wrap_note, "caption", "");
721            self.nodes[e as usize].base_style.insert("flexBasis".into(), json!("100%"));
722            self.add_state(e, "invalid");
723            self.style_default(index, &[("flexWrap", json!("wrap"))]);
724        }
725    }
726
727    pub(crate) fn radio(&mut self, index: u32) {
728        let owner = self.nodes[index as usize].clone();
729        let horizontal = str_prop(&owner.props, "orientation") == Some("horizontal");
730        // The gap is the `Radio/root` recipe's ($spacing.sm, round 2 §7).
731        self.style_default(index, &[("display", json!("flex")), ("flexDirection", json!("column"))]);
732        self.nodes[index as usize].kind = NodeKind::Container;
733        if let Some(label) = str_prop(&owner.props, "label") {
734            self.text_part(index, &owner, "label", label, "label", "");
735        }
736        let items = self.part(&owner, "items", "Box", NodeKind::Container, json!({"display": "flex", "flexDirection": if horizontal { "row" } else { "column" }, "gap": "$spacing.sm", "flexWrap": "wrap"}), json!({}));
737        let value = owner.props.get("value").map(js);
738        // VAPP-103: only OBJECT options paint (`options: [null]` skips the entry).
739        let options: Vec<Value> = owner.props.get("options").and_then(Value::as_array).map(|a| a.iter().filter(|o| o.is_object()).cloned().collect()).unwrap_or_default();
740        for (i, option) in options.iter().enumerate() {
741            let v = option.get("value").map(js).unwrap_or_default();
742            let checked = value.as_deref() == Some(v.as_str());
743            // VAPP-90: the ROW is a plain option row (no recipe — the
744            // `Radio/item` recipe is the 16 px circle); the `.dot` LEAF wears
745            // the `item` circle recipe and the painter draws `dot` inside it.
746            let row = self.part_in(items, &owner, "item", "Box", NodeKind::Container, json!({"display": "flex", "flexDirection": "row", "alignItems": "center", "gap": "$spacing.sm"}), json!({"value": v, "checked": checked}), &format!(".{i}"));
747            let disabled = bool_prop(&owner.props, "disabled") || option.get("disabled").and_then(Value::as_bool).unwrap_or(false);
748            self.nodes[row as usize].pressable = !disabled;
749            self.nodes[row as usize].part_query = None;
750            let dot = self.part_in(row, &owner, "dot", "Radio", NodeKind::Leaf, json!({"flexShrink": 0}), json!({"checked": checked}), &format!(".{i}"));
751            self.nodes[dot as usize].pressable = !disabled;
752            let mut circle = RecipeQuery::new("Radio", "item", self.ctx.recipes.native_recipe_props("Radio", &owner.props), Vec::new());
753            if checked {
754                circle.states.push("checked".into());
755            }
756            self.nodes[dot as usize].part_query = Some(circle);
757            let label = option.get("label").map(js).unwrap_or_default();
758            self.text_part(row, &owner, "label", &label, "body", &format!(".{i}"));
759        }
760        let errors = self.field_errors(&owner);
761        if !errors.is_empty() {
762            let e = self.text_part(index, &owner, "error", &errors.join("\n"), "caption", "");
763            self.add_state(e, "invalid");
764        }
765    }
766
767    /// ChoiceTiles (EXP-1280): a grid of `columns` (1–4) equal tiles, one
768    /// per option: `tile.<i>` (pressable, `selected` when its value is in
769    /// `value`, comma-joined when `type: multiple`) holding `picture.<i>`
770    /// (an Image, `aspectRatio` 1.6, cover; EVERY tile has one once any
771    /// option has an `image`, so a row keeps one height: a missing picture
772    /// paints the Image fallback), `label.<i>` and `description.<i>`.
773    pub(crate) fn choice_tiles(&mut self, index: u32) {
774        let owner = self.nodes[index as usize].clone();
775        let columns = owner.props.get("columns").and_then(Value::as_f64).filter(|c| c.is_finite()).unwrap_or(2.0).round().clamp(1.0, 4.0) as u32;
776        // The gap is the `ChoiceTiles/root` recipe's.
777        self.style_default(index, &[("display", json!("grid")), ("gridTemplateColumns", json!(format!("repeat({columns}, minmax(0, 1fr))"))), ("alignItems", json!("stretch"))]);
778        self.nodes[index as usize].kind = NodeKind::Container;
779        let picked = choice_values(&owner.props);
780        let options: Vec<Value> = owner.props.get("options").and_then(Value::as_array).map(|a| a.iter().filter(|o| o.is_object()).cloned().collect()).unwrap_or_default();
781        let pictures = options.iter().any(|o| o.get("image").and_then(Value::as_str).is_some_and(|s| !s.is_empty()));
782        let all_disabled = bool_prop(&owner.props, "disabled");
783        let invalid = !self.field_errors(&owner).is_empty();
784        for (i, option) in options.iter().enumerate() {
785            let suffix = format!(".{i}");
786            let v = option.get("value").map(js).unwrap_or_default();
787            let selected = picked.contains(&v);
788            let disabled = all_disabled || option.get("disabled").and_then(Value::as_bool).unwrap_or(false);
789            let label = option.get("label").map(js).unwrap_or_default();
790            let tile = self.part_in(index, &owner, "tile", "Box", NodeKind::Container, json!({"display": "flex", "flexDirection": "column", "minWidth": 0}), json!({"value": v, "label": label, "selected": selected, "disabled": disabled}), &suffix);
791            self.nodes[tile as usize].pressable = !disabled;
792            for (on, state) in [(selected, "selected"), (disabled, "disabled"), (invalid, "invalid")] {
793                if on {
794                    self.add_state(tile, state);
795                }
796            }
797            if pictures {
798                let src = option.get("image").and_then(Value::as_str).filter(|s| !s.is_empty());
799                let mut props = json!({"alt": "", "fit": "cover", "aspectRatio": CHOICE_PICTURE_ASPECT});
800                if let Some(src) = src {
801                    props["src"] = json!(src);
802                }
803                let pic = self.part_in(tile, &owner, "picture", "Image", NodeKind::Leaf, json!({"alignSelf": "stretch", "aspectRatio": CHOICE_PICTURE_ASPECT, "flexShrink": 0, "minWidth": 0}), props, &suffix);
804                let _ = pic;
805            }
806            self.text_part(tile, &owner, "label", &label, "body", &suffix);
807            if let Some(d) = option.get("description").map(js).filter(|d| !d.is_empty()) {
808                self.text_part(tile, &owner, "description", &d, "muted", &suffix);
809            }
810        }
811        let errors = self.field_errors(&owner);
812        if !errors.is_empty() {
813            let e = self.text_part(index, &owner, "error", &errors.join("\n"), "caption", "");
814            // The error spans every column.
815            self.nodes[e as usize].base_style.insert("gridColumn".into(), json!("1 / -1"));
816            self.add_state(e, "invalid");
817        }
818    }
819
820    pub(crate) fn slider(&mut self, index: u32) {
821        let owner = self.nodes[index as usize].clone();
822        self.style_default(index, &[("display", json!("flex")), ("flexDirection", json!("column")), ("gap", json!("$spacing.xs"))]);
823        self.nodes[index as usize].kind = NodeKind::Container;
824        if owner.props.get("label").is_some() {
825            let head = self.part(&owner, "header", "Box", NodeKind::Container, json!({"display": "flex", "flexDirection": "row", "alignItems": "center", "justifyContent": "space-between"}), json!({}));
826            if let Some(label) = str_prop(&owner.props, "label") {
827                self.text_part(head, &owner, "label", label, "label", "");
828            }
829            if let Some(v) = owner.props.get("value") {
830                let text = js(v);
831                self.text_part(head, &owner, "value", &text, "muted", "");
832            }
833        }
834        let mut props = owner.props.clone();
835        props.shift_remove("label");
836        let track = self.part_in(index, &owner, "track", "Slider", NodeKind::Leaf, json!({"alignSelf": "stretch"}), Value::Object(props), "");
837        self.nodes[track as usize].pressable = !bool_prop(&owner.props, "disabled");
838    }
839
840    /// Form: a column of its children; `summary` lists every failed check
841    /// above them after a refused submit (a live region).
842    pub(crate) fn form(&mut self, index: u32, node: &UiNode, scope: &str) {
843        self.style_default(index, &[("display", json!("flex")), ("flexDirection", json!("column")), ("gap", json!("$spacing.md"))]);
844        let owner = self.nodes[index as usize].clone();
845        self.forms.insert(owner.id.clone(), (bool_prop(&owner.props, "disabled"), bool_prop(&owner.props, "busy")));
846        if bool_prop(&owner.props, "summary") {
847            let mut messages: Vec<String> = Vec::new();
848            for field in self.form_fields_of(node) {
849                if let Some(e) = self.ctx.local.errors.get(&field) {
850                    messages.extend(e.messages.iter().cloned());
851                }
852            }
853            if !messages.is_empty() {
854                let s = self.text_part(index, &owner, "summary", &messages.join("\n"), "body", "");
855                self.nodes[s as usize].live = Some("assertive".into());
856                self.add_state(s, "invalid");
857            }
858        }
859        self.children_of(index, node, scope, false);
860    }
861
862    /// The ids of the named fields a Form collects (not inside a nested Form).
863    pub(crate) fn form_fields_of(&self, node: &UiNode) -> Vec<String> {
864        fn walk(n: &UiNode, out: &mut Vec<String>, top: bool) {
865            if !top && n.component == "Form" {
866                return;
867            }
868            if super::FORM_FIELDS.contains(&n.component.as_str()) && n.props.contains_key("name") {
869                out.push(n.id.clone());
870            }
871            for s in n.slots.iter().flat_map(|s| s.values()) {
872                walk(s, out, false);
873            }
874            for c in &n.children {
875                walk(c, out, false);
876            }
877        }
878        let mut out = Vec::new();
879        walk(node, &mut out, true);
880        out
881    }
882}
883
884/// A ChoiceTiles picture's width / height (`plan-card.json` `tiles.pictureAspect`).
885pub const CHOICE_PICTURE_ASPECT: f64 = 1.6;
886
887/// The values a ChoiceTiles `value` picks: one for `single`, the comma-joined
888/// list for `multiple` (empty entries dropped).
889pub fn choice_values(props: &serde_json::Map<String, Value>) -> Vec<String> {
890    let raw = props.get("value").filter(|v| !v.is_null()).map(js).unwrap_or_default();
891    if props.get("type").and_then(Value::as_str) == Some("multiple") {
892        raw.split(',').map(str::trim).filter(|s| !s.is_empty()).map(str::to_string).collect()
893    } else if raw.is_empty() {
894        Vec::new()
895    } else {
896        vec![raw]
897    }
898}
899
900/// A press on option `value`: `single` picks it; `multiple` toggles it in
901/// the list, kept in OPTION order. The new comma-joined value.
902pub fn choice_toggle(props: &serde_json::Map<String, Value>, value: &str) -> String {
903    if props.get("type").and_then(Value::as_str) != Some("multiple") {
904        return value.to_string();
905    }
906    let mut picked = choice_values(props);
907    if let Some(i) = picked.iter().position(|v| v == value) {
908        picked.remove(i);
909    } else {
910        picked.push(value.to_string());
911    }
912    let order: Vec<String> = props.get("options").and_then(Value::as_array).map(|a| a.iter().filter_map(|o| o.get("value")).map(js).collect()).unwrap_or_default();
913    picked.sort_by_key(|v| order.iter().position(|o| o == v).unwrap_or(usize::MAX));
914    picked.join(",")
915}
916
917#[cfg(test)]
918mod tests {
919    use super::dates::*;
920    use super::*;
921
922    #[test]
923    fn numbers_round_like_to_fixed_and_intl() {
924        // toFixed: the exact binary value, ties away from zero.
925        assert_eq!(to_fixed(2.5, 0), "3");
926        assert_eq!(to_fixed(-2.5, 0), "-3");
927        assert_eq!(to_fixed(0.125, 2), "0.13");
928        assert_eq!(to_fixed(1.005, 2), "1.00", "1.005 is 1.00499999…");
929        assert_eq!(to_fixed(0.1 + 0.2, 1), "0.3");
930        assert_eq!(to_fixed(9.95, 1), "9.9", "9.95 is 9.9499999…");
931        assert_eq!(to_fixed(99.5, 0), "100");
932        assert_eq!(to_fixed(-0.04, 1), "-0.0");
933        assert_eq!(to_fixed(1234.5678, 3), "1234.568");
934        // Intl: the shortest decimal, ties away from zero, grouped.
935        assert_eq!(format_number_value(1.005, 2), "1.01");
936        assert_eq!(format_number_value(2.5, 0), "3");
937        assert_eq!(format_number_value(1234.55, 1), "1,234.6");
938        assert_eq!(format_number_value(0.30000000000000004, 1), "0.3");
939        assert_eq!(format_number_value(999_999.5, 0), "1,000,000");
940        assert_eq!(format_number_value(-1234.5, 0), "-1,235");
941        // decimalsOf(String(step)).
942        assert_eq!(decimals_of(0.1), 1);
943        assert_eq!(decimals_of(0.25), 2);
944        assert_eq!(decimals_of(1.0), 0);
945        assert_eq!(decimals_of(1e-7), 0, "String(1e-7) = \"1e-7\"");
946        assert_eq!(decimals_of(1.5e-7), 4, "String(1.5e-7) = \"1.5e-7\"");
947        assert_eq!(clamp_number_value(0.30000000000000004, None, None, 1), 0.3);
948        assert_eq!(clamp_number_value(2.5, Some(0.0), Some(10.0), 0), 3.0);
949        assert_eq!(clamp_number_value(42.0, Some(0.0), Some(10.0), 0), 10.0);
950        let props = |v: serde_json::Value| v.as_object().cloned().unwrap();
951        assert_eq!(number_step_precision(&props(json!({"step": 0.1}))), (0.1, 1));
952        assert_eq!(number_step_precision(&props(json!({"step": 0}))), (1.0, 0), "step || 1");
953        assert_eq!(number_step_precision(&props(json!({"step": 0.5, "precision": 3}))), (0.5, 3));
954    }
955
956    #[test]
957    fn calendar_arithmetic() {
958        assert_eq!(weekday(2026, 10, 7), 3, "a Wednesday");
959        assert_eq!(weekday(2000, 1, 1), 6);
960        assert_eq!(days_in_month(2024, 2), 29);
961        assert_eq!(days_in_month(2100, 2), 28);
962        assert_eq!(add_days(2026, 1, 1, -1), (2025, 12, 31));
963        assert_eq!(add_days(2026, 2, 27, 3), (2026, 3, 2));
964        assert_eq!(shift_month(2026, 12, 1), (2027, 1));
965        assert_eq!(parse("2026-02-30"), None);
966        assert_eq!(label("2026-10-07"), "Oct 7, 2026");
967        assert_eq!(parse_time("09:30"), Some(570));
968        assert_eq!(format_time(570), "09:30");
969        assert_eq!(format_number_value(2.5, 2), "2.50");
970        assert_eq!(format_bytes(48213.0), "47.1 kB");
971        assert_eq!(format_bytes(2048.0), "2 kB");
972        assert_eq!(format_bytes(500.0), "500 byte");
973    }
974}