Forms
Angular's Signal Forms, reactive forms and template-driven forms all work on Lynx. Bind Lynx's <input> and <textarea>, or the UI components, and write the same form code as on the web.
Set up
Import LYNX_ELEMENTS, which includes the value accessors for <input> and <textarea>. Then import the Angular forms API you use:
What binds
Lynx has only <input> and <textarea>: no checkbox, radio or select element. For those, use the UI components. Every control below binds to all three APIs ([formField], [formControl]/formControlName and [(ngModel)]).
- Touched is when a form shows its errors. A phone's switch or select never takes the focus, so each control marks itself touched when the person is done with it.
- A single Toggle isn't a form control. Its state is
pressed, notvalueorchecked. Use a Switch or a Checkbox in a form.
Signal Forms
submit()marks every field touched, so every error shows, and runsactiononly when the form is valid.focusBoundControl()works, on a field or on the whole form, which focuses its first bound field. An<input>, a<textarea>and the text UI components take the focus and open the keyboard. A switch or a select has no focus to take.- Put the form on a
<view>. Lynx has no<form>element and no submit event, so submit from a tap.
Numbers
A type="number" or type="digit" field gives its form a number, and null while it's empty, as Angular's number fields do on the web. Bind it to a number | null:
- Text that isn't a number leaves the value as it was and puts a
parseerror on the field. That's anything but a finite decimal, as a web number field takes:1,5, a lone-,0x10orInfinity. - The
parseerror has no message. Show one of your own forerror.kind === 'parse', as the example above does. - A point with nothing after it is taken, as a browser's number field takes it typed.
1.is 1, on the way to1.5, so typing it shows no error. - A field bound to a string keeps strings. A
FormControl('')or astringfield on atype="number"field still gets'42'. Give it anumber | nullto get numbers. - Reactive forms differ from the web here. On the web, a reactive control on a number field gets numbers whatever it started as. Here, start it as a number or
null:new FormControl<number | null>(null). - The Input UI component holds a string. Use
<input type="number">for a number field.
Constraints
Rules in the form reach the field, so the field behaves as the form says:
- They reach
<input>,<textarea>, and the UI components that have the matching input. min(),max(),minLength(),pattern()andrequired()only validate. Lynx's fields have no attribute for them.- With reactive forms, bind
[maxLength]on the field, as on the web:<input [formControl]="note" [maxLength]="140" />.
Reactive forms
disable()andenable()work. They take the field's keyboard away and give it back.- Write
<textarea></textarea>in full. Angular doesn't allow<textarea />.
Template-driven forms
[(ngModel)] can bind straight to a signal.
Set a value from code
setValue(), reset() and [(ngModel)] all update what the field shows, and so does binding [value] on an <input> or <textarea>.
Don't bind [attr.value]. On iOS and Android a Lynx field has no value attribute, so it never shows. The form APIs and [value] set the field's text with its setValue method instead, and a field that renders with a value shows it from its first frame.
On-screen keyboard
confirm-typesets the keyboard's action key. Usedone(the default),next,go,searchorsend. Pressing it fires(bindconfirm), with the text indetail.value.(bindfocus)and(bindblur)fire when the field gains and loses focus, also withdetail.value.- Focus a field from code with
focus()on its element, as on the web:this.search().nativeElement.focus().blur()closes the keyboard. typepicks the keyboard:text,number,digit,password,teloremail.- The keyboard doesn't move the page. Lynx's
<input>doesn't scroll out of the keyboard's way, so the keyboard can cover the field being typed in. Put long forms in a<scroll-view>with extra space at the bottom, so people can scroll a covered field into view. - A field at the bottom of the screen can rise above the keyboard itself, as the Search Field does. The input's
(bindkeyboardheightchange)sends the keyboard's height indetail.height: points on iOS, physical pixels on Android. avoid-keyboardmoves the whole page. It's Lynx's own keyboard avoidance on an<input>:avoid-keyboard="true", withavoid-keyboard-spacing="8px". It shifts the entire Lynx view up until the focused field clears the keyboard, so the top of the page goes off screen. It suits a form. A bar at the bottom should rise on its own instead.
Limitations
- No
<label for>. Lynx doesn't link labels to fields. Put a<text>next to the field, or use the Label component. - No
:invalidor:focusstyles. Lynx has no form pseudo-classes. Style from the field's state instead, such as[class.border-destructive]="field().invalid()".
Related
<input>and<textarea>: every attribute and event- Form value accessors
- Signal Forms example
- Forms on angular.dev
