Skip to content

Any good examples of large complex forms? #3124

Answered by K1vs
nareshbhatia asked this question in Q&A
Discussion options

You must be logged in to vote

To complement the idea of nareshbhatia for issue #2:

Typed helper hook for subform, this increases the comfort of use in the IDE.

import {
  FieldValues,
  Path,
  RegisterOptions,
  useFormContext,
} from "react-hook-form";

const useFormSubContext = <TSubForm extends FieldValues>(
  contextPath: string
) => {
  const { register, ...methods } = useFormContext<TSubForm>();
  const subregister = <TFieldName extends Path<TSubForm>>(
    property: TFieldName,
    options?: RegisterOptions<TSubForm, TFieldName>
  ) => {
    const path = `${contextPath}.${property.toString()}`;
    return register(path as any, options);
  };

  return {
    register: subregister,
    ...methods,
  };
};

expor…

Replies: 1 comment 5 replies

Comment options

You must be logged in to vote
5 replies
@nareshbhatia
Comment options

@nareshbhatia
Comment options

@nareshbhatia
Comment options

@K1vs
Comment options

Answer selected by nareshbhatia
@NixBiks
Comment options

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
4 participants