Radio
Boxes for checking and unchecking single values in forms
Basic radio button
The isChecked
prop determines when this controlled component is checked.
function RadioExample() { const [isChecked, setIsChecked] = React.useState(undefined); return ( <Radio isChecked={isChecked} name="example-basic" onChange={v => setIsChecked(v)}> Long Distance Moving </Radio> ); }
Multiple radio buttons
This an example of how to use many Radio
components at once.
Here are a few points to note:
- Each
Radio
receives anid
. Thisid
is then passed as the second argument toonChange
. - The selected radio button is stored in the
selectedId
variable fromReact.useState()
. isChecked
performs a comparison to see if theid
matches the value ofselectedId
.
function RadioExample() { const [selectedId, setSelectedId] = React.useState('long-distance'); return ( <div> <Radio id="long-distance" isChecked={selectedId === 'long-distance'} name="example-full" labelPadding="4px 0" onChange={(isChecked, id) => setSelectedId(id)} > Long Distance Moving </Radio> <Radio id="furniture-moving" isChecked={selectedId === 'furniture-moving'} name="example-full" labelPadding="4px 0" onChange={(isChecked, id) => setSelectedId(id)} > Furniture Moving and Heavy Lifting </Radio> <Radio id="pool-table" isChecked={selectedId === 'pool-table'} name="example-full" labelPadding="4px 0" onChange={(isChecked, id) => setSelectedId(id)} > Pool Table Moving </Radio> </div> ); }
Disabled radio buttons
The isDisabled
prop visually and functionally disabled the radio button. It also visually disables the related label.
<React.Fragment> <Radio isDisabled isChecked name="example-disabled" onChange={() => {}} labelPadding="4px 0"> Long Distance Moving </Radio> <Radio isDisabled isChecked={false} name="example-disabled" onChange={() => {}} labelPadding="4px 0" > Furniture Moving and Heavy Lifting </Radio> <Radio isDisabled isChecked={false} name="example-disabled" onChange={() => {}} labelPadding="4px 0" > Pool Table Moving </Radio> </React.Fragment>
Radio button with an error
The hasError
prop can be used to visually represent an error.
This prop only changes the radio button’s color. It should be used alongside an error message that helps users advance through the form.
function RadioExample() { const [isChecked, setIsChecked] = React.useState(undefined); return ( <Radio isChecked={isChecked} hasError name="example-error" onChange={v => setIsChecked(v)}> Long Distance Moving </Radio> ); }
Multi-column content
It’s possible to provide complex UIs to the children
prop. Clicking on the content will select the related radio button.
This example puts the label content within children
. It’s also possible to not provide children
as undefined
and use a custom label
instead. In that case, you must use Radio
’s id
prop.
function RadioExample() { const [isChecked, setIsChecked] = React.useState(undefined); return ( <Radio isChecked={isChecked} name="example-multi-column-content" onChange={v => setIsChecked(v)} > <div className="flex"> <div className="flex-none"> <UserAvatar imageUrl="https://randomuser.me/api/portraits/women/63.jpg" /> </div> <div className="flex items-center pl4" style={{ flex: '1 0 0%' }}> <div> <span className="b">Austin Entertainment LLC.</span> <p>DJs, photo booths, and photography for all of your event needs.</p> </div> <div className="b ml-auto">$120/hr</div> </div> </div> </Radio> ); }
Radio input by default is vertically center-aligned with children
. If children
prop spans over multiple lines and you want it to align at the top, it is possible to provide radioVerticalAlign
prop as top
.
function RadioExample() { const [isChecked, setIsChecked] = React.useState(undefined); return ( <Radio name="vertical-align" isChecked={isChecked} onChange={v => setIsChecked(v)} radioVerticalAlign="top" > <Text> Lorem ipsum dolor sit amet, consectetur adipiscing elit. Vestibulum dapibus est nec eros congue, ac dapibus ipsum cursus. Quisque at odio viverra, consequat metus a, commodo ipsum. Donec sodales sapien in luctus sodales </Text> </Radio> ); }
Props
Radio
name
requiredRadio buttons that have the same name attribute are in the same radio button group. Only one radio in a group can be selected at a time. All of the radio buttons in the group must share a value that is unique to the page. This is required for keyboard navigation.
Typestring
onChange
requiredFunction that runs when a new radio button is selected. It receives the new boolean value, the provided
id
, and the underlyingevent
as such:props.onChange(event.target.checked, props.id, event)
.Type( isChecked: boolean, id: string | undefined, event: React.ChangeEvent<HTMLInputElement>, ) => void
isDisabled
Disable the input and the label.
Typeboolean
Defaultfalse
children
Text or elements that appear within the label. If
children
is not provided, the developer must use theRadio
'sid
prop to associate it with a custom<label>
element.TypeReact.ReactNode
Defaultnull
id
The
id
is added to the radio button as an HTML attribute and passed to theonChange
function.Typestring
isChecked
Boolean that determines if the radio is checked.
Typeboolean
Defaultfalse
isRequired
Adds the
required
HTML attribute.Typeboolean
Defaultfalse
hasError
Makes the radio and text color red.
Typeboolean
Defaultfalse
labelPadding
Determine that padding that gets applied to the label. This can be used to increase the label’s click target. The value supplied should be a string with a unit such as
8px
or8px 16px
.Typestring
Default'14px 0'
onKeyDown
Function that is called when the user presses a key while focused on the Radio.
Type(event: React.KeyboardEvent<HTMLInputElement>) => void
Default(): void => {}
dataTestId
A selector hook into the React component for use in automated testing environments. It is applied internally to the
<input />
element.Typestring
dataTest
deprecatedA selector hook into the React component for use in automated testing environments. It is applied internally to the
<input />
element.Note:
Deprecated in favor of the
dataTestId
propTypestring
radioVerticalAlign
Determines how the radio button input will be vertically aligned relative to
props.children
.Type'top' | 'center'
Default'center'
value
Determines the value that will be submitted if the radio is selected. The default value is
'on'
.Typestring | string[] | number