UNPKG

@cbpds/web-components

Version:
287 lines (286 loc) 10.6 kB
/*! * CPB Design System web components - built with Stencil */ import { Host, h } from "@stencil/core"; import { setCSSProps, createNamespaceKey } from "../../utils/utils"; export class CbpCheckbox { constructor() { this.name = undefined; this.value = undefined; this.fieldId = createNamespaceKey('cbp-checkbox'); this.checked = undefined; this.indeterminate = undefined; this.disabled = undefined; this.context = undefined; this.sx = {}; } handleChange(e) { this.checked = this.formField.checked; this.stateChanged.emit({ host: this.host, nativeElement: this.formField, value: this.formField.value, checked: this.formField.checked, nativeEvent: e }); } async reset() { this.checked = this.initialChecked; this.initialChecked ? this.formField.setAttribute('checked', '') : this.formField.removeAttribute('checked'); } watchDisabledHandler(newValue) { if (this.formField) { (newValue) ? this.formField.setAttribute('disabled', '') : this.formField.removeAttribute('disabled'); } } watchChecked(newValue) { if (this.formField) this.formField.checked = newValue; } watchIndeterminateHandler(newValue) { if (this.formField) { if (newValue == true) this.checked = false; this.formField.indeterminate = newValue; } } componentWillLoad() { if (typeof this.sx == 'string') { this.sx = JSON.parse(this.sx) || {}; } setCSSProps(this.host, Object.assign({}, this.sx)); this.formField = this.host.querySelector('input[type=checkbox]'); if (this.formField) { const checkboxId = this.formField.getAttribute('id'); checkboxId ? this.fieldId = checkboxId : this.formField.setAttribute('id', this.fieldId); this.formField.addEventListener('change', (e) => this.handleChange(e)); } } componentDidLoad() { if (this.formField) { if (this.checked) this.formField.checked = this.checked; if (this.indeterminate && !this.checked) this.formField.indeterminate = this.indeterminate; if (this.disabled) this.formField.setAttribute('disabled', ''); if (this.name) this.formField.name = this.name; if (this.value) this.formField.value = this.value; this.checked = this.formField.checked; } this.initialChecked = this.checked; } render() { return (h(Host, { key: '4c67fc1552a0088f334b657a8902969ad18cd92e' }, h("label", { key: 'a6567aacdcedf4167306b17eeff5da4484bb8373', htmlFor: this.fieldId }, h("slot", { key: '23530ac906336c7aee4c80e2f435365f5bb13b12' })))); } static get is() { return "cbp-checkbox"; } static get originalStyleUrls() { return { "$": ["cbp-checkbox.scss"] }; } static get styleUrls() { return { "$": ["cbp-checkbox.css"] }; } static get properties() { return { "name": { "type": "string", "mutable": false, "complexType": { "original": "string", "resolved": "string", "references": {} }, "required": false, "optional": false, "docs": { "tags": [], "text": "The `name` attribute of the checkbox, which is passed as part of formData (as a key) only when the checkbox is checked." }, "attribute": "name", "reflect": false }, "value": { "type": "string", "mutable": false, "complexType": { "original": "string", "resolved": "string", "references": {} }, "required": false, "optional": false, "docs": { "tags": [], "text": "Optionally set the `value` attribute of the checkbox at the component level. Not needed if the slotted checkbox has a value." }, "attribute": "value", "reflect": false }, "fieldId": { "type": "string", "mutable": true, "complexType": { "original": "string", "resolved": "string", "references": {} }, "required": false, "optional": false, "docs": { "tags": [], "text": "Optionally specify the ID of the checkbox input here, which is used to generate related pattern node IDs and associate everything for accessibility" }, "attribute": "field-id", "reflect": false, "defaultValue": "createNamespaceKey('cbp-checkbox')" }, "checked": { "type": "boolean", "mutable": true, "complexType": { "original": "boolean", "resolved": "boolean", "references": {} }, "required": false, "optional": false, "docs": { "tags": [], "text": "Marks the checkbox as checked by default when specified." }, "attribute": "checked", "reflect": true }, "indeterminate": { "type": "boolean", "mutable": false, "complexType": { "original": "boolean", "resolved": "boolean", "references": {} }, "required": false, "optional": false, "docs": { "tags": [], "text": "Marks the checkbox as checked by default when specified." }, "attribute": "indeterminate", "reflect": false }, "disabled": { "type": "boolean", "mutable": false, "complexType": { "original": "boolean", "resolved": "boolean", "references": {} }, "required": false, "optional": false, "docs": { "tags": [], "text": "Marks the checkbox in a disabled state when specified." }, "attribute": "disabled", "reflect": false }, "context": { "type": "string", "mutable": false, "complexType": { "original": "'light-inverts' | 'light-always' | 'dark-inverts' | 'dark-always'", "resolved": "\"dark-always\" | \"dark-inverts\" | \"light-always\" | \"light-inverts\"", "references": {} }, "required": false, "optional": false, "docs": { "tags": [], "text": "Specifies the context of the component as it applies to the visual design and whether it inverts when light/dark mode is toggled. Default behavior is \"light-inverts\" and does not have to be specified." }, "attribute": "context", "reflect": true }, "sx": { "type": "any", "mutable": false, "complexType": { "original": "any", "resolved": "any", "references": {} }, "required": false, "optional": false, "docs": { "tags": [], "text": "Supports adding inline styles as an object" }, "attribute": "sx", "reflect": false, "defaultValue": "{}" } }; } static get events() { return [{ "method": "stateChanged", "name": "stateChanged", "bubbles": true, "cancelable": true, "composed": true, "docs": { "tags": [], "text": "A custom event emitted when the checked state changes due to user interaction." }, "complexType": { "original": "any", "resolved": "any", "references": {} } }]; } static get methods() { return { "reset": { "complexType": { "signature": "() => Promise<void>", "parameters": [], "references": { "Promise": { "location": "global", "id": "global::Promise" } }, "return": "Promise<void>" }, "docs": { "text": "A custom method to reset the Checkbox component to its initial state and value since it does not update \nproperly on a native form reset when the checked state is set via the component property. This method may \nbe called manually, but is automatically called on form reset when using the `cbp-form` component.", "tags": [] } } }; } static get elementRef() { return "host"; } static get watchers() { return [{ "propName": "disabled", "methodName": "watchDisabledHandler" }, { "propName": "checked", "methodName": "watchChecked" }, { "propName": "indeterminate", "methodName": "watchIndeterminateHandler" }]; } } //# sourceMappingURL=cbp-checkbox.js.map