Checkbox
<pc-checkbox> Dieser Inhalt ist noch nicht in deiner Sprache verfügbar.
Checkboxes allow the user to toggle an option on or off.
This component works well with standard <form> elements. Please refer to the form controls page to learn more about form submission and client‐side validation.
Demos#
Checked#
Use the checked attribute to enable the checkbox.
Indeterminate#
Use the indeterminate attribute to make the checkbox indeterminate.
Disabled#
Use the disabled attribute to disable the checkbox.
Sizes#
Use the size attribute to change the checkbox’s size.
Hint#
Add a hint to a checkbox with the hint attribute. For hints that contain HTML, use the hint slot instead.
Custom validity#
Use the setCustomValidity() method to set a custom validation message. This will prevent the form from submitting and make the browser display the error message you provide. To clear the error, call this function with an empty string.
Eigenschaften#
| Name | Beschreibung | Reflektiert | Standard |
|---|---|---|---|
name | The name of the checkbox, submitted as a name/value pair with form data. Typ: string |
| "" |
value | The current value of the checkbox, submitted as a name/value pair with form data. Typ: string |
| "" |
size | The checkbox’s size. Typ: "small" | "medium" | "large" |
| "medium" |
disabled | Disables the checkbox. Typ: boolean |
| false |
checked | Checks the checkbox off. Typ: boolean |
| false |
indeterminate | Sets the checkbox’s state to indeterminate. This is usually applied to checkboxes that represent a “Select all/none” behaviour when associated checkboxes have a mix of checked and unchecked states. Typ: boolean |
| false |
defaultChecked | The default value of the checkbox. Primarily used for resetting the checkbox. Typ: 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.Typ: string |
| "" |
required | Indicates that the checkbox must be checked. Typ: boolean |
| false |
hint | The checkbox’s hint. If you need to display HTML, use the hint slot instead.Typ: string |
| "" |
validity | Gets the validity state object. |
| ‐ |
validationMessage | Gets the validation message. |
| ‐ |
updateComplete | Ein schreibgeschütztes Promise, das erfüllt ist, sobald die Komponente fertig aktualisiert wurde. | ‐ |
Erfahre mehr über Attribute und Eigenschaften.
Slots#
| Name | Beschreibung |
|---|---|
| (default) | The checkbox’s label. |
hint | Text that describes how to use the checkbox. Alternatively, you can use the hint attribute. |
Erfahre mehr über die Benutzung von Slots.
Methoden#
| Name | Beschreibung | Argumente |
|---|---|---|
click() | Simulates a click on the checkbox. | ‐ |
focus() | Focuses the checkbox. | options: FocusOptions |
blur() | Unfocuses the checkbox (i.e., blurs it). | ‐ |
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 checkbox is invalid. | ‐ |
setCustomValidity() | Sets a custom validation message. Pass an empty string to restore validity. | message: string |
Erfahre mehr über Methoden.
Events#
| Name | Beschreibung | Event‐Detail |
|---|---|---|
pc-input | Emitted when the checkbox receives input. | ‐ |
pc-change | Emitted when the checkbox’s state changes. | ‐ |
pc-focus | Emitted when the checkbox gains focus. | ‐ |
pc-blur | Emitted when the checkbox loses focus. | ‐ |
pc-invalid | Emitted when the checkbox has been checked for validity and its constraints aren’t satisfied. | ‐ |
Erfahre mehr über Events.
Parts#
| Name | Beschreibung |
|---|---|
base | The component’s base wrapper. |
control | The container that wraps the checkbox’s state. |
checkbox-checked | Matches the control part when the checkbox is checked. |
checkbox-indeterminate | Matches the control part when the checkbox is indeterminate. |
icon-checked | The checked icon, a <pc-icon> element. |
icon-indeterminate | The indeterminate icon, a <pc-icon> element. |
label | The container that wraps the checkbox’s label. |
hint | The container that wraps the checkbox’s hint. |
Erfahre mehr über das Anpassen von CSS‐Parts.
Importieren#
Wenn du den Autoloader oder den Standard‐Loader nutzt, kannst du diesen Abschnitt überspringen. Falls du „Cherry Picking“ betreibst, kannst du die folgenden Snippets verwenden, um diese Komponente zu importieren.
Um diese Komponente manuell vom CDN zu importieren, kopiere dieses Code‐Snippet und füge es in dein HTML ein.
<script type="module" src="https://cdn.jsdelivr.net/npm/placer-toolkit@1.0.0-alpha.3/cdn/components/checkbox/checkbox.js"></script> Um diese Komponente manuell vom CDN zu importieren, kopiere dieses Code‐Snippet und füge es in deine JavaScript‐Datei ein.
import "https://cdn.jsdelivr.net/npm/placer-toolkit@1.0.0-alpha.3/cdn/components/checkbox/checkbox.js"; Um diese Komponente manuell via npm zu importieren, kopiere dieses Code‐Snippet und füge es in deine JavaScript‐Datei ein.
import "placer-toolkit/dist/components/checkbox/checkbox.js"; Abhängigkeiten#
Diese Komponente importiert automatisch folgende Komponenten: