Skip to content

CheckboxField

CheckboxField allows users to select one or more options from a set.

Basic Usage

To implement the CheckboxField component, you need to import it first:

import { CheckboxField } from '@react-ui-org/react-ui';

And use it:

See API for all available options.

General Guidelines

  • Use the CheckboxField when there are lists of options and the user may select any number of choices, including zero, one, or several. For selecting just a single option from the list, use either the Radio or SelectField component.

  • Use the CheckboxField for boolean (true/false) input in forms that require to be confirmed by a button to become active. To toggle things on or off with an immediate effect (without confirmation), use rather the Toggle component.

  • Use positive wording for the checkbox label, so that it's clear what will happen when the user turns on the checkbox. Avoid negations such as “Don't send me any emails” which would mean that the user needs to turn the checkbox on in order for something not to happen.

  • Use text labels unless it is necessary to wrap text label into Popover-like to component to provide additional info about the field.

  • Only make the CheckboxField's label invisible when there is another visual clue to guide users through toggling the input.

  • When a short label is not enough, use help texts to guide users before they enter anything.

  • Use clear, calm error messages when there's a problem with what they entered.

  • Visually present groups of choices as groups, and clearly separate them from other groups on the same page.

📖 Read more about checkboxes and radios at Nielsen Norman Group.

Help Text

You may provide an additional help text to clarify what will be the impact of turning the checkbox on or off.

Label Options

In some cases, it may be convenient to visually hide the checkbox label. The label remains accessible to assistive technologies.

It's also possible to display label before input:

States

Validation States

Validation states visually present the result of validation of the input. You should always provide validation message for states other than valid so users know what happened and what action they should take or what options they have.

Required State

The required state indicates that the input is mandatory. Required fields display an asterisk * after the label by default.

Styling the Required State

All form fields in React UI can be styled to indicate the required state.

However, you may find yourself in a situation where a form field is valid in both checked and unchecked states, for example to turn on or off a feature. If your project uses the label color as the primary means to indicate the required state of input fields and the usual asterisk * is omitted, you may want to keep the label color consistent for both states to avoid confusion.

For this edge case, there is the renderAsRequired prop:

It renders the field as if it was required, but doesn't add the required attribute to the actual input.

Disabled State

Disabled state makes the input unavailable.

Forwarding HTML Attributes

In addition to the options below in the component's API section, you can specify any HTML attribute you like. All attributes that don't interfere with the API of the React component and that aren't filtered out by transferProps helper are forwarded to the <input> HTML element. This enables making the component interactive and helps to improve its accessibility.

👉 For the full list of supported attributes refer to:

Forwarding ref

If you provide ref, it is forwarded to the native HTML <input> element.

API

Prop Type Default Description
disabled bool
false

If true, the input will be disabled.

helpText node
null

Optional help text.

id string
undefined

ID of the <input> HTML element.

Also serves as base for ids of nested elements:

  • <ID>__label
  • <ID>__labelText
  • <ID>__helpText
  • <ID>__validationText
isLabelVisible bool
true

If false, the label will be visually hidden (but remains accessible by assistive technologies).

label* node

Checkbox field label.

labelPosition enum
  • 'before'
  • 'after'
'after'

Placement of the label relative to the input.

renderAsRequired bool
false

If true, the input will be rendered as if it was required.

required bool
false

If true, the input will be made and rendered as required, regardless of the renderAsRequired prop.

validationState enum
  • 'invalid'
  • 'valid'
  • 'warning'
null

Alter the field to provide feedback based on validation result.

validationText node
null

Validation message to be displayed.

Theming

Head to Forms Theming to see shared form theming options. On top of that, the following options are available for CheckboxField.

Custom Property Description
--rui-FormField--check__input--checkbox__border-radius Input corner radius
--rui-FormField--check__input--checkbox--checked__background-image Background image of checked input