Skip to content

Radio

Radios allow users to select a single option from a list.

React

released

Vue

released

Elements

unsupported

Android

released

iOS

unsupported

Example

Usage

Design Guidelines

See Figma: Warp - Components / Radio Button

Questions?

Feel free to ask any questions on usage in the Warp DS Slack channel: #nmp-warp-design-system

Frameworks

Import

The toggle component allows you to render radios. All you have to do is specify type="radio" for the Toggle and let the component handle the rest. Toggle is built to handle both single and multiple options.

You must keep track of state yourself. The state has to be handled differently depending on whether you want to use single or multiple options. See examples below.

You can import the component like so:

js
import { Toggle } from '@warp-ds/react';

or import it individually to optimize your JS bundle size by adding only the components you need:

js
import { Toggle } from '@warp-ds/react/components/toggle';

Syntax

Radio with multiple options

ts
type ToggleEntry = {
  label: string;
  value: unknown;
};
jsx
function Example() {
  const options = [
    { label: 'Red', value: 'red' },
    { label: 'Blue', value: 'blue' },
    { label: 'Green', value: 'green' },
  ];

  return (
    <Toggle
      type="radio"
      title="Favorite color"
      helpText="Choose your absolute favorite color"
      options={options}
      onChange={(selected) => console.log(selected)}
    />
  );
}

Note the helpText property. This gives you the option to further explain the purpose of the toggle and the action to take.

Props

Optional Props

nametypedefaultnotes

type

"radio"
|"checkbox"
|"radio-button"

The type of Toggle

title

string

The option(s) title

invalid

boolean

Renders the field in an invalid state. Often paired together with helpText to provide feedback about the error.

helpText

string

Help text beneath options

options

ToggleEntry[]

An array of options to render

selected

ToggleEntry[]

An array of options to be selected / checked (controlled)

defaultSelected

ToggleEntry[]

An array of options to be selected / checked on mount (uncontrolled)

label

string

If you only need to render a single option, use this prop instead

noVisibleLabel

boolean

Whether label should be invisible

checked

boolean

Whether the single option should be checked (controlled)

defaultChecked

boolean

Whether the single option should be checked on mount (uncontrolled)

indeterminate

boolean

Whether a single option is indeterminate, or "partially checked." The checkbox will appear with a small dash instead of a tick to indicate that the option is not exactly checked or unchecked.

optional

boolean

Whether the toggle is optional Appends (optional) to the end of the title for indication

className

string

Custom classes applied to the wrapping container

equalWidth

boolean

Will make each option equal width, only applied when "radio-button" type is set

small

boolean

Whether the elements should be small, only applied when "radio-button" type is set

Validation

Validation is as easy as passing the property invalid with a helpText to further explain the error. helpText can also be used as an assistance label before an error occurs as long as invalid is set to false.

jsx
function Example() {
  const [selected, setSelected] = React.useState([
    { label: 'Blue', value: 'blue' },
  ]);

  return (
    <Toggle
      type="checkbox"
      title="Favorite colors"
      helpText="Blue surely isn't amongst your favorites?"
      invalid
      selected={selected}
      options={[
        { label: 'Red', value: 'red' },
        { label: 'Blue', value: 'blue' },
        { label: 'Green', value: 'green' },
      ]}
    />
  );
}

In the next example, we'll take a closer look at one way to conditionally render error and help messages. You should probably take this a step further and implement your own error handler, but for the sake of the demo we've kept it quite simple.

jsx
function Example() {
  const [selected, setSelected] = React.useState([
    { label: 'Blue', value: 'blue' },
  ]);

  const handleSelect = (entry) => {
    if (selected.some((e) => e.value === entry.value)) {
      return setSelected(selected.filter((s) => s.value !== entry.value));
    }

    return setSelected([...selected, entry]);
  };

  return (
    <Toggle
      type="checkbox"
      title="Favorite colors"
      helpText={
        selected.some((e) => e.value === 'blue')
          ? "Blue surely isn't amongst your favorites?"
          : 'Choose your favorite colors'
      }
      invalid={!!selected.some((e) => e.value === 'blue')}
      selected={selected}
      onChange={handleSelect}
      options={[
        { label: 'Red', value: 'red' },
        { label: 'Blue', value: 'blue' },
        { label: 'Green', value: 'green' },
      ]}
    />
  );
}