# Forms

> One to three text fields in a notification, sent from the Island — the fields, the checks before sending, drafts and the keyboard.

Source: https://cloudlet.dev/docs/components/toaster/forms

A form action puts one to three text fields and a round send button in the open Island, so a reply, a name or a short note goes out without a dialog or a page change. The collapsed pill stays a pill.

## A form action

A form is the third kind of primary action, next to a command and a link: `{ kind: 'form', label, fields, run }`; every prop is in [`FormAction`](https://cloudlet.dev/docs/components/toaster/api/types#primaryaction). The send button shows an arrow, and `label` is its name for screen readers. `run` receives `values`, typed by the field names.

Snippets assume `const cloudlet = useCloudlet();`.

```tsx
cloudlet.show({
  title: 'A cloud asks where to drift',
  action: {
    kind: 'form',
    label: 'Send',
    fields: [{ name: 'place', kind: 'text', label: 'Where to', required: true }],
    run: ({ values }) => {
      cloudlet.show({ title: `Drifting to ${values.place}`, kind: 'success' });
    },
  },
});
```

## Fields

Each field is `'text'` for one line or `'textarea'` for several; a textarea starts at three rows and grows to six, then scrolls. Its `label` is required and doubles as the placeholder. Every option is in [`FormField`](https://cloudlet.dev/docs/components/toaster/api/types#primaryaction).

## Checks before sending

On send, `required`, then `maxLength`, then your own `validate(values)` run in that order; the first that fails stops the send without calling `run`.

```tsx
cloudlet.show({
  title: 'Leave a note for the sky',
  action: {
    kind: 'form',
    label: 'Send note',
    fields: [
      { name: 'name', kind: 'text', label: 'Your name', autoComplete: 'name' },
      { name: 'note', kind: 'textarea', label: 'Note', required: true, maxLength: 280 },
    ],
    validate: (values) => values.note.trim().length >= 3 || { note: 'A few more words, please' },
    run: () => {
      cloudlet.show({ title: 'Note tucked into a cloud', kind: 'success' });
    },
  },
});
```

A message under a field marks it invalid and moves focus to the first such field. Editing a field clears its message.

## Sending

Sending works like a [command](https://cloudlet.dev/docs/components/toaster/operations#actions). While `run` is pending, the button shows a spinner, the fields turn read-only and a second send is refused. When `run` returns or resolves, the notification closes. When it throws or rejects, the text stays, the message from `errorMessage(error)` appears under the form and is announced, and the user can send again.

```tsx
let tries = 0;
cloudlet.show({
  title: 'Send a postcard to the clouds',
  action: {
    kind: 'form',
    label: 'Send',
    fields: [{ name: 'message', kind: 'text', label: 'Message', required: true }],
    errorMessage: () => 'The wind took it. Try again?',
    run: async () => {
      await new Promise((resolve) => setTimeout(resolve, 800));
      tries += 1;
      if (tries === 1) throw new Error('Gust of wind');
      cloudlet.show({ title: 'Postcard delivered', kind: 'success' });
    },
  },
});
```

## Drafts

Typed text belongs to the notification, not to the screen. It stays when the Island collapses, when the user swipes to another notification and when the Toaster remounts. A new action object, a successful send or closing the notification starts over.

While a form holds unsent text:

- its time stands still (the pause reason `'draft'`), even with a `duration`;
- a press outside does not collapse the Island; Escape and the pill still do;
- a notification arriving with [attention](https://cloudlet.dev/docs/components/toaster/notifications#attention) waits in the stack instead of taking the Island over;
- the collapsed pill adds `labels.draft` to its name, so a screen reader says there is a draft.

Your code sees only that there is a draft ([`NotificationSnapshot.draft`](https://cloudlet.dev/docs/components/toaster/api/types#notificationsnapshot)); the text stays inside Cloudlet.

## Keyboard

- **Enter** sends from a text field; in a textarea it starts a new line, and **⌘ Enter** or **Ctrl Enter** sends.
- **Arrows, Home and End** move the caret inside a field; they switch notifications only outside the fields.
- **Escape** collapses the Island and keeps the draft.

Focus never moves into a field on its own: a new notification, an auto-opened Island or a collapse leaves it where it was. The on-screen keyboard appears only when the user taps a field.
