Basic Example
This example demonstrates a basic form with two fields using the generic element
<kin-field>.
For reusable form fields, consider creating custom elements that extend
FormField<TValue, TParentValue> or FieldGroup<TValue, TParentValue>.
Key Concepts
This example showcases the simplest way to get a form up and running.
-
FormController: We initialize aFormControllerwith aUsertype, providing it with aninitialValueand anonSubmithandler. The controller manages the entire state of the form. -
<kin-field>: This is a generic, unstyled wrapper element provided by the library. It's useful for simple forms or for when you want to provide a completely custom template for a field. -
Custom Template: The
<kin-field>element accepts a.templateproperty. This is a function that receives the field's state (f) and returns a Lithtmltemplate. This gives you full control over the field's appearance and behavior. -
Binding with
field(): The${this.#form.field('name')}directive binds the<kin-field>element to thenameproperty of the form's data. This handles the two-way data flow between the input and the form's state. -
Event Handling: The template connects standard DOM events like
@blurand@changeto the handler methods provided by the field state (f.handleBlur,f.handleChange), which updates the form controller automatically.