Introducing a new way to review your PRs.

Read the announcement

spartan-ng / spartan

Cutting-edge tools powering Angular full-stack development.

View on GitHub

18 documents · Updated Nov 23, 2026

docs / brain-form-and-selection-controls

Brain form and selection controls

Brain form controls provide headless behavior for collecting text, choice, date, boolean, and numeric values. Helm controls add the visual layer while the Brain layer keeps value changes, focus, keyboard use, disabled state, accessibility, and form validation in one shared model. This page complements brain-primitives and the overlay behavior described in brain-overlay-navigation-and-feedback.

Form control composition

Form controls are built from three related parts: a field gives the control its label and validation context, a Brain primitive supplies behavior, and a Helm primitive supplies the visual form. The same controls work with template-driven forms, reactive forms, and Angular Signal Forms through Angular form bindings.

The normal composition is:

  1. Place one control inside hlmField.
  2. Connect hlmFieldLabel to the control's labelable ID.
  3. Add hlmFieldDescription for supporting text when needed.
  4. Add hlm-field-error for validation feedback.
flowchart LR
    Form[Angular form model] --> Control[Brain control behavior]
    Control --> Field[Field state and a11y context]
    Field --> Label[Label and description]
    Field --> Error[Validation error]
    Control --> Helm[Helm visual control]
    Helm --> UI[Rendered form control]

BrnFieldControl reads the bound Angular control and exposes invalid, dirty, touched, and error state to the field. A control can also force an invalid state for cases where the application owns the error rule. The field links its label, description, and active error to the labelable control. A separate BrnFieldControlDescribedBy directive merges manual description IDs with IDs supplied by the field, so custom descriptions do not replace field messages. References: libs/…/brn-field.ts• L18-81, libs/…/brn-field-control.ts• L8-97, libs/…/brn-field-control-described-by.ts• L4-23, libs/…/hlm-field.ts• L29-43, libs/…/hlm-field-description.ts• L12-52, libs/…/hlm-field-error.ts• L31-97

Field layout

The field is the shared layout and validation boundary for one control. It supports vertical, horizontal, and responsive orientation, while field groups and field sets provide structure for related controls.

Use a native fieldset with hlmFieldSet and a legend with hlmFieldLegend when several checkboxes or radios form one question. Give that group one shared validation message instead of treating each option as an unrelated field. This also gives a questionnaire a clear boundary for each question and its answer controls. References: libs/…/hlm-field-set.ts• L4-11, libs/…/hlm-field-group.ts• L4-11, libs/…/hlm-field-label.ts• L5-16, skills/…/forms.md• L9-23, skills/…/forms.md• L59-81

Text input controls

Text controls expose native text editing while adding the field contract. input handles one-line values and textarea handles multi-line values. Both report their form state to the surrounding field and remain usable with any Angular form API.

Use the related primitives for the shape of the input rather than for a new value model:

ControlUser valueMain purpose
inputString or native input valueOne-line text and browser input types
textareaStringMulti-line text
input-otpOne-time codeSegmented code entry
input-groupValue of its child inputLeading, trailing, or inline actions

input-group is a visual and interaction wrapper. It composes an input or textarea with addons and buttons, so the child control remains the source of the form value. input-otp follows the same idea for one-time codes and adds slots without changing the surrounding form pattern. References: libs/…/brn-input.ts• L5-36, libs/…/brn-textarea.ts• L5-34, libs/…/hlm-input.ts• L6-17, libs/…/hlm-textarea.ts• L6-17, libs/…/hlm-input-group.ts• L4-17, libs/…/hlm-input-otp.ts• L4-11

Boolean controls

Boolean controls model an on or off decision and expose that value to Angular forms. checkbox is suited to independent choices and can also represent a mixed state. switch presents the same boolean behavior as an on/off setting.

