UNPKG

@halo-lab/vue-form-hubspot

Version:

Vue custom form integration with the Hubspot service

802 lines (801 loc) 22.5 kB
import { onBeforeMount as Y, computed as F, reactive as Z, provide as j, openBlock as m, createElementBlock as f, normalizeClass as c, withModifiers as W, renderSlot as q, toDisplayString as I, ref as T, resolveComponent as p, Fragment as B, createCommentVNode as O, createVNode as S, inject as M, withDirectives as k, createElementVNode as y, vModelDynamic as K, vModelText as H, createTextVNode as J, renderList as V, vModelRadio as X, vModelCheckbox as $ } from "vue"; const ee = (a, t) => a ? "" : `It seems like you didn’t fill in the field. Please, write your ${t} to continue`, te = (a, t) => /^\w+([.-]?\w+)*@\w+([.-]?\w+)*(\.\w{2,3})+$/.test(a) ? "" : `It seems like your ${t} is not valid. Please, enter valid ${t} to continue`, le = (a, t) => /^[0-9]+$/.test(a) ? "" : `It seems like your ${t} is not valid. Your ${t} must contain only numbers`, ae = (a, t) => /^([a-zA-Z]*)$/.test(a) ? "" : `It seems like your ${t} is not valid. Please, enter ${t} with only alphabetical characters to continue`, ne = (a, t, e) => !isNaN(a) && +a > +e ? `It seems like your ${t} is not valid. Your ${t} must be less than ${e}` : "", se = (a, t, e) => !isNaN(a) && +a < +e ? `It seems like your ${t} is not valid. Your ${t} must be at least ${e}` : "", re = (a, t, e) => e && a.length < e ? `It seems like your ${t} is not valid. Your ${t} must contain at least ${e} charackters` : "", ie = (a, t, e) => e && a.length > e ? `It seems like your ${t} is not valid. Your ${t} must contain less charackters than ${e}` : "", oe = (a, t, e) => e.test(a) ? "" : `It seems like you fill in the ${t} field incorrectly. Please, check it to continue`, G = { required: ee, email: te, number: le, letters: ae, min: se, max: ne, minLength: re, maxLength: ie, regexp: oe }, ue = (a, t, e, l) => a === "func" ? l(e) : G[a] ? G[a](e, t, l) : "", de = (a, t, e, l, s) => { Y(() => { e(a, t), s[a] = t ?? void 0; }); const r = F({ get: () => l(a) ?? t, set: (d) => { e(a, d); } }); return { value: r, validate: (d, C) => { if (d) for (const N of d) { const { name: h, message: _, value: o } = N; let b = ue(h, a, r.value, o); if (b) return h === "func" && (b = _), C(a, b), b; } }, fileValidate: (d, C, N) => { if (!d) return; const h = d.name.toLowerCase().split(".").slice(-1)[0], _ = C.map((o) => o.toLowerCase()).includes(h) ? "" : "This document is not supported, please delete and upload another file."; return _ && N(a, _), _; }, resetError: (d) => { d(a, ""); } }; }, ce = (a, t) => { const e = Z({}), l = {}, s = Z({}), r = {}, u = (o) => e[o], i = (o, b) => { e[o] = b; }, n = (o) => s[o], d = (o, b) => { s[o] = b; }, C = () => Object.values(s).some((o) => !!o), N = (o, b, x) => { const { value: A, validate: D, resetError: E, fileValidate: U } = de( o, b, i, u, l ), g = () => D(x, d), L = () => E(d), P = (Q, R) => U(Q, R, d); return r[o] = o.includes("[]") ? P : g, { value: A, validate: g, resetError: L, fileFieldValidate: P }; }, h = () => { Object.keys(e).forEach( (o) => i(o, l[o]) ), Object.keys(s).forEach((o) => s[o] = void 0); }; return { registerField: N, getFieldError: n, onSubmit: async (o) => { for (const D in r) if (r[D]()) return; if (o) return o({ ...e }); if (!t) throw new Error( 'You did not provided "portalId" and "formId" or submit callback function. Please, provide either' ); const b = `https://api.hsforms.com/submissions/v3/integration/submit/${a}/${t}`, x = []; for (let [D, E] of Object.entries(e)) Array.isArray(E) && (E = E.join("; ")), x.push({ name: D, value: E }); const A = JSON.stringify({ fields: x }); try { await fetch(b, { method: "POST", body: A, headers: { Accept: "application/json", "Content-Type": "application/json" } }); } catch (D) { console.log(D); } finally { h(); } }, hasFormErrors: C, setFieldError: d }; }; const v = (a, t) => { const e = a.__vccOpts || a; for (const [l, s] of t) e[l] = s; return e; }, me = { props: { hubspotPortalId: { type: String, default: "" }, hubspotFormId: { type: String, default: "" }, className: { type: String, default: "" }, submitHandler: { type: Function, default: null } }, setup(a) { const { hubspotPortalId: t, hubspotFormId: e } = a, { registerField: l, onSubmit: s, hasFormErrors: r, getFieldError: u, setFieldError: i } = ce(t, e); return j("registerField", l), j("getFieldError", u), j("hasFormErrors", r), j("setFieldError", i), { onSubmit: s }; } }; function fe(a, t, e, l, s, r) { return m(), f("form", { class: c(["form", { [e.className]: e.className }]), onSubmit: t[0] || (t[0] = W(() => l.onSubmit(e.submitHandler), ["prevent"])), novalidate: "" }, [ q(a.$slots, "default") ], 34); } const Re = /* @__PURE__ */ v(me, [["render", fe]]), be = { props: { label: { type: String }, isDisabled: { type: Boolean, default: !1 }, className: { type: String, default: "" } } }; function ye(a, t, e, l, s, r) { return m(), f("label", { class: c({ label: !0, [e.className]: e.className, "label-disabled": e.isDisabled }) }, I(e.label), 3); } const w = /* @__PURE__ */ v(be, [["render", ye]]), ge = { props: { type: { type: String, default: "info" }, text: { type: String, default: "", required: !0 }, isVisible: { type: Boolean, default: !1 } } }; function Ne(a, t, e, l, s, r) { return m(), f("div", { class: c([ "tooltip", { [`tooltip-${e.type}`]: !0, [`tooltip-${e.isVisible ? "visible" : "hidden"}`]: !0 } ]) }, I(e.text), 3); } const ve = /* @__PURE__ */ v(ge, [["render", Ne]]), Ce = "data:image/svg+xml;base64,PHN2ZyB3aWR0aD0iMjQiIGhlaWdodD0iMjQiIHZpZXdCb3g9IjAgMCAyNCAyNCIgZmlsbD0ibm9uZSIgeG1sbnM9Imh0dHA6Ly93d3cudzMub3JnLzIwMDAvc3ZnIj4KPGNpcmNsZSBjeD0iMTIiIGN5PSIxNyIgcj0iMSIgdHJhbnNmb3JtPSJyb3RhdGUoLTE4MCAxMiAxNykiIGZpbGw9IiNGMjdDNzQiLz4KPHBhdGggZD0iTTEyIDE0TDEyIDYuOTk5OTdNMyA5LjEzMTkyVjE0Ljg2ODFDMyAxNi4zOTE0IDMuNzkyMyAxNy43OTkgNS4wNzg0NiAxOC41NjA3TDkuOTIxNTQgMjEuNDI4OEMxMS4yMDc3IDIyLjE5MDQgMTIuNzkyMyAyMi4xOTA0IDE0LjA3ODUgMjEuNDI4OEwxOC45MjE1IDE4LjU2MDdDMjAuMjA3NyAxNy43OTkgMjEgMTYuMzkxNCAyMSAxNC44NjgxVjkuMTMxOTJDMjEgNy42MDg2IDIwLjIwNzcgNi4yMDA5OSAxOC45MjE1IDUuNDM5MzJMMTQuMDc4NSAyLjU3MTI1QzEyLjc5MjMgMS44MDk1OCAxMS4yMDc3IDEuODA5NTggOS45MjE1NCAyLjU3MTI1TDUuMDc4NDYgNS40MzkzMkMzLjc5MjMgNi4yMDA5OSAzIDcuNjA4NiAzIDkuMTMxOTJaIiBzdHJva2U9IiNGMjdDNzQiIHN0cm9rZS13aWR0aD0iMS41IiBzdHJva2UtbGluZWNhcD0icm91bmQiLz4KPC9zdmc+Cg==", he = { components: { ToolTip: ve }, props: { error: { type: String, default: "" } }, setup() { return { tooltipShow: T(!1), imageUrl: Ce }; } }, pe = ["src"]; function Se(a, t, e, l, s, r) { const u = p("ToolTip"); return m(), f(B, null, [ e.error ? (m(), f("img", { key: 0, src: l.imageUrl, alt: "Error icon", class: "error-icon", onMouseenter: t[0] || (t[0] = (i) => l.tooltipShow = !0), onMouseleave: t[1] || (t[1] = (i) => l.tooltipShow = !1) }, null, 40, pe)) : O("", !0), S(u, { type: "error", text: e.error, isVisible: l.tooltipShow }, null, 8, ["text", "isVisible"]) ], 64); } const z = /* @__PURE__ */ v(he, [["render", Se]]), Me = { components: { Label: w, Error: z }, props: { label: { type: String, default: "" }, validator: { type: Array, default: null }, placeholder: { type: String }, type: { type: String, default: "text" }, isDisabled: { type: Boolean }, name: { type: String, requiered: !0 }, inputClassName: { type: String, default: "" }, labelClassName: { type: String, default: "" }, errorClassName: { type: String, default: "" }, fieldClassName: { type: String, default: "" }, defaultValue: { type: String, default: "" } }, setup(a) { const { name: t, defaultValue: e, validator: l } = a, s = M("registerField"), r = M("getFieldError"), u = F(() => r(t)), { value: i, validate: n, resetError: d } = s(t, e, l); return { value: i, error: u, onBlur: () => n(), onFocus: () => d(t) }; } }, _e = ["placeholder", "type", "disabled", "name"]; function De(a, t, e, l, s, r) { const u = p("Label"), i = p("Error"); return m(), f("div", { class: c(["input-container", { [e.fieldClassName]: e.fieldClassName }]) }, [ S(u, { label: e.label, for: e.name, className: e.labelClassName }, null, 8, ["label", "for", "className"]), k(y("input", { class: c({ input: !0, "input-error": l.error, [e.inputClassName]: e.inputClassName, [e.errorClassName]: l.error && e.errorClassName }), placeholder: e.placeholder, type: e.type, disabled: e.isDisabled, name: e.name, "onUpdate:modelValue": t[0] || (t[0] = (n) => l.value = n), onBlur: t[1] || (t[1] = (...n) => l.onBlur && l.onBlur(...n)), onFocus: t[2] || (t[2] = (...n) => l.onFocus && l.onFocus(...n)) }, null, 42, _e), [ [ K, l.value, void 0, { trim: !0 } ] ]), S(i, { error: l.error }, null, 8, ["error"]) ], 2); } const Ye = /* @__PURE__ */ v(Me, [["render", De]]), xe = { components: { Label: w, Error: z }, props: { label: { type: String, default: "" }, validator: { type: Array, default: null }, placeholder: { type: String }, isDisabled: { type: Boolean }, name: { type: String, requiered: !0 }, inputClassName: { type: String, default: "" }, labelClassName: { type: String, default: "" }, errorClassName: { type: String, default: "" }, fieldClassName: { type: String, default: "" }, defaultValue: { type: String, default: "" } }, setup(a) { const { name: t, defaultValue: e, validator: l } = a, s = M("registerField"), r = M("getFieldError"), u = F(() => r(t)), { value: i, validate: n, resetError: d } = s(t, e, l); return { value: i, error: u, onBlur: () => n(), onFocus: () => d(t) }; } }, Ee = ["placeholder", "disabled", "name"]; function Fe(a, t, e, l, s, r) { const u = p("Label"), i = p("Error"); return m(), f("div", { class: c(["input-container", { [e.fieldClassName]: e.fieldClassName }]) }, [ S(u, { label: e.label, for: e.name, className: e.labelClassName }, null, 8, ["label", "for", "className"]), k(y("textarea", { class: c({ "input input-textarea": !0, "input-error": l.error, [e.inputClassName]: e.inputClassName, [e.errorClassName]: l.error && e.errorClassName }), "onUpdate:modelValue": t[0] || (t[0] = (n) => l.value = n), placeholder: e.placeholder, disabled: e.isDisabled, onBlur: t[1] || (t[1] = (...n) => l.onBlur && l.onBlur(...n)), onFocus: t[2] || (t[2] = (...n) => l.onFocus && l.onFocus(...n)), name: e.name }, null, 42, Ee), [ [ H, l.value, void 0, { trim: !0 } ] ]), S(i, { error: l.error }, null, 8, ["error"]) ], 2); } const We = /* @__PURE__ */ v(xe, [["render", Fe]]), Ie = { props: { isStroke: { type: Boolean, default: !1 }, label: { type: String }, type: { type: String, default: "button" }, isDisabled: { type: Boolean, default: !1 }, isArrow: { type: Boolean, default: !1 }, isIcon: { type: Boolean, default: !1 }, className: { type: String, default: "" } }, setup(a) { const { isDisabled: t, type: e } = a, l = M("hasFormErrors"); return { disabled: F(() => l() && e === "submit" || t || t) }; } }, Le = ["type", "disabled"]; function ke(a, t, e, l, s, r) { return m(), f("button", { type: e.type, class: c([ "button", { "button-arrow": e.isArrow, "button-stroked": e.isStroke, "button-filled": !e.isIcon && !e.isStroke, "button-icon": e.isIcon, [e.className]: e.className } ]), disabled: l.disabled }, [ J(I(e.label) + " ", 1), q(a.$slots, "default") ], 10, Le); } const Ke = /* @__PURE__ */ v(Ie, [["render", ke]]), we = { components: { Label: w, Error: z }, props: { label: { type: String, default: "" }, name: { type: String, requiered: !0 }, options: { type: Array, requiered: !0 }, defaultValue: { type: String }, search: { type: Boolean, default: !1 }, validator: { type: Array }, inputClassName: { type: String, default: "" }, labelClassName: { type: String, default: "" }, errorClassName: { type: String, default: "" }, fieldClassName: { type: String, default: "" }, isDisabled: { type: Boolean, default: !1 } }, setup(a) { const { name: t, defaultValue: e, validator: l, options: s } = a, r = T(""), u = M("registerField"), i = M("getFieldError"), n = F(() => i(t)), d = F( () => s.filter((g) => g.label.toLowerCase().includes(r.value.toLowerCase())) ), C = s.map((g) => g.value).includes(e), { value: N, validate: h, resetError: _ } = u(t, C ? e : void 0, l), o = F( () => { var g; return ((g = s.find((L) => L.value === N.value)) == null ? void 0 : g.label) ?? s[0].label; } ), b = T(!1), x = T(); return { value: N, error: n, currentOptions: d, onBlur: (g) => { var L; if (((L = g.relatedTarget) == null ? void 0 : L.name) === "selectSearch") { g.preventDefault(); return; } h(), r.value = "", b.value = !1; }, onFocus: () => _(t), currentLabel: o, isOpened: b, onChange: (g) => { N.value = g, r.value = "", b.value = !1, x.value.blur(), h(); }, selectRef: x, resetOpened: () => { b.value = !b.value; }, searchValue: r }; } }, Ae = /* @__PURE__ */ y("svg", { class: "icon-arrow", width: "12", height: "6", viewBox: "0 0 12 6", fill: "none", xmlns: "http://www.w3.org/2000/svg" }, [ /* @__PURE__ */ y("path", { d: "M1 1L6 5L11 0.999999", stroke: "#060811", "stroke-width": "1.5", "stroke-linecap": "round", "stroke-linejoin": "round" }) ], -1), je = { key: 0, class: "input-select-list" }, Te = { key: 0, class: /* @__PURE__ */ c(["input-select-item input-select-item-search"]) }, Be = /* @__PURE__ */ y("svg", { class: "search-icon", width: "16", height: "16", viewBox: "0 0 16 16", fill: "none", xmlns: "http://www.w3.org/2000/svg" }, [ /* @__PURE__ */ y("path", { d: "M7.27268 0C11.2726 0 14.5454 3.27278 14.5454 7.27305C14.5454 9.01854 13.9273 10.6185 12.909 11.8549L15.7818 14.7278C16.0727 15.0186 16.0727 15.455 15.7818 15.746C15.6363 15.9277 15.4546 16.0005 15.2727 16.0005C15.0909 16.0005 14.909 15.9278 14.7636 15.7823L11.8908 12.9095C10.6181 13.9277 9.0181 14.5459 7.27274 14.5459C3.27278 14.5459 6.88781e-08 11.2731 6.88781e-08 7.27312C-0.00012171 3.27285 3.27278 6.54553e-05 7.27274 6.54553e-05L7.27268 0ZM7.27268 13.0911C10.4728 13.0911 13.0911 10.4729 13.0911 7.27274C13.0909 4.0729 10.4727 1.45467 7.27268 1.45467C4.07252 1.45467 1.4543 4.0729 1.4543 7.27305C1.4543 10.4729 4.07252 13.0911 7.27268 13.0911Z", fill: "#9B9CA0" }) ], -1), Oe = ["onMousedown"]; function Ve(a, t, e, l, s, r) { const u = p("Label"), i = p("Error"); return m(), f("div", { class: c(["input-container", { [e.fieldClassName]: e.fieldClassName }]) }, [ S(u, { label: e.label, className: e.labelClassName, isDisabled: e.isDisabled }, null, 8, ["label", "className", "isDisabled"]), y("div", { onFocusin: t[2] || (t[2] = (...n) => l.onFocus && l.onFocus(...n)), onFocusout: t[3] || (t[3] = (...n) => l.onBlur && l.onBlur(...n)), tabindex: 0, ref: "selectRef", class: c({ "input input-select": !0, "input-error": l.error, [e.inputClassName]: e.inputClassName, [e.errorClassName]: l.error && e.errorClassName, disabled: e.isDisabled }) }, [ y("div", { class: "input-select-current", onClick: t[0] || (t[0] = (...n) => l.resetOpened && l.resetOpened(...n)) }, [ J(I(l.currentLabel) + " ", 1), Ae ]), l.isOpened ? (m(), f("ul", je, [ e.search ? (m(), f("li", Te, [ k(y("input", { class: "input input-search", type: "text", placeholder: "Type in something", "onUpdate:modelValue": t[1] || (t[1] = (n) => l.searchValue = n), name: "selectSearch" }, null, 512), [ [H, l.searchValue] ]), Be ])) : O("", !0), (m(!0), f(B, null, V(l.currentOptions, (n) => (m(), f("li", { class: c([ "input-select-item", { active: n.value === l.value } ]), key: n.value, onMousedown: () => l.onChange(n.value) }, I(n.label), 43, Oe))), 128)) ])) : O("", !0) ], 34), S(i, { error: l.error }, null, 8, ["error"]) ], 2); } const Xe = /* @__PURE__ */ v(we, [["render", Ve]]), ze = { components: { Label: w }, props: { label: { type: String, requiered: !0 }, isDisabled: { type: Boolean }, name: { type: String, requiered: !0 }, fields: { type: Array, requiered: !0 }, fieldClassName: { type: String, default: "" }, labelClassName: { type: String, default: "" }, inputClassName: { type: String, default: "" }, inputLabelClassName: { type: String, default: "" } }, setup(a) { var u; const { name: t, fields: e } = a, l = M("registerField"), s = ((u = e.find((i) => i.checked)) == null ? void 0 : u.value) ?? e[0].value, { value: r } = l(t, s); return { value: r }; } }, Ue = { class: "box-field" }, Pe = ["disabled", "name", "value"]; function Ze(a, t, e, l, s, r) { const u = p("Label"); return m(), f("div", { class: c(["input-container", { [e.fieldClassName]: e.fieldClassName }]) }, [ S(u, { label: e.label, className: e.labelClassName, isDisabled: e.isDisabled }, null, 8, ["label", "className", "isDisabled"]), y("div", { class: c(["box-container", { disabled: e.isDisabled }]) }, [ (m(!0), f(B, null, V(e.fields, (i) => (m(), f("label", Ue, [ k(y("input", { class: "hidden", type: "radio", disabled: e.isDisabled, name: e.name, "onUpdate:modelValue": t[0] || (t[0] = (n) => l.value = n), value: i.value }, null, 8, Pe), [ [X, l.value] ]), y("span", { class: c({ "input input-box input-radio": !0, "input-radio-checked": l.value === i.value, [e.inputClassName]: e.inputClassName }) }, null, 2), y("span", { class: c( { "box-label": !0, [e.inputLabelClassName]: e.inputLabelClassName } ) }, I(i.label), 3) ]))), 256)) ], 2) ], 2); } const $e = /* @__PURE__ */ v(ze, [["render", Ze]]), Ge = { components: { Label: w }, props: { label: { type: String, requiered: !0 }, isDisabled: { type: Boolean }, name: { type: String, requiered: !0 }, fields: { type: Array, requiered: !0 }, fieldClassName: { type: String, default: "" }, labelClassName: { type: String, default: "" }, inputClassName: { type: String, default: "" }, inputLabelClassName: { type: String, default: "" } }, setup(a) { const { name: t } = a, e = M("registerField"), { value: l } = e(t, []); return { value: l }; } }, qe = { class: "box-field" }, He = ["disabled", "name", "value"]; function Je(a, t, e, l, s, r) { const u = p("Label"); return m(), f("div", { class: c(["input-container", { [e.fieldClassName]: e.fieldClassName }]) }, [ S(u, { label: e.label, className: e.labelClassName, isDisabled: e.isDisabled }, null, 8, ["label", "className", "isDisabled"]), y("div", { class: c(["box-container", { disabled: e.isDisabled }]) }, [ (m(!0), f(B, null, V(e.fields, (i) => (m(), f("label", qe, [ k(y("input", { class: "hidden", type: "checkbox", disabled: e.isDisabled, name: e.name, "onUpdate:modelValue": t[0] || (t[0] = (n) => l.value = n), value: i.value }, null, 8, He), [ [$, l.value] ]), y("span", { class: c({ "input input-box input-checkbox": !0, "input-checkbox-checked": l.value.includes(i.value), [e.inputClassName]: e.inputClassName }) }, null, 2), y("span", { class: c( { "box-label": !0, [e.inputLabelClassName]: e.inputLabelClassName } ) }, I(i.label), 3) ]))), 256)) ], 2) ], 2); } const et = /* @__PURE__ */ v(Ge, [["render", Je]]); export { Ke as Button, et as CheckBoxGroup, Re as Form, Ye as Input, $e as RadioGroup, Xe as Select, We as TextArea };