Skip to content
Source
Sign inSign up
TanStack/form
Open History to see the last commit

FieldGroupArrayFieldComponent.md

78 lines2.0 KB

Defined in: packages/react-form/src/FieldGroup/FieldGroupApi.public.ts:85

Type Parameters

TFieldData

TFieldData

TFieldComponents

TFieldComponents extends ReactComponentTree

FieldGroupArrayFieldComponent<TFieldName>(props): ReactNode;

Defined in: packages/react-form/src/FieldGroup/FieldGroupApi.public.ts:129

Renders an array field from this field group.

The field name is automatically connected to the form section where the field group is used.

Type Parameters

TFieldName

TFieldName extends never

Parameters

props

ReactFormFieldProps<TFieldData, TFieldName, DeepValue<TFieldData, TFieldName>, FieldValidators<TFieldData, TFieldName, DeepValue<TFieldData, TFieldName>>, ValidationIssue, unknown, FormErrorTypes<ValidationIssue, ValidationIssue>, TFieldComponents>

Returns

ReactNode

Example

const contactFieldGroup = defineFieldGroup(({ strict }) => ({
  emails: strict<Array<{ value: string }>>(),
}))

interface ContactFieldsProps {
  fields: typeof contactFieldGroup.fields
}

function ContactFields({ fields }: ContactFieldsProps) {
  return (
    <fields.ArrayField name="emails">
      {(emails) => (
        <>
          {emails.value.map((_, index) => (
            <fields.Field key={index} name={`emails[${index}].value`}>
              {(field) => (
                <input
                  value={field.value}
                  onChange={(event) =>
                    field.handleChange(event.target.value)
                  }
                />
              )}
            </fields.Field>
          ))}
        </>
      )}
    </fields.ArrayField>
  )
}