The checkbox has three visual states: checked, unchecked, and indeterminate. Toggling an indeterminate checkbox moves it to checked. A switch has only checked and unchecked states. Both controls ignore user changes while disabled and notify the form when the user changes or leaves the control. Labels can wrap a checkbox or point to it with for and id; the checkbox keeps the label relationship and disabled state aligned. References: libs/…/brn-checkbox.ts• L103-141, libs/…/brn-checkbox.ts• L250-320, libs/…/brn-checkbox.spec.ts• L164-216, libs/…/brn-switch.ts• L108-116, libs/…/brn-switch.ts• L243-256, libs/…/hlm-checkbox.ts• L67-151, libs/…/hlm-switch.ts• L54-116

Choice controls

Choice controls keep a selected value separate from the option content. radio-group and toggle-group suit small, visible sets of options. select and native-select suit dropdown choice. Each option reports its state through the parent control rather than maintaining an unrelated form value.

ControlChoice modelBest fit
radio-groupOne value from a groupFew mutually exclusive options
toggle-groupOne value or an array of valuesCompact option buttons; single or multiple selection
selectOne value or an array in its multiple formCustom dropdown with many options
native-selectOne string valueNative browser dropdown behavior

Radio group behavior

The radio group owns the selected value and gives each radio item a value. Selecting an item updates the group and the form once. Focus leaving the group marks the group touched. A fieldset can wrap the group when its options need one legend and one validation boundary. References: libs/…/brn-radio-group.ts• L27-40, libs/…/brn-radio-group.ts• L98-133, libs/…/hlm-radio-group.ts• L7-41, libs/…/hlm-radio.ts• L21-52

Toggle group behavior

The toggle group presents choices as buttons while preserving a selection contract. A single group emits one value. A multiple group emits an array. Its nullable rule controls whether the current choice can be cleared. The standalone toggle is the same on/off state model without a group. It exposes the state as aria-pressed and blocks clicks while disabled. References: libs/…/brn-toggle-group.ts• L20-59, libs/…/brn-toggle-group.ts• L87-171, libs/…/brn-toggle.ts• L4-51, libs/…/hlm-toggle-group.ts• L8-37, libs/…/hlm-toggle-group-item.ts• L7-39

Select behavior

The custom select separates its trigger from its option list. The trigger opens a listbox-like popup, keeps keyboard focus for active-option navigation, and selecting an option writes the selected value before closing the popup. The multiple form keeps an array and toggles each option without replacing the other selected values.

This diagram shows the value path for a custom select.

sequenceDiagram
    participant User
    participant Trigger
    participant List as Option list
    participant Select
    participant Form
    User->>Trigger: Open or use arrow key
    Trigger->>Select: Open popup and move active option
    User->>List: Choose option or press Enter
    List->>Select: Commit one value
    Select->>Form: Notify value change
    Select->>Trigger: Close and show selected label

The custom select compares option values with a configurable equality rule and can convert values to display text. It can show scroll controls for long lists and keeps their visibility tied to the list's scroll position. The native select uses the browser's <select> behavior instead, so its options remain native content while its value and touched state still connect to the field and Angular forms.

The trigger exposes the expanded popup and active option only while the popup is open. Enter or Space commits the active option when open, while Tab closes the popup without committing. When closed, Enter and the arrow keys open it; Enter does not submit an enclosing form. A trigger click restores focus to the trigger before it opens or closes the popup. Disabling the select also disables its options, including options in an already open popup. References: libs/…/README.md• L1-5, libs/…/brn-select.ts• L47-125, libs/…/brn-select.ts• L168-227, libs/…/brn-select-multiple.ts• L51-125, libs/…/brn-select-multiple.ts• L169-234, libs/…/brn-select-content.ts• L8-76, libs/…/brn-select-trigger.ts• L9-135, libs/…/brn-select-item.ts• L9-99, libs/…/brn-select-trigger.spec.ts• L37-49, libs/…/brn-select.spec.ts• L36-60, libs/…/hlm-select.ts• L6-32, libs/…/hlm-select-multiple.ts• L6-32, libs/…/hlm-native-select.ts• L27-56

