/*! * All material copyright ESRI, All Rights Reserved, unless otherwise specified. * See https://github.com/Esri/calcite-components/blob/master/LICENSE.md for details. * v1.0.0-beta.82 */ import { Component, Element, Event, h, Host, Method, Prop } from "@stencil/core"; import { guid } from "../../utils/guid"; import { HiddenFormInputSlot } from "../../utils/form"; import { connectLabel, disconnectLabel, getLabelText } from "../../utils/label"; import { connectForm, disconnectForm } from "../../utils/form"; import { updateHostInteraction } from "../../utils/interactive"; import { toAriaBoolean } from "../../utils/dom"; export class Checkbox { constructor() { //-------------------------------------------------------------------------- // // Properties // //-------------------------------------------------------------------------- /** The checked state of the checkbox. */ this.checked = false; /** True if the checkbox is disabled */ this.disabled = false; /** * The hovered state of the checkbox. * @internal */ this.hovered = false; /** * True if the checkbox is initially indeterminate, * which is independent from its checked state * https://css-tricks.com/indeterminate-checkboxes/ * */ this.indeterminate = false; /** * When true, makes the component required for form-submission. * * @internal */ this.required = false; /** specify the scale of the checkbox, defaults to m */ this.scale = "m"; //-------------------------------------------------------------------------- // // Private Properties // //-------------------------------------------------------------------------- this.checkedPath = "M5.5 12L2 8.689l.637-.636L5.5 10.727l8.022-7.87.637.637z"; this.indeterminatePath = "M13 8v1H3V8z"; //-------------------------------------------------------------------------- // // Private Methods // //-------------------------------------------------------------------------- this.getPath = () => this.indeterminate ? this.indeterminatePath : this.checked ? this.checkedPath : ""; this.toggle = () => { if (!this.disabled) { this.checked = !this.checked; this.setFocus(); this.indeterminate = false; this.calciteCheckboxChange.emit(); } }; this.keyDownHandler = (event) => { if (event.key === " " || event.key === "Enter") { this.toggle(); event.preventDefault(); } }; this.clickHandler = () => { this.toggle(); }; //-------------------------------------------------------------------------- // // Event Listeners // //-------------------------------------------------------------------------- this.onToggleBlur = () => { this.calciteInternalCheckboxBlur.emit(false); }; this.onToggleFocus = () => { this.calciteInternalCheckboxFocus.emit(true); }; this.onLabelClick = () => { this.toggle(); }; } //-------------------------------------------------------------------------- // // Public Methods // //-------------------------------------------------------------------------- /** Sets focus on the component. */ async setFocus() { var _a; (_a = this.toggleEl) === null || _a === void 0 ? void 0 : _a.focus(); } //-------------------------------------------------------------------------- // // Lifecycle // //-------------------------------------------------------------------------- connectedCallback() { this.guid = this.el.id || `calcite-checkbox-${guid()}`; connectLabel(this); connectForm(this); } disconnectedCallback() { disconnectLabel(this); disconnectForm(this); } componentDidRender() { updateHostInteraction(this); } // -------------------------------------------------------------------------- // // Render Methods // // -------------------------------------------------------------------------- render() { return (h(Host, { onClick: this.clickHandler, onKeyDown: this.keyDownHandler }, h("div", { "aria-checked": toAriaBoolean(this.checked), "aria-label": getLabelText(this), class: "toggle", onBlur: this.onToggleBlur, onFocus: this.onToggleFocus, ref: (toggleEl) => (this.toggleEl = toggleEl), role: "checkbox", tabIndex: this.disabled ? undefined : 0 }, h("svg", { class: "check-svg", viewBox: "0 0 16 16" }, h("path", { d: this.getPath() })), h("slot", null)), h(HiddenFormInputSlot, { component: this }))); } static get is() { return "calcite-checkbox"; } static get encapsulation() { return "shadow"; } static get originalStyleUrls() { return { "$": ["checkbox.scss"] }; } static get styleUrls() { return { "$": ["checkbox.css"] }; } static get properties() { return { "checked": { "type": "boolean", "mutable": true, "complexType": { "original": "boolean", "resolved": "boolean", "references": {} }, "required": false, "optional": false, "docs": { "tags": [], "text": "The checked state of the checkbox." }, "attribute": "checked", "reflect": true, "defaultValue": "false" }, "disabled": { "type": "boolean", "mutable": false, "complexType": { "original": "boolean", "resolved": "boolean", "references": {} }, "required": false, "optional": false, "docs": { "tags": [], "text": "True if the checkbox is disabled" }, "attribute": "disabled", "reflect": true, "defaultValue": "false" }, "guid": { "type": "string", "mutable": true, "complexType": { "original": "string", "resolved": "string", "references": {} }, "required": false, "optional": false, "docs": { "tags": [], "text": "The id attribute of the checkbox. When omitted, a globally unique identifier is used." }, "attribute": "guid", "reflect": true }, "hovered": { "type": "boolean", "mutable": true, "complexType": { "original": "boolean", "resolved": "boolean", "references": {} }, "required": false, "optional": false, "docs": { "tags": [{ "name": "internal", "text": undefined }], "text": "The hovered state of the checkbox." }, "attribute": "hovered", "reflect": true, "defaultValue": "false" }, "indeterminate": { "type": "boolean", "mutable": true, "complexType": { "original": "boolean", "resolved": "boolean", "references": {} }, "required": false, "optional": false, "docs": { "tags": [], "text": "True if the checkbox is initially indeterminate,\nwhich is independent from its checked state\nhttps://css-tricks.com/indeterminate-checkboxes/" }, "attribute": "indeterminate", "reflect": true, "defaultValue": "false" }, "label": { "type": "string", "mutable": false, "complexType": { "original": "string", "resolved": "string", "references": {} }, "required": false, "optional": true, "docs": { "tags": [{ "name": "internal", "text": undefined }], "text": "The label of the checkbox input" }, "attribute": "label", "reflect": false }, "name": { "type": "any", "mutable": false, "complexType": { "original": "any", "resolved": "any", "references": {} }, "required": false, "optional": false, "docs": { "tags": [], "text": "The name of the checkbox input" }, "attribute": "name", "reflect": true }, "required": { "type": "boolean", "mutable": false, "complexType": { "original": "boolean", "resolved": "boolean", "references": {} }, "required": false, "optional": false, "docs": { "tags": [{ "name": "internal", "text": undefined }], "text": "When true, makes the component required for form-submission." }, "attribute": "required", "reflect": true, "defaultValue": "false" }, "scale": { "type": "string", "mutable": false, "complexType": { "original": "Scale", "resolved": "\"l\" | \"m\" | \"s\"", "references": { "Scale": { "location": "import", "path": "../interfaces" } } }, "required": false, "optional": false, "docs": { "tags": [], "text": "specify the scale of the checkbox, defaults to m" }, "attribute": "scale", "reflect": true, "defaultValue": "\"m\"" }, "value": { "type": "any", "mutable": false, "complexType": { "original": "any", "resolved": "any", "references": {} }, "required": false, "optional": false, "docs": { "tags": [], "text": "The value of the checkbox input" }, "attribute": "value", "reflect": false } }; } static get events() { return [{ "method": "calciteInternalCheckboxBlur", "name": "calciteInternalCheckboxBlur", "bubbles": true, "cancelable": true, "composed": true, "docs": { "tags": [{ "name": "internal", "text": undefined }], "text": "Emitted when the checkbox is blurred" }, "complexType": { "original": "any", "resolved": "any", "references": {} } }, { "method": "calciteCheckboxChange", "name": "calciteCheckboxChange", "bubbles": true, "cancelable": true, "composed": true, "docs": { "tags": [], "text": "Emitted when the checkbox checked status changes" }, "complexType": { "original": "any", "resolved": "any", "references": {} } }, { "method": "calciteInternalCheckboxFocus", "name": "calciteInternalCheckboxFocus", "bubbles": true, "cancelable": true, "composed": true, "docs": { "tags": [{ "name": "internal", "text": undefined }], "text": "Emitted when the checkbox is focused" }, "complexType": { "original": "any", "resolved": "any", "references": {} } }]; } static get methods() { return { "setFocus": { "complexType": { "signature": "() => Promise", "parameters": [], "references": { "Promise": { "location": "global" } }, "return": "Promise" }, "docs": { "text": "Sets focus on the component.", "tags": [] } } }; } static get elementRef() { return "el"; } }