frust-material 0.6.0

Material 3 (Expressive) design-system catalog for Frust: HCT seed-color tokens, shape morphing, icons, widgets and bundled Roboto fonts.
Documentation
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
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
// Ported from `material_3_expressive` v1.0.8's date input field (MIT, © 2026
// Paa Developments;
// `tmp/material_3_expressive/lib/components/date_pickers/components/m3e_input_date_picker_form_field.dart`,
// retrieved 2026-08-20).
// Upstream: <https://github.com/paadevelopments/material_3_expressive>
//
// Porting decisions (documented in the module docs below): validation is live
// rather than deferred behind Flutter's `AutovalidateMode.disabled`; the parse
// format is the fixed `en_US` compact one; and there is no autofocus, the
// framework exposing no programmatic-focus seam.

//! The date picker's **input mode**: [`crate::text_field`] wrapped in
//! `mm/dd/yyyy` parsing, bounds checking, and the two error strings the
//! reference's form validator produces.
//!
//! [`date_input_field`] is controlled like everything else in this family (see
//! [`mod@super`]): it shows [`DatePickerState::input_text`] and reports the next
//! state — the new draft text, plus the parsed selection whenever the text
//! resolves to a selectable date.
//!
//! # Live validation, not deferred
//!
//! The reference's dialog builds the field inside a `Form` with
//! `AutovalidateMode.disabled`, so nothing is flagged until OK is pressed
//! (`_handleOk` flips it to `always` on the first failure). This port validates
//! on every keystroke once the field is non-empty, and [`DateInputField::validate`]
//! turns that off for a caller that wants the deferred shape. The trade is
//! deliberate: an error appears while a half-typed `08/2` is still being typed,
//! but the user is never told "no" only after committing, and the picker needs
//! no extra "has OK been pressed yet" state to carry the reference's
//! `_RestorableAutovalidateMode`.
//!
//! An **empty** field never shows an error — the reference's `acceptEmptyDate`
//! branch reached from the other side: nothing typed is not yet wrong.
//!
//! # No autofocus
//!
//! The reference opens input mode with `autofocus: true` so the keyboard is up
//! immediately. The framework's baseline editable exposes no programmatic-focus
//! seam ([`crate::search`]'s view documents the same gap), so a user taps the
//! field once before the keyboard appears.

use std::rc::Rc;

use frust::authoring::{BuildCtx, ChangeFlags, View};

use super::calendar::SelectableDay;
use super::date::MaterialDate;
use super::{DatePickerState, DatePickerStrings, OnDatePickerChange};
use crate::text_field::{TextFieldView, TextFieldWidget, text_field};

/// Why a typed date was refused — the two errors the reference's `_validate`
/// distinguishes.
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum DateInputError {
    /// The text is not `mm/dd/yyyy` at all (`invalidDateFormatLabel`).
    Format,
    /// A real date, but outside the selectable bounds or refused by the
    /// caller's predicate (`invalidDateRangeLabel`).
    OutOfRange,
}

/// Parse `text` as a compact date and check it against `first..=last`.
///
/// The bounds half of the reference's `_isValidAcceptableDate`; a caller's
/// `selectableDayPredicate` is applied on top by
/// [`DateInputField::parse`], which owns the closure.
///
/// ```
/// use frust_material::{DateInputError, MaterialDate, parse_bounded};
///
/// let first = MaterialDate::new(2026, 1, 1);
/// let last = MaterialDate::new(2026, 12, 31);
/// assert_eq!(parse_bounded("08/20/2026", first, last), Ok(MaterialDate::new(2026, 8, 20)));
/// assert_eq!(parse_bounded("08/20/2027", first, last), Err(DateInputError::OutOfRange));
/// assert_eq!(parse_bounded("nonsense", first, last), Err(DateInputError::Format));
/// ```
pub fn parse_bounded(
    text: &str,
    first: MaterialDate,
    last: MaterialDate,
) -> Result<MaterialDate, DateInputError> {
    let date = MaterialDate::parse_compact(text).ok_or(DateInputError::Format)?;
    if date.is_within(first, last) {
        Ok(date)
    } else {
        Err(DateInputError::OutOfRange)
    }
}

/// A declarative date input field. See the [module docs](self).
pub struct DateInputField<State: 'static> {
    state: DatePickerState,
    first_date: MaterialDate,
    last_date: MaterialDate,
    strings: DatePickerStrings,
    selectable: Option<SelectableDay>,
    validate: bool,
    on_change: OnDatePickerChange<State>,
}