Search and suggestion controls

combobox and autocomplete combine text search with a selected value. Both use a popup option list and active-item keyboard navigation. The combobox filters its own options and supports single or multiple selection. Autocomplete supplies suggestions while keeping the selected item separate from the text being searched.

Combobox behavior

The combobox input has two roles. In combobox mode, it shows the selected item's label when no search is active and exposes the form's validation state. In popup mode, it serves as the search input inside the option panel, so it does not repeat the parent control's validation attributes. The trigger uses the panel ID only while expanded, and text-input triggers open an already open panel instead of toggling it closed.

Multiple selection keeps an array of values and renders selected values as chips. The chip input opens the panel as the user types, removes the last selected chip when Backspace is pressed on an empty search, and prevents Enter from submitting the parent form before it selects the active option. Chip removal is disabled with the parent control. References: libs/…/brn-combobox.ts• L55-171, libs/…/brn-combobox.ts• L206-294, libs/…/brn-combobox-input.ts• L9-154, libs/…/brn-combobox-trigger.ts• L5-59, libs/…/brn-combobox-multiple.ts• L59-197, libs/…/brn-combobox-multiple.ts• L212-315, libs/…/brn-combobox-chip-input.ts• L7-123, libs/…/brn-combobox-chip.ts• L4-19, libs/…/brn-combobox-chip-remove.ts• L5-34, libs/…/brn-combobox-popover-trigger.ts• L7-38

Autocomplete behavior

Autocomplete keeps the selected value and search text as separate states. Typing opens the suggestion panel. Choosing an item commits its value and display label. Pressing Escape while the panel is closed clears the current value, while Tab closes the panel without replacing text with the highlighted suggestion.

Items form an accessible listbox with disabled and active states. Groups can carry labels, empty and status content uses polite live regions, and auto-highlight can select the first visible suggestion when search results appear. Autocomplete supports object values through an item-to-string conversion and a configurable equality rule. References: libs/…/brn-autocomplete.ts• L54-155, libs/…/brn-autocomplete.ts• L170-255, libs/…/brn-autocomplete-input.ts• L8-129, libs/…/brn-autocomplete-item.ts• L9-99, libs/…/brn-autocomplete-list.ts• L5-42, libs/…/brn-autocomplete-group.ts• L5-29, libs/…/brn-autocomplete-content.ts• L4-23, libs/…/brn-autocomplete-empty.ts• L3-11, libs/…/brn-autocomplete-status.ts• L3-11, libs/…/brn-autocomplete-clear.ts• L4-37, libs/…/brn-autocomplete-anchor.ts• L6-24

Date controls

Date controls let a user choose a date or date range from a calendar popup and can also accept formatted text. The picker owns the date value while a date input mirrors that value for editing, parses text on blur or Enter, and keeps invalid text visible so the user can correct it.

Calendar month and year selects provide alternate navigation controls inside the calendar. They mirror the calendar's focused date into a select value, then update that focused date when the user chooses a month or year. The controls preserve the day when possible, clamp it to the last valid day when needed, and constrain the result to the calendar's allowed date range. References: libs/…/brn-calendar-month-select.ts• L25-65, libs/…/brn-calendar-year-select.ts• L25-56

ControlValue shapeCommit behavior
Date pickerOne dateCalendar selection commits the date and may close the popup
Date range pickerStart and end dateThe range commits when both dates exist and may close after the end date
Date inputPicker-specific date valueText commits on blur or Enter; clear removes the value

Minimum and maximum dates limit calendar selection. A transform rule can normalize a date before it reaches the form. Range selection keeps the start and end parts separate until the range is complete. References: libs/…/hlm-date-picker.ts• L31-57, libs/…/hlm-date-picker.ts• L59-128, libs/…/hlm-date-picker.ts• L130-187, libs/…/hlm-date-range-picker.ts• L32-63, libs/…/hlm-date-range-picker.ts• L65-137, libs/…/hlm-date-range-picker.ts• L140-184, libs/…/hlm-date-range-picker.ts• L221-235, libs/…/brn-date-input.ts• L17-25, libs/…/brn-date-input.ts• L64-83, libs/…/brn-date-input.ts• L131-183

