UNPKG

@nysds/nys-textinput

Version:

The Textinput component from the NYS Design System.

1,101 lines (1,076 loc) 40.4 kB
import { css as lt, LitElement as ht, html as F } from "lit"; /*! * ▒█▄░▒█ ▒█░░▒█ ▒█▀▀▀█ ▒█▀▀▄ ▒█▀▀▀█ * ▒█▒█▒█ ▒█▄▄▄█ ░▀▀▀▄▄ ▒█░▒█ ░▀▀▀▄▄ * ▒█░░▀█ ░░▒█░░ ▒█▄▄▄█ ▒█▄▄▀ ▒█▄▄▄█ * * TextInput Component * Part of the New York State Design System * Repository: https://github.com/its-hcd/nysds * License: MIT */ /** * @license * Copyright 2019 Google LLC * SPDX-License-Identifier: BSD-3-Clause */ const I = globalThis, D = I.ShadowRoot && (I.ShadyCSS === void 0 || I.ShadyCSS.nativeShadow) && "adoptedStyleSheets" in Document.prototype && "replace" in CSSStyleSheet.prototype, it = Symbol(), Y = /* @__PURE__ */ new WeakMap(); let ut = class { constructor(t, e, s) { if (this._$cssResult$ = !0, s !== it) throw Error("CSSResult is not constructable. Use `unsafeCSS` or `css` instead."); this.cssText = t, this.t = e; } get styleSheet() { let t = this.o; const e = this.t; if (D && t === void 0) { const s = e !== void 0 && e.length === 1; s && (t = Y.get(e)), t === void 0 && ((this.o = t = new CSSStyleSheet()).replaceSync(this.cssText), s && Y.set(e, t)); } return t; } toString() { return this.cssText; } }; const dt = (o) => new ut(typeof o == "string" ? o : o + "", void 0, it), ct = (o, t) => { if (D) o.adoptedStyleSheets = t.map((e) => e instanceof CSSStyleSheet ? e : e.styleSheet); else for (const e of t) { const s = document.createElement("style"), i = I.litNonce; i !== void 0 && s.setAttribute("nonce", i), s.textContent = e.cssText, o.appendChild(s); } }, G = D ? (o) => o : (o) => o instanceof CSSStyleSheet ? ((t) => { let e = ""; for (const s of t.cssRules) e += s.cssText; return dt(e); })(o) : o; /** * @license * Copyright 2017 Google LLC * SPDX-License-Identifier: BSD-3-Clause */ const { is: pt, defineProperty: yt, getOwnPropertyDescriptor: _t, getOwnPropertyNames: ft, getOwnPropertySymbols: vt, getPrototypeOf: gt } = Object, m = globalThis, J = m.trustedTypes, bt = J ? J.emptyScript : "", V = m.reactiveElementPolyfillSupport, M = (o, t) => o, T = { toAttribute(o, t) { switch (t) { case Boolean: o = o ? bt : null; break; case Object: case Array: o = o == null ? o : JSON.stringify(o); } return o; }, fromAttribute(o, t) { let e = o; switch (t) { case Boolean: e = o !== null; break; case Number: e = o === null ? null : Number(o); break; case Object: case Array: try { e = JSON.parse(o); } catch { e = null; } } return e; } }, j = (o, t) => !pt(o, t), K = { attribute: !0, type: String, converter: T, reflect: !1, hasChanged: j }; Symbol.metadata ?? (Symbol.metadata = Symbol("metadata")), m.litPropertyMetadata ?? (m.litPropertyMetadata = /* @__PURE__ */ new WeakMap()); class E extends HTMLElement { static addInitializer(t) { this._$Ei(), (this.l ?? (this.l = [])).push(t); } static get observedAttributes() { return this.finalize(), this._$Eh && [...this._$Eh.keys()]; } static createProperty(t, e = K) { if (e.state && (e.attribute = !1), this._$Ei(), this.elementProperties.set(t, e), !e.noAccessor) { const s = Symbol(), i = this.getPropertyDescriptor(t, s, e); i !== void 0 && yt(this.prototype, t, i); } } static getPropertyDescriptor(t, e, s) { const { get: i, set: n } = _t(this.prototype, t) ?? { get() { return this[e]; }, set(r) { this[e] = r; } }; return { get() { return i == null ? void 0 : i.call(this); }, set(r) { const l = i == null ? void 0 : i.call(this); n.call(this, r), this.requestUpdate(t, l, s); }, configurable: !0, enumerable: !0 }; } static getPropertyOptions(t) { return this.elementProperties.get(t) ?? K; } static _$Ei() { if (this.hasOwnProperty(M("elementProperties"))) return; const t = gt(this); t.finalize(), t.l !== void 0 && (this.l = [...t.l]), this.elementProperties = new Map(t.elementProperties); } static finalize() { if (this.hasOwnProperty(M("finalized"))) return; if (this.finalized = !0, this._$Ei(), this.hasOwnProperty(M("properties"))) { const e = this.properties, s = [...ft(e), ...vt(e)]; for (const i of s) this.createProperty(i, e[i]); } const t = this[Symbol.metadata]; if (t !== null) { const e = litPropertyMetadata.get(t); if (e !== void 0) for (const [s, i] of e) this.elementProperties.set(s, i); } this._$Eh = /* @__PURE__ */ new Map(); for (const [e, s] of this.elementProperties) { const i = this._$Eu(e, s); i !== void 0 && this._$Eh.set(i, e); } this.elementStyles = this.finalizeStyles(this.styles); } static finalizeStyles(t) { const e = []; if (Array.isArray(t)) { const s = new Set(t.flat(1 / 0).reverse()); for (const i of s) e.unshift(G(i)); } else t !== void 0 && e.push(G(t)); return e; } static _$Eu(t, e) { const s = e.attribute; return s === !1 ? void 0 : typeof s == "string" ? s : typeof t == "string" ? t.toLowerCase() : void 0; } constructor() { super(), this._$Ep = void 0, this.isUpdatePending = !1, this.hasUpdated = !1, this._$Em = null, this._$Ev(); } _$Ev() { var t; this._$ES = new Promise((e) => this.enableUpdating = e), this._$AL = /* @__PURE__ */ new Map(), this._$E_(), this.requestUpdate(), (t = this.constructor.l) == null || t.forEach((e) => e(this)); } addController(t) { var e; (this._$EO ?? (this._$EO = /* @__PURE__ */ new Set())).add(t), this.renderRoot !== void 0 && this.isConnected && ((e = t.hostConnected) == null || e.call(t)); } removeController(t) { var e; (e = this._$EO) == null || e.delete(t); } _$E_() { const t = /* @__PURE__ */ new Map(), e = this.constructor.elementProperties; for (const s of e.keys()) this.hasOwnProperty(s) && (t.set(s, this[s]), delete this[s]); t.size > 0 && (this._$Ep = t); } createRenderRoot() { const t = this.shadowRoot ?? this.attachShadow(this.constructor.shadowRootOptions); return ct(t, this.constructor.elementStyles), t; } connectedCallback() { var t; this.renderRoot ?? (this.renderRoot = this.createRenderRoot()), this.enableUpdating(!0), (t = this._$EO) == null || t.forEach((e) => { var s; return (s = e.hostConnected) == null ? void 0 : s.call(e); }); } enableUpdating(t) { } disconnectedCallback() { var t; (t = this._$EO) == null || t.forEach((e) => { var s; return (s = e.hostDisconnected) == null ? void 0 : s.call(e); }); } attributeChangedCallback(t, e, s) { this._$AK(t, s); } _$EC(t, e) { var n; const s = this.constructor.elementProperties.get(t), i = this.constructor._$Eu(t, s); if (i !== void 0 && s.reflect === !0) { const r = (((n = s.converter) == null ? void 0 : n.toAttribute) !== void 0 ? s.converter : T).toAttribute(e, s.type); this._$Em = t, r == null ? this.removeAttribute(i) : this.setAttribute(i, r), this._$Em = null; } } _$AK(t, e) { var n; const s = this.constructor, i = s._$Eh.get(t); if (i !== void 0 && this._$Em !== i) { const r = s.getPropertyOptions(i), l = typeof r.converter == "function" ? { fromAttribute: r.converter } : ((n = r.converter) == null ? void 0 : n.fromAttribute) !== void 0 ? r.converter : T; this._$Em = i, this[i] = l.fromAttribute(e, r.type), this._$Em = null; } } requestUpdate(t, e, s) { if (t !== void 0) { if (s ?? (s = this.constructor.getPropertyOptions(t)), !(s.hasChanged ?? j)(this[t], e)) return; this.P(t, e, s); } this.isUpdatePending === !1 && (this._$ES = this._$ET()); } P(t, e, s) { this._$AL.has(t) || this._$AL.set(t, e), s.reflect === !0 && this._$Em !== t && (this._$Ej ?? (this._$Ej = /* @__PURE__ */ new Set())).add(t); } async _$ET() { this.isUpdatePending = !0; try { await this._$ES; } catch (e) { Promise.reject(e); } const t = this.scheduleUpdate(); return t != null && await t, !this.isUpdatePending; } scheduleUpdate() { return this.performUpdate(); } performUpdate() { var s; if (!this.isUpdatePending) return; if (!this.hasUpdated) { if (this.renderRoot ?? (this.renderRoot = this.createRenderRoot()), this._$Ep) { for (const [n, r] of this._$Ep) this[n] = r; this._$Ep = void 0; } const i = this.constructor.elementProperties; if (i.size > 0) for (const [n, r] of i) r.wrapped !== !0 || this._$AL.has(n) || this[n] === void 0 || this.P(n, this[n], r); } let t = !1; const e = this._$AL; try { t = this.shouldUpdate(e), t ? (this.willUpdate(e), (s = this._$EO) == null || s.forEach((i) => { var n; return (n = i.hostUpdate) == null ? void 0 : n.call(i); }), this.update(e)) : this._$EU(); } catch (i) { throw t = !1, this._$EU(), i; } t && this._$AE(e); } willUpdate(t) { } _$AE(t) { var e; (e = this._$EO) == null || e.forEach((s) => { var i; return (i = s.hostUpdated) == null ? void 0 : i.call(s); }), this.hasUpdated || (this.hasUpdated = !0, this.firstUpdated(t)), this.updated(t); } _$EU() { this._$AL = /* @__PURE__ */ new Map(), this.isUpdatePending = !1; } get updateComplete() { return this.getUpdateComplete(); } getUpdateComplete() { return this._$ES; } shouldUpdate(t) { return !0; } update(t) { this._$Ej && (this._$Ej = this._$Ej.forEach((e) => this._$EC(e, this[e]))), this._$EU(); } updated(t) { } firstUpdated(t) { } } E.elementStyles = [], E.shadowRootOptions = { mode: "open" }, E[M("elementProperties")] = /* @__PURE__ */ new Map(), E[M("finalized")] = /* @__PURE__ */ new Map(), V == null || V({ ReactiveElement: E }), (m.reactiveElementVersions ?? (m.reactiveElementVersions = [])).push("2.0.4"); /** * @license * Copyright 2017 Google LLC * SPDX-License-Identifier: BSD-3-Clause */ const mt = { attribute: !0, type: String, converter: T, reflect: !1, hasChanged: j }, $t = (o = mt, t, e) => { const { kind: s, metadata: i } = e; let n = globalThis.litPropertyMetadata.get(i); if (n === void 0 && globalThis.litPropertyMetadata.set(i, n = /* @__PURE__ */ new Map()), n.set(e.name, o), s === "accessor") { const { name: r } = e; return { set(l) { const a = t.get.call(this); t.set.call(this, l), this.requestUpdate(r, a, o); }, init(l) { return l !== void 0 && this.P(r, void 0, o), l; } }; } if (s === "setter") { const { name: r } = e; return function(l) { const a = this[r]; t.call(this, l), this.requestUpdate(r, a, o); }; } throw Error("Unsupported decorator location: " + s); }; function d(o) { return (t, e) => typeof e == "object" ? $t(o, t, e) : ((s, i, n) => { const r = i.hasOwnProperty(n); return i.constructor.createProperty(n, r ? { ...s, wrapped: !0 } : s), r ? Object.getOwnPropertyDescriptor(i, n) : void 0; })(o, t, e); } /** * @license * Copyright 2017 Google LLC * SPDX-License-Identifier: BSD-3-Clause */ function xt(o) { return d({ ...o, state: !0, attribute: !1 }); } const wt = lt` :host { /* Anything that can be overridden should be defined here */ /* Global Text Input Styles */ --_nys-textinput-width: 100%; --_nys-textinput-height: var(--nys-size-500, 40px); --_nys-textinput-radius: var(--nys-radius-md, 4px); --_nys-textinput-width-border: var(--nys-border-width-sm, 1px); --_nys-textinput-color-border: var(--nys-color-neutral-400, #909395); --_nys-textinput-text-color: var( --nys-color-text, var(--nys-color-neutral-900, #1b1b1b) ); --_nys-textinput-placeholder-color: var( --nys-color-text-weaker, var(--nys-color-neutral-500, #797c7f) ); --_nys-textinput-padding: var(--nys-space-100, 8px); --_nys-textinput-gap: var(--nys-space-50, 4px); --_nys-textinput-background-color: var( --nys-color-ink-reverse, var(--nys-color-white, #ffffff) ); --_nys-textinput-icon-color: var( --nys-color-ink, var(--nys-color-neutral-900, #1b1b1b) ); /* Hovered */ --_nys-textinput-hover-color-outline: var(--nys-color-neutral-900, #1b1b1b); --_nys-textinput-hover-width-outline: var(--nys-border-width-sm, 1px); /* Focused */ --_nys-textinput-focus-color-outline: var(--nys-color-focus, #004dd1); --_nys-textinput-focus-width-outline: var(--nys-border-width-sm, 1px); /* Disabled */ --_nys-textinput-disabled-color: var(--nys-color-neutral-10, #f6f6f6); --_nys-textinput-disabled-color-border: var( --nys-color-neutral-200, #bec0c1 ); --_nys-textinput-disabled-color-text: var( --nys-color-text-disabled, var(--nys-color-neutral-200, #bec0c1) ); /* Global Font Styles */ --_nys-textinput-family-ui: var( --nys-font-family-ui, var( --nys-font-family-sans, "Proxima Nova", "Helvetica Neue", "Helvetica", "Arial", sans-serif ) ); --_nys-textinput-size-ui-md: var(--nys-font-size-ui-md, 16px); --_nys-textinput-weight-ui: var(--nys-font-weight-regular, 400); --_nys-textinput-lineheight-ui: var(--nys-font-lineheight-ui-md, 24px); --nys-textinput-letterspacing-ui: var( --nys-font-letterspacing-ui-md, var(--nys-font-letterspacing-400, 0.044px) ); --_nys-textinput-color-ui: var(--nys-color-ink, #1b1b1b); } :host([width="sm"]) { --_nys-textinput-width: var(--nys-form-width-sm, 88px); } :host([width="md"]) { --_nys-textinput-width: var(--nys-form-width-md, 200px); } :host([width="lg"]) { --_nys-textinput-width: var(--nys-form-width-lg, 384px); } :host([width="full"]) { --_nys-textinput-width: 100%; flex: 1; /* stretches width for flex container */ } :host([showError]) { --_nys-textinput-color-border: var(--nys-color-danger, #b52c2c); } .nys-textinput { font-weight: var(--_nys-textinput-weight-ui); font-family: var(--_nys-textinput-family-ui); line-height: var(--_nys-textinput-lineheight-ui); letter-spacing: var(--nys-textinput-letterspacing-ui); color: var(--_nys-textinput-color-ui); gap: var(--_nys-textinput-gap); display: flex; flex-direction: column; } .nys-textinput__mask-overlay { position: absolute; margin: calc( var(--_nys-textinput-padding) + var(--_nys-textinput-width-border) ); color: var(--nys-color-text-weaker, #797c7f); display: inline; overflow: hidden; white-space: nowrap; width: stretch; width: -webkit-fill-available; width: -moz-available; font: inherit; letter-spacing: normal; } .nys-textinput__input { color: var(--_nys-textinput-text-color); border-radius: var(--_nys-textinput-radius); border: solid var(--_nys-textinput-color-border) var(--_nys-textinput-width-border); padding: var(--_nys-textinput-padding); width: 100%; height: var(--_nys-textinput-height); box-sizing: border-box; -webkit-box-sizing: border-box; -moz-box-sizing: border-box; background-color: transparent; position: relative; font: inherit; } .nys-textinput__input::placeholder { color: var(--_nys-textinput-placeholder-color); } .nys-textinput__buttoncontainer { width: var(--_nys-textinput-width); max-width: 100%; display: flex; } .nys-textinput__buttoncontainer.has-end-button .nys-textinput__input { border-top-right-radius: 0; border-bottom-right-radius: 0; border-right: none; } .nys-textinput__buttoncontainer.has-start-button .nys-textinput__input { border-top-left-radius: 0; border-bottom-left-radius: 0; border-left: none; } .nys-textinput__container { position: relative; display: flex; align-items: center; width: 100%; background-color: var(--_nys-textinput-background-color); border-radius: var(--_nys-textinput-radius); } ::slotted(nys-button) { --_nys-button-height: var(--_nys-textinput-height); --_nys-button-radius-left: var(--_nys-textinput-radius); --_nys-button-radius-right: var(--_nys-textinput-radius); --_nys-button-color-bg-disabled: var(--_nys-textinput-disabled-color); --_nys-button-color-border-disabled: var( --_nys-textinput-disabled-color-text ); --_nys-button-color-text-disabled: var( --_nys-textinput-disabled-color-text ); --_nys-button-width-border: var(--_nys-textinput-width-border); z-index: 1; /* to make sure the button outline renders on top of the input */ } .nys-textinput__buttoncontainer.has-start-button ::slotted(nys-button) { --_nys-button-radius-right: 0; } .nys-textinput__buttoncontainer.has-end-button ::slotted(nys-button) { --_nys-button-radius-left: 0; } .eye-icon { position: absolute; right: var(--nys-space-50, 4px); top: 50%; transform: translateY(-50%); cursor: pointer; color: var(--_nys-textinput-icon-color); --_nys-button-color-bg: var(--_nys-textinput-background-color); --_nys-button-color-bg-hover: var(--_nys-textinput-background-color); --_nys-button-color-bg-active: var(--_nys-textinput-background-color); --_nys-button-offset-focus: calc( var(--_nys-button-width-focus) * -1 ); /* Needs to be negative of the offset width */ --_nys-button-padding-y: var(--nys-space-50, 4px); --_nys-button-padding-x: var(--nys-space-50, 4px); --_nys-button-height: var(--nys-size-300, 32px); --_nys-button-width: var(--nys-size-400, 32px); } /* Hovered */ .nys-textinput__input:hover:not(:disabled):not(:focus) { outline: solid var(--_nys-textinput-hover-width-outline) var(--_nys-textinput-hover-color-outline); border-color: var(--_nys-textinput-hover-color-outline); } /* Focused */ .nys-textinput__input:focus { outline: solid var(--_nys-textinput-focus-width-outline) var(--_nys-textinput-focus-color-outline); border-color: var(--_nys-textinput-focus-color-outline); caret-color: var(--_nys-textinput-focus-color-outline); } /* Disabled */ .nys-textinput__input:disabled, .nys-textinput__input:disabled::placeholder, .nys-textinput__input:disabled + .eye-icon { background-color: var(--_nys-textinput-disabled-color); border-color: var(--_nys-textinput-disabled-color-border); color: var(--_nys-textinput-disabled-color-text); cursor: not-allowed; } `; /** * @license * Copyright 2017 Google LLC * SPDX-License-Identifier: BSD-3-Clause */ const C = globalThis, R = C.trustedTypes, Z = R ? R.createPolicy("lit-html", { createHTML: (o) => o }) : void 0, nt = "$lit$", b = `lit$${Math.random().toFixed(9).slice(2)}$`, rt = "?" + b, At = `<${rt}>`, A = document, N = () => A.createComment(""), U = (o) => o === null || typeof o != "object" && typeof o != "function", W = Array.isArray, St = (o) => W(o) || typeof (o == null ? void 0 : o[Symbol.iterator]) == "function", q = `[ \f\r]`, P = /<(?:(!--|\/[^a-zA-Z])|(\/?[a-zA-Z][^>\s]*)|(\/?$))/g, Q = /-->/g, X = />/g, $ = RegExp(`>|${q}(?:([^\\s"'>=/]+)(${q}*=${q}*(?:[^ \f\r"'\`<>=]|("|')|))|$)`, "g"), tt = /'/g, et = /"/g, ot = /^(?:script|style|textarea|title)$/i, k = Symbol.for("lit-noChange"), y = Symbol.for("lit-nothing"), st = /* @__PURE__ */ new WeakMap(), w = A.createTreeWalker(A, 129); function at(o, t) { if (!W(o) || !o.hasOwnProperty("raw")) throw Error("invalid template strings array"); return Z !== void 0 ? Z.createHTML(t) : t; } const Et = (o, t) => { const e = o.length - 1, s = []; let i, n = t === 2 ? "<svg>" : t === 3 ? "<math>" : "", r = P; for (let l = 0; l < e; l++) { const a = o[l]; let p, _, h = -1, v = 0; for (; v < a.length && (r.lastIndex = v, _ = r.exec(a), _ !== null); ) v = r.lastIndex, r === P ? _[1] === "!--" ? r = Q : _[1] !== void 0 ? r = X : _[2] !== void 0 ? (ot.test(_[2]) && (i = RegExp("</" + _[2], "g")), r = $) : _[3] !== void 0 && (r = $) : r === $ ? _[0] === ">" ? (r = i ?? P, h = -1) : _[1] === void 0 ? h = -2 : (h = r.lastIndex - _[2].length, p = _[1], r = _[3] === void 0 ? $ : _[3] === '"' ? et : tt) : r === et || r === tt ? r = $ : r === Q || r === X ? r = P : (r = $, i = void 0); const g = r === $ && o[l + 1].startsWith("/>") ? " " : ""; n += r === P ? a + At : h >= 0 ? (s.push(p), a.slice(0, h) + nt + a.slice(h) + b + g) : a + b + (h === -2 ? l : g); } return [at(o, n + (o[e] || "<?>") + (t === 2 ? "</svg>" : t === 3 ? "</math>" : "")), s]; }; class H { constructor({ strings: t, _$litType$: e }, s) { let i; this.parts = []; let n = 0, r = 0; const l = t.length - 1, a = this.parts, [p, _] = Et(t, e); if (this.el = H.createElement(p, s), w.currentNode = this.el.content, e === 2 || e === 3) { const h = this.el.content.firstChild; h.replaceWith(...h.childNodes); } for (; (i = w.nextNode()) !== null && a.length < l; ) { if (i.nodeType === 1) { if (i.hasAttributes()) for (const h of i.getAttributeNames()) if (h.endsWith(nt)) { const v = _[r++], g = i.getAttribute(h).split(b), O = /([.?@])?(.*)/.exec(v); a.push({ type: 1, index: n, name: O[2], strings: g, ctor: O[1] === "." ? Mt : O[1] === "?" ? Ct : O[1] === "@" ? Ut : L }), i.removeAttribute(h); } else h.startsWith(b) && (a.push({ type: 6, index: n }), i.removeAttribute(h)); if (ot.test(i.tagName)) { const h = i.textContent.split(b), v = h.length - 1; if (v > 0) { i.textContent = R ? R.emptyScript : ""; for (let g = 0; g < v; g++) i.append(h[g], N()), w.nextNode(), a.push({ type: 2, index: ++n }); i.append(h[v], N()); } } } else if (i.nodeType === 8) if (i.data === rt) a.push({ type: 2, index: n }); else { let h = -1; for (; (h = i.data.indexOf(b, h + 1)) !== -1; ) a.push({ type: 7, index: n }), h += b.length - 1; } n++; } } static createElement(t, e) { const s = A.createElement("template"); return s.innerHTML = t, s; } } function S(o, t, e = o, s) { var r, l; if (t === k) return t; let i = s !== void 0 ? (r = e._$Co) == null ? void 0 : r[s] : e._$Cl; const n = U(t) ? void 0 : t._$litDirective$; return (i == null ? void 0 : i.constructor) !== n && ((l = i == null ? void 0 : i._$AO) == null || l.call(i, !1), n === void 0 ? i = void 0 : (i = new n(o), i._$AT(o, e, s)), s !== void 0 ? (e._$Co ?? (e._$Co = []))[s] = i : e._$Cl = i), i !== void 0 && (t = S(o, i._$AS(o, t.values), i, s)), t; } class Pt { constructor(t, e) { this._$AV = [], this._$AN = void 0, this._$AD = t, this._$AM = e; } get parentNode() { return this._$AM.parentNode; } get _$AU() { return this._$AM._$AU; } u(t) { const { el: { content: e }, parts: s } = this._$AD, i = ((t == null ? void 0 : t.creationScope) ?? A).importNode(e, !0); w.currentNode = i; let n = w.nextNode(), r = 0, l = 0, a = s[0]; for (; a !== void 0; ) { if (r === a.index) { let p; a.type === 2 ? p = new B(n, n.nextSibling, this, t) : a.type === 1 ? p = new a.ctor(n, a.name, a.strings, this, t) : a.type === 6 && (p = new kt(n, this, t)), this._$AV.push(p), a = s[++l]; } r !== (a == null ? void 0 : a.index) && (n = w.nextNode(), r++); } return w.currentNode = A, i; } p(t) { let e = 0; for (const s of this._$AV) s !== void 0 && (s.strings !== void 0 ? (s._$AI(t, s, e), e += s.strings.length - 2) : s._$AI(t[e])), e++; } } class B { get _$AU() { var t; return ((t = this._$AM) == null ? void 0 : t._$AU) ?? this._$Cv; } constructor(t, e, s, i) { this.type = 2, this._$AH = y, this._$AN = void 0, this._$AA = t, this._$AB = e, this._$AM = s, this.options = i, this._$Cv = (i == null ? void 0 : i.isConnected) ?? !0; } get parentNode() { let t = this._$AA.parentNode; const e = this._$AM; return e !== void 0 && (t == null ? void 0 : t.nodeType) === 11 && (t = e.parentNode), t; } get startNode() { return this._$AA; } get endNode() { return this._$AB; } _$AI(t, e = this) { t = S(this, t, e), U(t) ? t === y || t == null || t === "" ? (this._$AH !== y && this._$AR(), this._$AH = y) : t !== this._$AH && t !== k && this._(t) : t._$litType$ !== void 0 ? this.$(t) : t.nodeType !== void 0 ? this.T(t) : St(t) ? this.k(t) : this._(t); } O(t) { return this._$AA.parentNode.insertBefore(t, this._$AB); } T(t) { this._$AH !== t && (this._$AR(), this._$AH = this.O(t)); } _(t) { this._$AH !== y && U(this._$AH) ? this._$AA.nextSibling.data = t : this.T(A.createTextNode(t)), this._$AH = t; } $(t) { var n; const { values: e, _$litType$: s } = t, i = typeof s == "number" ? this._$AC(t) : (s.el === void 0 && (s.el = H.createElement(at(s.h, s.h[0]), this.options)), s); if (((n = this._$AH) == null ? void 0 : n._$AD) === i) this._$AH.p(e); else { const r = new Pt(i, this), l = r.u(this.options); r.p(e), this.T(l), this._$AH = r; } } _$AC(t) { let e = st.get(t.strings); return e === void 0 && st.set(t.strings, e = new H(t)), e; } k(t) { W(this._$AH) || (this._$AH = [], this._$AR()); const e = this._$AH; let s, i = 0; for (const n of t) i === e.length ? e.push(s = new B(this.O(N()), this.O(N()), this, this.options)) : s = e[i], s._$AI(n), i++; i < e.length && (this._$AR(s && s._$AB.nextSibling, i), e.length = i); } _$AR(t = this._$AA.nextSibling, e) { var s; for ((s = this._$AP) == null ? void 0 : s.call(this, !1, !0, e); t && t !== this._$AB; ) { const i = t.nextSibling; t.remove(), t = i; } } setConnected(t) { var e; this._$AM === void 0 && (this._$Cv = t, (e = this._$AP) == null || e.call(this, t)); } } class L { get tagName() { return this.element.tagName; } get _$AU() { return this._$AM._$AU; } constructor(t, e, s, i, n) { this.type = 1, this._$AH = y, this._$AN = void 0, this.element = t, this.name = e, this._$AM = i, this.options = n, s.length > 2 || s[0] !== "" || s[1] !== "" ? (this._$AH = Array(s.length - 1).fill(new String()), this.strings = s) : this._$AH = y; } _$AI(t, e = this, s, i) { const n = this.strings; let r = !1; if (n === void 0) t = S(this, t, e, 0), r = !U(t) || t !== this._$AH && t !== k, r && (this._$AH = t); else { const l = t; let a, p; for (t = n[0], a = 0; a < n.length - 1; a++) p = S(this, l[s + a], e, a), p === k && (p = this._$AH[a]), r || (r = !U(p) || p !== this._$AH[a]), p === y ? t = y : t !== y && (t += (p ?? "") + n[a + 1]), this._$AH[a] = p; } r && !i && this.j(t); } j(t) { t === y ? this.element.removeAttribute(this.name) : this.element.setAttribute(this.name, t ?? ""); } } class Mt extends L { constructor() { super(...arguments), this.type = 3; } j(t) { this.element[this.name] = t === y ? void 0 : t; } } class Ct extends L { constructor() { super(...arguments), this.type = 4; } j(t) { this.element.toggleAttribute(this.name, !!t && t !== y); } } class Ut extends L { constructor(t, e, s, i, n) { super(t, e, s, i, n), this.type = 5; } _$AI(t, e = this) { if ((t = S(this, t, e, 0) ?? y) === k) return; const s = this._$AH, i = t === y && s !== y || t.capture !== s.capture || t.once !== s.once || t.passive !== s.passive, n = t !== y && (s === y || i); i && this.element.removeEventListener(this.name, this, s), n && this.element.addEventListener(this.name, this, t), this._$AH = t; } handleEvent(t) { var e; typeof this._$AH == "function" ? this._$AH.call(((e = this.options) == null ? void 0 : e.host) ?? this.element, t) : this._$AH.handleEvent(t); } } class kt { constructor(t, e, s) { this.element = t, this.type = 6, this._$AN = void 0, this._$AM = e, this.options = s; } get _$AU() { return this._$AM._$AU; } _$AI(t) { S(this, t); } } const z = C.litHtmlPolyfillSupport; z == null || z(H, B), (C.litHtmlVersions ?? (C.litHtmlVersions = [])).push("3.2.1"); /** * @license * Copyright 2018 Google LLC * SPDX-License-Identifier: BSD-3-Clause */ const x = (o) => o ?? y; var Ht = Object.defineProperty, Ot = Object.getOwnPropertyDescriptor, c = (o, t, e, s) => { for (var i = s > 1 ? void 0 : s ? Ot(t, e) : t, n = o.length - 1, r; n >= 0; n--) (r = o[n]) && (i = (s ? r(t, e, i) : r(i)) || i); return s && i && Ht(t, e, i), i; }; let It = 0; var f; const u = (f = class extends ht { // allows use of elementInternals' API constructor() { super(), this.id = "", this.name = "", this._type = "text", this.label = "", this.description = "", this.placeholder = "", this.value = "", this.disabled = !1, this.readonly = !1, this.required = !1, this.optional = !1, this.form = "", this.pattern = "", this.maxlength = null, this.width = "full", this.step = null, this.min = null, this.max = null, this.showError = !1, this.errorMessage = "", this.showPassword = !1, this._originalErrorMessage = "", this._hasUserInteracted = !1, this._maskPatterns = { tel: "(___) ___-____" }, this._internals = this.attachInternals(); } get type() { return this._type; } set type(t) { this._type = f.VALID_TYPES.includes( t ) ? t : "text"; } // Ensure the "width" property is valid after updates async updated(t) { var e, s; if (t.has("width") && (await Promise.resolve(), this.width = f.VALID_WIDTHS.includes(this.width) ? this.width : "full"), t.has("disabled") && (this._validateButtonSlot("startButton"), this._validateButtonSlot("endButton")), t.has("type")) { const i = this._maskPatterns[this.type], n = (e = this.shadowRoot) == null ? void 0 : e.querySelector("input"); if (n) if (i) n.maxLength = i.length, this._updateOverlay(n.value, i); else { n.removeAttribute("maxLength"); const r = (s = this.shadowRoot) == null ? void 0 : s.querySelector( ".nys-textinput__mask-overlay" ); r && (r.textContent = ""); } } } // Generate a unique ID if one is not provided connectedCallback() { super.connectedCallback(), this.id || (this.id = `nys-textinput-${Date.now()}-${It++}`), this._originalErrorMessage = this.errorMessage ?? "", this.addEventListener("invalid", this._handleInvalid); } disconnectedCallback() { super.disconnectedCallback(), this.removeEventListener("invalid", this._handleInvalid); } firstUpdated() { this._setValue(); } // This callback is automatically called when the parent form is reset. formResetCallback() { this.value = ""; } /********************** Form Integration **********************/ _setValue() { this._internals.setFormValue(this.value), this._manageRequire(); } _manageRequire() { var i, n; const t = (i = this.shadowRoot) == null ? void 0 : i.querySelector("input"); if (!t) return; const e = this.errorMessage || "This field is required"; this.required && (!this.value || ((n = this.value) == null ? void 0 : n.trim()) === "") ? (this._internals.ariaRequired = "true", this._internals.setValidity({ valueMissing: !0 }, e, t)) : (this._internals.ariaRequired = "false", this._internals.setValidity({}), this._hasUserInteracted = !1); } _setValidityMessage(t = "") { var i, n; const e = (i = this.shadowRoot) == null ? void 0 : i.querySelector("input"); if (!e) return; this.showError = !!t, (n = this._originalErrorMessage) != null && n.trim() && t !== "" ? this.errorMessage = this._originalErrorMessage : this.errorMessage = t; const s = t ? { customError: !0 } : {}; this._internals.setValidity(s, this.errorMessage, e); } _validate() { var i; const t = (i = this.shadowRoot) == null ? void 0 : i.querySelector("input"); if (!t) return; const e = t.validity; let s = ""; e.valueMissing ? s = "This field is required" : e.typeMismatch ? s = "Invalid format for this type" : e.patternMismatch ? s = "Invalid format" : e.tooShort ? s = `Value is too short. Minimum length is ${t.minLength}` : e.tooLong ? s = `Value is too long. Maximum length is ${t.maxLength}` : e.rangeUnderflow ? s = `Value must be at least ${t.min}` : e.rangeOverflow ? s = `Value must be at most ${t.max}` : e.stepMismatch ? s = "Invalid step value" : s = t.validationMessage, this._setValidityMessage(s); } /********************** Functions **********************/ // This helper function is called to perform the element's native validation. checkValidity() { var e; const t = (e = this.shadowRoot) == null ? void 0 : e.querySelector("input"); return t ? t.checkValidity() : !0; } _handleInvalid(t) { var s; t.preventDefault(), this._hasUserInteracted = !0, this._validate(); const e = (s = this.shadowRoot) == null ? void 0 : s.querySelector("input"); if (e) { const i = this._internals.form; i ? Array.from(i.elements).find( (l) => typeof l.checkValidity == "function" && !l.checkValidity() ) === this && e.focus() : e.focus(); } } _togglePasswordVisibility() { this.showPassword = !this.showPassword; } _updateOverlay(t, e) { var r; const s = (r = this.shadowRoot) == null ? void 0 : r.querySelector( ".nys-textinput__mask-overlay" ); if (!s) return; const i = t, n = e.slice(i.length); s.textContent = i + n; } _applyMask(t, e) { const s = t.replace(/\D/g, ""); let i = ""; if (this.type === "tel") return s.length > 0 && (i = "(" + s.substring(0, 3)), s.length >= 4 && (i += ") " + s.substring(3, 6)), s.length > 6 && (i += "-" + s.substring(6, 10)), i; let n = 0; for (let r = 0; r < e.length; r++) if (e[r] === "_" || e[r].match(/[d9]/i)) if (n < s.length) i += s[n++]; else break; else i += e[r]; return i; } /******************** Event Handlers ********************/ // Handle input event to check pattern validity _handleInput(t) { const e = t.target; let s = e.value; const i = this._maskPatterns[this.type]; i && (s = this._applyMask(s, i), e.value = s, this._updateOverlay(s, i)), this.value = s, this._internals.setFormValue(this.value), this._hasUserInteracted && this._validate(), this.dispatchEvent( new CustomEvent("nys-input", { detail: { value: this.value }, bubbles: !0, composed: !0 }) ); } // Handle focus event _handleFocus() { this.dispatchEvent(new Event("nys-focus")); } // Handle blur event _handleBlur() { this._hasUserInteracted || (this._hasUserInteracted = !0), this._validate(), this.dispatchEvent(new Event("nys-blur")); } _validateButtonSlot(t) { var r, l; const e = (r = this.shadowRoot) == null ? void 0 : r.querySelector( 'slot[name="' + t + '"]' ), s = (l = this.shadowRoot) == null ? void 0 : l.querySelector( ".nys-textinput__buttoncontainer" ); if (!e || !s) return; const i = e.assignedElements(); let n = !1; i.forEach((a) => { a instanceof HTMLElement && a.tagName.toLowerCase() === "nys-button" && !n ? (n = !0, a.setAttribute("size", "sm"), a.setAttribute("variant", "primary"), this.disabled ? a.setAttribute("disabled", "true") : a.removeAttribute("disabled")) : (console.warn( "The '" + t + "' slot only accepts a single <nys-button> element. Removing invalid or extra node:", a ), a.remove()); }), t === "startButton" ? s.classList.toggle("has-start-button", n) : t === "endButton" && s.classList.toggle("has-end-button", n); } render() { return F` <div class="nys-textinput"> <nys-label for=${this.id} label=${this.label} description=${this.description} flag=${this.required ? "required" : this.optional ? "optional" : ""} > <slot name="description" slot="description">${this.description}</slot> </nys-label> <div class="nys-textinput__buttoncontainer"> <slot name="startButton" @slotchange=${this._validateButtonSlot("startButton")} ></slot> <div class="nys-textinput__container"> <span class="nys-textinput__mask-overlay"></span> <input class="nys-textinput__input" type=${this.type === "password" ? this.showPassword ? "text" : "password" : this.type} name=${this.name} id=${this.id} ?disabled=${this.disabled} ?required=${this.required} ?readonly=${this.readonly} aria-required=${this.required} aria-disabled="${this.disabled}" aria-label="${[this.label, this.description].filter(Boolean).join(" ")}" .value=${this.value} placeholder=${x( this.placeholder ? this.placeholder : void 0 )} pattern=${x(this.pattern ? this.pattern : void 0)} min=${x(this.min !== "" ? this.min : void 0)} maxlength=${x( this.maxlength !== "" ? this.maxlength : void 0 )} step=${x(this.step !== "" ? this.step : void 0)} max=${x(this.max !== "" ? this.max : void 0)} form=${x(this.form ? this.form : void 0)} @input=${this._handleInput} @focus="${this._handleFocus}" @blur="${this._handleBlur}" /> ${this.type === "password" ? F` <nys-button class="eye-icon" id="password-toggle" suffixIcon="slotted" ariaLabel="password toggle" .onClick=${() => !this.disabled && this._togglePasswordVisibility()} variant="ghost" size="sm" > <nys-icon slot="suffix-icon" size="2xl" name=${this.showPassword ? "visibility_off" : "visibility"} ></nys-icon> </nys-button>` : ""} </div> <slot name="endButton" @slotchange=${this._validateButtonSlot("endButton")} ></slot> </div> <nys-errormessage ?showError=${this.showError} errorMessage=${this.errorMessage} ></nys-errormessage> </div> `; } }, f.VALID_TYPES = [ "email", "number", "password", "search", "tel", "text", "url" ], f.VALID_WIDTHS = ["sm", "md", "lg", "full"], f.styles = wt, f.formAssociated = !0, f); c([ d({ type: String }) ], u.prototype, "id", 2); c([ d({ type: String, reflect: !0 }) ], u.prototype, "name", 2); c([ d({ reflect: !0 }) ], u.prototype, "type", 1); c([ d({ type: String }) ], u.prototype, "label", 2); c([ d({ type: String }) ], u.prototype, "description", 2); c([ d({ type: String }) ], u.prototype, "placeholder", 2); c([ d({ type: String }) ], u.prototype, "value", 2); c([ d({ type: Boolean, reflect: !0 }) ], u.prototype, "disabled", 2); c([ d({ type: Boolean, reflect: !0 }) ], u.prototype, "readonly", 2); c([ d({ type: Boolean, reflect: !0 }) ], u.prototype, "required", 2); c([ d({ type: Boolean, reflect: !0 }) ], u.prototype, "optional", 2); c([ d({ type: String }) ], u.prototype, "form", 2); c([ d({ type: String }) ], u.prototype, "pattern", 2); c([ d({ type: Number }) ], u.prototype, "maxlength", 2); c([ d({ reflect: !0 }) ], u.prototype, "width", 2); c([ d({ type: Number }) ], u.prototype, "step", 2); c([ d({ type: Number }) ], u.prototype, "min", 2); c([ d({ type: Number }) ], u.prototype, "max", 2); c([ d({ type: Boolean, reflect: !0 }) ], u.prototype, "showError", 2); c([ d({ type: String }) ], u.prototype, "errorMessage", 2); c([ xt() ], u.prototype, "showPassword", 2); let Tt = u; customElements.get("nys-textinput") || customElements.define("nys-textinput", Tt); export { Tt as NysTextinput }; //# sourceMappingURL=nys-textinput.js.map