UNPKG

jb-national-input

Version:
48 lines (47 loc) 1.86 kB
import { JBInputWebComponent as t } from "jb-input"; import { faToEnDigits as n } from "jb-core"; import { JBDictionary as e, i18n as i } from "jb-core/i18n"; const a = new e({ fa: { invalidValue: "کد ملی وارد شده نامعتبر است" }, en: { invalidValue: "The entered national code is invalid" } }); var o = class extends t { constructor() { super(), this.initMobileInputWebComponent(); } initMobileInputWebComponent() { const t = document.createElement("template"); t.innerHTML = "<style>:host{--input-direction:var(--jb-national-input-input-direction,ltr)}</style><style>.jb-input-web-component{--jb-input-input-direction:var(--input-direction)}</style>", this.shadowRoot.appendChild(t.content.cloneNode(!0)), this.validation.addValidationListGetter(this.#t.bind(this)), this.#n(), this.addStandardValueCallback(this.#e.bind(this)); } #e(t) { let e = "", i = ""; const a = /(?<nationalId>[\u06F0-\u06F90-9]{1,10})/g.exec(t); return e = a?.groups ? a.groups.nationalId : "", i = n(e), { value: i, displayValue: e }; } #n() { this.elements.input.addEventListener("beforeinput", this.#i.bind(this)); } #i(t) { const n = t.data; /[\u06F0-\u06F90-9]{1,10}/g.test(n ?? "") || "deleteContentBackward" == t.inputType || t.preventDefault(); } #t() { return [{ validator: ({ value: t }) => function(t) { if (0 === t.length) return !0; const e = n(t); if (!/^\d{10}$/.test(e)) return !1; const i = +e[9], a = e.split("").slice(0, 9).reduce((t, n, e) => t + +n * (10 - e), 0) % 11; return a < 2 ? i === a : i + a === 11; }(t), message: a.get(i, "invalidValue"), stateType: "badInput" }]; } }; customElements.get("jb-national-input") || window.customElements.define("jb-national-input", o); export { o as JBNationalInputWebComponent }; //# sourceMappingURL=jb-national-input.js.map