Command Palette

Search for a command to run...

Components · Core component

Radio

Radio allows the user to choose one option from a list of exclusive choices, guaranteeing a single selection for related options. Radio component must be wrapped by either Radio.Group or Radio.GroupProvider. Radio.Group provides styles such as hint text and layout, while Radio.GrouProvider offers greater flexibility without any styling.

Usage

TSX
import { Radio } from '@console/nimbus-ui/core';

Features

This radio group component simplifies HTML radio group creation with the useRadioGroup and useRadio hooks, eliminating the complexity of styling issues associated with the <fieldset> and <input> elements. The radio and radio group have the following features:

  • Radios use native HTML for seamless assistive tech integration with ARIA attributes.
  • Compatible with form autofill, validation, and consistent behavior across browsers.
  • Easily customize appearance by hiding the HTML <input> for full styling control.
  • Supports keyboard navigation, horizontal/vertical orientation, and controlled/uncontrolled usage.
  • Adheres to WAI-ARIA, maintaining focus management with roving tabindex for compliance.

Examples

Disabled

The disabled prop can be used to render an error state on either the Radio.Group or the individual Radio buttons. If a single radio is disabled, it will be taken out of the focus order flow.

Invalid

The invalid prop can be used to render an error state on the Radio.Group.

Custom Label

The label prop lets you customize labels dynamically using a render prop. By supplying a function to label, you can create labels with special features like tooltips and icons. The function receives labelProps and required values for further customization. In the example below, the Radio.Group component showcases how to use the label render prop to generate dynamic labels for different input choices.

Required

The required prop can be used to change the form element into a required form element. This has two benefits, firstly it creates a visual cue as a red * and it natively changes the froms behaviour by setting required as an aria-label.

Default Value

Use the defaultValue prop to create select a default radio. This prop takes a value as a string.

With Hint

The hint prop can be used to create a prop for the description of the input on either the Radio.Group or the individual Radio buttons. This props sets the aria-describedby attribute to a random generated id when used for Radio.Group. The hint appears below the input.

Orientation

The oritentation prop can be used to set the display of the radios in a radio group. This props sets the aria-orientation attribute as well. By default it's set to [aria-orientation="vertical"].

Rich Content

Individual radios allow you to set Rich content as they accept any valid JSX element. You can create custom hints this way for the individual radio that also enable clicks. If you want to go even further you can create titles, cards etc using radio groups.

Controlled

The Radio.Group can be controlled to use a custom state. This example shows controlling the selected item with the value and onChange props.

Group Provider

The Radio.GroupProvider provides state information to child components. It can be used when you don't want to propagate Radio.Group styles (e.g: wrapping a Table element).

API

Radio.Group

Root component that takes Radio and any valid JSX as children, controls the group state and provides styles.

JSX
<Radio.Group>{/* Any valid JSX can go here*/}</Radio.Group>

RadioGroupProvider

PropTypeDefaultDescription
disabledboolean—Whether the input is disabled.
invalidboolean—Whether the input value is invalid.
readonlyboolean—Whether the input can be selected but not changed by the user.
requiredboolean—Whether user input is required on the input before form submission.

Radio

Radio component representing a single radio button.

PropTypeDefaultDescription
classes{ radio?: string; indicator?: string; label?: string; hint?: string; labelContainer?: string; }—Custom class names to add to underlying DOM elements for styling. @selector `radio` controls Radio Root element @selector `indicator` controls circle indicator element; @selector `label` controls label element; @selector `hint` controls hint `<span>` element; @selector `labelContainer` controls label container `<div>` element;
data-testidstring—Data attribute for E2E testing purposes.
disabledboolean—Whether the input is disabled.
hintstring—

Radio.GroupProvider

PropTypeDefaultDescription
disabledboolean—Whether the input is disabled.
invalidboolean—Whether the input value is invalid.
readonlyboolean—Whether the input can be selected but not changed by the user.
requiredboolean—Whether user input is required on the input before form submission.

Radio.Group

PropTypeDefaultDescription
classes{ root?: string; group?: string; label?: string; hint?: string; }—Custom class names to add to underlying DOM elements for styling. @selector `root` controls `Radio.Group` root element @selector `group` controls `div` surrounding the radios element @selector `label` controls label element; @selector `hint` controls hint `<span>` element;
data-testidstring—Data attribute for E2E testing purposes.
disabledboolean—Whether the input is disabled.
hintReactNode—
invalidboolean—Whether the input value is invalid.
labelReactNode | RenderLabel—The content to display as the label. Optional prop for customizing labels. Can be a React node or a function that receives `labelProps` and an optional `required` boolean to dynamically generate label content.
orientationenum'vertical'The axis the Radio Button(s) should align with.
readonlyboolean—Whether the input can be selected but not changed by the user.
requiredboolean—Whether user input is required on the input before form submission.

RadioGroupStateProvider

Provides state to Radio components.

PropTypeDefaultDescription
disabledboolean—Whether the input is disabled.
invalidboolean—Whether the input value is invalid.
readonlyboolean—Whether the input can be selected but not changed by the user.
requiredboolean—Whether user input is required on the input before form submission.

Radio.GroupProvider

Root component to Provide state to Radio components without any styling.

JSX
<Radio.GroupProvider>{/* Any valid JSX can go here*/}</Radio.GroupProvider>

RadioGroupProvider

PropTypeDefaultDescription
disabledboolean—Whether the input is disabled.
invalidboolean—Whether the input value is invalid.
readonlyboolean—Whether the input can be selected but not changed by the user.
requiredboolean—Whether user input is required on the input before form submission.

Radio

Radio component representing a single radio button.

PropTypeDefaultDescription
classes{ radio?: string; indicator?: string; label?: string; hint?: string; labelContainer?: string; }—Custom class names to add to underlying DOM elements for styling. @selector `radio` controls Radio Root element @selector `indicator` controls circle indicator element; @selector `label` controls label element; @selector `hint` controls hint `<span>` element; @selector `labelContainer` controls label container `<div>` element;
data-testidstring—Data attribute for E2E testing purposes.
disabledboolean—Whether the input is disabled.
hintstring—

Radio.GroupProvider

PropTypeDefaultDescription
disabledboolean—Whether the input is disabled.
invalidboolean—Whether the input value is invalid.
readonlyboolean—Whether the input can be selected but not changed by the user.
requiredboolean—Whether user input is required on the input before form submission.

Radio.Group

PropTypeDefaultDescription
classes{ root?: string; group?: string; label?: string; hint?: string; }—Custom class names to add to underlying DOM elements for styling. @selector `root` controls `Radio.Group` root element @selector `group` controls `div` surrounding the radios element @selector `label` controls label element; @selector `hint` controls hint `<span>` element;
data-testidstring—Data attribute for E2E testing purposes.
disabledboolean—Whether the input is disabled.
hintReactNode—
invalidboolean—Whether the input value is invalid.
labelReactNode | RenderLabel—The content to display as the label. Optional prop for customizing labels. Can be a React node or a function that receives `labelProps` and an optional `required` boolean to dynamically generate label content.
orientationenum'vertical'The axis the Radio Button(s) should align with.
readonlyboolean—Whether the input can be selected but not changed by the user.
requiredboolean—Whether user input is required on the input before form submission.

RadioGroupStateProvider

Provides state to Radio components.

PropTypeDefaultDescription
disabledboolean—Whether the input is disabled.
invalidboolean—Whether the input value is invalid.
readonlyboolean—Whether the input can be selected but not changed by the user.
requiredboolean—Whether user input is required on the input before form submission.