Checkbox
Checkboxes allow users to select one or more options from a set. Each checkbox can toggle between checked and unchecked states.
Usage
Import the component:
import 'mdui/components/checkbox.js';
Import the TypeScript type:
import type { Checkbox } from 'mdui/components/checkbox.js';
Example:
<mdui-checkbox>Checkbox</mdui-checkbox>
Examples
Checked State
The checked
property is true
when the checkbox is checked. Add the checked
attribute to set the checkbox to the checked state by default.
Disabled State
Use the disabled
attribute to disable the checkbox.
Indeterminate State
The indeterminate
attribute indicates an indeterminate state for the checkbox.
Icons
Use the unchecked-icon
, checked-icon
, and indeterminate-icon
attributes to set Material Icons for the checkbox in unchecked, checked, and indeterminate states, respectively. Alternatively, use the corresponding slots for setting icons.
API
Properties
Attribute | Property | Reflect | Type | Default |
---|---|---|---|---|
disabled | disabled | boolean | false | |
Dsiables the checkbox. | ||||
checked | checked | boolean | false | |
Sets the checkbox to the checked state. | ||||
defaultChecked | boolean | false | ||
Sets the default checked state. Resets to this state when the form is reset. Can only be set via JavaScript property. | ||||
indeterminate | indeterminate | boolean | false | |
Sets the checkbox to an indeterminate state. | ||||
required | required | boolean | false | |
Requires the checkbox to be checked for form submission. | ||||
form | form | string | - | |
Associates the checkbox with a This attribute allows checkbox elements to be associated with | ||||
name | name | string | '' | |
Sets the checkbox's name, which is submitted with form data. | ||||
value | value | string | 'on' | |
Sets the checkbox's value, which is submitted with form data. | ||||
unchecked-icon | uncheckedIcon | string | - | |
Sets the Material Icons name for the unchecked state. Alternatively, use | ||||
checked-icon | checkedIcon | string | - | |
Sets the Material Icons name for the checked state. Alternatively, use | ||||
indeterminate-icon | indeterminateIcon | string | - | |
Sets the Material Icons name for the indeterminate state. Alternatively, use | ||||
validity | ValidityState | - | ||
A | ||||
validationMessage | string | - | ||
The element's validation message. This is empty if the element meets its constraints. | ||||
autofocus | autofocus | boolean | false | |
Determines if the element should be focused when the page loads. | ||||
tabindex | tabIndex | number | - | |
Specifies the order in which the element receives focus when navigating with the Tab key. |
Methods
Name | Parameters | Returns |
---|---|---|
checkValidity | boolean | |
Checks the validity of the form field. If it's invalid, it triggers an | ||
reportValidity | boolean | |
Checks the validity of the form field. If it's invalid, it triggers an | ||
setCustomValidity |
| void |
Sets a custom error message. If the text is non-empty, it indicates that the field is invalid. | ||
click | void | |
Simulates a mouse click on the element. | ||
focus |
| void |
Sets focus on the element. An optional parameter can be an object with a | ||
blur | void | |
Removes focus from the element. |
Slots
Name |
---|
(default) |
Text. |
unchecked-icon |
Icon for the unchecked state. |
checked-icon |
Icon for the checked state. |
indeterminate-icon |
Icon for the indeterminate state. |
CSS Parts
Name |
---|
control |
Container for the left icon. |
unchecked-icon |
Icon for the unchecked state. |
checked-icon |
Icon for the checked state. |
indeterminate-icon |
Icon for the indeterminate state. |
label |
Text. |