Sajari UI

Radio

Radio buttons are used when only one choice may be selected in a series of options.

Import

Usage

Basic usage of radio button.

Editable Example

Disabled

Editable Example

Horizontal alignment

Editable Example

Invalid

Editable Example

Custom Radio Buttons

In some cases, you might need to create components that work like radios but don't look like radios. Chakra exports a RadioButtonGroup to help with this scenario. Here's what you need to do:

  1. Create a component that accepts the checked and disabled props. Be sure to forwardRef to the component because it's used for keyboard accessibility.
  2. Add the component as children of RadioButtonGroup and pass a value prop to it.
  3. If you pass disabled to any of it's children, it'll be skipped in the keyboard navigation.
Editable Example

Passing a TypeScript generic type to custom radio buttons

If you want the value of a radio button to be a custom type other than string, number or string array, you can use a generic type parameter.

In the following example, the radio buttons allow the user to filter between two charge cycles, which are objects.

It is important to use the same reference to the object in the value prop. If you create new objects on the fly then the component cannot determine which radio button is checked.

Props

Radio Props

NameTypeDefaultDescription
namestringThe name of the input field in a radio (Useful for form submission)
valuestring or numberThe value to be used in the radio input. This is the value that will be returned on form submission
checkedbooleanIf true, the radio will be checked. You'll need to pass onChange to update it's value (since it's now controlled)
defaultCheckedbooleanIf true, the radio will be initially checked
disabledbooleanIf true, the radio will be disabled
invalidbooleanIf true, the radio is marked as invalid. Changes style of unchecked state
childrenReact.ReactNodeThe children of the radio
onChangefunctionFunction called when the state of the radio changes
onBlurfunctionFunction called when you blur out of the radio
onFocusfunctionFunction called when the radio receive focus
aria-labelstringAn accessible label for the radio in event there's no visible label or children was passed
aria-labelledbystringId that points to the label for the radio in event no children was passed

RadioGroup & RadioButtonGroup Props

NameTypeDefaultDescription
idstringThe id of the checkbox group.
namestringThe name of the input field in a radio (Useful for form submission)
defaultValuestringThe initial value of the radio group
valueRadioProps['value']The value of the radio group
onChangefunctionThe callback fired when any children Radio is checked
spacingFlexProps['space']The space between each checkbox
inlinebooleanIf true, the checkboxes will aligned horizontally.

RadioButtonGroup composes the ButtonGroup component.