Checkbox
Capture boolean input from user
Source
LLM docs
Docs
PackageIcon
Usage
Controlled state
Use checked and onChange props to control Checkbox state:
Checkbox with @mantine/form
Example of using Checkbox with @mantine/form:
Checkbox with uncontrolled forms
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:
States
Error state
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}.
Must be checked
No error styles
Change icons
Change icon color
Use the iconColor prop to change the icon color. You can reference colors from theme.colors or use any valid CSS color:
Indeterminate state
Checkbox supports indeterminate state. When the indeterminate prop is set,
the checked prop is ignored (checkbox always has checked styles):
Label with link
Checkbox with tooltip
You can change the target element to which the tooltip is attached with refProp:
- If
refPropis not set, the tooltip is attached to the checkbox input - If
refProp="rootRef"is set, the tooltip is attached to the root element (contains label, input and other elements)
Pointer cursor
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:
autoContrast
Checkbox supports the autoContrast prop and theme.autoContrast. If autoContrast is set either on Checkbox or on the theme, the content color will be adjusted to have sufficient contrast with the value specified in the color prop.
Note that the autoContrast feature works only if you use the color prop to change the background color. autoContrast works only with the filled variant.
Add custom sizes
You can add any number of custom sizes with data-size attribute:
Add props to the root element
All props passed to the component are forwarded to the input element. If you need to add props to the root element, use wrapperProps. In the following example:
data-testid="wrapper"is added to the root elementdata-testid="input"is added to the input element
Checkbox.Group
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.
Checkbox.Group component supports all Input.Wrapper
props.
This is anonymous
Checkbox.Group disabled
This is anonymous
maxSelectedValues
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.
Checkbox.Group with @mantine/form
Example of using Checkbox.Group with @mantine/form:
Checkbox.Group with uncontrolled forms
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 input
Checkbox.Indicator
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
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:
Choose all packages that you will need in your application
CurrentValue: –
Get element ref
The example above shows how to get ref of the checkbox input element.
To get ref of the root element, use rootRef prop:
Styles API
Checkbox supports the Styles API; you can add styles to any inner element of the component with the classNames prop. Follow the Styles API documentation to learn more.
Checkbox description
Checkbox error
Component Styles API
Hover over selectors to highlight corresponding elements
Example: Table with row selection
| Element position | Element name | Symbol | Atomic mass | |
|---|---|---|---|---|
| 6 | Carbon | C | 12.011 | |
| 7 | Nitrogen | N | 14.007 | |
| 39 | Yttrium | Y | 88.906 | |
| 56 | Barium | Ba | 137.33 | |
| 58 | Cerium | Ce | 140.12 |
Example: Customize with Styles API
wrapperProps
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.
id attribute
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.
Accessibility
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: