Skip to content

Form values organized by step and pre-populating related fields? #77

Answered by ivan-dalmet
lscmaro asked this question in Q&A
Discussion options

You must be logged in to vote

@lscmaro You can handle this use case with a custom hook like this:

import { useEffect } from "react";
import { useForm } from "@formiz/core";
import get from "lodash/get";

export const useDependentField = (fieldNameOrigin, fieldNameDestination) => {
  const { setFieldsValues, values, fields } = useForm({ subscribe: { fields: [fieldNameOrigin, fieldNameDestination] } });
  const fieldOriginValue = get(values, fieldNameOrigin);  // Needs Lodash here to handle nested fields names
  const fieldDestinationPristine = get(fields, fieldNameDestination)?.isPristine;  // Needs Lodash here to handle nested fields names

  useEffect(() => {
    // Update only if the destination field is pristine
    

Replies: 2 comments 9 replies

Comment options

You must be logged in to vote
2 replies
@ivan-dalmet
Comment options

@lscmaro
Comment options

Comment options

You must be logged in to vote
7 replies
@ivan-dalmet
Comment options

@lscmaro
Comment options

@ivan-dalmet
Comment options

@ivan-dalmet
Comment options

@ivan-dalmet
Comment options

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