Form and input machines
Form and input machines turn user input into controlled, accessible state. They cover binary choices, text editing, numeric and segmented entry, tag and file collection, and date entry. Each machine keeps user-facing state aligned with form controls while exposing value, focus, validation, and commit events to the host application. See the component machines, selection and navigation machines, and collections, geometry, and localization machines pages for the wider machine model and related interaction patterns.
Input machine model
An input machine owns the state that changes while a person edits a value. It separates the value that a form or application submits from temporary editing state such as focus, a partial number, an active date segment, a highlighted tag, or a drag target. This separation lets the machine validate at the right time without losing the text that the person is still entering.
The machines share a small set of contracts:
- Value state: A controlled
valuefollows the host application. AdefaultValueordefaultCheckedsupplies the initial value when the host does not control it. - Change notifications: Value callbacks report the machine's current value in the form that best fits the control. Numeric and date controls also report parsed or formatted values.
- Validity state: The host can mark a control invalid. Some machines also report a reason when input violates a range, pattern, count, or file rule.
- Form state: Hidden or native inputs carry the current value into form submission. Form reset restores the initial machine value where the control supports it.
- Interaction state: Focus, keyboard focus visibility, pressed state, editing state, and drag state are available for rendering and accessibility.
The main value paths are shown below. Temporary input state stays local until the machine can accept, commit, or reject it.
flowchart LR
U[User input] --> K[Keyboard or pointer handling]
K --> T[Temporary editing state]
T --> V{Valid or complete?}
V -->|yes| C[Committed value]
V -->|no| R[Invalid notice or continued editing]
C --> F[Form control and value callback]
H[Host value] --> S[Machine synchronization]
S --> T
S --> C
References: packages/…/checkbox.machine.ts• L22-48 packages/…/editable.machine.ts• L25-62 packages/…/number-input.machine.ts• L58-117 packages/…/date-input.machine.ts• L106-167 packages/…/file-upload.machine.ts• L26-65
Choice controls
Choice controls model a boolean value or one value from a group. They use native input semantics for form submission and add state for visual feedback, focus visibility, disabled fieldsets, and programmatic updates.
Checkbox
The checkbox represents a boolean choice with a third indeterminate state for mixed selections. Its public checked view is boolean, while checkedState preserves whether the source state is true, false, or indeterminate; toggling an indeterminate checkbox first moves it to checked. Programmatic changes update the hidden input and dispatch a native change signal so form listeners see the same state as the machine. Form reset restores the initial checked state.
References: packages/…/checkbox.machine.ts• L22-68 packages/…/checkbox.machine.ts• L74-149 packages/…/checkbox.types.ts• L8-13
Radio group
The radio group represents one selected string among its radio items. The group owns the selected value while each item exposes checked, focused, hovered, active, disabled, and focus-visible state. Keyboard and pointer handling select an item, then synchronize every native radio input and the group's value callback. A selected value can be cleared through the API, and form reset returns the group to its initial value.
The group also keeps a visual indicator aligned with the selected item. That indicator follows the item's measured position, so selection feedback remains correct when the layout changes without changing the value contract.
References: packages/…/radio-group.machine.ts• L22-80 packages/…/radio-group.machine.ts• L110-200 packages/…/radio-group.types.ts• L22-69
Switch
The switch is a boolean control presented as an on/off choice. It follows the checkbox value pattern but exposes a switch-specific label and thumb state for the visual control. Pointer and Space-key presses toggle the checked value, while the hidden input stays synchronized for forms and programmatic changes emit a native change signal. A disabled fieldset and form reset affect the switch in the same way as direct disabled and initial-value state.
References: packages/…/switch.machine.ts• L23-63 packages/…/switch.machine.ts• L65-153 packages/…/switch.types.ts• L24-76
Text editing controls
Text editing controls separate display state from input state when the interaction has a clear edit boundary. This lets a person inspect a value, enter changes, and either commit or discard them without forcing the host application to recreate that flow.
Editable
The editable control switches between preview and edit modes. A preview activation method can enter edit mode through focus, click, double-click, or an explicit API call. While editing, the machine keeps the current value and the value captured at entry so Cancel can revert it. Submit can occur on Enter, blur, both, or neither, and outside interaction follows the same submit-or-cancel rule unless the host prevents it.
The host can control both the value and the edit mode. The machine reports ordinary value changes as well as edit, commit, and revert events, so a display can update during typing while persistence waits for a commit.
References: packages/…/editable.machine.ts• L7-62 packages/…/editable.machine.ts• L64-127 packages/…/editable.types.ts• L21-24 packages/…/editable.types.ts• L133-152
Numeric and segmented entry
Numeric and segmented controls treat keyboard input as structured data. They keep raw or partial input during editing and expose a normalized value after parsing, validation, or completion.
Number input
The number input accepts a localized numeric string and derives a numeric value from it. It supports direct typing, increment and decrement controls, Arrow keys, Home and End bounds, optional wheel changes, press-and-hold spinning, and pointer scrubbing. The step, smallStep, and largeStep settings define the size of these changes.
Range rules can allow temporary overflow or clamp the value as soon as it changes. On blur or Enter, the machine formats the value and reports a commit; an out-of-range value can instead report underflow or overflow. Locale and number-format settings control parsing and display, while cursor state is retained when the machine synchronizes an external value during typing.
References: packages/…/number-input.machine.ts• L32-117 packages/…/number-input.machine.ts• L121-264 packages/…/number-input.machine.ts• L365-455 packages/…/number-input.types.ts• L55-177
Pin input
The pin input presents one logical value as a fixed set of single-character fields. It tracks the focused index, advances after valid entry, supports paste into the remaining fields, and moves focus with arrows, Home, End, Backspace, and Delete. The logical value is available both as an array and as one joined string.
Input type or pattern rules reject invalid characters. When all fields contain valid values, the machine reports completion and can blur the active field or request form submission. A hidden input carries the joined value, and updates to the logical value are copied back to each visible field.
References: packages/…/pin-input.machine.ts• L24-72 packages/…/pin-input.machine.ts• L87-147 packages/…/pin-input.machine.ts• L183-308 packages/…/pin-input.types.ts• L34-137
Collection inputs
Collection inputs manage a list of values rather than one scalar value. They add rules for insertion, removal, ordering, duplicates, focus movement, and form serialization.
Tags input
The tags input builds a list of string values from a text entry field. Enter or the configured delimiter can add a tag, and optional paste handling can split one paste into several tags. Before insertion, the machine sanitizes the text and calls the validation function with both the proposed tag and the current list. It can reject duplicates, enforce a maximum count, or report an invalid tag.
After insertion, the input returns to an empty entry state. Backspace and arrow keys move from the input into tag navigation when the caret is at the start. A highlighted tag can be deleted, and an editable tag can enter its own editing state through Enter or double-click. Blur can add the pending text or clear it, based on the configured policy. The machine keeps the list, entry text, and edited-tag text separately synchronized with controlled props and hidden form input.
References: packages/…/tags-input.machine.ts• L13-110 packages/…/tags-input.machine.ts• L159-341 packages/…/tags-input.machine.ts• L616-685 packages/…/tags-input.types.ts• L68-198
File upload
The file upload machine turns file picker and drop-zone activity into accepted and rejected file lists. It supports one or many files, click-to-open selection, drag-over and drop state, optional directory input, and asynchronous file transformation. The accepted list is the form value; rejected files remain available with their reasons so the UI can explain what needs correction.
Each incoming file passes through type, size, duplicate, count, and host validation. Only files that pass all checks enter the accepted list. The machine updates the native file input after accepted-file changes and reports accepted and rejected changes through separate callbacks and a combined file-change callback. It also prevents unrelated document drops when the drop zone owns drop handling.
References: packages/…/file-upload.machine.ts• L26-85 packages/…/file-upload.machine.ts• L87-165 packages/…/file-upload.machine.ts• L166-237 packages/…/file-upload.utils.ts• L14-79 packages/…/is-valid-file-type.ts• L43-56 packages/…/is-valid-file-size.ts• L5-17
Date inputs
Date controls support both direct date entry and calendar selection. They keep a typed or focused date separate from the committed value so locale formatting, partial segments, ranges, and calendar rules can work together.
Date input
The date input presents a date as editable locale-aware segments such as year, month, day, and time. It tracks the active date group and segment, accepts typed digits and paste, moves between segments with arrows, and supports Backspace, Home, and End adjustments. A segment can remain partial while the person edits; complete display values commit to the date value, while blur confirms complete deferred values and applies the allowed range.
The machine derives segments from locale, calendar, time zone, granularity, and hour cycle. It exposes date values and formatted strings, supports single and range entry, and reports placeholder changes separately from committed value changes. The hidden input path lets the structured date participate in forms without making the visible segments behave like one plain text field.
References: packages/…/date-input.machine.ts• L37-161 packages/…/date-input.machine.ts• L164-314 packages/…/date-input.machine.ts• L453-629 packages/…/date-input.types.ts• L59-194
Date picker
The date picker combines text input with an open calendar view. It supports single, multiple, and range selection, date and month/year views, min and max bounds, unavailable dates, presets, and optional inline rendering. The focused date drives keyboard movement through days, weeks, months, years, or decades; selecting a cell updates the value and can close the calendar according to the selection mode and close policy.
Its text field has its own input value until parsing succeeds. On blur, valid text becomes a constrained date; invalid text falls back to the last focused date rather than entering an invalid committed value. The machine synchronizes formatted values back to the input, announces value and visible-range changes, and restores focus to the input or trigger when the calendar closes.
References: packages/…/date-picker.machine.ts• L66-276 packages/…/date-picker.machine.ts• L278-357 packages/…/date-picker.machine.ts• L420-714 packages/…/date-picker.machine.ts• L1168-1221 packages/…/date-picker.types.ts• L102-302
Value synchronization
Value synchronization keeps three surfaces aligned: machine context, rendered inputs, and host callbacks. A controlled prop can replace the machine value, user events can change the machine value, and watchers then write the result back to the visible or hidden input. The machine compares structured values by content where needed, so a new array or date object does not create a false change when the logical value is the same.
The synchronization contract differs by input kind:
| Input kind | Machine value | Editing value | Form-facing value |
|---|---|---|---|
| Checkbox and switch | Checked state | Press and focus state | Checked native input |
| Radio group | Selected item value | Focus and hover item state | Group radio inputs |
| Editable | Text value | Previous value during edit | Text input |
| Number input | Numeric text with parsed number | Raw or partially formatted text | Number input |
| Pin input | Character array | Focused field and paste buffer | Joined hidden value |
| Tags input | String list | Entry and edited-tag text | Serialized hidden input |
| File upload | Accepted file list | Drag and transform state | Native file input |
| Date input | Structured date values | Segment display and placeholder | Hidden date input values |
| Date picker | Selected date values | Focused date and text input | Formatted date input values |
When a machine changes a native input from code, it also emits the relevant native signal where form integrations depend on it. This is important for checkboxes, radios, tags, pin values, and files because an application may listen to the input rather than only to the machine callback.
References: packages/…/checkbox.machine.ts• L39-46 packages/…/radio-group.machine.ts• L76-80 packages/…/number-input.machine.ts• L105-117 packages/…/pin-input.machine.ts• L62-72 packages/…/tags-input.machine.ts• L89-108 packages/…/file-upload.machine.ts• L61-65 packages/…/date-picker.machine.ts• L243-267
Change checklist
When these files or sections are changed, remember to consider these:
- Keep controlled and uncontrolled value paths equivalent. A change to a bindable value must still update its visible input and its callback details.
- Preserve native form behavior when changing hidden-input synchronization. Programmatic updates may need a native input or change event.
- Keep temporary editing state separate from committed state for numbers, dates, editable text, pin fields, and tags. Early formatting can break typing, paste, or cancel behavior.
- Keep validation timing aligned with the user action. Range and pattern errors may be temporary during typing but must be resolved on blur, Enter, or completion.
- Keep keyboard movement and focus restoration aligned with the value index. This is especially important for pin fields, date ranges, radio items, and highlighted tags.
- Keep accepted and rejected file lists distinct. A change to file validation or transformation must preserve rejection reasons and the combined file-change contract.
- Keep locale, calendar, time-zone, parse, and format changes synchronized. A displayed date string must still map to the same structured value.
Important failure modes
| Trigger | Consequence | Guard |
|---|---|---|
| A controlled value changes without syncing the rendered input | The UI shows the previous value while callbacks and form submission use the new value | Watch the logical value and update each visible or hidden input |
| A number value is formatted while the person is typing | Partial input is lost or the cursor jumps | Keep raw input during change events and format on commit or blur |
| A date segment is committed from the wrong group after auto-advance | A range start or end receives the wrong segment value | Capture the active group before moving focus |
| A pin paste skips normalization or validation | Invalid characters enter the logical value or fields disagree | Validate each entered value and synchronize the full field array |
| A tag is added without sanitizing or validating it | Duplicate, empty, or disallowed tags enter the list | Sanitize first and call the tag validator with the current list |
| Incoming files exceed type, size, duplicate, or count rules | Invalid files appear accepted or the native file input does not match the list | Build accepted and rejected results together and retain file errors |
| A form reset updates the native input but not machine context | The next render restores the old application state | Restore the initial bindable value through the machine |
| A selection control ignores fieldset disabled state | A child control remains interactive inside a disabled fieldset | Include observed fieldset state in the computed disabled value |
Key files
| File | Why to read it |
|---|---|
| packages/…/checkbox.machine.ts | Shows the shared boolean-control pattern, indeterminate handling, form reset, and native checked-input synchronization. |
| packages/…/radio-group.machine.ts | Shows group value ownership, item state, native radio synchronization, and selected-indicator state. |
| packages/…/editable.machine.ts | Shows the preview/edit boundary, commit and revert behavior, and outside-interaction handling. |
| packages/…/number-input.machine.ts | Shows parsed numeric state, range validity, keyboard stepping, spinning, scrubbing, and commit timing. |
| packages/…/pin-input.machine.ts | Shows segmented value storage, focus movement, paste, completion, and form submission. |
| packages/…/tags-input.machine.ts | Shows list editing, validation, tag navigation, paste rules, and separate entry state. |
| packages/…/file-upload.machine.ts | Shows picker and drop-zone flow, accepted/rejected state, transformation, and file-input synchronization. |
| packages/…/file-upload.utils.ts | Shows the combined file validation and rejection contract. |
| packages/…/date-input.machine.ts | Shows segment editing, partial values, range groups, locale-aware display, and date commitment. |
| packages/…/date-picker.machine.ts | Shows calendar selection, text parsing, focused-date navigation, and open/close focus flow. |
| packages/…/is-valid-file-type.ts | Defines accepted file type matching by MIME type, base MIME, or extension. |
| packages/…/is-valid-file-size.ts | Defines the minimum and maximum file-size checks used by upload validation. |