Skip to content</> asasvirtuais
Docs menuForms
Docs/Core concepts

Forms

Form holds a set of fields and an async action. The render prop gets both.

tsx
import { Form } from 'asasvirtuais/form'

<Form defaults={{ email: '', password: '' }} action={login} onResult={() => router.push('/')}>
  {login => (
    <form onSubmit={login.submit}>
      <input value={login.fields.email} onChange={e => login.setField('email', e.target.value)} />
      <input type='password' value={login.fields.password} onChange={e => login.setField('password', e.target.value)} />
      <button disabled={login.loading}>Log in</button>
      {login.error && <p>{login.error.message}</p>}
    </form>
  )}
</Form>

Render props#

PropWhat it is
fieldsthe current values
setField(name, value)change one field
setFields(partial)change several fields
submitsubmit handler for <form onSubmit> or a button
callback(params)run the action directly with params
loadingtrue while the action runs
resultthe last value the action returned
errorthe last error the action threw
errorsevery error the action has thrown, oldest first

Props: defaults (initial fields), action, onResult (runs with the result once the action resolves), onError (runs with the error) and autoTrigger (runs the action once on mount).

FieldsProvider (asasvirtuais/fields) and ActionProvider (asasvirtuais/action) are the two halves of Form. You can use either one on its own: fields with no action, or an action with no fields (a delete button, for example).

Name the render prop after the thing#

Call the render prop what the form represents (login, order, project), not form. Nested forms then share one closure and stay readable.

Nested and multi-step forms#

An inner form runs its own async action, and its onResult can write into the outer form's fields before the outer form submits:

tsx
<Form defaults={{ item: '', address: '' }} action={placeOrder} onResult={order => router.push(`/orders/${order.id}`)}>
  {order => (
    <form onSubmit={order.submit}>
      <input value={order.fields.item} onChange={e => order.setField('item', e.target.value)} />

      <Form defaults={{ zip: '' }} action={lookupZip} onResult={a => order.setField('address', a.street)}>
        {zip => (
          <div>
            <input value={zip.fields.zip} onChange={e => zip.setField('zip', e.target.value)} />
            <button type='button' onClick={zip.submit} disabled={zip.loading}>Fill address</button>
          </div>
        )}
      </Form>

      <button type='submit' disabled={order.loading}>Place order</button>
      {order.error && <p>{order.error.message}</p>}
    </form>
  )}
</Form>

order.loading and zip.loading are independent. Multi-step forms and most workflows are built this way: each step is a form, and its result opens the next.

Table forms#

For database operations, use the table forms from asasvirtuais/forms. They work like Form, run against a table, and update the reactive index when they resolve:

  • CreateForm: creates a row.
  • UpdateForm: updates a row. It sends only the fields in its state (defaults plus what setField changed), so a one-column, one-click update is just form.callback({ done: true }).
  • FilterForm: lists rows from the client, for searches and pagination. It keeps its result local.

See The interface.