Search code examples
react-final-form

Unable to access mutator functions in Wizard form page while using react-final-form


I am trying to create a Wizard form using react-final-form by referring to this code https://codesandbox.io/s/km2n35kq3v. For my use case I need some mutator functions to be used inside my form fields. This example illustrates how to do that - https://codesandbox.io/s/kx8qv67nk5?from-embed. I am not sure how to access mutator functions in my form steps when I am using a wizard form instead of a single page form.

I tried to combine both the examples by modifying the <Form> component rendered by Wizard.js to pass in the mutators. However I cannot access these mutators in the Wizard form pages.

In Wizard.js

    return (
      <Form
        mutators={{
          // potentially other mutators could be merged here
          ...arrayMutators,
        }}
        render={({
          handleSubmit,
          submitting,
          values,
          pristine,
          invalid,
          form: {
            mutators: {push, pop, remove},
          },
        }) => {
          return (
            <form onSubmit={handleSubmit}>

Another file index.js


    <Wizard
      initialValues={{ employed: true, stooge: "larry" }}
      onSubmit={onSubmit}
    >
      <Wizard.Page>
        <FieldArray name="customers">
          {({ fields }) =>
            fields.map((name, index) => (
              <div key={name}>
                <label>Cust. #{index + 1}</label>

                <Field
                  name={`${name}.firstName`}
                  component="input"
                  placeholder="First Name"
                />
                <span
                  onClick={() => fields.remove(index)}
                  style={{ cursor: "pointer" }}
                >
                  ❌
                </span>
              </div>
            ))
          }
        </FieldArray>
      </Wizard.Page>
</Wizard>

It errors out - remove is undefined in index.js


Solution

  • Look at this working example: https://codesandbox.io/s/znzlqvzvnx

    changes I have made:

    Wizard.js

         static Page = ({ children, mutators }) => {
            if(typeof children === 'function'){
              return children(mutators);
            }
    
            return children;
          };
    
         ...
    
         <form onSubmit={handleSubmit}>
           {
              // activePage
              <activePage.type {...activePage.props} mutators={mutators} />
           }
    
         ...
    
    

    index.js (only first <Wizard.page>)

          <Wizard.Page>
            {
              ({ upper }) => (
                <React.Fragment>
                  <div>
                    <label>First Name</label>
                    <Field
                      name="firstName"
                      component="input"
    
                      ...
    
                  </div>
                </React.Fragment>
              )
            }        
          </Wizard.Page>