voc-lib-js 1.0.13 → 1.0.15

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/main.d.mts CHANGED
@@ -40,11 +40,11 @@ type FormData = {
40
40
  fields: Field[];
41
41
  };
42
42
  type SubmitConfig = {
43
- api_endpoint?: string;
44
- method?: "GET" | "POST" | "PUT" | "PATCH" | "DELETE";
43
+ api_endpoint: string;
44
+ method: "GET" | "POST" | "PUT" | "PATCH" | "DELETE";
45
45
  api_key?: string;
46
46
  headers?: Record<string, string>;
47
- body?: Record<string, any> | FormData;
47
+ additional_data?: Record<string, any> | FormData;
48
48
  contentType?: string | null;
49
49
  credentials?: "omit" | "same-origin" | "include";
50
50
  timeoutMs?: number;
@@ -63,6 +63,7 @@ interface RenderedForm {
63
63
  }
64
64
  declare function renderForm(element: string | HTMLElement, data: FormData, options?: {
65
65
  onSubmit?: (result: any, error?: any) => void;
66
+ onSubmitResult?: (response: any, error: Error | null) => void;
66
67
  enableAutoSubmit?: boolean;
67
68
  style?: "bootstrap" | undefined | null;
68
69
  submitConfig?: SubmitConfig;
package/dist/main.d.ts CHANGED
@@ -40,11 +40,11 @@ type FormData = {
40
40
  fields: Field[];
41
41
  };
42
42
  type SubmitConfig = {
43
- api_endpoint?: string;
44
- method?: "GET" | "POST" | "PUT" | "PATCH" | "DELETE";
43
+ api_endpoint: string;
44
+ method: "GET" | "POST" | "PUT" | "PATCH" | "DELETE";
45
45
  api_key?: string;
46
46
  headers?: Record<string, string>;
47
- body?: Record<string, any> | FormData;
47
+ additional_data?: Record<string, any> | FormData;
48
48
  contentType?: string | null;
49
49
  credentials?: "omit" | "same-origin" | "include";
50
50
  timeoutMs?: number;
@@ -63,6 +63,7 @@ interface RenderedForm {
63
63
  }
64
64
  declare function renderForm(element: string | HTMLElement, data: FormData, options?: {
65
65
  onSubmit?: (result: any, error?: any) => void;
66
+ onSubmitResult?: (response: any, error: Error | null) => void;
66
67
  enableAutoSubmit?: boolean;
67
68
  style?: "bootstrap" | undefined | null;
68
69
  submitConfig?: SubmitConfig;
@@ -1 +1 @@
1
- "use strict";var VocLibJs=(()=>{var x=Object.defineProperty;var T=Object.getOwnPropertyDescriptor;var A=Object.getOwnPropertyNames;var M=Object.prototype.hasOwnProperty;var H=(p,c)=>{for(var h in c)x(p,h,{get:c[h],enumerable:!0})},q=(p,c,h,l)=>{if(c&&typeof c=="object"||typeof c=="function")for(let f of A(c))!M.call(p,f)&&f!==h&&x(p,f,{get:()=>c[f],enumerable:!(l=T(c,f))||l.enumerable});return p};var S=p=>q(x({},"__esModule",{value:!0}),p);var N={};H(N,{FormsLib:()=>_,default:()=>D});function L(p,c=!0){let h=new FormData(p);if(c)return h;let l={};return h.forEach((f,m)=>{let a=(f instanceof File,f);Object.prototype.hasOwnProperty.call(l,m)?Array.isArray(l[m])?l[m].push(a):l[m]=[l[m],a]:l[m]=a}),l}function C(p,c,h){let l=[];if(typeof p=="string"?l=Array.from(document.querySelectorAll(p)):l=[p],l.length===0)return null;function f(){let a=h?.style==="bootstrap",u=document.createElement("form");u.classList.add("voc-form"),a&&u.classList.add("needs-validation"),u.id=c.id;let v=document.createElement("h2");v.textContent=c.form_name,u.appendChild(v);function E(e,i){i&&(i.min_length!==void 0&&(e.minLength=i.min_length),i.max_length!==void 0&&(e.maxLength=i.max_length),i.regex&&(e instanceof HTMLInputElement?e.pattern=i.regex:e.dataset.pattern=i.regex))}function F(e){if(!e.key&&!e.label)return null;let i=c.default_values||{},g=e.name||e.key,r=g?i[g]:void 0,y=document.createElement("div");if(y.className="voc-form-group",a&&y.classList.add("mb-3"),e.label){let t=document.createElement("label");t.classList.add("voc-form-label"),a&&t.classList.add("form-label"),e.key?e.type==="radio"?t.htmlFor="":t.htmlFor=`voc-form-${e.key}`:t.htmlFor="",t.textContent=e.label+(e.required?" *":""),y.appendChild(t)}let b=null;switch(e.type){case"checkbox":{let t=document.createElement("div");if(t.className="voc-form-checkbox-group "+(e.direction||"vertical"),e.width&&(t.style.width=e.width),e.options)e.options.forEach((n,s)=>{let d=document.createElement("label");d.classList.add("voc-form-checkbox-label"),a&&d.classList.add("form-check"),d.style.display=e.direction==="horizontal"?"inline-block":"block",e.width&&(d.style.width=e.width);let o=document.createElement("input");o.type="checkbox",o.classList.add("voc-form-checkbox"),a&&o.classList.add("form-check-input"),o.name=e.name||e.key||"",o.value=n.value,e.key&&(o.id=`voc-form-${e.key}-${s}`),e.required&&(o.required=!0),Array.isArray(r)?r.includes(n.value)&&(o.checked=!0):r!==void 0?r===n.value&&(o.checked=!0):e.default===n.value&&(o.checked=!0),d.appendChild(o),d.appendChild(document.createTextNode(n.label)),t.appendChild(d)});else{let n=document.createElement("label");n.classList.add("voc-form-checkbox-label"),a&&n.classList.add("form-check"),e.width&&(n.style.width=e.width);let s=document.createElement("input");s.type="checkbox",s.classList.add("voc-form-checkbox"),(e.key||e.name)&&(s.name=e.name||e.key,e.key&&(s.id=`voc-form-${e.key}`)),a&&s.classList.add("form-check-input"),e.required&&(s.required=!0),r!==void 0?s.checked=!!r:e.default&&(s.checked=!!e.default),n.appendChild(s),n.appendChild(document.createTextNode(e.label||"")),t.appendChild(n)}b=t;break}case"textarea":{let t=document.createElement("textarea");t.classList.add("voc-form-textarea"),a&&t.classList.add("form-control"),(e.key||e.name)&&(t.name=e.name||e.key,e.key&&(t.id=`voc-form-${e.key}`)),e.placeholder&&(t.placeholder=e.placeholder),r!=null?t.value=String(r):e.default!==void 0&&e.default!==null&&(t.value=String(e.default)),e.width&&(t.style.width=e.width),e.required&&(t.required=!0),E(t,e.validation),b=t;break}case"select":{let t=document.createElement("select");if(t.classList.add("voc-form-select"),a&&t.classList.add("form-select"),(e.key||e.name)&&(t.name=e.name||e.key,e.key&&(t.id=`voc-form-${e.key}`)),e.placeholder){let n=document.createElement("option");n.value="",n.textContent=e.placeholder,n.disabled=!0,n.selected=!0,t.appendChild(n)}e.required&&(t.required=!0),e.options&&e.options.forEach(n=>{let s=document.createElement("option");s.value=n.value,s.textContent=n.label,Array.isArray(r)?r.includes(n.value)&&(s.selected=!0):r!==void 0?r===n.value&&(s.selected=!0):e.default===n.value&&(s.selected=!0),t.appendChild(s)}),e.width&&(t.style.width=e.width),b=t;break}case"radio":{let t=document.createElement("div");t.className="voc-form-radio-group "+(e.direction||"vertical"),e.width&&(t.style.width=e.width),e.options&&e.options.forEach((n,s)=>{let d=document.createElement("label");d.classList.add("voc-form-radio-label"),a&&d.classList.add("form-check"),d.style.display=e.direction==="horizontal"?"inline-block":"block",e.width&&(d.style.width=e.width);let o=document.createElement("input");o.type="radio",o.classList.add("voc-form-radio"),a&&o.classList.add("form-check-input"),o.name=e.name||e.key||"",o.value=n.value,e.key&&(o.id=`voc-form-${e.key}-${s}`),Array.isArray(r)?r.includes(n.value)&&(o.checked=!0):r!==void 0?r===n.value&&(o.checked=!0):e.default===n.value&&(o.checked=!0),d.appendChild(o),d.appendChild(document.createTextNode(n.label)),t.appendChild(d)}),b=t;break}case"file":{let t=document.createElement("div");t.className="voc-form-file-wrapper",e.width&&(t.style.width=e.width);let n=document.createElement("input");n.type="file",n.classList.add("voc-form-file"),a&&n.classList.add("form-control"),(e.key||e.name)&&(n.name=e.name||e.key,e.key&&(n.id=`voc-form-${e.key}`)),e.width&&(n.style.width=e.width),e.required&&(n.required=!0),e.accept&&(n.accept=e.accept),e.multiple&&(n.multiple=!0),t.appendChild(n),b=t;break}default:{let t=document.createElement("input");t.type=e.type||"text",t.classList.add("voc-form-input"),a&&t.classList.add("form-control"),(e.key||e.name)&&(t.name=e.name||e.key,e.key&&(t.id=`voc-form-${e.key}`)),e.placeholder&&(t.placeholder=e.placeholder),r!=null?t.value=String(r):e.default!==void 0&&e.default!==null&&(t.value=String(e.default)),e.width&&(t.style.width=e.width),e.required&&(t.required=!0),E(t,e.validation),b=t;break}}if(b&&y.appendChild(b),e.hint_text){let t=document.createElement("small");t.className="voc-form-hint",t.textContent=e.hint_text,y.appendChild(t)}return y}c.fields.forEach(e=>{if(e.type==="row"&&"items"in e){let i=document.createElement("div");if(i.className="voc-form-group voc-form-row",a&&i.classList.add("mb-3"),e.label){let r=document.createElement("h3");r.className="voc-form-row-label",r.textContent=e.label,i.appendChild(r)}if(e.description){let r=document.createElement("p");r.className="voc-form-row-description",r.textContent=e.description,i.appendChild(r)}let g=document.createElement("div");g.className="voc-form-row-items "+(e.direction||"horizontal"),i.appendChild(g),e.items.forEach(r=>{let y=F(r);y&&g.appendChild(y)}),u.appendChild(i)}else{let i=F(e);i&&u.appendChild(i)}});let k=document.createElement("button");return k.type="submit",k.className="voc-form-submit",k.textContent="Submit",a&&k.classList.add("btn","btn-primary"),u.appendChild(k),u}let m=f();return l[0].appendChild(m),h&&h.submitConfig&&m.addEventListener("submit",a=>{a.preventDefault();let u=L(m);console.log(u)}),{element:m,validate:()=>{let a=[];return m.querySelectorAll(":invalid").forEach(v=>{let E=v.validationMessage;a.push({element:v,message:E})}),a},getValues:()=>L(m),onSubmit:a=>{m.addEventListener("submit",u=>{u.preventDefault();let v=L(m);a(v,u)})}}}var w={renderForm:C};var _=w,D={FormsLib:w};return S(N);})();
1
+ "use strict";var VocLibJs=(()=>{var x=Object.defineProperty;var S=Object.getOwnPropertyDescriptor;var M=Object.getOwnPropertyNames;var A=Object.prototype.hasOwnProperty;var H=(p,i)=>{for(var l in i)x(p,l,{get:i[l],enumerable:!0})},R=(p,i,l,d)=>{if(i&&typeof i=="object"||typeof i=="function")for(let f of M(i))!A.call(p,f)&&f!==l&&x(p,f,{get:()=>i[f],enumerable:!(d=S(i,f))||d.enumerable});return p};var _=p=>R(x({},"__esModule",{value:!0}),p);var N={};H(N,{FormsLib:()=>q,default:()=>D});function k(p,i=!0){let l=new FormData(p);if(i)return l;let d={};return l.forEach((f,m)=>{let a=(f instanceof File,f);Object.prototype.hasOwnProperty.call(d,m)?Array.isArray(d[m])?d[m].push(a):d[m]=[d[m],a]:d[m]=a}),d}async function C(p,i,l){let d=i.api_endpoint,f=p.method||"POST",m={"Content-Type":"application/json"};i.headers&&Object.assign(m,i.headers);let a=JSON.stringify(k(p,!1));try{let y=await(await fetch(d,{method:f,headers:m,body:a,credentials:i.credentials||"same-origin"})).json();l&&l(y,null)}catch(u){l&&l(null,u)}}function T(p,i,l){let d=[];if(typeof p=="string"?d=Array.from(document.querySelectorAll(p)):d=[p],d.length===0)return null;function f(){let a=l?.style==="bootstrap",u=document.createElement("form");u.classList.add("voc-form"),a&&u.classList.add("needs-validation"),u.id=i.id;let y=document.createElement("h2");y.textContent=i.form_name,u.appendChild(y);function L(e,s){s&&(s.min_length!==void 0&&(e.minLength=s.min_length),s.max_length!==void 0&&(e.maxLength=s.max_length),s.regex&&(e instanceof HTMLInputElement?e.pattern=s.regex:e.dataset.pattern=s.regex))}function F(e){if(!e.key&&!e.label)return null;let s=i.default_values||{},v=e.name||e.key,r=v?s[v]:void 0,b=document.createElement("div");if(b.className="voc-form-group",a&&b.classList.add("mb-3"),e.label){let t=document.createElement("label");t.classList.add("voc-form-label"),a&&t.classList.add("form-label"),e.key?e.type==="radio"?t.htmlFor="":t.htmlFor=`voc-form-${e.key}`:t.htmlFor="",t.textContent=e.label+(e.required?" *":""),b.appendChild(t)}let g=null;switch(e.type){case"checkbox":{let t=document.createElement("div");if(t.className="voc-form-checkbox-group "+(e.direction||"vertical"),e.width&&(t.style.width=e.width),e.options)e.options.forEach((n,c)=>{let h=document.createElement("label");h.classList.add("voc-form-checkbox-label"),a&&h.classList.add("form-check"),h.style.display=e.direction==="horizontal"?"inline-block":"block",e.width&&(h.style.width=e.width);let o=document.createElement("input");o.type="checkbox",o.classList.add("voc-form-checkbox"),a&&o.classList.add("form-check-input"),o.name=e.name||e.key||"",o.value=n.value,e.key&&(o.id=`voc-form-${e.key}-${c}`),e.required&&(o.required=!0),Array.isArray(r)?r.includes(n.value)&&(o.checked=!0):r!==void 0?r===n.value&&(o.checked=!0):e.default===n.value&&(o.checked=!0),h.appendChild(o),h.appendChild(document.createTextNode(n.label)),t.appendChild(h)});else{let n=document.createElement("label");n.classList.add("voc-form-checkbox-label"),a&&n.classList.add("form-check"),e.width&&(n.style.width=e.width);let c=document.createElement("input");c.type="checkbox",c.classList.add("voc-form-checkbox"),(e.key||e.name)&&(c.name=e.name||e.key,e.key&&(c.id=`voc-form-${e.key}`)),a&&c.classList.add("form-check-input"),e.required&&(c.required=!0),r!==void 0?c.checked=!!r:e.default&&(c.checked=!!e.default),n.appendChild(c),n.appendChild(document.createTextNode(e.label||"")),t.appendChild(n)}g=t;break}case"textarea":{let t=document.createElement("textarea");t.classList.add("voc-form-textarea"),a&&t.classList.add("form-control"),(e.key||e.name)&&(t.name=e.name||e.key,e.key&&(t.id=`voc-form-${e.key}`)),e.placeholder&&(t.placeholder=e.placeholder),r!=null?t.value=String(r):e.default!==void 0&&e.default!==null&&(t.value=String(e.default)),e.width&&(t.style.width=e.width),e.required&&(t.required=!0),L(t,e.validation),g=t;break}case"select":{let t=document.createElement("select");if(t.classList.add("voc-form-select"),a&&t.classList.add("form-select"),(e.key||e.name)&&(t.name=e.name||e.key,e.key&&(t.id=`voc-form-${e.key}`)),e.placeholder){let n=document.createElement("option");n.value="",n.textContent=e.placeholder,n.disabled=!0,n.selected=!0,t.appendChild(n)}e.required&&(t.required=!0),e.options&&e.options.forEach(n=>{let c=document.createElement("option");c.value=n.value,c.textContent=n.label,Array.isArray(r)?r.includes(n.value)&&(c.selected=!0):r!==void 0?r===n.value&&(c.selected=!0):e.default===n.value&&(c.selected=!0),t.appendChild(c)}),e.width&&(t.style.width=e.width),g=t;break}case"radio":{let t=document.createElement("div");t.className="voc-form-radio-group "+(e.direction||"vertical"),e.width&&(t.style.width=e.width),e.options&&e.options.forEach((n,c)=>{let h=document.createElement("label");h.classList.add("voc-form-radio-label"),a&&h.classList.add("form-check"),h.style.display=e.direction==="horizontal"?"inline-block":"block",e.width&&(h.style.width=e.width);let o=document.createElement("input");o.type="radio",o.classList.add("voc-form-radio"),a&&o.classList.add("form-check-input"),o.name=e.name||e.key||"",o.value=n.value,e.key&&(o.id=`voc-form-${e.key}-${c}`),Array.isArray(r)?r.includes(n.value)&&(o.checked=!0):r!==void 0?r===n.value&&(o.checked=!0):e.default===n.value&&(o.checked=!0),h.appendChild(o),h.appendChild(document.createTextNode(n.label)),t.appendChild(h)}),g=t;break}case"file":{let t=document.createElement("div");t.className="voc-form-file-wrapper",e.width&&(t.style.width=e.width);let n=document.createElement("input");n.type="file",n.classList.add("voc-form-file"),a&&n.classList.add("form-control"),(e.key||e.name)&&(n.name=e.name||e.key,e.key&&(n.id=`voc-form-${e.key}`)),e.width&&(n.style.width=e.width),e.required&&(n.required=!0),e.accept&&(n.accept=e.accept),e.multiple&&(n.multiple=!0),t.appendChild(n),g=t;break}default:{let t=document.createElement("input");t.type=e.type||"text",t.classList.add("voc-form-input"),a&&t.classList.add("form-control"),(e.key||e.name)&&(t.name=e.name||e.key,e.key&&(t.id=`voc-form-${e.key}`)),e.placeholder&&(t.placeholder=e.placeholder),r!=null?t.value=String(r):e.default!==void 0&&e.default!==null&&(t.value=String(e.default)),e.width&&(t.style.width=e.width),e.required&&(t.required=!0),L(t,e.validation),g=t;break}}if(g&&b.appendChild(g),e.hint_text){let t=document.createElement("small");t.className="voc-form-hint",t.textContent=e.hint_text,b.appendChild(t)}return b}i.fields.forEach(e=>{if(e.type==="row"&&"items"in e){let s=document.createElement("div");if(s.className="voc-form-group voc-form-row",a&&s.classList.add("mb-3"),e.label){let r=document.createElement("h3");r.className="voc-form-row-label",r.textContent=e.label,s.appendChild(r)}if(e.description){let r=document.createElement("p");r.className="voc-form-row-description",r.textContent=e.description,s.appendChild(r)}let v=document.createElement("div");v.className="voc-form-row-items "+(e.direction||"horizontal"),s.appendChild(v),e.items.forEach(r=>{let b=F(r);b&&v.appendChild(b)}),u.appendChild(s)}else{let s=F(e);s&&u.appendChild(s)}});let E=document.createElement("button");return E.type="submit",E.className="voc-form-submit",E.textContent="Submit",a&&E.classList.add("btn","btn-primary"),u.appendChild(E),u}let m=f();return d[0].appendChild(m),l&&l.submitConfig&&m.addEventListener("submit",a=>{a.preventDefault(),C(m,l.submitConfig,(u,y)=>{l.onSubmitResult&&l.onSubmitResult(u,y)})}),{element:m,validate:()=>{let a=[];return m.querySelectorAll(":invalid").forEach(y=>{let L=y.validationMessage;a.push({element:y,message:L})}),a},getValues:()=>k(m),onSubmit:a=>{m.addEventListener("submit",u=>{u.preventDefault();let y=k(m);a(y,u)})}}}var w={renderForm:T};var q=w,D={FormsLib:w};return _(N);})();
package/dist/main.js CHANGED
@@ -1 +1 @@
1
- "use strict";var x=Object.defineProperty;var T=Object.getOwnPropertyDescriptor;var A=Object.getOwnPropertyNames;var M=Object.prototype.hasOwnProperty;var H=(p,c)=>{for(var h in c)x(p,h,{get:c[h],enumerable:!0})},q=(p,c,h,l)=>{if(c&&typeof c=="object"||typeof c=="function")for(let f of A(c))!M.call(p,f)&&f!==h&&x(p,f,{get:()=>c[f],enumerable:!(l=T(c,f))||l.enumerable});return p};var S=p=>q(x({},"__esModule",{value:!0}),p);var N={};H(N,{FormsLib:()=>_,default:()=>D});module.exports=S(N);function L(p,c=!0){let h=new FormData(p);if(c)return h;let l={};return h.forEach((f,m)=>{let a=(f instanceof File,f);Object.prototype.hasOwnProperty.call(l,m)?Array.isArray(l[m])?l[m].push(a):l[m]=[l[m],a]:l[m]=a}),l}function C(p,c,h){let l=[];if(typeof p=="string"?l=Array.from(document.querySelectorAll(p)):l=[p],l.length===0)return null;function f(){let a=h?.style==="bootstrap",u=document.createElement("form");u.classList.add("voc-form"),a&&u.classList.add("needs-validation"),u.id=c.id;let v=document.createElement("h2");v.textContent=c.form_name,u.appendChild(v);function E(e,i){i&&(i.min_length!==void 0&&(e.minLength=i.min_length),i.max_length!==void 0&&(e.maxLength=i.max_length),i.regex&&(e instanceof HTMLInputElement?e.pattern=i.regex:e.dataset.pattern=i.regex))}function F(e){if(!e.key&&!e.label)return null;let i=c.default_values||{},g=e.name||e.key,r=g?i[g]:void 0,y=document.createElement("div");if(y.className="voc-form-group",a&&y.classList.add("mb-3"),e.label){let t=document.createElement("label");t.classList.add("voc-form-label"),a&&t.classList.add("form-label"),e.key?e.type==="radio"?t.htmlFor="":t.htmlFor=`voc-form-${e.key}`:t.htmlFor="",t.textContent=e.label+(e.required?" *":""),y.appendChild(t)}let b=null;switch(e.type){case"checkbox":{let t=document.createElement("div");if(t.className="voc-form-checkbox-group "+(e.direction||"vertical"),e.width&&(t.style.width=e.width),e.options)e.options.forEach((n,s)=>{let d=document.createElement("label");d.classList.add("voc-form-checkbox-label"),a&&d.classList.add("form-check"),d.style.display=e.direction==="horizontal"?"inline-block":"block",e.width&&(d.style.width=e.width);let o=document.createElement("input");o.type="checkbox",o.classList.add("voc-form-checkbox"),a&&o.classList.add("form-check-input"),o.name=e.name||e.key||"",o.value=n.value,e.key&&(o.id=`voc-form-${e.key}-${s}`),e.required&&(o.required=!0),Array.isArray(r)?r.includes(n.value)&&(o.checked=!0):r!==void 0?r===n.value&&(o.checked=!0):e.default===n.value&&(o.checked=!0),d.appendChild(o),d.appendChild(document.createTextNode(n.label)),t.appendChild(d)});else{let n=document.createElement("label");n.classList.add("voc-form-checkbox-label"),a&&n.classList.add("form-check"),e.width&&(n.style.width=e.width);let s=document.createElement("input");s.type="checkbox",s.classList.add("voc-form-checkbox"),(e.key||e.name)&&(s.name=e.name||e.key,e.key&&(s.id=`voc-form-${e.key}`)),a&&s.classList.add("form-check-input"),e.required&&(s.required=!0),r!==void 0?s.checked=!!r:e.default&&(s.checked=!!e.default),n.appendChild(s),n.appendChild(document.createTextNode(e.label||"")),t.appendChild(n)}b=t;break}case"textarea":{let t=document.createElement("textarea");t.classList.add("voc-form-textarea"),a&&t.classList.add("form-control"),(e.key||e.name)&&(t.name=e.name||e.key,e.key&&(t.id=`voc-form-${e.key}`)),e.placeholder&&(t.placeholder=e.placeholder),r!=null?t.value=String(r):e.default!==void 0&&e.default!==null&&(t.value=String(e.default)),e.width&&(t.style.width=e.width),e.required&&(t.required=!0),E(t,e.validation),b=t;break}case"select":{let t=document.createElement("select");if(t.classList.add("voc-form-select"),a&&t.classList.add("form-select"),(e.key||e.name)&&(t.name=e.name||e.key,e.key&&(t.id=`voc-form-${e.key}`)),e.placeholder){let n=document.createElement("option");n.value="",n.textContent=e.placeholder,n.disabled=!0,n.selected=!0,t.appendChild(n)}e.required&&(t.required=!0),e.options&&e.options.forEach(n=>{let s=document.createElement("option");s.value=n.value,s.textContent=n.label,Array.isArray(r)?r.includes(n.value)&&(s.selected=!0):r!==void 0?r===n.value&&(s.selected=!0):e.default===n.value&&(s.selected=!0),t.appendChild(s)}),e.width&&(t.style.width=e.width),b=t;break}case"radio":{let t=document.createElement("div");t.className="voc-form-radio-group "+(e.direction||"vertical"),e.width&&(t.style.width=e.width),e.options&&e.options.forEach((n,s)=>{let d=document.createElement("label");d.classList.add("voc-form-radio-label"),a&&d.classList.add("form-check"),d.style.display=e.direction==="horizontal"?"inline-block":"block",e.width&&(d.style.width=e.width);let o=document.createElement("input");o.type="radio",o.classList.add("voc-form-radio"),a&&o.classList.add("form-check-input"),o.name=e.name||e.key||"",o.value=n.value,e.key&&(o.id=`voc-form-${e.key}-${s}`),Array.isArray(r)?r.includes(n.value)&&(o.checked=!0):r!==void 0?r===n.value&&(o.checked=!0):e.default===n.value&&(o.checked=!0),d.appendChild(o),d.appendChild(document.createTextNode(n.label)),t.appendChild(d)}),b=t;break}case"file":{let t=document.createElement("div");t.className="voc-form-file-wrapper",e.width&&(t.style.width=e.width);let n=document.createElement("input");n.type="file",n.classList.add("voc-form-file"),a&&n.classList.add("form-control"),(e.key||e.name)&&(n.name=e.name||e.key,e.key&&(n.id=`voc-form-${e.key}`)),e.width&&(n.style.width=e.width),e.required&&(n.required=!0),e.accept&&(n.accept=e.accept),e.multiple&&(n.multiple=!0),t.appendChild(n),b=t;break}default:{let t=document.createElement("input");t.type=e.type||"text",t.classList.add("voc-form-input"),a&&t.classList.add("form-control"),(e.key||e.name)&&(t.name=e.name||e.key,e.key&&(t.id=`voc-form-${e.key}`)),e.placeholder&&(t.placeholder=e.placeholder),r!=null?t.value=String(r):e.default!==void 0&&e.default!==null&&(t.value=String(e.default)),e.width&&(t.style.width=e.width),e.required&&(t.required=!0),E(t,e.validation),b=t;break}}if(b&&y.appendChild(b),e.hint_text){let t=document.createElement("small");t.className="voc-form-hint",t.textContent=e.hint_text,y.appendChild(t)}return y}c.fields.forEach(e=>{if(e.type==="row"&&"items"in e){let i=document.createElement("div");if(i.className="voc-form-group voc-form-row",a&&i.classList.add("mb-3"),e.label){let r=document.createElement("h3");r.className="voc-form-row-label",r.textContent=e.label,i.appendChild(r)}if(e.description){let r=document.createElement("p");r.className="voc-form-row-description",r.textContent=e.description,i.appendChild(r)}let g=document.createElement("div");g.className="voc-form-row-items "+(e.direction||"horizontal"),i.appendChild(g),e.items.forEach(r=>{let y=F(r);y&&g.appendChild(y)}),u.appendChild(i)}else{let i=F(e);i&&u.appendChild(i)}});let k=document.createElement("button");return k.type="submit",k.className="voc-form-submit",k.textContent="Submit",a&&k.classList.add("btn","btn-primary"),u.appendChild(k),u}let m=f();return l[0].appendChild(m),h&&h.submitConfig&&m.addEventListener("submit",a=>{a.preventDefault();let u=L(m);console.log(u)}),{element:m,validate:()=>{let a=[];return m.querySelectorAll(":invalid").forEach(v=>{let E=v.validationMessage;a.push({element:v,message:E})}),a},getValues:()=>L(m),onSubmit:a=>{m.addEventListener("submit",u=>{u.preventDefault();let v=L(m);a(v,u)})}}}var w={renderForm:C};var _=w,D={FormsLib:w};0&&(module.exports={FormsLib});
1
+ "use strict";var x=Object.defineProperty;var S=Object.getOwnPropertyDescriptor;var M=Object.getOwnPropertyNames;var A=Object.prototype.hasOwnProperty;var H=(p,i)=>{for(var l in i)x(p,l,{get:i[l],enumerable:!0})},R=(p,i,l,d)=>{if(i&&typeof i=="object"||typeof i=="function")for(let f of M(i))!A.call(p,f)&&f!==l&&x(p,f,{get:()=>i[f],enumerable:!(d=S(i,f))||d.enumerable});return p};var _=p=>R(x({},"__esModule",{value:!0}),p);var N={};H(N,{FormsLib:()=>q,default:()=>D});module.exports=_(N);function k(p,i=!0){let l=new FormData(p);if(i)return l;let d={};return l.forEach((f,m)=>{let a=(f instanceof File,f);Object.prototype.hasOwnProperty.call(d,m)?Array.isArray(d[m])?d[m].push(a):d[m]=[d[m],a]:d[m]=a}),d}async function C(p,i,l){let d=i.api_endpoint,f=p.method||"POST",m={"Content-Type":"application/json"};i.headers&&Object.assign(m,i.headers);let a=JSON.stringify(k(p,!1));try{let y=await(await fetch(d,{method:f,headers:m,body:a,credentials:i.credentials||"same-origin"})).json();l&&l(y,null)}catch(u){l&&l(null,u)}}function T(p,i,l){let d=[];if(typeof p=="string"?d=Array.from(document.querySelectorAll(p)):d=[p],d.length===0)return null;function f(){let a=l?.style==="bootstrap",u=document.createElement("form");u.classList.add("voc-form"),a&&u.classList.add("needs-validation"),u.id=i.id;let y=document.createElement("h2");y.textContent=i.form_name,u.appendChild(y);function L(e,s){s&&(s.min_length!==void 0&&(e.minLength=s.min_length),s.max_length!==void 0&&(e.maxLength=s.max_length),s.regex&&(e instanceof HTMLInputElement?e.pattern=s.regex:e.dataset.pattern=s.regex))}function F(e){if(!e.key&&!e.label)return null;let s=i.default_values||{},v=e.name||e.key,r=v?s[v]:void 0,b=document.createElement("div");if(b.className="voc-form-group",a&&b.classList.add("mb-3"),e.label){let t=document.createElement("label");t.classList.add("voc-form-label"),a&&t.classList.add("form-label"),e.key?e.type==="radio"?t.htmlFor="":t.htmlFor=`voc-form-${e.key}`:t.htmlFor="",t.textContent=e.label+(e.required?" *":""),b.appendChild(t)}let g=null;switch(e.type){case"checkbox":{let t=document.createElement("div");if(t.className="voc-form-checkbox-group "+(e.direction||"vertical"),e.width&&(t.style.width=e.width),e.options)e.options.forEach((n,c)=>{let h=document.createElement("label");h.classList.add("voc-form-checkbox-label"),a&&h.classList.add("form-check"),h.style.display=e.direction==="horizontal"?"inline-block":"block",e.width&&(h.style.width=e.width);let o=document.createElement("input");o.type="checkbox",o.classList.add("voc-form-checkbox"),a&&o.classList.add("form-check-input"),o.name=e.name||e.key||"",o.value=n.value,e.key&&(o.id=`voc-form-${e.key}-${c}`),e.required&&(o.required=!0),Array.isArray(r)?r.includes(n.value)&&(o.checked=!0):r!==void 0?r===n.value&&(o.checked=!0):e.default===n.value&&(o.checked=!0),h.appendChild(o),h.appendChild(document.createTextNode(n.label)),t.appendChild(h)});else{let n=document.createElement("label");n.classList.add("voc-form-checkbox-label"),a&&n.classList.add("form-check"),e.width&&(n.style.width=e.width);let c=document.createElement("input");c.type="checkbox",c.classList.add("voc-form-checkbox"),(e.key||e.name)&&(c.name=e.name||e.key,e.key&&(c.id=`voc-form-${e.key}`)),a&&c.classList.add("form-check-input"),e.required&&(c.required=!0),r!==void 0?c.checked=!!r:e.default&&(c.checked=!!e.default),n.appendChild(c),n.appendChild(document.createTextNode(e.label||"")),t.appendChild(n)}g=t;break}case"textarea":{let t=document.createElement("textarea");t.classList.add("voc-form-textarea"),a&&t.classList.add("form-control"),(e.key||e.name)&&(t.name=e.name||e.key,e.key&&(t.id=`voc-form-${e.key}`)),e.placeholder&&(t.placeholder=e.placeholder),r!=null?t.value=String(r):e.default!==void 0&&e.default!==null&&(t.value=String(e.default)),e.width&&(t.style.width=e.width),e.required&&(t.required=!0),L(t,e.validation),g=t;break}case"select":{let t=document.createElement("select");if(t.classList.add("voc-form-select"),a&&t.classList.add("form-select"),(e.key||e.name)&&(t.name=e.name||e.key,e.key&&(t.id=`voc-form-${e.key}`)),e.placeholder){let n=document.createElement("option");n.value="",n.textContent=e.placeholder,n.disabled=!0,n.selected=!0,t.appendChild(n)}e.required&&(t.required=!0),e.options&&e.options.forEach(n=>{let c=document.createElement("option");c.value=n.value,c.textContent=n.label,Array.isArray(r)?r.includes(n.value)&&(c.selected=!0):r!==void 0?r===n.value&&(c.selected=!0):e.default===n.value&&(c.selected=!0),t.appendChild(c)}),e.width&&(t.style.width=e.width),g=t;break}case"radio":{let t=document.createElement("div");t.className="voc-form-radio-group "+(e.direction||"vertical"),e.width&&(t.style.width=e.width),e.options&&e.options.forEach((n,c)=>{let h=document.createElement("label");h.classList.add("voc-form-radio-label"),a&&h.classList.add("form-check"),h.style.display=e.direction==="horizontal"?"inline-block":"block",e.width&&(h.style.width=e.width);let o=document.createElement("input");o.type="radio",o.classList.add("voc-form-radio"),a&&o.classList.add("form-check-input"),o.name=e.name||e.key||"",o.value=n.value,e.key&&(o.id=`voc-form-${e.key}-${c}`),Array.isArray(r)?r.includes(n.value)&&(o.checked=!0):r!==void 0?r===n.value&&(o.checked=!0):e.default===n.value&&(o.checked=!0),h.appendChild(o),h.appendChild(document.createTextNode(n.label)),t.appendChild(h)}),g=t;break}case"file":{let t=document.createElement("div");t.className="voc-form-file-wrapper",e.width&&(t.style.width=e.width);let n=document.createElement("input");n.type="file",n.classList.add("voc-form-file"),a&&n.classList.add("form-control"),(e.key||e.name)&&(n.name=e.name||e.key,e.key&&(n.id=`voc-form-${e.key}`)),e.width&&(n.style.width=e.width),e.required&&(n.required=!0),e.accept&&(n.accept=e.accept),e.multiple&&(n.multiple=!0),t.appendChild(n),g=t;break}default:{let t=document.createElement("input");t.type=e.type||"text",t.classList.add("voc-form-input"),a&&t.classList.add("form-control"),(e.key||e.name)&&(t.name=e.name||e.key,e.key&&(t.id=`voc-form-${e.key}`)),e.placeholder&&(t.placeholder=e.placeholder),r!=null?t.value=String(r):e.default!==void 0&&e.default!==null&&(t.value=String(e.default)),e.width&&(t.style.width=e.width),e.required&&(t.required=!0),L(t,e.validation),g=t;break}}if(g&&b.appendChild(g),e.hint_text){let t=document.createElement("small");t.className="voc-form-hint",t.textContent=e.hint_text,b.appendChild(t)}return b}i.fields.forEach(e=>{if(e.type==="row"&&"items"in e){let s=document.createElement("div");if(s.className="voc-form-group voc-form-row",a&&s.classList.add("mb-3"),e.label){let r=document.createElement("h3");r.className="voc-form-row-label",r.textContent=e.label,s.appendChild(r)}if(e.description){let r=document.createElement("p");r.className="voc-form-row-description",r.textContent=e.description,s.appendChild(r)}let v=document.createElement("div");v.className="voc-form-row-items "+(e.direction||"horizontal"),s.appendChild(v),e.items.forEach(r=>{let b=F(r);b&&v.appendChild(b)}),u.appendChild(s)}else{let s=F(e);s&&u.appendChild(s)}});let E=document.createElement("button");return E.type="submit",E.className="voc-form-submit",E.textContent="Submit",a&&E.classList.add("btn","btn-primary"),u.appendChild(E),u}let m=f();return d[0].appendChild(m),l&&l.submitConfig&&m.addEventListener("submit",a=>{a.preventDefault(),C(m,l.submitConfig,(u,y)=>{l.onSubmitResult&&l.onSubmitResult(u,y)})}),{element:m,validate:()=>{let a=[];return m.querySelectorAll(":invalid").forEach(y=>{let L=y.validationMessage;a.push({element:y,message:L})}),a},getValues:()=>k(m),onSubmit:a=>{m.addEventListener("submit",u=>{u.preventDefault();let y=k(m);a(y,u)})}}}var w={renderForm:T};var q=w,D={FormsLib:w};0&&(module.exports={FormsLib});
package/dist/main.mjs CHANGED
@@ -1 +1 @@
1
- function L(v,f=!0){let y=new FormData(v);if(f)return y;let d={};return y.forEach((g,c)=>{let a=(g instanceof File,g);Object.prototype.hasOwnProperty.call(d,c)?Array.isArray(d[c])?d[c].push(a):d[c]=[d[c],a]:d[c]=a}),d}function F(v,f,y){let d=[];if(typeof v=="string"?d=Array.from(document.querySelectorAll(v)):d=[v],d.length===0)return null;function g(){let a=y?.style==="bootstrap",m=document.createElement("form");m.classList.add("voc-form"),a&&m.classList.add("needs-validation"),m.id=f.id;let h=document.createElement("h2");h.textContent=f.form_name,m.appendChild(h);function E(e,i){i&&(i.min_length!==void 0&&(e.minLength=i.min_length),i.max_length!==void 0&&(e.maxLength=i.max_length),i.regex&&(e instanceof HTMLInputElement?e.pattern=i.regex:e.dataset.pattern=i.regex))}function w(e){if(!e.key&&!e.label)return null;let i=f.default_values||{},b=e.name||e.key,r=b?i[b]:void 0,u=document.createElement("div");if(u.className="voc-form-group",a&&u.classList.add("mb-3"),e.label){let t=document.createElement("label");t.classList.add("voc-form-label"),a&&t.classList.add("form-label"),e.key?e.type==="radio"?t.htmlFor="":t.htmlFor=`voc-form-${e.key}`:t.htmlFor="",t.textContent=e.label+(e.required?" *":""),u.appendChild(t)}let p=null;switch(e.type){case"checkbox":{let t=document.createElement("div");if(t.className="voc-form-checkbox-group "+(e.direction||"vertical"),e.width&&(t.style.width=e.width),e.options)e.options.forEach((n,s)=>{let l=document.createElement("label");l.classList.add("voc-form-checkbox-label"),a&&l.classList.add("form-check"),l.style.display=e.direction==="horizontal"?"inline-block":"block",e.width&&(l.style.width=e.width);let o=document.createElement("input");o.type="checkbox",o.classList.add("voc-form-checkbox"),a&&o.classList.add("form-check-input"),o.name=e.name||e.key||"",o.value=n.value,e.key&&(o.id=`voc-form-${e.key}-${s}`),e.required&&(o.required=!0),Array.isArray(r)?r.includes(n.value)&&(o.checked=!0):r!==void 0?r===n.value&&(o.checked=!0):e.default===n.value&&(o.checked=!0),l.appendChild(o),l.appendChild(document.createTextNode(n.label)),t.appendChild(l)});else{let n=document.createElement("label");n.classList.add("voc-form-checkbox-label"),a&&n.classList.add("form-check"),e.width&&(n.style.width=e.width);let s=document.createElement("input");s.type="checkbox",s.classList.add("voc-form-checkbox"),(e.key||e.name)&&(s.name=e.name||e.key,e.key&&(s.id=`voc-form-${e.key}`)),a&&s.classList.add("form-check-input"),e.required&&(s.required=!0),r!==void 0?s.checked=!!r:e.default&&(s.checked=!!e.default),n.appendChild(s),n.appendChild(document.createTextNode(e.label||"")),t.appendChild(n)}p=t;break}case"textarea":{let t=document.createElement("textarea");t.classList.add("voc-form-textarea"),a&&t.classList.add("form-control"),(e.key||e.name)&&(t.name=e.name||e.key,e.key&&(t.id=`voc-form-${e.key}`)),e.placeholder&&(t.placeholder=e.placeholder),r!=null?t.value=String(r):e.default!==void 0&&e.default!==null&&(t.value=String(e.default)),e.width&&(t.style.width=e.width),e.required&&(t.required=!0),E(t,e.validation),p=t;break}case"select":{let t=document.createElement("select");if(t.classList.add("voc-form-select"),a&&t.classList.add("form-select"),(e.key||e.name)&&(t.name=e.name||e.key,e.key&&(t.id=`voc-form-${e.key}`)),e.placeholder){let n=document.createElement("option");n.value="",n.textContent=e.placeholder,n.disabled=!0,n.selected=!0,t.appendChild(n)}e.required&&(t.required=!0),e.options&&e.options.forEach(n=>{let s=document.createElement("option");s.value=n.value,s.textContent=n.label,Array.isArray(r)?r.includes(n.value)&&(s.selected=!0):r!==void 0?r===n.value&&(s.selected=!0):e.default===n.value&&(s.selected=!0),t.appendChild(s)}),e.width&&(t.style.width=e.width),p=t;break}case"radio":{let t=document.createElement("div");t.className="voc-form-radio-group "+(e.direction||"vertical"),e.width&&(t.style.width=e.width),e.options&&e.options.forEach((n,s)=>{let l=document.createElement("label");l.classList.add("voc-form-radio-label"),a&&l.classList.add("form-check"),l.style.display=e.direction==="horizontal"?"inline-block":"block",e.width&&(l.style.width=e.width);let o=document.createElement("input");o.type="radio",o.classList.add("voc-form-radio"),a&&o.classList.add("form-check-input"),o.name=e.name||e.key||"",o.value=n.value,e.key&&(o.id=`voc-form-${e.key}-${s}`),Array.isArray(r)?r.includes(n.value)&&(o.checked=!0):r!==void 0?r===n.value&&(o.checked=!0):e.default===n.value&&(o.checked=!0),l.appendChild(o),l.appendChild(document.createTextNode(n.label)),t.appendChild(l)}),p=t;break}case"file":{let t=document.createElement("div");t.className="voc-form-file-wrapper",e.width&&(t.style.width=e.width);let n=document.createElement("input");n.type="file",n.classList.add("voc-form-file"),a&&n.classList.add("form-control"),(e.key||e.name)&&(n.name=e.name||e.key,e.key&&(n.id=`voc-form-${e.key}`)),e.width&&(n.style.width=e.width),e.required&&(n.required=!0),e.accept&&(n.accept=e.accept),e.multiple&&(n.multiple=!0),t.appendChild(n),p=t;break}default:{let t=document.createElement("input");t.type=e.type||"text",t.classList.add("voc-form-input"),a&&t.classList.add("form-control"),(e.key||e.name)&&(t.name=e.name||e.key,e.key&&(t.id=`voc-form-${e.key}`)),e.placeholder&&(t.placeholder=e.placeholder),r!=null?t.value=String(r):e.default!==void 0&&e.default!==null&&(t.value=String(e.default)),e.width&&(t.style.width=e.width),e.required&&(t.required=!0),E(t,e.validation),p=t;break}}if(p&&u.appendChild(p),e.hint_text){let t=document.createElement("small");t.className="voc-form-hint",t.textContent=e.hint_text,u.appendChild(t)}return u}f.fields.forEach(e=>{if(e.type==="row"&&"items"in e){let i=document.createElement("div");if(i.className="voc-form-group voc-form-row",a&&i.classList.add("mb-3"),e.label){let r=document.createElement("h3");r.className="voc-form-row-label",r.textContent=e.label,i.appendChild(r)}if(e.description){let r=document.createElement("p");r.className="voc-form-row-description",r.textContent=e.description,i.appendChild(r)}let b=document.createElement("div");b.className="voc-form-row-items "+(e.direction||"horizontal"),i.appendChild(b),e.items.forEach(r=>{let u=w(r);u&&b.appendChild(u)}),m.appendChild(i)}else{let i=w(e);i&&m.appendChild(i)}});let k=document.createElement("button");return k.type="submit",k.className="voc-form-submit",k.textContent="Submit",a&&k.classList.add("btn","btn-primary"),m.appendChild(k),m}let c=g();return d[0].appendChild(c),y&&y.submitConfig&&c.addEventListener("submit",a=>{a.preventDefault();let m=L(c);console.log(m)}),{element:c,validate:()=>{let a=[];return c.querySelectorAll(":invalid").forEach(h=>{let E=h.validationMessage;a.push({element:h,message:E})}),a},getValues:()=>L(c),onSubmit:a=>{c.addEventListener("submit",m=>{m.preventDefault();let h=L(c);a(h,m)})}}}var x={renderForm:F};var S=x,_={FormsLib:x};export{S as FormsLib,_ as default};
1
+ function k(y,p=!0){let d=new FormData(y);if(p)return d;let u={};return d.forEach((g,c)=>{let a=(g instanceof File,g);Object.prototype.hasOwnProperty.call(u,c)?Array.isArray(u[c])?u[c].push(a):u[c]=[u[c],a]:u[c]=a}),u}async function F(y,p,d){let u=p.api_endpoint,g=y.method||"POST",c={"Content-Type":"application/json"};p.headers&&Object.assign(c,p.headers);let a=JSON.stringify(k(y,!1));try{let h=await(await fetch(u,{method:g,headers:c,body:a,credentials:p.credentials||"same-origin"})).json();d&&d(h,null)}catch(l){d&&d(null,l)}}function C(y,p,d){let u=[];if(typeof y=="string"?u=Array.from(document.querySelectorAll(y)):u=[y],u.length===0)return null;function g(){let a=d?.style==="bootstrap",l=document.createElement("form");l.classList.add("voc-form"),a&&l.classList.add("needs-validation"),l.id=p.id;let h=document.createElement("h2");h.textContent=p.form_name,l.appendChild(h);function L(e,i){i&&(i.min_length!==void 0&&(e.minLength=i.min_length),i.max_length!==void 0&&(e.maxLength=i.max_length),i.regex&&(e instanceof HTMLInputElement?e.pattern=i.regex:e.dataset.pattern=i.regex))}function w(e){if(!e.key&&!e.label)return null;let i=p.default_values||{},v=e.name||e.key,r=v?i[v]:void 0,f=document.createElement("div");if(f.className="voc-form-group",a&&f.classList.add("mb-3"),e.label){let t=document.createElement("label");t.classList.add("voc-form-label"),a&&t.classList.add("form-label"),e.key?e.type==="radio"?t.htmlFor="":t.htmlFor=`voc-form-${e.key}`:t.htmlFor="",t.textContent=e.label+(e.required?" *":""),f.appendChild(t)}let b=null;switch(e.type){case"checkbox":{let t=document.createElement("div");if(t.className="voc-form-checkbox-group "+(e.direction||"vertical"),e.width&&(t.style.width=e.width),e.options)e.options.forEach((n,s)=>{let m=document.createElement("label");m.classList.add("voc-form-checkbox-label"),a&&m.classList.add("form-check"),m.style.display=e.direction==="horizontal"?"inline-block":"block",e.width&&(m.style.width=e.width);let o=document.createElement("input");o.type="checkbox",o.classList.add("voc-form-checkbox"),a&&o.classList.add("form-check-input"),o.name=e.name||e.key||"",o.value=n.value,e.key&&(o.id=`voc-form-${e.key}-${s}`),e.required&&(o.required=!0),Array.isArray(r)?r.includes(n.value)&&(o.checked=!0):r!==void 0?r===n.value&&(o.checked=!0):e.default===n.value&&(o.checked=!0),m.appendChild(o),m.appendChild(document.createTextNode(n.label)),t.appendChild(m)});else{let n=document.createElement("label");n.classList.add("voc-form-checkbox-label"),a&&n.classList.add("form-check"),e.width&&(n.style.width=e.width);let s=document.createElement("input");s.type="checkbox",s.classList.add("voc-form-checkbox"),(e.key||e.name)&&(s.name=e.name||e.key,e.key&&(s.id=`voc-form-${e.key}`)),a&&s.classList.add("form-check-input"),e.required&&(s.required=!0),r!==void 0?s.checked=!!r:e.default&&(s.checked=!!e.default),n.appendChild(s),n.appendChild(document.createTextNode(e.label||"")),t.appendChild(n)}b=t;break}case"textarea":{let t=document.createElement("textarea");t.classList.add("voc-form-textarea"),a&&t.classList.add("form-control"),(e.key||e.name)&&(t.name=e.name||e.key,e.key&&(t.id=`voc-form-${e.key}`)),e.placeholder&&(t.placeholder=e.placeholder),r!=null?t.value=String(r):e.default!==void 0&&e.default!==null&&(t.value=String(e.default)),e.width&&(t.style.width=e.width),e.required&&(t.required=!0),L(t,e.validation),b=t;break}case"select":{let t=document.createElement("select");if(t.classList.add("voc-form-select"),a&&t.classList.add("form-select"),(e.key||e.name)&&(t.name=e.name||e.key,e.key&&(t.id=`voc-form-${e.key}`)),e.placeholder){let n=document.createElement("option");n.value="",n.textContent=e.placeholder,n.disabled=!0,n.selected=!0,t.appendChild(n)}e.required&&(t.required=!0),e.options&&e.options.forEach(n=>{let s=document.createElement("option");s.value=n.value,s.textContent=n.label,Array.isArray(r)?r.includes(n.value)&&(s.selected=!0):r!==void 0?r===n.value&&(s.selected=!0):e.default===n.value&&(s.selected=!0),t.appendChild(s)}),e.width&&(t.style.width=e.width),b=t;break}case"radio":{let t=document.createElement("div");t.className="voc-form-radio-group "+(e.direction||"vertical"),e.width&&(t.style.width=e.width),e.options&&e.options.forEach((n,s)=>{let m=document.createElement("label");m.classList.add("voc-form-radio-label"),a&&m.classList.add("form-check"),m.style.display=e.direction==="horizontal"?"inline-block":"block",e.width&&(m.style.width=e.width);let o=document.createElement("input");o.type="radio",o.classList.add("voc-form-radio"),a&&o.classList.add("form-check-input"),o.name=e.name||e.key||"",o.value=n.value,e.key&&(o.id=`voc-form-${e.key}-${s}`),Array.isArray(r)?r.includes(n.value)&&(o.checked=!0):r!==void 0?r===n.value&&(o.checked=!0):e.default===n.value&&(o.checked=!0),m.appendChild(o),m.appendChild(document.createTextNode(n.label)),t.appendChild(m)}),b=t;break}case"file":{let t=document.createElement("div");t.className="voc-form-file-wrapper",e.width&&(t.style.width=e.width);let n=document.createElement("input");n.type="file",n.classList.add("voc-form-file"),a&&n.classList.add("form-control"),(e.key||e.name)&&(n.name=e.name||e.key,e.key&&(n.id=`voc-form-${e.key}`)),e.width&&(n.style.width=e.width),e.required&&(n.required=!0),e.accept&&(n.accept=e.accept),e.multiple&&(n.multiple=!0),t.appendChild(n),b=t;break}default:{let t=document.createElement("input");t.type=e.type||"text",t.classList.add("voc-form-input"),a&&t.classList.add("form-control"),(e.key||e.name)&&(t.name=e.name||e.key,e.key&&(t.id=`voc-form-${e.key}`)),e.placeholder&&(t.placeholder=e.placeholder),r!=null?t.value=String(r):e.default!==void 0&&e.default!==null&&(t.value=String(e.default)),e.width&&(t.style.width=e.width),e.required&&(t.required=!0),L(t,e.validation),b=t;break}}if(b&&f.appendChild(b),e.hint_text){let t=document.createElement("small");t.className="voc-form-hint",t.textContent=e.hint_text,f.appendChild(t)}return f}p.fields.forEach(e=>{if(e.type==="row"&&"items"in e){let i=document.createElement("div");if(i.className="voc-form-group voc-form-row",a&&i.classList.add("mb-3"),e.label){let r=document.createElement("h3");r.className="voc-form-row-label",r.textContent=e.label,i.appendChild(r)}if(e.description){let r=document.createElement("p");r.className="voc-form-row-description",r.textContent=e.description,i.appendChild(r)}let v=document.createElement("div");v.className="voc-form-row-items "+(e.direction||"horizontal"),i.appendChild(v),e.items.forEach(r=>{let f=w(r);f&&v.appendChild(f)}),l.appendChild(i)}else{let i=w(e);i&&l.appendChild(i)}});let E=document.createElement("button");return E.type="submit",E.className="voc-form-submit",E.textContent="Submit",a&&E.classList.add("btn","btn-primary"),l.appendChild(E),l}let c=g();return u[0].appendChild(c),d&&d.submitConfig&&c.addEventListener("submit",a=>{a.preventDefault(),F(c,d.submitConfig,(l,h)=>{d.onSubmitResult&&d.onSubmitResult(l,h)})}),{element:c,validate:()=>{let a=[];return c.querySelectorAll(":invalid").forEach(h=>{let L=h.validationMessage;a.push({element:h,message:L})}),a},getValues:()=>k(c),onSubmit:a=>{c.addEventListener("submit",l=>{l.preventDefault();let h=k(c);a(h,l)})}}}var x={renderForm:C};var N=x,B={FormsLib:x};export{N as FormsLib,B as default};
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "voc-lib-js",
3
- "version": "1.0.13",
3
+ "version": "1.0.15",
4
4
  "description": "A JavaScript library for VocPhone",
5
5
  "main": "./dist/main.cjs",
6
6
  "module": "./dist/main.mjs",