Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Submit with multi level components doesn't work #52

Open
italoiz opened this issue Nov 18, 2019 · 1 comment
Open

Submit with multi level components doesn't work #52

italoiz opened this issue Nov 18, 2019 · 1 comment

Comments

@italoiz
Copy link

italoiz commented Nov 18, 2019

First of all, thank you to the creator, a sensational library. 🙂

It is quite common when we have large form with multiple sections (fieldset), to separate the sections into different components, but for some reason the library does not work with nested components. Example:

// App.jsx
function App() {
  function handleSubmit(data) {
    console.log(data);
  }

  return (
    <Form onSubmit={handleSubmit}>
      <fieldset name="section1">
        <input name="field1" type="text" placeholder="field1" />
      </fieldset>

      <Section2 />

      <button type="submit">submit</button>
    </Form>
  )
}

// Section2.jsx
export default function SectionFields() {
  return (
    <fieldset name="section2">
      <input name="field2" type="text" placeholder="field2" />
    </fieldset>
  )
}

StackBlitz Code

I expected to receive something like:

{
  "section1": {
    "field1": "1"
  },
  "section2": {
    "field2": "2"
  }
}

But I'm getting this:

Without section2 object.

{
  "section1": {
    "field1": "1"
  }
}
@derekstavis
Copy link
Owner

derekstavis commented Dec 20, 2019

unfortunately this use case is not supported because of how react and this library works. you can, instead, call the function inline on the parent render function:

function App() {
  function handleSubmit(data) {
    console.log(data);
  }

  return (
    <Form onSubmit={handleSubmit}>
      <fieldset name="section1">
        <input name="field1" type="text" placeholder="field1" />
      </fieldset>

      {Section2()}

      <button type="submit">submit</button>
    </Form>
  )
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants