Form

Groups fields into sections, lays them out, and checks them.

import { Form, FormSection, FormRow, FormActions } from '@halo-ds/react';

When to use it

Use a Form for anything people fill in and submit. Group fields into sections with a title, put short pairs side by side, and end with the buttons. In code, Form checks required fields and your own rules, and shows the errors on the right fields.

Do

  • Group related fields into sections with a short title.
  • Put short pairs side by side, like first and last name.
  • Show errors after submit, then clear them as people fix them.
  • Put the main button last, and keep Cancel next to it.

Don't

  • Disable the submit button; let people submit and see what to fix.
  • Use placeholders as labels; they disappear as people type.
  • Put more than two or three fields in a row.
  • Split one question across several fields when one will do.

Props

Form props

PropTypeWhat it does
onSubmit`((values: FormValues, event: FormEvent<HTMLFormElement>) => void \Promise<void>) \undefined`Called with the values when every check passes. Return a promise to keep aria-busy on while it runs.
validate`((values: FormValues) => FormErrors \undefined) \undefined`Your own checks. Return an error message for each field name that has a problem.
errors`FormErrors \undefined`Errors from elsewhere, like the server. Shown next to the matching fields.
requiredMessage`((label: string) => ReactNode) \undefined`The message for an empty required field. Defaults to "{Label} is required."
layout`"vertical" \"horizontal" \undefined`Labels above fields, or beside them. Figma: Layout.
labelWidth`string \number \undefined`Width of the label column in the horizontal layout. Default 200px.
classNames`Partial<Record<"root", string>> \undefined`Extra class names for the parts inside the component, by part name, like { header: 'my-header' }.
styles`Partial<Record<"root", CSSProperties>> \undefined`Inline styles for the parts inside the component, by part name, like { footer: { gap: 4 } }.

It also takes the usual HTML attributes for its element.

FormSection props

PropTypeWhat it does
titleReactNodeFigma: Title.
descriptionReactNodeFigma: Description.

It also takes the usual HTML attributes for its element.

FormRow props

PropTypeWhat it does
columns`2 \3 \4 \undefined`Fields side by side. They stack on screens under 600px. Default 2.

It also takes the usual HTML attributes for its element.

FormActions props

PropTypeWhat it does
align`"end" \"start" \"between" \undefined`Where the buttons sit. Put the main button last. Default end.
divider`boolean \undefined`A line above the buttons.

It also takes the usual HTML attributes for its element.

Examples

Overview

import { useState } from 'react';
import { Button, Form, FormActions, FormRow, FormSection, InputField, SelectField } from '@halo-ds/react';

export default function Example() {
  const [saved, setSaved] = useState('');
  return (
    <div style={{ width: '100%', maxWidth: 480 }}>
      <Form onSubmit={(values) => setSaved(`Saved ${values.firstName}`)}>
        <FormSection title="Your details" description="Submit it empty to see the checks.">
          <FormRow>
            <InputField name="firstName" label="First name" placeholder="John" required />
            <InputField name="lastName" label="Last name" placeholder="Doe" required />
          </FormRow>
          <SelectField
            name="team"
            label="Team"
            required
            placeholder="Choose a team"
            options={[
              { value: 'design', label: 'Design' },
              { value: 'engineering', label: 'Engineering' },
            ]}
          />
        </FormSection>
        <FormActions>
          <Button type="submit">Save</Button>
        </FormActions>
      </Form>
      {saved && <p style={{ marginTop: 16 }}>{saved}</p>}
    </div>
  );
}

Horizontal layout

Labels beside fields, common in settings. They move above the fields on small screens.

import { Button, Form, FormActions, InputField, SelectField } from '@halo-ds/react';

export default function Demo() {
  return (
    <div style={{ width: '100%', maxWidth: 560 }}>
      <Form layout="horizontal" labelWidth={160} onSubmit={() => {}}>
        <InputField name="workspace" label="Workspace name" placeholder="Acme Inc." required defaultValue="Northwind" />
        <InputField name="url" label="Workspace URL" placeholder="acme" hint="Letters, numbers and dashes." defaultValue="northwind" />
        <SelectField name="region" label="Data region" options={[{ value: 'eu', label: 'Europe' }, { value: 'us', label: 'United States' }]} defaultValue="eu" />
        <FormActions>
          <Button type="submit">Save</Button>
        </FormActions>
      </Form>
    </div>
  );
}

More: https://halodesignsystem.dev/components/form