/// Create a controlled date input field over `state`, bounded by
/// `first_date..=last_date`, reporting each edit through `on_change`.
pub fn date_input_field<State: 'static, F>(
    state: DatePickerState,
    first_date: MaterialDate,
    last_date: MaterialDate,
    on_change: F,
) -> DateInputField<State>
where
    F: Fn(&mut State, DatePickerState) + 'static,
{
    let (first_date, last_date) = if last_date < first_date {
        (last_date, first_date)
    } else {
        (first_date, last_date)
    };
    DateInputField {
        state,
        first_date,
        last_date,
        strings: DatePickerStrings::ENGLISH,
        selectable: None,
        validate: true,
        on_change: Rc::new(on_change),
    }
}

impl<State: 'static> DateInputField<State> {
    /// Replace the localized strings (default [`DatePickerStrings::ENGLISH`]).
    pub fn strings(mut self, strings: DatePickerStrings) -> Self {
        self.strings = strings;
        self
    }

    /// Refuse individual dates inside the bounds — the reference's
    /// `selectableDayPredicate`, reported as [`DateInputError::OutOfRange`].
    pub fn selectable<F: Fn(MaterialDate) -> bool + 'static>(mut self, predicate: F) -> Self {
        self.selectable = Some(Rc::new(predicate));
        self
    }

    /// Whether the field shows its error while typing (default `true`). `false`
    /// is the reference's deferred `AutovalidateMode.disabled` shape — see the
    /// [module docs](self).
    pub fn validate(mut self, validate: bool) -> Self {
        self.validate = validate;
        self
    }

    /// Parse `text` against this field's bounds *and* its predicate.
    pub fn parse(&self, text: &str) -> Result<MaterialDate, DateInputError> {
        let date = parse_bounded(text, self.first_date, self.last_date)?;
        if self.selectable.as_ref().is_none_or(|p| p(date)) {
            Ok(date)
        } else {
            Err(DateInputError::OutOfRange)
        }
    }

    /// The error string this field currently shows, if any: `None` for an empty
    /// field, for a valid date, or when [`Self::validate`] is off.
    pub fn error(&self) -> Option<&'static str> {
        if !self.validate || self.state.input_text.trim().is_empty() {
            return None;
        }
        match self.parse(&self.state.input_text) {
            Ok(_) => None,
            Err(DateInputError::Format) => Some(self.strings.error_format),
            Err(DateInputError::OutOfRange) => Some(self.strings.error_invalid),
        }
    }

    /// The state typing `text` into this field requests: the draft text
    /// recorded always, and the selection updated too whenever `text` resolves
    /// to a selectable date.
    ///
    /// The draft text is re-applied *after* the selection so the user's own
    /// keystrokes survive — [`DatePickerState::with_selected`] would otherwise
    /// reformat the field out from under them.
    pub fn next_state(&self, text: &str) -> DatePickerState {
        match self.parse(text) {
            Ok(date) => self.state.with_selected(date).with_input_text(text),
            Err(_) => self.state.with_input_text(text),
        }
    }

    /// Compose the wrapped [`crate::text_field`] fresh from the current fields
    /// — the same recompose-per-pass shape [`crate::dialog`]'s own views take.
    fn compose(&self) -> TextFieldView<State> {
        let parsed = Parsed {
            state: self.state.clone(),
            first: self.first_date,
            last: self.last_date,
            selectable: self.selectable.clone(),
        };
        let on_change = self.on_change.clone();
        let field = text_field(
            self.state.input_text.clone(),
            move |app: &mut State, text: String| on_change(app, parsed.next_state(&text)),
        )
        .label(self.strings.field_label);
        match self.error() {
            Some(message) => field.error_text(message),
            None => field.supporting_text(self.strings.field_hint),
        }
    }
}

/// The parse inputs a composed field's `on_change` closure has to own (it
/// outlives the view it was built from).
struct Parsed {
    state: DatePickerState,
    first: MaterialDate,
    last: MaterialDate,
    selectable: Option<SelectableDay>,
}

impl Parsed {
    fn next_state(&self, text: &str) -> DatePickerState {
        match parse_bounded(text, self.first, self.last) {
            Ok(date) if self.selectable.as_ref().is_none_or(|p| p(date)) => {
                self.state.with_selected(date).with_input_text(text)
            }
            _ => self.state.with_input_text(text),
        }
    }
}

