Introducing PrimeReact v11-alpha 🎉Discover Now

RadioButton

An unstyled, accessible radio button component with compound composition and group support.

Build fully custom radio button controls with complete control over layout and styling.

basic-demo

Pre-styled Versions

Choose a pre-styled library to use RadioButton with ready-made designs.
For hook-based usage without components, see the Headless API.

Features#

  • Compound component API with three sub-components: Root, Box, Indicator
  • RadioButtonGroup for managing a shared value across multiple radio buttons
  • Conditional indicator rendering with match prop

Usage#

import { RadioButton } from 'primereact/radiobutton';
import { RadioButtonGroup } from 'primereact/radiobuttongroup';
<RadioButtonGroup>
    <RadioButton.Root value="a">
        <RadioButton.Box>
            <RadioButton.Indicator />
        </RadioButton.Box>
    </RadioButton.Root>
    <RadioButton.Root value="b">
        <RadioButton.Box>
            <RadioButton.Indicator />
        </RadioButton.Box>
    </RadioButton.Root>
</RadioButtonGroup>

Behavior#

Polymorphic Rendering#

Use as on any sub-component to change the rendered HTML element.

<RadioButton.Root as="label">
    <RadioButton.Box as="span">
        <RadioButton.Indicator as="div" />
    </RadioButton.Box>
</RadioButton.Root>

Default elements: Root=div, Box=div, Indicator=span.

Render Function Children#

RadioButton.Box and RadioButton.Indicator accept a render function as children, providing access to the component instance.

<RadioButton.Box>{(instance) => <span>{instance.radiobutton?.state.checked ? '●' : '○'}</span>}</RadioButton.Box>

Pass Through#

Some parts may not be visible in the preview depending on the component's current state.

Loading...
/* Select a part to see its CSS selector for custom styling */

API#

RadioButtonRoot#

NameTypeDefault
refRef<unknown>
The reference to the component instance.
pIfbooleantrue
Whether the component should be rendered.
styleCSSProperties | ((instance?: RadioButtonRootInstance) => CSSProperties)
The style to apply to the component.
classNamestring | ((instance?: RadioButtonRootInstance) => string)
The class name to apply to the component.
asstring | number | bigint | boolean | ComponentClass<any, any> | FunctionComponent<any> | ReactElement<unknown, string | JSXElementConstructor<any>> | Iterable<ReactNode, any, any> | ReactPortal | Promise<AwaitedReactNode>
The component type to render.
asChildbooleanfalse
Whether the component should be rendered as a child component.
instanceRadioButtonRootInstance
The instance to pass to the component.
ptSafeRecord<RadioButtonRootPassThrough>
The pass-through props to pass to the component.
ptOptionsPassThroughOptions
The pass-through options to pass to the component.
unstyledboolean
Whether the component should be rendered without classes.
dtunknown
The design token to use for the component.
stylesStylesOptions<ComponentInstance>
The styles to use for the component.
render(instance: RadioButtonRootInstance) => ReactNode
The render function to render the component with instance access.
childrenany
The children to render. Accepts `React.ReactNode` for static content or a render function `(instance: I) => React.ReactNode` for instance access. Typed as `any` to avoid JSX type errors when used directly in templates.
checkedboolean
When present, it specifies the input's checked state.
defaultCheckedboolean
The default value for the input when not controlled by `checked` and `onCheckedChange` .
tabIndexnumber
Tab index for keyboard navigation.
valueunknown
Value of the radio button.
namestring
The name of the radio button.
size"small" | "large" | "normal"
Defines the size of the radio button.
variant"outlined" | "filled"
Specifies the input variant of the component.
disabledbooleanfalse
When present, it specifies that the element should be disabled.
readOnlybooleanfalse
When present, it specifies that an input field is read-only.
requiredbooleanfalse
When present, it specifies that the element is required.
invalidbooleanfalse
When present, it specifies that the component should have invalid state style.
inputIdstring
Identifier of the underlying input element.
inputStyleCSSProperties
Inline style of the input field.
inputClassNamestring
Style class of the input field.
ariaLabelstring
Establishes a string value that labels the component.
ariaLabelledbystring
Establishes relationships between the component and label(s) where its value should be one or more element IDs.
onFocus(event: FocusEvent<HTMLInputElement>) => void
Callback function that is called when the radio button is focused.
onBlur(event: FocusEvent<HTMLInputElement>) => void
Callback function that is called when the radio button loses focus.
onCheckedChange(event: RadioButtonRootChangeEvent) => void
Callback fired when the radio button's checked state changes.
[key: string]any
pt-{optionName}-*-
Pass through attributes for customizing component. For more info, see Pass Through tab.
AttributeValue
data-scope"radiobutton"
data-part"root"
data-checkedPresent when checked
data-uncheckedPresent when unchecked
data-disabledPresent when disabled

