Skip to content

Releases

v0.2.0

Choose a tag to compare

@tristendillon tristendillon released this 21 Nov 15:10
6c100f4

Minor Changes

  • #190 ce5885d Thanks @tristendillon! - Lists moved into a new arrays() plugin. It brings item keys that follow the items and the array commands push, insert, remove and move. Items, useItems and createItems take only array handles of forms that have it. A form with no lists no longer ships any of this, which saves about 550 B brotli. A form with a list pays about 150 B more than before.

    form.Items and scope.Items are gone. Import Items from the adapter and pass it the array's handle:

    // Before
    <form.Items name="tasks">{(task, index) => ...}</form.Items>
    
    // After
    import { Items } from "@lunarhue/forms-react"; // or "@lunarhue/forms-solid"
    
    <Items handle={form.root.at("tasks")}>{(task, index) => ...}</Items>

    To migrate, add arrays() to every form that renders or changes a list:

    import { arrays } from "@lunarhue/forms-react/plugins"; // or "@lunarhue/forms-solid/plugins"
    
    const forms = createForms({ plugins: [arrays()] });

    You can also put it in a single definition's plugins. Without it, passing an array handle to Items, useItems or createItems, or calling a command, is a type error that says "Add arrays() to this form's plugins". Code without types that calls a command throws arrays:missing in development, and the command does nothing in production.

    A component that takes an array field and renders or changes it says so in its type, as ArrayHandle<string[], typeof arrays>. It then fits only forms with arrays(). A plugin whose setup calls the commands should list arrays in requires. PluginContext.store is now typed with the names of the plugin's requires, so such a plugin gets the commands in its types.

    A form without arrays() can still hold an array and replace it whole with setValue. Its rows are then keyed by position (k0, k1, ...), so a row's state, such as touched, belongs to the position. If setValue shortens the list and a later write lengthens it again, the rows past the old end come back under their old keys with their old state. With arrays() they stay gone and new rows get new keys, as before.

  • #203 5638f90 Thanks @tristendillon! - errorsByKind(handle), a new core function the adapters re-export, returns a field's validation errors grouped by the kind of validator that set them: { schema, form, field }. schema holds the definition's schema errors, form the errors from validate.form, and field those from validate.fields and registerValidator, in the order they were added. A thrown validator's error goes under its validator's kind. Errors from setErrors aren't included, since the snapshot's userErrors has them. It is a read, not a subscription, and a standalone function rather than a store method, so a form that never calls it doesn't ship it. The new ErrorsByKind<E> and ValidatorKind types name the result and its keys.

  • #200 bcc99bb Thanks @tristendillon! - Errors from setErrors now make a field invalid, and core can list every invalid field.

    invalidFields(store), a new core function, returns every field with an error, in form order, as { field, name, errors }. The root comes first, with name "". subscribeInvalid(store, listener) fires when that list may change. Both are standalone functions rather than store methods, so a form that never lists its errors doesn't ship them. useInvalidFields(form) in React and createInvalidFields(form) in Solid follow the list, so an error summary is a few lines. The new InvalidField<E> type names an entry.

    The field snapshot has a new userErrors member, the errors setErrors put there. They come last in errors, so the validation errors are errors.slice(0, errors.length - userErrors.length).

    Breaking changes:

    • snapshot.invalid is true for a field with only setErrors errors. Before, it counted validation errors only. Code that read invalid to mean "a validator failed" should check errors.length > userErrors.length instead. The form's valid hasn't changed, so server errors still don't block the next submit.
    • store.firstInvalid() and store.disabledInvalid() now include fields with only setErrors errors. After a server refuses a save, firstInvalid() returns the field it named, so the app can focus it. The adapters still don't move focus after onSubmit sets errors, because that submit succeeded.
    • DisabledInvalid<E> is now an alias of InvalidField<E>. Its shape is unchanged.
  • #230 23b8bef Thanks @tristendillon! - A definition's validators go through two functions: validate.fields takes fieldValidators({...}) and validate.form takes formValidator(check). Core and both adapters export them. A form that uses neither doesn't ship the code that walks a validator tree or reads an ErrorMap. validate.on is gone: wrap a check in validateOn(check, timing), or set validate on createForms for every form.

    // Before
    validate: {
      on: "input",
      fields: { username: checkName },
      form: checkOrder,
    }
    
    // After
    validate: {
      fields: fieldValidators({ username: validateOn(checkName, "input") }),
      form: formValidator(validateOn(checkOrder, "input")),
    }

    Written inline in define, both take the form's value type, with a schema plugin's Draft where there is one. Defined on their own, they need it as a type argument: fieldValidators<Order>({...}). A schema passed to formValidator alone types the set by its input.

  • #284 c8a59a2 Thanks @tristendillon! - define in /mini takes a function of per-form options, as the main entry's does: forms.define((options: { q: string }) => ({ defaultValues: { q: options.q } })). The function runs once per store. createStore(options, instance?) and useForm(definition, options, settings?) take the options first, and a plain config still takes none. useForm reads the options on the first render only. Mini's define type parameters are now <V, S, O, R>, so O sits where it does on the main entry.

  • #262 2ee8b18 Thanks @tristendillon! - New experimental entries, @lunarhue/forms-core/mini and @lunarhue/forms-react/mini, for small forms such as a search box or a login. A search form on /mini is about 2.6 kB brotli with React external, against about 11 kB on the main entry. Expect breaking changes in minor releases while it is experimental.

    /mini keeps createForms().define(), useForm, form.Form, form.Field, form.State, inputProps, fieldValidators, Standard Schema validation, setErrors, reset and the transform, text and observe parts, pinned at a field with forms.fieldset(...parts).at(name). Every member has the same name and call as in the main entry, and its types are a subset of the main entry's, so a form moves up by changing the import. Validators run sync, on blur and on submit, then on each change once the field was blurred, has an error, or the form was submitted.

    It leaves out async validation, plugins, arrays, object fields in form.Field, dirty state, meta, native mode and devtools. Passing a plugin or a plugin's part to its define is a type error.

    By default a /mini form wakes every listener on each change, which is the least code. createForms({ subscribe: perField() }) wakes only the fields that changed, for about 220 B more. The main entry takes the same option and ignores it, since it always wakes per field.

  • #182 d6b2008 Thanks @tristendillon! - dirty and changes() exist in the types only on forms with dirty(). Reading dirty from a form or field snapshot, from useField, useFormState, a Field, form.State or the Solid readers, or calling store.changes(), is a type error without it, and so is giving equals. The snapshots still hold dirty: false at runtime. In development changes() without dirty() now throws dirty:missing instead of logging dirty:untracked, and in production it returns an empty list.

    Stores and snapshots take the installed plugin names as a new last type parameter. Handles and the adapters' field types take the plugins by factory, before the error type. A component that reads dirty from its field types it as FieldApi<string, typeof dirty> and then fits only forms with dirty(). AnyFormStore<E, "dirty"> is a store of any form that has it.

    One Plugin<A, N, M, F, S> type replaces Plugin, HookPlugin, SchemaPlugin, DirtyPlugin, AnyPlugin and AnyHookPlugin. Use Plugins for a list of any plugins. defineSchemaPlugin(adapter) is gone. Write definePlugin({ name, schema: adapter }).

    A plugin with field or form meta must give initialMeta or `initialFormMe...

Read more

v0.1.1

Choose a tag to compare

@tristendillon tristendillon released this 18 Nov 03:33
e7a015b

The first patch. It moves the Solid adapter to the stable Solid 2 release and fixes the bugs people hit first.

Install

npm install @lunarhue/forms-react    # React 16.8 and later
npm install @lunarhue/forms-solid    # Solid 2

Changes

  • @lunarhue/forms-solid, @lunarhue/forms-bind: the adapter and the Solid binding now ask for the stable Solid 2 release. The solid-js and @solidjs/web peer ranges start at 2.0.0, so the 2.0 release candidates no longer satisfy them. (#147)
  • @lunarhue/forms-solid: an input shows the form's value after hydration even when the browser filled it back in first. Firefox restores typed text on a reload, and on Solid 2.0.0 the input kept that text while the form held something else. (#161)
  • @lunarhue/forms-core: define accepts a value type that refers to itself, such as a tree node with a children array of tree nodes. Before, TypeScript rejected these with TS2615. (#152)
  • @lunarhue/forms-core: you can write a value you read from a field straight back. setValue, push, insert and the rebase plugin take the readonly values that peek() and field objects return, so tags.setValue(tags.peek().value) compiles without a spread or a cast. (#154)
  • @lunarhue/forms-core: in a form without a schema, onSubmit gets a copy of the form value that it may change. Sorting or pushing to an array in onSubmit no longer throws in development. (#150)
  • @lunarhue/forms-core: a plugin can write from the cleanup its onActive returns. Before, that write waited for the next write to the store, which might never come. (#148)
  • @lunarhue/forms-bind: the zustand binding is built and tested against zustand 5.1. The peer range is still ^5.0.0. (#113)

Docs

  • The READMEs said Solid 1.8 works through @lunarhue/forms-solid/v1. That entry only re-exports core for now, so they now say the adapter needs Solid 2. #124 tracks the Solid 1 adapter. (#149)
  • docs/plugins.md explains the TS2353 error define gives inside a function that is generic over the schema type, and shows the helper that works. (#153)

Known issue

  • On Solid in Safari, moving an array row takes focus off the row's input. (#157)

All five packages share one version. Each has its own CHANGELOG.md.

v0.1.0

Choose a tag to compare

@tristendillon tristendillon released this 14 Nov 23:06
e25d25f

The first release of forms: forms for React and Solid, with the form logic in one small core that knows nothing about either.

Install

npm install @lunarhue/forms-react    # React 16.8 and later
npm install @lunarhue/forms-solid    # Solid 2, and Solid 1 at ./v1

The adapter brings the core and the plugins that need no other library. Add these when you want them:

npm install @lunarhue/forms-schema zod    # or valibot
npm install @lunarhue/forms-bind          # Zustand or a Solid signal

Packages

All five share one version.

Package What it is
@lunarhue/forms-react Type-safe forms for React, with features as plugins
@lunarhue/forms-solid Type-safe forms for Solid, with features as plugins
@lunarhue/forms-schema Zod and Valibot plugins
@lunarhue/forms-bind Binds a form to a Zustand store or a Solid signal
@lunarhue/forms-core The framework-free core: values, arrays, submit and plugins. The adapters depend on it, so you rarely install it yourself

Changes

  • First release. (#119)
  • @lunarhue/forms-core: a write that a plugin queues from onSubmitted is now applied when the hook returns, with one notification. Before, it waited for the next write from anywhere else. (#118)

Each package has its own CHANGELOG.md.