apps/mantine.dev/src/pages/core/checkbox.mdx
import { CheckboxDemos, TableDemos, ThemingDemos } from '@docs/demos'; import { Layout } from '@/layout'; import { MDX_DATA } from '@/mdx';
export default Layout(MDX_DATA.Checkbox);
Use checked and onChange props to control Checkbox state:
import { useState } from 'react';
import { Checkbox } from '@mantine/core';
function Demo() {
const [checked, setChecked] = useState(false);
return (
<Checkbox
checked={checked}
onChange={(event) => setChecked(event.currentTarget.checked)}
/>
);
}
Example of using Checkbox with @mantine/form:
Checkbox can be used with uncontrolled forms the same way as native input[type="checkbox"].
Set the name attribute to include checkbox value in FormData object on form submission.
To control initial checked state in uncontrolled forms, use defaultChecked prop.
Example usage of uncontrolled Checkbox with FormData:
import { Checkbox } from '@mantine/core';
function Demo() {
return (
<form
onSubmit={(event) => {
event.preventDefault();
const formData = new FormData(event.currentTarget);
console.log('Checkbox value:', !!formData.get('terms'));
}}
>
<Checkbox label="Accept terms and conditions" name="terms" defaultChecked />
<button type="submit">Submit</button>
</form>
);
}
Use the error prop to display error message below the checkbox label.
If you want to apply error styles to checkbox without error message, user boolean error prop.
If you want to display error message without applying error styles, set withErrorStyles={false}.
Use the iconColor prop to change the icon color. You can reference colors from theme.colors or use any valid CSS color:
Checkbox supports indeterminate state. When the indeterminate prop is set,
the checked prop is ignored (checkbox always has checked styles):
<Demo data={CheckboxDemos.indeterminate} demoProps={{ toggle: true }} />
You can change the target element to which the tooltip is attached with refProp:
refProp is not set, the tooltip is attached to the checkbox inputrefProp="rootRef" is set, the tooltip is attached to the root element (contains label, input and other elements)By default, checkbox input and label have cursor: default (same as native input[type="checkbox"]).
To change the cursor to pointer, set cursorType on theme:
You can add any number of custom sizes with data-size attribute:
<Demo data={CheckboxDemos.customSize} /> <WrapperProps component="Checkbox" />Checkbox.Group manages the state of multiple checkboxes, it accepts value and onChange
props, which are used to control the state of checkboxes inside the group. The value prop should be an array of strings, where each string is the value of a checkbox.
The onChange prop should be a function that receives the new value as an array of strings.
import { useState } from 'react';
import { Checkbox } from '@mantine/core';
function Demo() {
const [value, setValue] = useState<string[]>([]);
return (
<Checkbox.Group value={value} onChange={setValue}>
<Checkbox value="react" label="React" />
<Checkbox value="svelte" label="Svelte" />
</Checkbox.Group>
);
}
Checkbox.Group component supports all Input.Wrapper
props.
Use maxSelectedValues prop to limit the number of selected values in Checkbox.Group.
When the limit is reached, the remaining checkboxes are disabled and cannot be selected.
Example of using Checkbox.Group with @mantine/form:
Checkbox.Group can be used with uncontrolled forms, it renders a hidden input
which joins all checked values into a single string using hiddenInputValuesSeparator prop.
Props for usage with uncontrolled forms:
name – name attribute passed to the hidden inputhiddenInputValuesSeparator – string used to join checked values into a single string, ',' by defaulthiddenInputProps – additional props passed to the hidden inputexport function UncontrolledForm() {
return (
<form
onSubmit={(event) => {
event.preventDefault();
const formData = new FormData(event.currentTarget);
console.log('Checkbox group value:', formData.get('frameworks'));
}}
>
<Checkbox.Group label="Frameworks" name="frameworks" hiddenInputValuesSeparator="|">
<Checkbox label="React" value="react" />
<Checkbox label="Angular" value="ng" />
</Checkbox.Group>
<button type="submit">Submit</button>
</form>
);
}
Checkbox.Indicator looks exactly the same as the Checkbox component, but it does not
have any semantic meaning, it's just a visual representation of checkbox state. You
can use it in any place where you need to display checkbox state without any interaction
related to the indicator. For example, it is useful in cards based on buttons, trees, etc.
Note that Checkbox.Indicator cannot be focused or selected with keyboard. It is not
accessible and should not be used as a replacement for the Checkbox component.
Checkbox.Card component can be used as a replacement for Checkbox to build custom
cards/buttons/other things that work as checkboxes. The root element of the component
has role="checkbox" attribute, it is accessible by default and supports the same
keyboard interactions as input[type="checkbox"].
You can use Checkbox.Card with Checkbox.Group the same way as the Checkbox component:
The example above shows how to get ref of the checkbox input element.
To get ref of the root element, use rootRef prop:
import { useRef } from 'react';
import { Checkbox } from '@mantine/core';
function Demo() {
const ref = useRef<HTMLDivElement>(null);
return <Checkbox rootRef={ref} />;
}
Most of the Checkbox props are passed down to the input element.
If you want to pass props to the root element instead, use wrapperProps prop.
import { Checkbox } from '@mantine/core';
function Demo() {
return (
<Checkbox
label="My checkbox"
wrapperProps={{ 'data-root-element': true }}
/>
);
}
By default, Checkbox generates a random id attribute for the input element
to associate it with the label. You can supply your own id attribute with id prop.
It will be used in id attribute of the input element and htmlFor attribute of the label element.
import { Checkbox } from '@mantine/core';
function Demo() {
return <Checkbox id="my-checkbox" label="My checkbox" />;
}
Checkbox component is based on the native input[type="checkbox"] element, so it is accessible by default.
Set aria-label or label prop to make the checkbox accessible for screen readers:
import { Checkbox } from '@mantine/core';
// Not ok, input is not labeled
function Bad() {
return <Checkbox />;
}
// Ok, input is labelled by aria-label
function GoodAriaLabel() {
return <Checkbox aria-label="My checkbox" />;
}
// Ok, input is labelled by label element
function GoodLabel() {
return <Checkbox label="My checkbox" />;
}