Defines passthrough(pt) options of RadioButton component.

labeltypedescription
rootRadioButtonRootPassThroughType<HTMLAttributes<HTMLDivElement>>Used to pass attributes to the root's DOM element.
inputRadioButtonRootPassThroughType<InputHTMLAttributes<HTMLInputElement>>Used to pass attributes to the input's DOM element.
boxRadioButtonRootPassThroughType<HTMLAttributes<HTMLDivElement>>Used to pass attributes to the box's DOM element.
indicatorRadioButtonRootPassThroughType<HTMLAttributes<HTMLSpanElement>>Used to pass attributes to the indicator's DOM element.

RadioButtonBox#

NameTypeDefault
refRef<unknown>
The reference to the component instance.
pIfbooleantrue
Whether the component should be rendered.
styleCSSProperties | ((instance?: RadioButtonBoxInstance) => CSSProperties)
The style to apply to the component.
classNamestring | ((instance?: RadioButtonBoxInstance) => string)
The class name to apply to the component.
asstring | number | bigint | boolean | ComponentClass<any, any> | FunctionComponent<any> | ReactElement<unknown, string | JSXElementConstructor<any>> | Iterable<ReactNode, any, any> | ReactPortal | Promise<AwaitedReactNode>
The component type to render.
asChildbooleanfalse
Whether the component should be rendered as a child component.
instanceRadioButtonBoxInstance
The instance to pass to the component.
ptSafeRecord<RadioButtonBoxPassThrough>
The pass-through props to pass to the component.
ptOptionsPassThroughOptions
The pass-through options to pass to the component.
unstyledboolean
Whether the component should be rendered without classes.
dtunknown
The design token to use for the component.
stylesStylesOptions<ComponentInstance>
The styles to use for the component.
render(instance: RadioButtonBoxInstance) => ReactNode
The render function to render the component with instance access.
childrenany
The children to render. Accepts `React.ReactNode` for static content or a render function `(instance: I) => React.ReactNode` for instance access. Typed as `any` to avoid JSX type errors when used directly in templates.
[key: string]any
pt-{optionName}-*-
Pass through attributes for customizing component. For more info, see Pass Through tab.
AttributeValue
data-scope"radiobutton"
data-part"box"
data-checkedPresent when checked
data-uncheckedPresent when unchecked
data-disabledPresent when disabled

Defines passthrough(pt) options of RadioButtonBox component.

labeltypedescription
rootRadioButtonBoxPassThroughType<HTMLAttributes<HTMLDivElement>>Used to pass attributes to the root's DOM element.

RadioButtonIndicator#

NameTypeDefault
refRef<unknown>
The reference to the component instance.
pIfbooleantrue
Whether the component should be rendered.
styleCSSProperties | ((instance?: RadioButtonIndicatorInstance) => CSSProperties)
The style to apply to the component.
classNamestring | ((instance?: RadioButtonIndicatorInstance) => string)
The class name to apply to the component.
asstring | number | bigint | boolean | ComponentClass<any, any> | FunctionComponent<any> | ReactElement<unknown, string | JSXElementConstructor<any>> | Iterable<ReactNode, any, any> | ReactPortal | Promise<AwaitedReactNode>
The component type to render.
asChildbooleanfalse
Whether the component should be rendered as a child component.
instanceRadioButtonIndicatorInstance
The instance to pass to the component.
ptSafeRecord<RadioButtonIndicatorPassThrough>
The pass-through props to pass to the component.
ptOptionsPassThroughOptions
The pass-through options to pass to the component.
unstyledboolean
Whether the component should be rendered without classes.
dtunknown
The design token to use for the component.
stylesStylesOptions<ComponentInstance>
The styles to use for the component.
render(instance: RadioButtonIndicatorInstance) => ReactNode
The render function to render the component with instance access.
childrenany
The children to render. Accepts `React.ReactNode` for static content or a render function `(instance: I) => React.ReactNode` for instance access. Typed as `any` to avoid JSX type errors when used directly in templates.
match"checked" | "unchecked"
Determines the visibility of the indicator based on the state of the radio button. Valid values are: - "checked": Indicator is visible when the radio button is checked. - "unchecked": Indicator is visible when the radio button is unchecked.
[key: string]any
pt-{optionName}-*-
Pass through attributes for customizing component. For more info, see Pass Through tab.
AttributeValue
data-scope"radiobutton"
data-part"indicator"
data-checkedPresent when checked
data-uncheckedPresent when unchecked
data-disabledPresent when disabled

