Skip to content

Form

Forms are used to capture user input.

Usage

Form and its child components FormFields and FormActions are designed to make creating forms simple and consistent. Using the these components has a number of benefits:

  • Ensure the HTML form component is used and has the novalidate prop set by default
  • Standardise the spacing between form fields, notifications, and CTAs
  • Ensure that error summaries and notifications are consistently and conveniently used
  • Ensure error notifications receive keyboard focus when they appear
Edit live code

FormFields

FormFields should only be used as a direct child of the Form component.

The FormFields component manages the vertical spacing of their direct children using the Stack component.

Avoid placing anything other than fields and actions in this component. In cases where it is essential that other content is added it is recommend to ensure that it is wrapped in a <div>.

Do:

Edit live code

Don't:

Edit live code

FormActions

FormActions should only be used as a direct child of the Form component and should always appear at the bottom of the form.

The FormActions component manages the horizontal spacing of their direct children using the Cluster component. It also manages the vertical spacing from the FormFields using the Margin component.

Edit live code

Errors

The form component makes it easy to consistently show errors related to forms, ensuring that they are located in the correct place at the top of the form and automatically receive keyboard focus when they appear.

Error summary

Error summary should be used when there are issues with the user input, related to specific fields. The summary will list all issues and link to the relevant field.

The error summary will automatically receive keyboard focus when it appears.

Note that when used within the Form component, Nebula field components do not need to define the error prop - it will be automatically set using Context, using the name prop for each field.

Edit live code

General Error

Sometimes things go wrong that aren't related to a specific field, for example and API timing out. These errors should not use the error summary but instead show a general error that explains what has happened and what the user should do.

Like error summaries, these notifications will automatically receive keyboard focus when they appear.

Edit live code

Using the form context

Nebula components will automatically check for the Form Context when used within the Form component to check for any errors, but if you want this automation for you own reusable components you will need to check this yourself.

If using Nebula's Field sub-component (see documentation) you may find this already works:

If you are not using the Field component you will need to add the check yourself. For example:

Properties

Form

NameValuesDefault

errors

{id: string; message: ReactNode;}[]

generalError

{ title?: ReactNode; message?: ReactNode };

...

JSX.IntrinsicElements["form"]

FormFields

NameValuesDefault

...

JSX.IntrinsicElements["div"]

FormActions

NameValuesDefault

...

JSX.IntrinsicElements["div"]