Slider controls

The slider represents one numeric value or a numeric range. Its value is an ordered number array, so one thumb produces one value and multiple thumbs produce a range. The control clamps values to its minimum and maximum and snaps changes to the configured step.

The slider supports horizontal or vertical orientation, inversion, tick marks, and optional range dragging. A minimum or maximum gap between thumbs prevents invalid overlap. Keyboard or pointer changes update the value and focus the thumb that changed. A draggable range moves all thumbs together while preserving its span inside the slider bounds. References: libs/…/brn-slider.ts• L65-172, libs/…/brn-slider.ts• L242-372, libs/…/brn-slider-track.ts• L19-104, libs/…/brn-slider-thumb.ts• L9-186, libs/…/brn-slider-range.ts• L4-45, libs/…/brn-slider-tick.ts• L13-65, libs/…/hlm-slider.ts• L5-49

Form state behavior

All controls that act as Angular form values use the same value contract: external form writes update the control, user changes call the form change callback, and blur or popup exit calls the touched callback. Disabled state blocks user changes and can come from either the control input or the form model.

Validation state flows from the bound control to the field and then to the control's accessible and visual state. invalid describes the form result. touched and dirty describe user progress. spartanInvalid is the display state used for validation feedback. forceInvalid can override the display state without changing the form model. References: libs/…/brn-field-control.ts• L19-31, libs/…/brn-field.ts• L35-61, libs/…/brn-input.ts• L9-36, libs/…/brn-select.ts• L212-227, libs/…/brn-combobox.ts• L259-287, libs/…/brn-slider.ts• L263-394, skills/…/forms.md• L27-57, skills/…/forms.md• L109-113

Change checklist

When these files or sections are changed, remember to consider these:

  • Keep the value shape aligned between single and multiple variants. A single option emits one value while a multiple option emits an array.
  • Keep labelable IDs and field registration aligned when a control gains a new trigger, input, or nested control.
  • Preserve the split between search text and selected value in combobox and autocomplete controls.
  • Keep listbox IDs, active-descendant IDs, expanded state, and trigger or input roles aligned in select, combobox, and autocomplete controls.
  • Keep select trigger focus, key handling, and option disabled state aligned when the select changes state while its popup is open.
  • Keep combobox chip-input ARIA state aligned with panel expansion and active-option changes. This includes aria-controls, aria-activedescendant, and form-state attributes.
  • Keep popup focus and touched behavior aligned when changing select, search, or date-picker keyboard handling.
  • Keep date parsing, display formatting, and form transformation consistent when changing date inputs or picker value rules.
  • Keep calendar month and year select values synchronized with the constrained focused date when changing calendar navigation or date limits. This includes short months and leap years.
  • Keep slider normalization rules aligned with thumb movement, range dragging, and the form value callback. This includes programmatic writes, step snapping, bounds, and thumb-gap rules.
  • Keep field description merging aligned when custom aria-describedby values and field-generated error IDs are both present.

Important failure modes

