<FieldGroup>

A wrapper for wrapping dynamic fields.

It's recommended to use <FieldGroup /> to wrap any and all dynamic fields.

Field groups affect what's output from onSubmit and how validations and groupValidations listen for field updates.

import {
  Field,
  FieldGroup,
  OneForm,
} from '@oneform/react'

const FieldGroupExample = () => (
  <OneForm>
    <FieldGroup
      id="1"
      name="addressId"
    >
      <Field>
        <input name="name" />
      </Field>
    </FieldGroup>
  </OneForm>
)

export default FieldGroupExample

Props

Versatility

There's no limit to how many <FieldGroup /> wrappers you use, how deeply you nest fields.

You can even have duplicate id props!:

import {
  Field,
  FieldGroup,
  OneForm,
} from '@oneform/react'

const SharedFieldGroupIdExample = () => (
  <OneForm>
    <FieldGroup
      id="1"
      name="accountId"
    >
      <Field>
        <input name="email" />
      </Field>
      
      <Field>
        <input name="name" />
      </Field>
    </FieldGroup>
  </OneForm>
)

export default SharedFieldGroupIdExample

Deeply nesting dynamic fields

import {
  Field,
  FieldGroup,
  OneForm,
} from '@oneform/react'

const BasicDeeplyNestedFieldGroupsExample = () => (
  <OneForm>
    <FieldGroup
      id="1"
      name="addressId"
    >
      <Field>
        <input name="name" />
      </Field>

      <FieldGroup
        id="1"
        name="emailId"
      >
        <Field>
          <input name="email" />
        </Field>
      </FieldGroup>

      <FieldGroup
        id="2"
        name="emailId"
      >
        <Field>
          <input name="email" />
        </Field>
      </FieldGroup>
    </FieldGroup>
  </OneForm>
)

export default BasicDeeplyNestedFieldGroupsExample

Form submission

When submitting a form, you'll get back flattened values.

In this form:

import {
  Field,
  FieldGroup,
  OneForm,
} from '@oneform/react'
import {
  useCallback,
} from 'react'

const FieldGroupSubmissionExample = () => {
  const formSubmitted = (
    useCallback(
      ({
        registeredValues,
      }) => {
        console.log(
          registeredValues
        )
      },
      [], 
    )
  )

  return (
    <OneForm
      onSubmit={formSubmitted}
    >
      <FieldGroup
        id="1"
        name="addressId"
      >
        <Field>
          <input name="name" />
        </Field>
      </FieldGroup>
    </OneForm>
  )
}

export default FieldGroupSubmissionExample

console.log will log this object:

{
  'name/addressId:1': '',
}

Last updated