apps/mantine.dev/src/pages/core/radio.mdx
import { RadioDemos } from '@docs/demos'; import { Layout } from '@/layout'; import { MDX_DATA } from '@/mdx';
export default Layout(MDX_DATA.Radio);
import { useState } from 'react';
import { Radio } from '@mantine/core';
function Demo() {
const [checked, setChecked] = useState(false);
return (
<Radio
checked={checked}
onChange={(event) => setChecked(event.currentTarget.checked)}
/>
);
}
Radio can be used with uncontrolled forms the same way as a native input[type="radio"].
Set the name and value attributes to include radio value in FormData object on form submission.
To control the initial checked state in uncontrolled forms, use defaultChecked prop.
Example usage of uncontrolled Radio with FormData:
import { Radio } from '@mantine/core';
function Demo() {
return (
<form
onSubmit={(event) => {
event.preventDefault();
const formData = new FormData(event.currentTarget);
console.log('Radio value:', formData.get('option'));
}}
>
<Radio name="option" value="option1" label="Option 1" />
<Radio name="option" value="option2" label="Option 2" defaultChecked />
<button type="submit">Submit</button>
</form>
);
}
By default, the radio input and label have cursor: default (same as native input[type="radio"]).
To change the cursor to pointer, set cursorType on the theme:
import { createTheme, MantineProvider, Radio } from '@mantine/core';
const theme = createTheme({
cursorType: 'pointer',
});
function Demo() {
return (
<MantineProvider theme={theme}>
<Radio label="Pointer cursor" />
</MantineProvider>
);
}
You can change the target element to which the tooltip is attached with refProp:
refProp is not set, the tooltip is attached to the radio inputrefProp="rootRef" is set, the tooltip is attached to the root element (contains label, input, and other elements)import { useState } from 'react';
import { Radio } from '@mantine/core';
function Demo() {
const [value, setValue] = useState('react');
return (
<Radio.Group
value={value}
onChange={setValue}
name="favoriteFramework"
label="Select your favorite framework/library"
description="This is anonymous"
withAsterisk
>
<Radio value="react" label="React" />
<Radio value="svelte" label="Svelte" />
<Radio value="ng" label="Angular" />
<Radio value="vue" label="Vue" />
</Radio.Group>
);
}
Radio.Indicator looks exactly the same as the Radio component, but it does not
have any semantic meaning; it's just a visual representation of the radio state. You
can use it in any place where you need to display the radio state without any interaction
related to the indicator. For example, it is useful in cards based on buttons, trees, etc.
Note that Radio.Indicator cannot be focused or selected with the keyboard. It is not
accessible and should not be used as a replacement for the Radio component.
Radio.Card component can be used as a replacement for Radio to build custom
cards/buttons/other things that work as radios. The root element of the component
has the role="radio" attribute; it is accessible by default and supports the same
keyboard interactions as input[type="radio"].
You can use Radio.Card with Radio.Group the same way as the Radio component:
Set the aria-label or label prop to make the radio accessible:
import { Radio } from '@mantine/core';
// Not ok, input is not labeled
function Bad() {
return <Radio />;
}
// Ok, input is labeled by aria-label
function GoodAriaLabel() {
return <Radio aria-label="My radio" />;
}
// Ok, input is labeled by label element
function GoodLabel() {
return <Radio label="My radio" />;
}