Reusable fields

Field components that work in any form and with use-field

Usage

A field component that accepts UseFieldReturnType can be used in any form. Pass it form.getField('path') from use-form or the return value of useField:

import { TextInput } from '@mantine/core';
import { useField, useForm, UseFieldReturnType } from '@mantine/form';

function EmailField({ field }: { field: UseFieldReturnType<string> }) {
  return <TextInput label="Email" {...field.getInputProps()} />;
}

function Demo() {
  const signup = useForm({ initialValues: { email: '', password: '' } });
  const checkout = useForm({ initialValues: { contact: { email: '' } } });
  const newsletter = useField({ initialValue: '' });

  return (
    <>
      <EmailField field={signup.getField('email')} />
      <EmailField field={checkout.getField('contact.email')} />
      <EmailField field={newsletter} />
    </>
  );
}
Open reusable fields example

Field object

form.getField('path') returns the same object as use-field. Its methods read and change the field without knowing which form renders it:

import { TextInput } from '@mantine/core';
import { UseFieldReturnType } from '@mantine/form';

function EmailField({ field }: { field: UseFieldReturnType<string> }) {
  const inputProps = field.getInputProps();

  const handleBlur = () => {
    inputProps.onBlur?.();
    field.setValue(field.getValue().trim().toLowerCase());
  };

  return <TextInput label="Email" {...inputProps} onBlur={handleBlur} />;
}

Type checking

The path passed to form.getField must exist in form values, and its value type must match the type of the field component:

const form = useForm({
  initialValues: { email: '', age: 0 },
});

<EmailField field={form.getField('email')} />;

// Type error: age is a number
<EmailField field={form.getField('age')} />;

// Type error: emial is not a path of form values
<EmailField field={form.getField('emial')} />;

Validation

Validation rules are defined on useForm or useField, the field component only renders the input. Export the rule next to the component to reuse it:

import { isEmail, useField, useForm } from '@mantine/form';

export const emailRule = isEmail('Invalid email');

function Demo() {
  const signup = useForm({
    initialValues: { email: '' },
    validate: { email: emailRule },
  });

  const newsletter = useField({ initialValue: '', validate: emailRule });
}

Checkbox fields

For checkbox fields, type the component with UseFieldReturnType<boolean, 'checkbox'> and pass { type: 'checkbox' } to form.getField or useField:

import { Checkbox } from '@mantine/core';
import { useField, useForm, UseFieldReturnType } from '@mantine/form';

function TermsField({ field }: { field: UseFieldReturnType<boolean, 'checkbox'> }) {
  return <Checkbox label="I accept terms of service" {...field.getInputProps()} />;
}

function Demo() {
  const form = useForm({ initialValues: { terms: false } });
  const terms = useField({ initialValue: false, type: 'checkbox' });

  return (
    <>
      <TermsField field={form.getField('terms', { type: 'checkbox' })} />
      <TermsField field={terms} />
    </>
  );
}

Groups of fields

A component that renders several fields accepts the form object. UseFormReturnType<{ email: string }> accepts any form with a top-level email string. To render fields under a path that differs between forms, type the path with FormPath:

import { TextInput } from '@mantine/core';
import { FormPath, UseFormReturnType } from '@mantine/form';

interface Address {
  street: string;
  city: string;
}

interface AddressFieldsProps<Values> {
  form: UseFormReturnType<Values>;
  path: FormPath<Values, Address>;
}

function AddressFields<Values>({ form, path }: AddressFieldsProps<Values>) {
  return (
    <>
      <TextInput label="Street" {...form.getInputProps(`${path}.street`)} />
      <TextInput label="City" {...form.getInputProps(`${path}.city`)} />
    </>
  );
}

// <AddressFields form={form} path="shipping" />
// <AddressFields form={form} path="billing" />