@cbpds/web-components
Version:
Web components for the CBP Design System.
251 lines (250 loc) • 9.41 kB
JavaScript
/*!
* 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
});
}
async reset() {
var _a;
this.checked = this.initialChecked;
this.initialChecked ? (_a = this.formField) === null || _a === void 0 ? void 0 : _a.setAttribute('checked', '') : this === null || this === void 0 ? void 0 : this.formField.removeAttribute('checked');
}
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;
this.checked = this.formField.checked;
}
this.initialChecked = this.checked;
}
render() {
return (h(Host, { key: '51477fafb2d6676611a327b6407e051866b146a8' }, h("label", { key: '077c2ac3f7b77f508b9779e614a4b71437aede35', htmlFor: this.fieldId }, h("slot", { key: '9ae409f0a97c39c0a6567d6949cea46b23d8e6ed' }))));
}
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 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 Radio 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"
}];
}
}
//# sourceMappingURL=cbp-radio.js.map