Skip to content

Export useFieldState, splitFieldProps and usePublishField for custom fields #23

Closed

Description

@n4ff4h

javaabu/block-editor ships a React field for @javaabu/forms (<BlockEditorField name="body" />, Javaabu/block-editor#8). It should behave like the built-in fields, so it needs the hooks Wysiwyg uses, and they aren't exported from @javaabu/forms:

  • useFieldState(name, initial, options): reads and writes the form's external store (Inertia useForm() data), returns to the starting value on reset, and clears the field's errors when edited.
  • splitFieldProps(props): separates FieldProps from control props.
  • usePublishField(entry): publishes the value so ConditionalWrapper / useWatchField can react to it.

Request: export these three (and their option types) from the package root, documented as the API for custom fields in other packages. A short "Custom fields" docs page with a Wysiwyg-like example would help other package agents too.

Until then: block-editor's field keeps its own state and posts a hidden input. With useForm(), apps bind it by hand (value={form.data.body} onChange={(v) => form.setData("body", v)}), and reset and ConditionalWrapper don't see it.

Activity

  1. added
    next-genNext-gen Javaabu packages
    cross-packageRequested by or affecting another next-gen package
    on Nov 5, 2026
  2. n4ff4h commented on Nov 5, 2026

    @n4ff4h
    CollaboratorAuthor

    Done in 204e616 on next-gen (PR #19), CI green: https://github.com/Javaabu/react-forms/actions/runs/42055926515

    Exported from @javaabu/forms: useFieldState, splitFieldProps, usePublishField, and the types FieldStateOptions, UseFieldOptions, FieldEntry, FieldKind. With useField and useFieldValue (already exported), a field from another package gets the same behaviour as the built-in ones: useForm() data, resetOnSuccess, ConditionalWrapper / useWatchField, and errors that clear on edit.

    • Docs: docs/advanced-usage/custom-fields.md (a Wysiwyg-like example plus a checklist: hidden input for native submits, ARIA on non-native controls, controlled, toStore / fromStore, key, store: false, nested-key errors). It's also in the Boost skill (references/custom-fields.md) and llms.txt.
    • Tests: src/components/custom-field.test.tsx builds a field with only root imports and covers binding, native submit, errors clearing on edit, ConditionalWrapper, resetOnSuccess and useForm() data.

    For BlockEditorField: replace the own state and onChange → clearErrors with useFieldState(name, initial, { toStore: JSON.stringify, fromStore: … }), and add usePublishField. Block-level errors can keep reading useFormState()?.errors.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    cross-packageRequested by or affecting another next-gen packagenext-genNext-gen Javaabu packages

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions