vaadin / proposal-for-vaadin-form

Small and fast framework-agnostic library for creating forms with Web Components
Apache License 2.0
9 stars 0 forks source link

Form rendering: 2-way binding extention to `lit-html` #19

Open vlukashov opened 5 years ago

vlukashov commented 5 years ago

This approach to reactive form rendering depends on the lit-html library. Vaadin Form comes with a custom form directive and an extention to the lit-html syntax to allow 2-way data binding.

import { VaadinForm } from '@vaadin/form';
import { html } from 'lit-html';

const form = new VaadinForm();

const template = html`
  ${form(form => html`
    <!-- well-known form inputs just work -->
    <div class="form-field">
      <label>User Name <input type="text" !value=${form.username}></label>
      ${form.username.touched && form.username.invalid
        ? html`<p class="error">${form.username.message}</p>`
        : ''}
    </div>

    <button @click=${form.submit} ?disabled=${!form.canSubmit}>
      Submit
    </button>
  `)}
`;