bietkul / react-reactive-form

Angular like reactive forms in React.
MIT License
309 stars 32 forks source link

Add controls to FormGenerator onMount? #81

Closed bostondevin closed 2 years ago

bostondevin commented 2 years ago

Is there a way to do this when the form mounts so I can add form fields to the form dynamically?

  const config = {
    controls: {},
  };

  const mountForm = (f) => {
    f.addControl("firstName", Input); // This doesn't work...
    f.addControl("lastName", Input); // This doesn't work...
  };

  return (
    <Form className="grid grid-cols-2 gap-2 p-2">
      <FormGenerator onMount={mountForm} fieldConfig={config} />
    </Form>
  );
bietkul commented 2 years ago

@bostondevin No, it is not possible. The second option in addControl method should be an instance of FormGroup, FormControl or FormArray classes.

bostondevin commented 2 years ago

Thanks! I figured it out with just changing the config around of the FormBuilder