Radio
A control element that allows the user to choose only one of a predefined set of mutually exclusive options. The singular property of a radio button makes it distinct from checkboxes, where the user can select and unselect any number of items.
import {QRadioGroup, QRadio} from "@qui/react"
Accessibility
Our radio component is accessible by default.
- When you supply the label prop, it is automatically associated with the input element.
- If you omit the label, it's recommended that you provide an
aria-label
using the inputProps prop:
<QRadio inputProps={{"aria-label": "TypeScript"}} value="typescript" />
Examples
Showcase
The QRadio
component is rendered as a child of the QRadioGroup
component, which handles state and form control bindings. The QRadioGroup
component extends the fieldset element for optimal accessibility.
import {ReactNode} from "react"import {QRadio, QRadioGroup} from "@qui/react"export default function Showcase(): ReactNode {return (<form className="grid justify-center"><QRadioGroup className="flex gap-4" defaultValue="html" name="language"><QRadio label="HTML" value="html" /><QRadio label="CSS" value="css" /><QRadio label="TypeScript" value="ts" /></QRadioGroup></form>)}
Controlled State
The component can be controlled or uncontrolled.
When you supply the onChange and value props, the component is controlled
. Otherwise, the component is uncontrolled
and the value is tracked internally.
Learn more about controlled and uncontrolled components in the related React Documentation.
import {ChangeEvent, ReactNode, useState} from "react"import {QRadio, QRadioGroup} from "@qui/react"export default function Controlled(): ReactNode {const [value, setValue] = useState<string>("html")const handleChange = (event: ChangeEvent<HTMLInputElement>,value: string,) => {setValue(value)}return (<div className="grid justify-center gap-4"><div className="grid gap-2"><div className="flex gap-1"><span className="text-primary q-font-metadata-lg">Favorite Language:</span><span className="text-primary q-font-metadata-lg">{value}</span></div><QRadioGroupclassName="flex gap-4"onChange={handleChange}value={value}><QRadio label="HTML" value="html" /><QRadio label="CSS" value="css" /><QRadio label="TypeScript" value="ts" /></QRadioGroup></div></div>)}
Union Type
The type of the value emitted by the change event aligns with the type of the value property. Here’s an illustration using a union type:
import {ChangeEvent, ReactNode, useState} from "react"import {QRadio, QRadioGroup} from "@qui/react"type WebLanguage = "html" | "css" | "ts"export default function UnionType(): ReactNode {const [value, setValue] = useState<WebLanguage>("html")const handleChange = (event: ChangeEvent<HTMLInputElement>,// the change event's value type matches the type of the `value` prop.value: WebLanguage,) => {setValue(value)}return (<div className="grid justify-center gap-4"><div className="grid gap-2"><div className="flex gap-1"><span className="text-primary q-font-metadata-lg">Favorite Language:</span><span className="text-primary q-font-metadata-lg">{value}</span></div><QRadioGroupclassName="flex gap-4"onChange={handleChange}value={value}><QRadio label="HTML" value="html" /><QRadio label="CSS" value="css" /><QRadio label="TypeScript" value="ts" /></QRadioGroup></div></div>)}
API
QRadioGroup
Name & Description | Option(s) | Default |
---|---|---|
The component used for the root node. It can be a React component or
element. |
| 'fieldset' |
ReactNode | ||
The className property of the Element
interface gets and sets the value of the
class attribute
of the specified element. |
| |
Default value. |
| |
If true, the component will not be interactive and will appear dimmed. |
| false |
Specifies the id of the <form> this input belongs to. If omitted, it’s the
closest parent form. |
| |
Specifies the name for this input that’s submitted with the form. |
| |
An Event handler function. Required for controlled inputs.
Fires immediately when the input’s value is changed by the user (for
example, it fires on every keystroke). Behaves like the browser
input event |
| |
If true, the component is not editable by the user. |
| false |
| 'radiogroup' | |
The style global attribute
contains CSS styling
declarations to be applied to the element. | CSSProperties | |
Controlled value for this radio group. |
|
Type
| ElementType| ComponentType
Default
'fieldset'
Description
The component used for the root node. It can be a React component or
element.
Type
string
Description
The className property of the Element
interface gets and sets the value of the
class attribute
of the specified element.
Type
string
Description
Default value.
Type
boolean
Default
false
Description
If true, the component will not be interactive and will appear dimmed.
Type
string
Description
Specifies the id of the
<form>
this input belongs to. If omitted, it’s the
closest parent form.Type
string
Description
Specifies the name for this input that’s submitted with the form.
Type
(event: ChangeEvent<HTMLInputElement>,value: Value,) => void
Description
An Event handler function. Required for controlled inputs.
Fires immediately when the input’s value is changed by the user (for
example, it fires on every keystroke). Behaves like the browser
input event
Type
boolean
Default
false
Description
If true, the component is not editable by the user.
Type
CSSProperties
Description
The style global attribute
contains CSS styling
declarations to be applied to the element.
Type
string
Description
Controlled value for this radio group.
QRadio
Name & Description | Option(s) | Default |
---|---|---|
Unique value that identifies this radio. |
| |
The component used for the root node. It can be a React component element. |
| 'span' |
If true , React will focus the component on mount. |
| |
The className property of the Element
interface gets and sets the value of the
class attribute
of the specified element. |
| |
A boolean. Specifies the initial value for type="checkbox" and type="radio"
inputs. |
| |
If true, the component will not be interactive and will appear dimmed. |
| false |
Specifies the id of the <form> this input belongs to. If omitted, it’s the
closest parent form. |
| |
Controlled id. If omitted, a unique identifier will be automatically generated
for error, label, and hint accessibility. |
| |
attributes
applied to the input element. Use with caution. |
| {} |
Pass a ref to the input element. |
| |
Optional label describing the radio. Recommended. This property is
automatically associated with the component's input element for optimal
accessibility. | ReactNode | |
Specifies the name for this input that’s submitted with the form. |
| |
An Event handler function. Fires when the input element loses focus. |
| |
An Event handler function. Required for controlled inputs. Fires immediately
when the input’s value is changed by the user (for example, it fires on every
keystroke). Behaves like the browser input event. |
| |
An Event handler function. Fires when the input element gains focus. |
| |
If true, the component is not editable by the user. |
| false |
The style global attribute
contains CSS styling
declarations to be applied to the element. | CSSProperties |
Type
string
Description
Unique value that identifies this radio.
Type
| ElementType| ComponentType
Default
'span'
Description
The component used for the root node. It can be a React component element.
Type
boolean
Description
If
true
, React will focus the component on mount.Type
string
Description
The className property of the Element
interface gets and sets the value of the
class attribute
of the specified element.
Type
boolean
Description
A boolean. Specifies the initial value for type="checkbox" and type="radio"
inputs.
Type
boolean
Default
false
Description
If true, the component will not be interactive and will appear dimmed.
Type
string
Description
Specifies the id of the
<form>
this input belongs to. If omitted, it’s the
closest parent form.Type
string
Description
Controlled id. If omitted, a unique identifier will be automatically generated
for error, label, and hint accessibility.
Type
InputHTMLAttributes<HTMLInputElement>
Default
{}
Description
attributes
applied to the
input
element. Use with caution.Type
Ref<HTMLInputElement>
Description
Pass a ref to the input element.
Type
ReactNode
Description
Optional label describing the radio. Recommended. This property is
automatically associated with the component's input element for optimal
accessibility.
Type
string
Description
Specifies the name for this input that’s submitted with the form.
Type
FocusEventHandler<HTMLInputElement>
Description
An Event handler function. Fires when the input element loses focus.
Type
ChangeEventHandler<HTMLInputElement>
Description
An Event handler function. Required for controlled inputs. Fires immediately
when the input’s value is changed by the user (for example, it fires on every
keystroke). Behaves like the browser input event.
Type
FocusEventHandler<HTMLInputElement>
Description
An Event handler function. Fires when the input element gains focus.
Type
boolean
Default
false
Description
If true, the component is not editable by the user.
Type
CSSProperties
Description
The style global attribute
contains CSS styling
declarations to be applied to the element.