Skip to main content
Default Gray Amethyst

Checkbox

<mo-checkbox> | MOCheckbox
Since 1.0 stable

Checkboxes allow the user to toggle an option on or off.

Checkbox
<mo-checkbox>Checkbox</mo-checkbox>
import { MOCheckbox } from '@metsooutotec/modes-web-components/dist/react';

const App = () => <MOCheckbox>Checkbox</MOCheckbox>;

Examples

Checked

Use the checked attribute to activate the checkbox.

Checked
<mo-checkbox checked>Checked</mo-checkbox>
import { MOCheckbox } from '@metsooutotec/modes-web-components/dist/react';

const App = () => <MOCheckbox checked>Checked</MOCheckbox>;

Indeterminate

Use the indeterminate attribute to make the checkbox indeterminate.

Indeterminate
<mo-checkbox indeterminate>Indeterminate</mo-checkbox>
import { MOCheckbox } from '@metsooutotec/modes-web-components/dist/react';

const App = () => <MOCheckbox indeterminate>Indeterminate</MOCheckbox>;

Sizes

Use the size attribute to change a checkbox’s size.

Small
Medium
Large
<mo-checkbox size="small">Small</mo-checkbox>
<br />
<mo-checkbox size="medium">Medium</mo-checkbox>
<br />
<mo-checkbox size="large">Large</mo-checkbox>
import { MOCheckbox } from '@metsooutotec/modes-web-components/dist/react';

const App = () => (
  <>
    <MOCheckbox size="small">Small</MOCheckbox>
    <br />
    <MOCheckbox size="medium">Medium</MOCheckbox>
    <br />
    <MOCheckbox size="large">Large</MOCheckbox>
  </>
);

Error and success states

Use the error and success attributes to indicate the checkbox state.

Error

Success
<mo-checkbox error-text="This must be checked.">Error</mo-checkbox>
<br /><br />
<mo-checkbox checked success-text="You have accepted the terms.">Success</mo-checkbox>
import { MOCheckbox } from '@metsooutotec/modes-web-components/dist/react';

const App = () => (
  <>
    <MOCheckbox errorText="This must be checked.">Error</MOCheckbox>
    <br /><br />
    <MOCheckbox  checked successText="You have accepted the terms.">Success</MOCheckbox>
  </>
);

Disabled

Use the disabled attribute to disable the checkbox.

Disabled
<mo-checkbox disabled>Disabled</mo-checkbox>
import { MOCheckbox } from '@metsooutotec/modes-web-components/dist/react';

const App = () => <MOCheckbox disabled>Disabled</MOCheckbox>;

Importing

If you’re using the autoloader or the traditional loader, you can ignore this section. Otherwise, feel free to use any of the following snippets to cherry pick this component.

Bundler React Script

To import this component using a bundler:

import '@metsooutotec/modes-web-components/dist/components/checkbox/checkbox.js';

To import this component as a React component:

import MOCheckbox from '@metsooutotec/modes-web-components/dist/react/checkbox/';

To import this component using a script tag:

<script type="module" src="https://modes-web.metso.com/dist/components/cdn/components/checkbox/checkbox.js"></script>

Slots

Name Description
(default) The checkbox’s label.
help-text Text that describes how to use the checkbox. Alternatively, you can use the help-text attribute.

Learn more about using slots.

Properties

Name Description Reflects Type Default
name The name of the checkbox, submitted as a name/value pair with form data. string ''
value The current value of the checkbox, submitted as a name/value pair with form data. string -
size The checkbox’s size. 'small' | 'medium' | 'large' 'medium'
disabled Disables the checkbox. boolean false
checked Draws the checkbox in a checked state. boolean false
indeterminate Draws the checkbox in an indeterminate state. This is usually applied to checkboxes that represents a “select all/none” behavior when associated checkboxes have a mix of checked and unchecked states. boolean false
error Renders the field in an error state boolean false
success Renders the field in a success state boolean false
errorText
error-text
Error text to show in place of help text when checkbox is invalid. string ''
successText
success-text
Success text to show in place of help text when checkbox is valid. string ''
defaultChecked The default value of the form control. Primarily used for resetting the form control. boolean false
form By default, form controls are associated with the nearest containing <form> element. This attribute allows you to place the form control outside of a form and associate it with the form that has this id. The form must be in the same document or shadow root for this to work. string ''
required Makes the checkbox a required field. boolean false
helpText
help-text
The checkbox’s help text. If you need to display HTML, use the help-text slot instead. string ''
validity Gets the validity state object - -
validationMessage Gets the validation message - -
updateComplete A read-only promise that resolves when the component has finished updating.

Learn more about attributes and properties.

Events

Name React Event Description Event Detail
mo-blur onMoBlur Emitted when the checkbox loses focus. -
mo-change onMoChange Emitted when the checked state changes. -
mo-focus onMoFocus Emitted when the checkbox gains focus. -
mo-input onMoInput Emitted when the checkbox receives input. -
mo-invalid onMoInvalid Emitted when the form control has been checked for validity and its constraints aren’t satisfied. -

Learn more about events.

Methods

Name Description Arguments
click() Simulates a click on the checkbox. -
focus() Sets focus on the checkbox. options: FocusOptions
blur() Removes focus from the checkbox. -
checkValidity() Checks for validity but does not show a validation message. Returns true when valid and false when invalid. -
getForm() Gets the associated form, if one exists. -
reportValidity() Checks for validity and shows the browser’s validation message if the control is invalid. -
setCustomValidity() Sets a custom validation message. The value provided will be shown to the user when the form is submitted. To clear the custom validation message, call this method with an empty string. message: string

Learn more about methods.

Parts

Name Description
base The component’s base wrapper.
control The square container that wraps the checkbox’s checked state.
control--checked Matches the control part when the checkbox is checked.
control--indeterminate Matches the control part when the checkbox is indeterminate.
checked-icon The checked icon, an <mo-icon> element.
indeterminate-icon The indeterminate icon, an <mo-icon> element.
label The container that wraps the checkbox’s label.
form-control-help-text The help text’s wrapper.

Learn more about customizing CSS parts.

Dependencies

This component automatically imports the following dependencies.