impl<State: 'static> View<State> for DateInputField<State> {
    type Element = TextFieldWidget;

    fn build(&self, ctx: &mut BuildCtx<'_>) -> TextFieldWidget {
        View::build(&self.compose(), ctx)
    }

    fn rebuild(
        &self,
        prev: &Self,
        element: &mut TextFieldWidget,
        ctx: &mut BuildCtx<'_>,
    ) -> ChangeFlags {
        View::rebuild(&self.compose(), &prev.compose(), element, ctx)
    }

    fn teardown(&self, element: &mut TextFieldWidget, ctx: &mut BuildCtx<'_>) {
        View::teardown(&self.compose(), element, ctx);
    }
}

/// The retained widget for a [`DateInputField`] — the wrapped baseline-backed
/// [`crate::text_field`]'s own, since this view contributes decoration only.
pub type DateInputFieldWidget = TextFieldWidget;

#[cfg(test)]
mod tests {
    use super::*;

    fn d(year: i32, month: u32, day: u32) -> MaterialDate {
        MaterialDate::new(year, month, day)
    }

    const FIRST: fn() -> MaterialDate = || MaterialDate::new(2020, 1, 1);
    const LAST: fn() -> MaterialDate = || MaterialDate::new(2030, 12, 31);

    #[derive(Default)]
    struct App {
        reported: Vec<DatePickerState>,
    }

    fn field(state: DatePickerState) -> DateInputField<App> {
        date_input_field(state, FIRST(), LAST(), |app: &mut App, next| {
            app.reported.push(next)
        })
    }

    fn seeded() -> DatePickerState {
        DatePickerState::new(None, d(2026, 8, 20))
    }

    // ---- parsing ---------------------------------------------------------

    #[test]
    fn parsing_splits_format_errors_from_range_errors() {
        let first = d(2026, 1, 1);
        let last = d(2026, 12, 31);
        assert_eq!(parse_bounded("06/15/2026", first, last), Ok(d(2026, 6, 15)));
        assert_eq!(parse_bounded("6/5/2026", first, last), Ok(d(2026, 6, 5)));
        // Both bounds are inclusive.
        assert_eq!(parse_bounded("01/01/2026", first, last), Ok(first));
        assert_eq!(parse_bounded("12/31/2026", first, last), Ok(last));

        assert_eq!(
            parse_bounded("12/31/2025", first, last),
            Err(DateInputError::OutOfRange)
        );
        assert_eq!(
            parse_bounded("01/01/2027", first, last),
            Err(DateInputError::OutOfRange)
        );
        for bad in ["", "08/2", "2026-06-15", "13/01/2026", "02/30/2026", "abc"] {
            assert_eq!(
                parse_bounded(bad, first, last),
                Err(DateInputError::Format),
                "{bad:?}"
            );
        }
    }

    #[test]
    fn a_predicate_refusal_reads_as_out_of_range() {
        let f = field(seeded()).selectable(|date| date.day() % 2 == 0);
        assert_eq!(f.parse("06/16/2026"), Ok(d(2026, 6, 16)));
        assert_eq!(f.parse("06/15/2026"), Err(DateInputError::OutOfRange));
    }

    #[test]
    fn reversed_bounds_are_normalized_rather_than_asserted() {
        let f = date_input_field(seeded(), LAST(), FIRST(), |app: &mut App, next| {
            app.reported.push(next)
        });
        assert_eq!(f.parse("06/15/2026"), Ok(d(2026, 6, 15)));
    }

    // ---- error text -------------------------------------------------------

    #[test]
    fn an_empty_field_never_errors() {
        assert_eq!(field(seeded()).error(), None);
        assert_eq!(field(seeded().with_input_text("   ")).error(), None);
    }

    #[test]
    fn the_two_errors_map_onto_the_reference_strings() {
        let strings = DatePickerStrings::ENGLISH;
        assert_eq!(
            field(seeded().with_input_text("08/2")).error(),
            Some(strings.error_format)
        );
        assert_eq!(
            field(seeded().with_input_text("08/20/2045")).error(),
            Some(strings.error_invalid)
        );
        assert_eq!(field(seeded().with_input_text("08/20/2026")).error(), None);
    }

