UNPKG

@randevcx/ranui

Version:

UI Component library based on `Web Component`

104 lines (103 loc) 3.25 kB
var __defProp = Object.defineProperty; var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value; var __publicField = (obj, key, value) => { __defNormalProp(obj, typeof key !== "symbol" ? key + "" : key, value); return value; }; import { a as addClassToElement, r as removeClassToElement } from "./utils-74Icp-PI.js"; import { H as HTMLElementSSR, f as falseList, c as createCustomError } from "./index-CSnBqUsQ.js"; class Checkbox extends HTMLElementSSR() { constructor() { super(); __publicField(this, "checkInput"); __publicField(this, "checkInner"); __publicField(this, "context"); __publicField(this, "updateChecked", () => { const { checked } = this.context; if (checked) { addClassToElement(this, "ran-checkbox-checked"); } else { removeClassToElement(this, "ran-checkbox-checked"); } }); __publicField(this, "update", () => { this.updateChecked(); }); __publicField(this, "onChange", () => { const { checked } = this.context; this.context.checked = !checked; this.dispatchEvent( new CustomEvent("change", { detail: { checked: this.context.checked } }) ); this.update(); }); this.checkInput = document.createElement("input"); this.checkInput.setAttribute("class", "ran-checkbox-input"); this.checkInput.setAttribute("type", "checkbox"); this.checkInner = document.createElement("span"); this.checkInner.setAttribute("class", "ran-checkbox-inner"); this.context = { checked: false }; } static get observedAttributes() { return ["disabled", "checked"]; } get disabled() { return this.getAttribute("disabled") || ""; } set disabled(value) { this.setAttribute("disabled", value); } get checked() { const checked = this.getAttribute("checked"); if (falseList.includes(checked)) { this.context.checked = false; } return `${this.context.checked}`; } set checked(value) { if (falseList.includes(value)) { this.setAttribute("checked", "false"); this.context.checked = false; } else { this.setAttribute("checked", "true"); this.context.checked = true; } this.updateChecked(); } connectedCallback() { this.setAttribute("class", "ran-checkbox"); this.appendChild(this.checkInput); this.appendChild(this.checkInner); this.addEventListener("click", this.onChange); } disconnectCallback() { this.removeEventListener("click", this.onChange); } attributeChangedCallback(name, oldValue, newValue) { } } function Custom() { if (typeof document !== "undefined" && !customElements.get("r-checkbox")) { customElements.define("r-checkbox", Checkbox); return Checkbox; } else { return createCustomError("document is undefined or r-checkbox is exist"); } } const index = Custom(); const index$1 = /* @__PURE__ */ Object.freeze(/* @__PURE__ */ Object.defineProperty({ __proto__: null, Checkbox, default: index }, Symbol.toStringTag, { value: "Module" })); export { Checkbox as C, index as a, index$1 as i };