Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 13 additions & 10 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@

**Ask the user for an object, the way `prompt()` asks for a string.**

Every other form library gives you a *component to mount*. This one gives you a
*function to call*. There is no component, no form state, no mount point, and no
Every other form library gives you a _component to mount_. This one gives you a
_function to call_. There is no component, no form state, no mount point, and no
place in your tree where the form has to live. You ask a question from wherever
you happen to be standing in your code, and you get the answer back.

Expand Down Expand Up @@ -48,14 +48,13 @@ const { register, handleSubmit } = useForm();
return (
<Modal open={open} onClose={() => setOpen(false)}>
<form onSubmit={handleSubmit(onSubmit)}>
<input {...register('title')} />
…
<input {...register('title')} />…
</form>
</Modal>
);
```

Now the form has a *location*. It needs a parent, a piece of `open` state, a
Now the form has a _location_. It needs a parent, a piece of `open` state, a
close handler, a submit handler, and a route from the answer back to the code
that wanted it. For a product's signup page, that is fine — you were going to
lay that page out anyway. For the two hundredth settings dialog in an internal
Expand Down Expand Up @@ -100,8 +99,8 @@ main-window script, or a `<script type="module">` tag in a static page. It never
touches your render tree, so there is nothing to integrate.

**The descriptor is live, not static.** `options`, `isActive`, `defaultValue`,
`placeholder`, `message`, `tab` and `compute` may each be a *function of the
current values*, and `reloadOnChangeOf` declares which field depends on which.
`placeholder`, `message`, `tab` and `compute` may each be a _function of the
current values_, and `reloadOnChangeOf` declares which field depends on which.
Options loaded from your API that change when another field changes are the
core feature, not an extension point.

Expand Down Expand Up @@ -171,9 +170,12 @@ Plainly, so you can rule it out fast:
- **It is not a general-purpose form library.** It renders one fixed layout. If
you need control over the markup — a product signup page, a marketing form —
use a state library and write the markup yourself.
- **There is no validation framework.** `isActive` hides fields, and
`isValidRecord` and a button's `isActive` gate submission, but there are no
validation rules, no error messages, and no schema validation.
- **There is no validation framework.** No `required`, no rule objects, no
schema. There _are_ per-field error, warning and loading messages
(`setTooltipError` and friends), and `isValidRecord` and a button's `isActive`
gate submission — but you write each check yourself, and the message and the
gate are separate things you wire up separately. See
[Validation](https://wolfoo2931.github.io/declarative-forms/guide/validation).
- **Accessibility is unfinished.** Labels, ids, focusable buttons and
keyboard-reachable checkboxes are correct. Dialog semantics, a focus trap, and
combobox ARIA are not. Read
Expand All @@ -189,6 +191,7 @@ Plainly, so you can rule it out fast:
- [Getting started](https://wolfoo2931.github.io/declarative-forms/guide/getting-started)
- [Field kinds](https://wolfoo2931.github.io/declarative-forms/guide/fields/)
- [Reactivity](https://wolfoo2931.github.io/declarative-forms/guide/reactivity) — `isActive`, `reloadOnChangeOf`, computed fields
- [Validation](https://wolfoo2931.github.io/declarative-forms/guide/validation) — checks, tooltip messages, gating submit
- [Modals & stacking](https://wolfoo2931.github.io/declarative-forms/guide/modals)
- [API reference](https://wolfoo2931.github.io/declarative-forms/reference/api)
- [Migrating from v1](https://wolfoo2931.github.io/declarative-forms/migration-v1)
Expand Down
1 change: 1 addition & 0 deletions docs/.vitepress/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ export default defineConfig({
text: 'Guide',
items: [
{ text: 'Reactivity', link: '/guide/reactivity' },
{ text: 'Validation', link: '/guide/validation' },
{ text: 'Tabs', link: '/guide/tabs' },
{ text: 'Modals & stacking', link: '/guide/modals' },
{ text: 'Buttons', link: '/guide/buttons' },
Expand Down
5 changes: 4 additions & 1 deletion docs/accessibility.md
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,10 @@ no accessible name.
### No validation semantics

There is no `aria-invalid`, no error-message association, and no live region.
Tooltip state changes (`setTooltipError`) are visual only and are not announced.
Tooltip state changes (`setTooltipError`) are visual only and are not announced,
so a screen-reader user gets no notice that a field they just left is in an
error state. This is the main accessibility caveat on
[Validation](/guide/validation).

### Unaudited

Expand Down
13 changes: 11 additions & 2 deletions docs/guide/fields/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -65,8 +65,17 @@ marker sits inside the input instead of next to the label.
{ name: 'key', displayName: 'Key', tooltip: { text: 'Secret.', inInput: true } }
```

You can also change a tooltip while the form is open — see
[`setTooltipError` and the related methods](/reference/api#tooltips).
You can also change a tooltip while the form is open — this is how a field
reports an error, a warning or a pending check. See
[Validation](/guide/validation) for the pattern, and
[`setTooltipError` and the related methods](/reference/api#tooltips) for the
signatures.

::: tip A field with no `tooltip` cannot show a message
`setTooltipError` and friends are silent no-ops when the descriptor declares no
`tooltip`, because there is no marker to write on. Declare one for any field you
intend to validate.
:::

## `Reactive<T>`: literal or function

Expand Down
28 changes: 22 additions & 6 deletions docs/guide/reactivity.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,15 @@ This is the part that makes the library worth using. A descriptor is not a
static schema. Most of its options can be **functions of the current form
data**, and the library runs them again whenever something changes.

There are four mechanisms, each for a different job:
There are five mechanisms, each for a different job:

| Mechanism | Question it answers |
| ------------------------------------------------------------ | ------------------------------------------------ |
| [`isActive`](#isactive-conditional-fields) | Should this field exist right now? |
| [`reloadOnChangeOf`](#reloadonchangeof-dependent-async-data) | Which changes should reload my options? |
| [`compute`](/guide/fields/computed) | Which value is calculated from the other values? |
| [`onFormChange`](#onformchange-side-effects) | What should happen elsewhere when values change? |
| [`setTooltipError`](/guide/validation) | What is wrong with what the user just typed? |

On top of that, the general [`Reactive<T>`](#reactive-options) options accept a
function as well: `placeholder`, `message`, `tab`, `defaultValue` and
Expand Down Expand Up @@ -169,13 +170,28 @@ things that happen outside the form: updating a preview, saving a draft.
the update was started from code. Checking it stops your effect from running on
every unrelated change.

::: warning Do not set form values here
Setting a value inside `onFormChange` starts the update cycle again. Use a
[`computed`](/guide/fields/computed) field for calculated values instead. The
library protects itself against endless loops, but it throws an error if the
updates never come to an end.
::: warning Guard any value you write from here
Setting a value inside `onFormChange` starts the update cycle again, which calls
`onFormChange` again. That is fine for the case it exists for — filling fields
in from a lookup — as long as you write only what actually changed:

```ts
if (data[name] !== value) form.field(name)?.setValue(value);
```

Without that guard it is an endless loop, and the library throws rather than
hangs. For a value derived purely from other values, do not write it at all —
use a [`computed`](/guide/fields/computed) field. See
[Validation](/guide/validation#filling-fields-in-from-a-lookup).
:::

## Validation

`onFormChange` plus `trigger` is also where per-field validation goes: run the
check, then report it with `form.setTooltipError(name, …)`. Because the message
and the submit gate are separate, this has a page of its own —
see [Validation](/guide/validation).

## Reactive options

These options accept either a fixed value or a function of the context:
Expand Down
Loading
Loading