Radio group

Radio Group allow users to select a single option from a list of mutually exclusive options.


Installation

nextui add radio
No need to install this package if @nextui-org/react is already installed globally.

Import

Usage

Disabled

Default Value

With Description

Horizontal

Controlled

You can use the value and onValueChange properties to control the radio input value.

Note: NextUI Radio also supports native events like onChange, useful for form libraries such as Formik and React Hook Form.

Invalid

Slots

  • RadioGroup Slots

    • base: Radio group root wrapper, it wraps the label and the wrapper.
    • wrapper: Radio group wrapper, it wraps all Radios.
    • label: Radio group label, it is placed before the wrapper.
    • description: Description slot for the radio group.
    • errorMessage: Error message slot for the radio group.
  • Radio Slots

    • base: Radio root wrapper, it wraps all elements.
    • wrapper: Radio wrapper, it wraps the control element.
    • labelWrapper: Label and description wrapper.
    • label: Label slot for the radio.
    • control: Control element, it is the circle element.
    • description: Description slot for the radio.

Custom Styles

You can customize the RadioGroup and Radio component by passing custom Tailwind CSS classes to the component slots.

Custom Implementation

In case you need to customize the radio group even further, you can use the useRadio hook to create your own implementation.

Data Attributes

  • RadioGroup has the following attributes on the base element:

    • data-orientation: The orientation of the radio group. Based on orientation prop.
  • Radio has the following attributes on the base element:

    • data-selected: When the radio is checked. Based on isSelected prop.
    • data-pressed: When the radio is pressed. Based on usePress.
    • data-invalid: When the radio is invalid. Based on validationState prop.
    • data-readonly: When the radio is readonly. Based on isReadOnly prop.
    • data-hover-unselected: When the radio is being hovered and unchecked. Based on useHover.
    • data-hover: When the radio is being hovered. Based on useHover.
    • data-focus: When the radio is being focused. Based on useFocusRing.
    • data-focus-visible: When the radio is being focused with the keyboard. Based on useFocusRing.
    • data-disabled: When the radio is disabled. Based on isDisabled prop.

Accessibility

  • Radio groups are exposed to assistive technology via ARIA.
  • Each radio is built with a native HTML <input> element, which can be optionally visually hidden to allow custom styling.
  • Full support for browser features like form autofill.
  • Keyboard event support for arrows keys.
  • Keyboard focus management and cross browser normalization.
  • Group and radio labeling support for assistive technology.

API

RadioGroup Props

AttributeTypeDescriptionDefault
childrenReactNode | ReactNode[]The list of radio elements.-
labelReactNodeThe label of the radio group.-
sizesm | md | lgThe size of the radios.md
colordefault | primary | secondary | success | warning | dangerThe color of the radios.primary
orientationhorizontal | verticalThe orientation of the radio group.vertical
namestringThe name of the RadioGroup, used when submitting an HTML form. See MDN.-
valuestring[]The current selected value. (controlled).-
defaultValuestring[]The default selected value. (uncontrolled).-
descriptionReactNodeRadio group description .-
errorMessageReactNode | ((v: ValidationResult) => ReactNode)Radio group error message.-
validate(value: string) => ValidationError | true | null | undefinedValidate input values when committing (e.g. on blur), and return error messages for invalid values.-
isDisabledbooleanWhether the radio group is disabled.false
isRequiredbooleanWhether user checkboxes are required on the input before form submission.false
isReadOnlybooleanWhether the checkboxes can be selected but not changed by the user.-
isInvalidbooleanWhether the radio group is invalid.false
validationStatevalid | invalidWhether the inputs should display its "valid" or "invalid" visual styling. (Deprecated) use isInvalid instead.false
disableAnimationbooleanWhether the animation should be disabled.false
classNamesRecord<"base"| "wrapper"| "label", string>Allows to set custom class names for the radio group slots.-

RadioGroup Events

AttributeTypeDescription
onChangeReact.ChangeEvent<HTMLInputElement>Handler that is called when the element's value changes. You can pull out the new value by accessing event.target.value (string).
onValueChange((value: string) => void)Handler that is called when the value changes.

Radio Props

AttributeTypeDescriptionDefault
childrenReactNodeThe label of the radio.-
labelReactNodeThe label of the radio.-
sizesm | md | lgThe size of the radio.md
colordefault | primary | secondary | success | warning | dangerThe color of the radio.primary
descriptionReactNodeA description for the field. Provides a hint such as specific requirements for what to choose.-
isDisabledbooleanWhether the radio is disabled.false
isRequiredbooleanWhether user checkboxes are required on the input before form submission.false
isReadOnlybooleanWhether the checkboxes can be selected but not changed by the user.-
isInvalidbooleanWhether the radio is invalid. This is based on the radio group validationState prop.false
disableAnimationbooleanWhether the animation should be disabled.false
classNamesRecord<"base"| "wrapper"| "labelWrapper" | "label" | "control" | "description", string>Allows to set custom class names for the radio slots.-