UNPKG

@cbpds/web-components

Version:
266 lines (265 loc) 9.37 kB
/*! * CPB Design System web components - built with Stencil */ import { Host, h } from "@stencil/core"; import { setCSSProps } from "../../utils/utils"; export class CbpToggle { constructor() { this.checked = undefined; this.disabled = undefined; this.hideStatus = undefined; this.statusTextOn = 'On'; this.statusTextOff = 'Off'; this.name = undefined; this.value = undefined; this.context = undefined; this.sx = {}; } toggleEvent(e) { this.checked = this.formField.checked; this.toggleClick.emit({ host: this.host, nativeElement: this.formField, value: this.formField.value, checked: this.formField.checked, nativeEvent: e }); } watchDisabledHandler(newValue) { if (this.formField) { (newValue) ? this.formField.setAttribute('disabled', '') : this.formField.removeAttribute('disabled'); } } watchCheckedHandler(newValue) { if (this.formField) { this.formField.checked = 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) { this.formField.addEventListener('change', e => this.toggleEvent(e)); } } componentDidLoad() { if (!!this.formField) { if (this.checked) this.formField.checked = this.checked; if (this.disabled) this.formField.setAttribute('disabled', ''); if (this.name) this.formField.name = this.name; if (this.value) this.formField.value = this.value; } } render() { return (h(Host, { key: 'c29533eb20c682842be3716a664b15e6cc8965d4' }, h("label", { key: 'da1aefcf5cdda57b35f713aa2405e8a8a44e16d1' }, h("slot", { key: 'e536a25d4740e7ab9952abcdb3d9dafda8a8f445' }), !this.hideStatus && h("span", { key: '8d9a9b3895d9f883b3a38fb6ad6adacd7ac6a7c8' }, this.checked ? this.statusTextOn : this.statusTextOff)))); } static get is() { return "cbp-toggle"; } static get originalStyleUrls() { return { "$": ["cbp-toggle.scss"] }; } static get styleUrls() { return { "$": ["cbp-toggle.css"] }; } static get properties() { return { "checked": { "type": "boolean", "mutable": false, "complexType": { "original": "boolean", "resolved": "boolean", "references": {} }, "required": false, "optional": false, "docs": { "tags": [], "text": "Marks the toggle as checked by default when specified." }, "attribute": "checked", "reflect": true }, "disabled": { "type": "boolean", "mutable": false, "complexType": { "original": "boolean", "resolved": "boolean", "references": {} }, "required": false, "optional": false, "docs": { "tags": [], "text": "Marks the toggle in a disabled state when specified." }, "attribute": "disabled", "reflect": true }, "hideStatus": { "type": "boolean", "mutable": false, "complexType": { "original": "boolean", "resolved": "boolean", "references": {} }, "required": false, "optional": false, "docs": { "tags": [], "text": "Determines if the status text is visible for the render" }, "attribute": "hide-status", "reflect": false }, "statusTextOn": { "type": "string", "mutable": false, "complexType": { "original": "string", "resolved": "string", "references": {} }, "required": false, "optional": false, "docs": { "tags": [], "text": "Determines the status text for the true toggle" }, "attribute": "status-text-on", "reflect": false, "defaultValue": "'On'" }, "statusTextOff": { "type": "string", "mutable": false, "complexType": { "original": "string", "resolved": "string", "references": {} }, "required": false, "optional": false, "docs": { "tags": [], "text": "Determines the status text for the false toggle" }, "attribute": "status-text-off", "reflect": false, "defaultValue": "'Off'" }, "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 }, "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": "toggleClick", "name": "toggleClick", "bubbles": true, "cancelable": true, "composed": true, "docs": { "tags": [], "text": "Custom event fired when the control is toggled by the user." }, "complexType": { "original": "any", "resolved": "any", "references": {} } }]; } static get elementRef() { return "host"; } static get watchers() { return [{ "propName": "disabled", "methodName": "watchDisabledHandler" }, { "propName": "checked", "methodName": "watchCheckedHandler" }]; } } //# sourceMappingURL=cbp-toggle.js.map