Defines passthrough(pt) options of RadioButtonIndicator component.

labeltypedescription
rootRadioButtonIndicatorPassThroughType<HTMLAttributes<HTMLSpanElement>>Used to pass attributes to the root's DOM element.

RadioButtonGroup#

NameTypeDefault
refRef<unknown>
The reference to the component instance.
pIfbooleantrue
Whether the component should be rendered.
styleCSSProperties | ((instance?: RadioButtonGroupInstance) => CSSProperties)
The style to apply to the component.
classNamestring | ((instance?: RadioButtonGroupInstance) => string)
The class name to apply to the component.
asstring | number | bigint | boolean | ComponentClass<any, any> | FunctionComponent<any> | ReactElement<unknown, string | JSXElementConstructor<any>> | Iterable<ReactNode, any, any> | ReactPortal | Promise<AwaitedReactNode>
The component type to render.
asChildbooleanfalse
Whether the component should be rendered as a child component.
instanceRadioButtonGroupInstance
The instance to pass to the component.
ptSafeRecord<RadioButtonGroupPassThrough>
The pass-through props to pass to the component.
ptOptionsPassThroughOptions
The pass-through options to pass to the component.
unstyledboolean
Whether the component should be rendered without classes.
dtunknown
The design token to use for the component.
stylesStylesOptions<ComponentInstance>
The styles to use for the component.
render(instance: RadioButtonGroupInstance) => ReactNode
The render function to render the component with instance access.
childrenany
The children to render. Accepts `React.ReactNode` for static content or a render function `(instance: I) => React.ReactNode` for instance access. Typed as `any` to avoid JSX type errors when used directly in templates.
valueunknown
Value of the radio button group.
defaultValueunknown
The default value of the radio button group.
namestring
The name of the radio buttons.
disabledbooleanfalse
When present, it specifies that the radio button group should be disabled.
invalidbooleanfalse
When present, it specifies that the radio button group is invalid.
onValueChange(event: RadioButtonGroupValueChangeEvent) => void
Callback fired when the radio button group's value state changes.
[key: string]any
pt-{optionName}-*-
Pass through attributes for customizing component. For more info, see Pass Through tab.
AttributeValue
data-scope"radiobuttongroup"
data-part"root"

Defines passthrough(pt) options of RadioButtonGroup component.

labeltypedescription
rootRadioButtonGroupPassThroughType<HTMLAttributes<HTMLDivElement>>Used to pass attributes to the root's DOM element.

Accessibility#

Screen Reader#

RadioButton component uses a hidden native radio button element internally that is only visible to screen readers. Value to describe the component can either be provided via label tag combined with inputId prop or using ariaLabelledby, ariaLabel props.

<label htmlFor="rb1">One</label>
<RadioButton.Root inputId="rb1">
    <RadioButton.Box>
        <RadioButton.Indicator />
    </RadioButton.Box>
</RadioButton.Root>
 
<span id="rb2">Two</span>
<RadioButton.Root ariaLabelledby="rb2">
    <RadioButton.Box>
        <RadioButton.Indicator />
    </RadioButton.Box>
</RadioButton.Root>
 
<RadioButton.Root ariaLabel="Three">
    <RadioButton.Box>
        <RadioButton.Indicator />
    </RadioButton.Box>
</RadioButton.Root>

Keyboard Support#

KeyFunction
tabMoves focus to the selected radio button in a group, or first one.
spaceSelects the focused radio button.
arrowIn a named radio group, moves focus between options and updates choice.