@cbpds/web-components
Version:
Web components for the CBP Design System.
252 lines (251 loc) • 9.16 kB
JavaScript
/*!
* 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
});
}
watchDisabledHandler(newValue) {
if (this.formField) {
(newValue)
? this.formField.setAttribute('disabled', '')
: this.formField.removeAttribute('disabled');
}
}
watchIndeterminateHandler(newValue) {
if (this.formField)
this.formField.indeterminate = newValue;
if (newValue == true)
this.checked = false;
}
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;
}
}
render() {
return (h(Host, { key: 'b8c518b700a73dfbf005165c00f3f27fd9a30b7f' }, h("label", { key: 'ae1bbdcfa0e047080d70e30b9e42f04b55122a39', htmlFor: this.fieldId }, h("slot", { key: '2ad2c977bcbb586f2055dcb6e33d19cd85244fc1' }))));
}
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": false,
"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": false
},
"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 elementRef() { return "host"; }
static get watchers() {
return [{
"propName": "disabled",
"methodName": "watchDisabledHandler"
}, {
"propName": "indeterminate",
"methodName": "watchIndeterminateHandler"
}];
}
}
//# sourceMappingURL=cbp-checkbox.js.map