Skip to content

[v2] Allow createFormHook to define default form options #2298

Description

@LeCarbonator

Applications often want forms created by the same hook to share a small set of behaviors. createFormHook could support this by accepting non-generic default options, creating a reusable preset that is automatically applied whenever the resulting useAppForm hook is used.

For example:

export const { useAppForm } = createFormHook({
  // ...
  defaultOptions: {
    // NOT: defaultValues, validators, onSubmit
    // YES: listeners, onSubmitInvalid, errorVisibility (generalized to `any`?)
    errorVisibility: ({ fieldState }) => fieldState.meta.isTouched,
  },
})

useAppForm({}) // Contains the default errorVisibility

This would let users configure shared behavior once while continuing to provide form-specific values, validators, and submission logic to each call to useAppForm.

Important

The defaults should be limited to options that do not depend on the form's generic types. Options such as defaultValues, validators, and onSubmit would therefore remain form-specific. Possible defaults include listeners, onSubmitInvalid, and errorVisibility, although supporting errorVisibility may require generalizing its types to any.

Metadata

Metadata

Assignees

No one assigned

    Labels

    area: runtimeThis issue affects the runtime of the library.area: typesThis issue affects the types of the library.scope: reactThis issue specifically affects the react adapter.type: enhancementThis requests an enhancement to existing behaviour.v2This issue affects v2 as well.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions