UNPKG

@cbpds/web-components

Version:
223 lines (222 loc) 8.09 kB
/*! * CPB Design System web components - built with Stencil */ import { Host, h } from "@stencil/core"; import { setCSSProps, createNamespaceKey } from "../../utils/utils"; export class CbpRadio { constructor() { this.name = undefined; this.value = undefined; this.fieldId = createNamespaceKey('cbp-radio'); this.checked = 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'); } } 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=radio]'); if (this.formField) { const radioId = this.formField.getAttribute('id'); radioId ? this.fieldId = radioId : 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.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: 'cfce61c722739114fcaf764acdf47ae120e74858' }, h("label", { key: 'f4e650d7b9d5d9025bcc1250754b88e81bb599ac', htmlFor: this.fieldId }, h("slot", { key: '7df14637c3135ad257baec2d2cf4486f2486fb06' })))); } static get is() { return "cbp-radio"; } static get originalStyleUrls() { return { "$": ["cbp-radio.scss"] }; } static get styleUrls() { return { "$": ["cbp-radio.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 radio button, which is passed as part of formData (as a key) only when the radio button 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 radio button at the component level. Not needed if the slotted radio button 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-radio')" }, "checked": { "type": "boolean", "mutable": false, "complexType": { "original": "boolean", "resolved": "boolean", "references": {} }, "required": false, "optional": false, "docs": { "tags": [], "text": "Marks the radio button as checked by default when specified." }, "attribute": "checked", "reflect": false }, "disabled": { "type": "boolean", "mutable": false, "complexType": { "original": "boolean", "resolved": "boolean", "references": {} }, "required": false, "optional": false, "docs": { "tags": [], "text": "Marks the radio button 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 click event occurs for either a rendered button or anchor/link." }, "complexType": { "original": "any", "resolved": "any", "references": {} } }]; } static get elementRef() { return "host"; } static get watchers() { return [{ "propName": "disabled", "methodName": "watchDisabledHandler" }]; } } //# sourceMappingURL=cbp-radio.js.map