    #[test]
    fn validation_off_shows_nothing_at_all() {
        let f = field(seeded().with_input_text("nonsense")).validate(false);
        assert_eq!(f.error(), None);
        // The parse itself still answers — only the surfaced error is gated.
        assert_eq!(f.parse("nonsense"), Err(DateInputError::Format));
    }

    #[test]
    fn an_override_supplies_its_own_error_strings() {
        let f = field(seeded().with_input_text("08/2")).strings(DatePickerStrings {
            error_format: "Format invalide.",
            ..DatePickerStrings::ENGLISH
        });
        assert_eq!(f.error(), Some("Format invalide."));
    }

    // ---- reported state ---------------------------------------------------

    #[test]
    fn a_valid_entry_reports_the_date_and_keeps_the_typed_text() {
        let f = field(seeded());
        let next = f.next_state("06/15/2026");
        assert_eq!(next.selected, Some(d(2026, 6, 15)));
        assert_eq!(next.displayed_month, d(2026, 6, 1));
        assert_eq!(
            next.input_text, "06/15/2026",
            "the user's own keystrokes survive the reformat"
        );
    }

    #[test]
    fn an_unpadded_entry_keeps_its_own_text_rather_than_reformatting() {
        let next = field(seeded()).next_state("6/5/2026");
        assert_eq!(next.selected, Some(d(2026, 6, 5)));
        assert_eq!(next.input_text, "6/5/2026");
    }

    #[test]
    fn a_half_typed_entry_records_the_text_and_nothing_else() {
        let start = seeded().with_selected(d(2026, 8, 20));
        let next = field(start.clone()).next_state("08/2");
        assert_eq!(next.input_text, "08/2");
        assert_eq!(
            next.selected, start.selected,
            "an unparseable draft never clears the selection"
        );
        assert_eq!(next.displayed_month, start.displayed_month);
    }

    #[test]
    fn an_out_of_range_entry_records_the_text_and_nothing_else() {
        let next = field(seeded()).next_state("08/20/2045");
        assert_eq!(next.input_text, "08/20/2045");
        assert_eq!(next.selected, None);
    }

    #[test]
    fn a_refused_entry_records_the_text_and_nothing_else() {
        let f = field(seeded()).selectable(|date| date.day() % 2 == 0);
        let next = f.next_state("06/15/2026");
        assert_eq!(next.input_text, "06/15/2026");
        assert_eq!(next.selected, None);
    }

    // ---- composition ------------------------------------------------------

    #[test]
    fn the_parse_inputs_a_composed_closure_owns_report_the_same_state() {
        // `compose()`'s `on_change` closure captures a `Parsed` rather than the
        // view (which does not outlive the pass). It must answer identically to
        // the view's own `next_state`, which every case above pins.
        let cases = ["06/15/2026", "6/5/2026", "08/2", "08/20/2045", ""];
        for text in cases {
            let view = field(seeded().with_selected(d(2026, 8, 20)));
            let parsed = Parsed {
                state: view.state.clone(),
                first: view.first_date,
                last: view.last_date,
                selectable: view.selectable.clone(),
            };
            assert_eq!(parsed.next_state(text), view.next_state(text), "{text:?}");
        }
    }

    #[test]
    fn a_predicate_reaches_the_composed_closure_too() {
        let view = field(seeded()).selectable(|date| date.day() % 2 == 0);
        let parsed = Parsed {
            state: view.state.clone(),
            first: view.first_date,
            last: view.last_date,
            selectable: view.selectable.clone(),
        };
        assert_eq!(parsed.next_state("06/15/2026").selected, None);
        assert_eq!(
            parsed.next_state("06/16/2026").selected,
            Some(d(2026, 6, 16))
        );
    }

    #[test]
    fn the_view_builds_its_wrapped_field() {
        // Exercises `compose()` end to end: a real `View::build` over the
        // wrapped baseline-backed field.
        let mut counter = 0u64;
        let view = field(seeded().with_input_text("08/2"));
        let mut element = View::<App>::build(&view, &mut BuildCtx::new(&mut counter));
        // ...and a rebuild against a changed draft reconciles rather than
        // rebuilding from scratch.
        let next = field(seeded().with_input_text("08/20/2026"));
        let flags =
            View::<App>::rebuild(&next, &view, &mut element, &mut BuildCtx::new(&mut counter));
        assert!(!flags.is_empty(), "a value change dirties the field");
    }
}