@scania/tegel
Version:
Tegel Design System
360 lines (359 loc) • 12.8 kB
JavaScript
import { h, } from "@stencil/core";
import generateUniqueId from "../../utils/generateUniqueId";
/**
* @slot label - Slot for the label text.
*/
export class TdsCheckbox {
constructor() {
this.handleChange = () => {
this.checked = !this.checked;
this.indeterminate = false;
this.tdsChange.emit({
checkboxId: this.checkboxId,
checked: this.checked,
indeterminate: this.indeterminate,
value: this.value,
});
};
this.name = undefined;
this.checkboxId = generateUniqueId();
this.disabled = false;
this.required = false;
this.checked = false;
this.indeterminate = false;
this.value = undefined;
this.tdsAriaLabel = undefined;
this.tdsAriaDescribedby = undefined;
}
/** Toggles the checked value of the component. */
async toggleCheckbox() {
this.checked = !this.checked;
this.indeterminate = false;
return {
checkboxId: this.checkboxId,
checked: this.checked,
};
}
/** Method to programmatically focus the checkbox element */
async focusElement() {
if (this.inputElement) {
this.inputElement.focus();
}
}
handleIndeterminateState() {
this.inputElement.indeterminate = this.indeterminate;
}
/** Set the input as focus when clicking the component */
handleFocus(event) {
this.tdsFocus.emit(event);
}
/** Set the input as blur when clicking outside the component */
handleBlur(event) {
this.tdsBlur.emit(event);
}
/** Listens for a reset event inside a form */
handleFormReset(event) {
if (this.host.closest('form') === event.target) {
this.checked = false;
this.indeterminate = false;
}
}
connectedCallback() {
if (!this.tdsAriaLabel) {
console.warn('Tegel Checkbox component: tdsAriaLabel prop is missing');
}
}
render() {
return (h("div", { key: '97afce57b6f6ce0d66dabd227f7271b5dc8fe707', class: "tds-checkbox" }, h("input", { key: '18b283a21ff649b9a4935a7b3e6f489cde16073d',
// eslint-disable-next-line no-return-assign
ref: (inputElement) => (this.inputElement = inputElement), indeterminate: this.indeterminate, "aria-checked": this.checked, "aria-required": this.required, "aria-label": this.tdsAriaLabel, "aria-describedby": this.tdsAriaDescribedby, required: this.required, type: "checkbox", name: this.name, value: this.value, id: this.checkboxId, checked: this.checked, disabled: this.disabled, onFocus: (event) => this.handleFocus(event), onBlur: (event) => this.handleBlur(event), onChange: () => {
this.handleChange();
} }), h("label", { key: '51d15b0eea1fd0f7517ca8d0f669bc02ac59781c', htmlFor: this.checkboxId }, h("slot", { key: 'bb10126c57b177f825ef8cd75da505465b2d6ae3', name: "label" }))));
}
static get is() { return "tds-checkbox"; }
static get encapsulation() { return "scoped"; }
static get originalStyleUrls() {
return {
"$": ["checkbox.scss"]
};
}
static get styleUrls() {
return {
"$": ["checkbox.css"]
};
}
static get properties() {
return {
"name": {
"type": "string",
"mutable": false,
"complexType": {
"original": "string",
"resolved": "string",
"references": {}
},
"required": false,
"optional": false,
"docs": {
"tags": [],
"text": "Name for the Checkbox's input element."
},
"attribute": "name",
"reflect": false
},
"checkboxId": {
"type": "string",
"mutable": false,
"complexType": {
"original": "string",
"resolved": "string",
"references": {}
},
"required": false,
"optional": false,
"docs": {
"tags": [],
"text": "ID for the Checkbox's input element. Randomly generated if not specified."
},
"attribute": "checkbox-id",
"reflect": false,
"defaultValue": "generateUniqueId()"
},
"disabled": {
"type": "boolean",
"mutable": false,
"complexType": {
"original": "boolean",
"resolved": "boolean",
"references": {}
},
"required": false,
"optional": false,
"docs": {
"tags": [],
"text": "Sets the Checkbox in a disabled state"
},
"attribute": "disabled",
"reflect": false,
"defaultValue": "false"
},
"required": {
"type": "boolean",
"mutable": false,
"complexType": {
"original": "boolean",
"resolved": "boolean",
"references": {}
},
"required": false,
"optional": false,
"docs": {
"tags": [],
"text": "Make the Checkbox required"
},
"attribute": "required",
"reflect": false,
"defaultValue": "false"
},
"checked": {
"type": "boolean",
"mutable": true,
"complexType": {
"original": "boolean",
"resolved": "boolean",
"references": {}
},
"required": false,
"optional": false,
"docs": {
"tags": [],
"text": "Sets the Checkbox as checked"
},
"attribute": "checked",
"reflect": true,
"defaultValue": "false"
},
"indeterminate": {
"type": "boolean",
"mutable": true,
"complexType": {
"original": "boolean",
"resolved": "boolean",
"references": {}
},
"required": false,
"optional": false,
"docs": {
"tags": [],
"text": "Sets the Checkbox as indeterminate"
},
"attribute": "indeterminate",
"reflect": false,
"defaultValue": "false"
},
"value": {
"type": "string",
"mutable": false,
"complexType": {
"original": "string",
"resolved": "string",
"references": {}
},
"required": false,
"optional": false,
"docs": {
"tags": [],
"text": "Value for the Checkbox"
},
"attribute": "value",
"reflect": false
},
"tdsAriaLabel": {
"type": "string",
"mutable": false,
"complexType": {
"original": "string",
"resolved": "string",
"references": {}
},
"required": false,
"optional": false,
"docs": {
"tags": [],
"text": "Value to be used for the aria-label attribute"
},
"attribute": "tds-aria-label",
"reflect": false
},
"tdsAriaDescribedby": {
"type": "string",
"mutable": false,
"complexType": {
"original": "string",
"resolved": "string",
"references": {}
},
"required": false,
"optional": false,
"docs": {
"tags": [],
"text": "Value to be used for the aria-describedby attribute"
},
"attribute": "tds-aria-describedby",
"reflect": false
}
};
}
static get events() {
return [{
"method": "tdsChange",
"name": "tdsChange",
"bubbles": true,
"cancelable": false,
"composed": true,
"docs": {
"tags": [],
"text": "Sends unique Checkbox identifier and checked status when it is checked/unchecked."
},
"complexType": {
"original": "{\n checkboxId: string;\n checked: boolean;\n indeterminate: boolean;\n value?: string;\n }",
"resolved": "{ checkboxId: string; checked: boolean; indeterminate: boolean; value?: string; }",
"references": {}
}
}, {
"method": "tdsFocus",
"name": "tdsFocus",
"bubbles": true,
"cancelable": false,
"composed": true,
"docs": {
"tags": [],
"text": "Focus event for the Checkbox"
},
"complexType": {
"original": "FocusEvent",
"resolved": "FocusEvent",
"references": {
"FocusEvent": {
"location": "global",
"id": "global::FocusEvent"
}
}
}
}, {
"method": "tdsBlur",
"name": "tdsBlur",
"bubbles": true,
"cancelable": false,
"composed": true,
"docs": {
"tags": [],
"text": "Blur event for the Checkbox"
},
"complexType": {
"original": "FocusEvent",
"resolved": "FocusEvent",
"references": {
"FocusEvent": {
"location": "global",
"id": "global::FocusEvent"
}
}
}
}];
}
static get methods() {
return {
"toggleCheckbox": {
"complexType": {
"signature": "() => Promise<{ checkboxId: string; checked: boolean; }>",
"parameters": [],
"references": {
"Promise": {
"location": "global",
"id": "global::Promise"
}
},
"return": "Promise<{ checkboxId: string; checked: boolean; }>"
},
"docs": {
"text": "Toggles the checked value of the component.",
"tags": []
}
},
"focusElement": {
"complexType": {
"signature": "() => Promise<void>",
"parameters": [],
"references": {
"Promise": {
"location": "global",
"id": "global::Promise"
}
},
"return": "Promise<void>"
},
"docs": {
"text": "Method to programmatically focus the checkbox element",
"tags": []
}
}
};
}
static get elementRef() { return "host"; }
static get watchers() {
return [{
"propName": "indeterminate",
"methodName": "handleIndeterminateState"
}];
}
static get listeners() {
return [{
"name": "reset",
"method": "handleFormReset",
"target": "document",
"capture": false,
"passive": false
}];
}
}