TriggerConsequenceGuard
A custom control is placed outside hlmField while it relies on field validation stateLabels, descriptions, and error state do not share one field contextWrap the control in hlmField or provide the control's validation and labeling context explicitly
A multiple control receives a single-value form modelSelection updates use the wrong value shape and can lose choicesBind an array to multiple select, combobox, or toggle-group variants
A search input is treated as the selected object valueTyping can submit text that does not match an optionKeep search state separate and commit only an option value
A collapsed select or combobox keeps aria-controls or aria-activedescendant exposedAssistive technology can receive a stale list relation or active descendantExpose popup relation IDs only while the panel is expanded
Enter is allowed to reach the parent form from a select or combobox inputThe form can submit before the active option is selectedPrevent default on Enter and commit the active option through the control
A select trigger does not restore focus after a pointer clickKeyboard navigation can start on the wrong element in browsers that do not focus clicked buttonsFocus the trigger before changing the popup state
A select is disabled while its popup is open but its options remain enabledA user can change the form value through an option that appears disabled at the triggerDerive each option's disabled state from both its own state and the parent select state
A combobox trigger toggles closed when a user clicks or types in an open inputSearch interaction hides the option panel under the userUse a non-toggling trigger for text-input mode
A disabled option is made active by direct active-item assignmentEnter can attempt to select an option that keyboard navigation should skipCheck the active item's disabled state before committing it
A date range is read before both endpoints are selectedThe form receives an incomplete rangeCommit the range only after start and end values exist
A calendar month or year change keeps an invalid day without clamping itThe focused date can move to an unintended month or yearClamp the day to the target period and apply the calendar's date limits before updating focus
A slider value falls outside its configured bounds or stepThe displayed thumb and form value do not represent the requested numberLet the slider clamp and snap values through its min, max, and step rules
A range thumb move violates an adjacent minimum or maximum gapThe move is rejected and the range can appear not to respondKeep thumb-gap constraints aligned with the step and the current thumb order
A disabled control receives a user eventThe UI may appear interactive while the form value must not changePreserve the disabled guard in the control's user-change path

Key files

FileWhy to read it
libs/…/brn-field.tsField-level state, labels, descriptions, and errors
libs/…/brn-field-control.tsBridge between Angular forms and field state
libs/…/brn-field-control-described-by.tsMerging field descriptions with manual description IDs
libs/…/hlm-field.tsShared field layout and orientation
libs/…/brn-input.tsMinimal headless text-control contract
libs/…/brn-checkbox.tsBoolean, indeterminate, label, and disabled state behavior
libs/…/brn-radio-group.tsOne-of-many value ownership
libs/…/brn-select.tsCustom popup selection flow
libs/…/brn-select-trigger.tsSelect keyboard, popup, and active-option behavior
libs/…/brn-select-item.tsOption selection, active state, and inherited disabled state
libs/…/brn-select-multiple.tsArray-valued select behavior
libs/…/brn-combobox.tsSearch plus selected-value behavior
libs/…/brn-combobox-input.tsCombobox and popup input display, keyboard, and form-state behavior
libs/…/brn-combobox-chip-input.tsMultiple-selection input state, chip removal, keyboard behavior, and popup accessibility state
libs/…/brn-combobox-multiple.tsArray-valued selection and chip lifecycle
libs/…/brn-autocomplete.tsSuggestion search and selected-value behavior
libs/…/brn-autocomplete-input.tsAutocomplete text, keyboard, and popup behavior
libs/…/brn-autocomplete-item.tsSuggestion option state and selection
libs/…/brn-checkbox.spec.tsGuarantees for checkbox labels, keyboard input, disabled state, and form accessibility
libs/…/brn-select-trigger.spec.tsGuarantee that trigger clicks restore focus before select interaction
libs/…/brn-select.spec.tsGuarantee that disabling an open select disables option interaction
libs/…/brn-date-input.tsDate text editing and commit lifecycle
libs/…/brn-calendar-month-select.tsCalendar month navigation through the shared select control
libs/…/brn-calendar-year-select.tsCalendar year navigation through the shared select control
libs/…/brn-slider.tsNumeric range normalization, bounds, step rules, and range movement
libs/…/brn-slider-track.tsPointer value changes and range dragging
libs/…/brn-slider-thumb.tsThumb keyboard and accessibility behavior
libs/…/brn-toggle.tsStandalone boolean button behavior
libs/…/brn-toggle-group.tsSingle and multiple button choices
libs/…/hlm-date-picker.tsCalendar date picker composition
libs/…/hlm-date-range-picker.tsCalendar range composition
skills/…/forms.mdRepository guidance for choosing and composing controls