voc-lib-js 1.0.23 → 1.0.25
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/README.md +2 -4
- package/dist/main.d.mts +1 -2
- package/dist/main.d.ts +1 -2
- package/dist/main.global.js +2 -2
- package/dist/main.js +2 -2
- package/dist/main.mjs +2 -2
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -83,7 +83,7 @@ renderForm(element, data, options?) => RenderedForm | null
|
|
|
83
83
|
- onSubmitResult?: callback that receives (response, error) when submitConfig is used
|
|
84
84
|
- enableAutoSubmit?: boolean
|
|
85
85
|
- style?: "bootstrap" — add Bootstrap classes to inputs
|
|
86
|
-
-
|
|
86
|
+
- submit_config?: SubmitConfig — alternative place to pass submit configuration
|
|
87
87
|
|
|
88
88
|
RenderedForm (returned object)
|
|
89
89
|
|
|
@@ -95,7 +95,6 @@ RenderedForm (returned object)
|
|
|
95
95
|
SubmitConfig
|
|
96
96
|
|
|
97
97
|
- url: string — endpoint to post to
|
|
98
|
-
- method: one of GET | POST | PUT | PATCH | DELETE
|
|
99
98
|
- api_token?: string — optional Authorization token
|
|
100
99
|
- additional_data?: Record<string, any> — merged into payload
|
|
101
100
|
|
|
@@ -228,7 +227,7 @@ rendered.onSubmit((data, event) => {
|
|
|
228
227
|
|
|
229
228
|
Auto-submit
|
|
230
229
|
|
|
231
|
-
If the schema includes `submit_config` or you pass `
|
|
230
|
+
If the schema includes `submit_config` or you pass `submit_config` in options, the library will intercept the form submit and call the internal submit helper which posts to the configured endpoint. You can pass `onSubmitResult` in options to receive (response, error).
|
|
232
231
|
|
|
233
232
|
Example submit_config on the schema:
|
|
234
233
|
|
|
@@ -236,7 +235,6 @@ Example submit_config on the schema:
|
|
|
236
235
|
{
|
|
237
236
|
"submit_config": {
|
|
238
237
|
"url": "https://api.example.com/forms/submit",
|
|
239
|
-
"method": "POST",
|
|
240
238
|
"api_token": "<token>",
|
|
241
239
|
"additional_data": { "source": "website" }
|
|
242
240
|
},
|
package/dist/main.d.mts
CHANGED
|
@@ -42,7 +42,6 @@ type FormData = {
|
|
|
42
42
|
};
|
|
43
43
|
type SubmitConfig = {
|
|
44
44
|
url: string;
|
|
45
|
-
method: "GET" | "POST" | "PUT" | "PATCH" | "DELETE";
|
|
46
45
|
api_token?: string;
|
|
47
46
|
additional_data?: Record<string, any>;
|
|
48
47
|
};
|
|
@@ -60,7 +59,7 @@ declare function renderForm(element: string | HTMLElement, data: FormData, optio
|
|
|
60
59
|
onSubmitResult?: (response: any, error: Error | null) => void;
|
|
61
60
|
enableAutoSubmit?: boolean;
|
|
62
61
|
style?: "bootstrap" | undefined | null;
|
|
63
|
-
|
|
62
|
+
submit_config?: SubmitConfig;
|
|
64
63
|
}): RenderedForm | null;
|
|
65
64
|
|
|
66
65
|
declare const FormsLib: {
|
package/dist/main.d.ts
CHANGED
|
@@ -42,7 +42,6 @@ type FormData = {
|
|
|
42
42
|
};
|
|
43
43
|
type SubmitConfig = {
|
|
44
44
|
url: string;
|
|
45
|
-
method: "GET" | "POST" | "PUT" | "PATCH" | "DELETE";
|
|
46
45
|
api_token?: string;
|
|
47
46
|
additional_data?: Record<string, any>;
|
|
48
47
|
};
|
|
@@ -60,7 +59,7 @@ declare function renderForm(element: string | HTMLElement, data: FormData, optio
|
|
|
60
59
|
onSubmitResult?: (response: any, error: Error | null) => void;
|
|
61
60
|
enableAutoSubmit?: boolean;
|
|
62
61
|
style?: "bootstrap" | undefined | null;
|
|
63
|
-
|
|
62
|
+
submit_config?: SubmitConfig;
|
|
64
63
|
}): RenderedForm | null;
|
|
65
64
|
|
|
66
65
|
declare const FormsLib: {
|
package/dist/main.global.js
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
"use strict";var VocLibJs=(()=>{var w=Object.defineProperty;var
|
|
2
|
-
`).reduce((
|
|
1
|
+
"use strict";var VocLibJs=(()=>{var w=Object.defineProperty;var H=Object.getOwnPropertyDescriptor;var R=Object.getOwnPropertyNames;var q=Object.prototype.hasOwnProperty;var N=(h,i)=>{for(var d in i)w(h,d,{get:i[d],enumerable:!0})},$=(h,i,d,u)=>{if(i&&typeof i=="object"||typeof i=="function")for(let y of R(i))!q.call(h,y)&&y!==d&&w(h,y,{get:()=>i[y],enumerable:!(u=H(i,y))||u.enumerable});return h};var B=h=>$(w({},"__esModule",{value:!0}),h);var O={};N(O,{FormsLib:()=>V,default:()=>z});function L(h,i=!0){let d=new FormData(h);if(i)return d;let u={};return d.forEach((y,l)=>{let b=(y instanceof File,y);Object.prototype.hasOwnProperty.call(u,l)?Array.isArray(u[l])?u[l].push(b):u[l]=[u[l],b]:u[l]=b}),u}async function T(h,i,d){let u=i.url,y={"Content-Type":"application/json"};i?.api_token&&(y.Authorization=`Bearer ${i.api_token}`);let l=L(h);if(i?.additional_data)for(let[a,f]of Object.entries(i.additional_data))l.append(a,f);let b,o=l instanceof FormData;b={method:"POST",headers:{Authorization:`Bearer ${i.api_token}`,...o?{}:{"Content-Type":"application/json"},Accept:"application/json"},body:o?l:JSON.stringify(Object.fromEntries(l.entries()))};try{fetch(u,b).then(a=>{let f=a.headers.get("content-type");if(!a.ok){let k=a.text();throw new Error(`HTTP ${a.status}: ${k}`)}return f&&f.includes("application/json")?a.json():a.text()}).then(a=>{d&&d(a,null)}).catch(a=>{d&&d(null,a)})}catch(a){d&&d(null,a)}}function _(h,i,d){let u=[];if(typeof h=="string"?u=Array.from(document.querySelectorAll(h)):u=[h],u.length===0)return null;function y(){let o=d?.style==="bootstrap",a=document.createElement("form");a.classList.add("voc-form"),o&&a.classList.add("needs-validation"),a.id=i.id;let f=document.createElement("h2");f.textContent=i.form_name,a.appendChild(f);function k(e,m){m&&(m.min_length!==void 0&&(e.minLength=m.min_length),m.max_length!==void 0&&(e.maxLength=m.max_length),m.regex&&(e instanceof HTMLInputElement?e.pattern=m.regex:e.dataset.pattern=m.regex))}function C(e){if(!e.key&&!e.label)return null;let m=i.default_values||{},E=e.name||e.key,r=E?m[E]:void 0,g=document.createElement("div");if(g.className="voc-form-group",o&&g.classList.add("mb-3"),e.label){let t=document.createElement("label");t.classList.add("voc-form-label"),o&&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?" *":""),g.appendChild(t)}let v=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 p=document.createElement("label");p.classList.add("voc-form-checkbox-label"),o&&p.classList.add("form-check"),p.style.display=e.direction==="horizontal"?"inline-block":"block",e.width&&(p.style.width=e.width);let s=document.createElement("input");s.type="checkbox",s.classList.add("voc-form-checkbox"),o&&s.classList.add("form-check-input"),s.name=e.name||e.key||"",s.value=n.value,e.key&&(s.id=`voc-form-${e.key}-${c}`),e.required&&(s.required=!0),Array.isArray(r)?r.includes(n.value)&&(s.checked=!0):r!==void 0?r===n.value&&(s.checked=!0):e.default===n.value&&(s.checked=!0),p.appendChild(s),p.appendChild(document.createTextNode(n.label)),t.appendChild(p)});else{let n=document.createElement("label");n.classList.add("voc-form-checkbox-label"),o&&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}`)),o&&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)}v=t;break}case"textarea":{let t=document.createElement("textarea");t.classList.add("voc-form-textarea"),o&&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));let n=e.default||r||"";if(n){let c=(n.match(/\n/g)||[]).length,p=80,s=n.split(`
|
|
2
|
+
`).reduce((A,D)=>Math.max(A,D.length),0),M=Math.ceil(s/p)||1,S=Math.max(2,c+M);t.rows=S,t.style.resize="vertical"}e.width&&(t.style.width=e.width),e.required&&(t.required=!0),k(t,e.validation),v=t;break}case"select":{let t=document.createElement("select");if(t.classList.add("voc-form-select"),o&&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),v=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 p=document.createElement("label");p.classList.add("voc-form-radio-label"),o&&p.classList.add("form-check"),p.style.display=e.direction==="horizontal"?"inline-block":"block",e.width&&(p.style.width=e.width);let s=document.createElement("input");s.type="radio",s.classList.add("voc-form-radio"),o&&s.classList.add("form-check-input"),s.name=e.name||e.key||"",s.value=n.value,e.key&&(s.id=`voc-form-${e.key}-${c}`),Array.isArray(r)?r.includes(n.value)&&(s.checked=!0):r!==void 0?r===n.value&&(s.checked=!0):e.default===n.value&&(s.checked=!0),p.appendChild(s),p.appendChild(document.createTextNode(n.label)),t.appendChild(p)}),v=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"),o&&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),v=t;break}default:{let t=document.createElement("input");t.type=e.type||"text",t.classList.add("voc-form-input"),o&&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),k(t,e.validation),v=t;break}}if(v&&g.appendChild(v),e.hint_text){let t=document.createElement("small");t.className="voc-form-hint",t.textContent=e.hint_text,g.appendChild(t)}return g}i.fields.forEach(e=>{if(e.type==="row"&&"items"in e){let m=document.createElement("div");if(m.className="voc-form-group voc-form-row",o&&m.classList.add("mb-3"),e.label){let r=document.createElement("h3");r.className="voc-form-row-label",r.textContent=e.label,m.appendChild(r)}if(e.description){let r=document.createElement("p");r.className="voc-form-row-description",r.textContent=e.description,m.appendChild(r)}let E=document.createElement("div");E.className="voc-form-row-items "+(e.direction||"horizontal"),m.appendChild(E),e.items.forEach(r=>{let g=C(r);g&&E.appendChild(g)}),a.appendChild(m)}else{let m=C(e);m&&a.appendChild(m)}});let x=document.createElement("button");return x.type="submit",x.className="voc-form-submit",x.textContent="Submit",o&&x.classList.add("btn","btn-primary"),a.appendChild(x),a}let l=y();u[0].appendChild(l);let b=i?.submit_config??d?.submit_config??null;return b&&l.addEventListener("submit",o=>{o.preventDefault(),T(l,b,(a,f)=>{d?.onSubmitResult&&d.onSubmitResult(a,f)})}),{element:l,validate:()=>{let o=[];return l.querySelectorAll(":invalid").forEach(f=>{let k=f.validationMessage;o.push({element:f,message:k})}),o},getValues:()=>L(l),onSubmit:o=>{l.addEventListener("submit",a=>{a.preventDefault();let f=L(l);o(f,a)})}}}var F={renderForm:_};var V=F,z={FormsLib:F};return B(O);})();
|
package/dist/main.js
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
"use strict";var w=Object.defineProperty;var
|
|
2
|
-
`).reduce((
|
|
1
|
+
"use strict";var w=Object.defineProperty;var H=Object.getOwnPropertyDescriptor;var R=Object.getOwnPropertyNames;var q=Object.prototype.hasOwnProperty;var N=(h,i)=>{for(var d in i)w(h,d,{get:i[d],enumerable:!0})},$=(h,i,d,u)=>{if(i&&typeof i=="object"||typeof i=="function")for(let y of R(i))!q.call(h,y)&&y!==d&&w(h,y,{get:()=>i[y],enumerable:!(u=H(i,y))||u.enumerable});return h};var B=h=>$(w({},"__esModule",{value:!0}),h);var O={};N(O,{FormsLib:()=>V,default:()=>z});module.exports=B(O);function L(h,i=!0){let d=new FormData(h);if(i)return d;let u={};return d.forEach((y,l)=>{let b=(y instanceof File,y);Object.prototype.hasOwnProperty.call(u,l)?Array.isArray(u[l])?u[l].push(b):u[l]=[u[l],b]:u[l]=b}),u}async function T(h,i,d){let u=i.url,y={"Content-Type":"application/json"};i?.api_token&&(y.Authorization=`Bearer ${i.api_token}`);let l=L(h);if(i?.additional_data)for(let[a,f]of Object.entries(i.additional_data))l.append(a,f);let b,o=l instanceof FormData;b={method:"POST",headers:{Authorization:`Bearer ${i.api_token}`,...o?{}:{"Content-Type":"application/json"},Accept:"application/json"},body:o?l:JSON.stringify(Object.fromEntries(l.entries()))};try{fetch(u,b).then(a=>{let f=a.headers.get("content-type");if(!a.ok){let k=a.text();throw new Error(`HTTP ${a.status}: ${k}`)}return f&&f.includes("application/json")?a.json():a.text()}).then(a=>{d&&d(a,null)}).catch(a=>{d&&d(null,a)})}catch(a){d&&d(null,a)}}function _(h,i,d){let u=[];if(typeof h=="string"?u=Array.from(document.querySelectorAll(h)):u=[h],u.length===0)return null;function y(){let o=d?.style==="bootstrap",a=document.createElement("form");a.classList.add("voc-form"),o&&a.classList.add("needs-validation"),a.id=i.id;let f=document.createElement("h2");f.textContent=i.form_name,a.appendChild(f);function k(e,m){m&&(m.min_length!==void 0&&(e.minLength=m.min_length),m.max_length!==void 0&&(e.maxLength=m.max_length),m.regex&&(e instanceof HTMLInputElement?e.pattern=m.regex:e.dataset.pattern=m.regex))}function C(e){if(!e.key&&!e.label)return null;let m=i.default_values||{},E=e.name||e.key,r=E?m[E]:void 0,g=document.createElement("div");if(g.className="voc-form-group",o&&g.classList.add("mb-3"),e.label){let t=document.createElement("label");t.classList.add("voc-form-label"),o&&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?" *":""),g.appendChild(t)}let v=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 p=document.createElement("label");p.classList.add("voc-form-checkbox-label"),o&&p.classList.add("form-check"),p.style.display=e.direction==="horizontal"?"inline-block":"block",e.width&&(p.style.width=e.width);let s=document.createElement("input");s.type="checkbox",s.classList.add("voc-form-checkbox"),o&&s.classList.add("form-check-input"),s.name=e.name||e.key||"",s.value=n.value,e.key&&(s.id=`voc-form-${e.key}-${c}`),e.required&&(s.required=!0),Array.isArray(r)?r.includes(n.value)&&(s.checked=!0):r!==void 0?r===n.value&&(s.checked=!0):e.default===n.value&&(s.checked=!0),p.appendChild(s),p.appendChild(document.createTextNode(n.label)),t.appendChild(p)});else{let n=document.createElement("label");n.classList.add("voc-form-checkbox-label"),o&&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}`)),o&&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)}v=t;break}case"textarea":{let t=document.createElement("textarea");t.classList.add("voc-form-textarea"),o&&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));let n=e.default||r||"";if(n){let c=(n.match(/\n/g)||[]).length,p=80,s=n.split(`
|
|
2
|
+
`).reduce((A,D)=>Math.max(A,D.length),0),M=Math.ceil(s/p)||1,S=Math.max(2,c+M);t.rows=S,t.style.resize="vertical"}e.width&&(t.style.width=e.width),e.required&&(t.required=!0),k(t,e.validation),v=t;break}case"select":{let t=document.createElement("select");if(t.classList.add("voc-form-select"),o&&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),v=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 p=document.createElement("label");p.classList.add("voc-form-radio-label"),o&&p.classList.add("form-check"),p.style.display=e.direction==="horizontal"?"inline-block":"block",e.width&&(p.style.width=e.width);let s=document.createElement("input");s.type="radio",s.classList.add("voc-form-radio"),o&&s.classList.add("form-check-input"),s.name=e.name||e.key||"",s.value=n.value,e.key&&(s.id=`voc-form-${e.key}-${c}`),Array.isArray(r)?r.includes(n.value)&&(s.checked=!0):r!==void 0?r===n.value&&(s.checked=!0):e.default===n.value&&(s.checked=!0),p.appendChild(s),p.appendChild(document.createTextNode(n.label)),t.appendChild(p)}),v=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"),o&&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),v=t;break}default:{let t=document.createElement("input");t.type=e.type||"text",t.classList.add("voc-form-input"),o&&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),k(t,e.validation),v=t;break}}if(v&&g.appendChild(v),e.hint_text){let t=document.createElement("small");t.className="voc-form-hint",t.textContent=e.hint_text,g.appendChild(t)}return g}i.fields.forEach(e=>{if(e.type==="row"&&"items"in e){let m=document.createElement("div");if(m.className="voc-form-group voc-form-row",o&&m.classList.add("mb-3"),e.label){let r=document.createElement("h3");r.className="voc-form-row-label",r.textContent=e.label,m.appendChild(r)}if(e.description){let r=document.createElement("p");r.className="voc-form-row-description",r.textContent=e.description,m.appendChild(r)}let E=document.createElement("div");E.className="voc-form-row-items "+(e.direction||"horizontal"),m.appendChild(E),e.items.forEach(r=>{let g=C(r);g&&E.appendChild(g)}),a.appendChild(m)}else{let m=C(e);m&&a.appendChild(m)}});let x=document.createElement("button");return x.type="submit",x.className="voc-form-submit",x.textContent="Submit",o&&x.classList.add("btn","btn-primary"),a.appendChild(x),a}let l=y();u[0].appendChild(l);let b=i?.submit_config??d?.submit_config??null;return b&&l.addEventListener("submit",o=>{o.preventDefault(),T(l,b,(a,f)=>{d?.onSubmitResult&&d.onSubmitResult(a,f)})}),{element:l,validate:()=>{let o=[];return l.querySelectorAll(":invalid").forEach(f=>{let k=f.validationMessage;o.push({element:f,message:k})}),o},getValues:()=>L(l),onSubmit:o=>{l.addEventListener("submit",a=>{a.preventDefault();let f=L(l);o(f,a)})}}}var F={renderForm:_};var V=F,z={FormsLib:F};0&&(module.exports={FormsLib});
|
package/dist/main.mjs
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
function L(g,d=!0){let u=new FormData(g);if(d)return u;let p={};return u.forEach((v,
|
|
2
|
-
`).reduce((
|
|
1
|
+
function L(g,d=!0){let u=new FormData(g);if(d)return u;let p={};return u.forEach((v,c)=>{let f=(v instanceof File,v);Object.prototype.hasOwnProperty.call(p,c)?Array.isArray(p[c])?p[c].push(f):p[c]=[p[c],f]:p[c]=f}),p}async function C(g,d,u){let p=d.url,v={"Content-Type":"application/json"};d?.api_token&&(v.Authorization=`Bearer ${d.api_token}`);let c=L(g);if(d?.additional_data)for(let[a,h]of Object.entries(d.additional_data))c.append(a,h);let f,o=c instanceof FormData;f={method:"POST",headers:{Authorization:`Bearer ${d.api_token}`,...o?{}:{"Content-Type":"application/json"},Accept:"application/json"},body:o?c:JSON.stringify(Object.fromEntries(c.entries()))};try{fetch(p,f).then(a=>{let h=a.headers.get("content-type");if(!a.ok){let k=a.text();throw new Error(`HTTP ${a.status}: ${k}`)}return h&&h.includes("application/json")?a.json():a.text()}).then(a=>{u&&u(a,null)}).catch(a=>{u&&u(null,a)})}catch(a){u&&u(null,a)}}function T(g,d,u){let p=[];if(typeof g=="string"?p=Array.from(document.querySelectorAll(g)):p=[g],p.length===0)return null;function v(){let o=u?.style==="bootstrap",a=document.createElement("form");a.classList.add("voc-form"),o&&a.classList.add("needs-validation"),a.id=d.id;let h=document.createElement("h2");h.textContent=d.form_name,a.appendChild(h);function k(e,l){l&&(l.min_length!==void 0&&(e.minLength=l.min_length),l.max_length!==void 0&&(e.maxLength=l.max_length),l.regex&&(e instanceof HTMLInputElement?e.pattern=l.regex:e.dataset.pattern=l.regex))}function F(e){if(!e.key&&!e.label)return null;let l=d.default_values||{},E=e.name||e.key,r=E?l[E]:void 0,y=document.createElement("div");if(y.className="voc-form-group",o&&y.classList.add("mb-3"),e.label){let t=document.createElement("label");t.classList.add("voc-form-label"),o&&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 m=document.createElement("label");m.classList.add("voc-form-checkbox-label"),o&&m.classList.add("form-check"),m.style.display=e.direction==="horizontal"?"inline-block":"block",e.width&&(m.style.width=e.width);let i=document.createElement("input");i.type="checkbox",i.classList.add("voc-form-checkbox"),o&&i.classList.add("form-check-input"),i.name=e.name||e.key||"",i.value=n.value,e.key&&(i.id=`voc-form-${e.key}-${s}`),e.required&&(i.required=!0),Array.isArray(r)?r.includes(n.value)&&(i.checked=!0):r!==void 0?r===n.value&&(i.checked=!0):e.default===n.value&&(i.checked=!0),m.appendChild(i),m.appendChild(document.createTextNode(n.label)),t.appendChild(m)});else{let n=document.createElement("label");n.classList.add("voc-form-checkbox-label"),o&&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}`)),o&&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"),o&&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));let n=e.default||r||"";if(n){let s=(n.match(/\n/g)||[]).length,m=80,i=n.split(`
|
|
2
|
+
`).reduce((S,A)=>Math.max(S,A.length),0),_=Math.ceil(i/m)||1,M=Math.max(2,s+_);t.rows=M,t.style.resize="vertical"}e.width&&(t.style.width=e.width),e.required&&(t.required=!0),k(t,e.validation),b=t;break}case"select":{let t=document.createElement("select");if(t.classList.add("voc-form-select"),o&&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"),o&&m.classList.add("form-check"),m.style.display=e.direction==="horizontal"?"inline-block":"block",e.width&&(m.style.width=e.width);let i=document.createElement("input");i.type="radio",i.classList.add("voc-form-radio"),o&&i.classList.add("form-check-input"),i.name=e.name||e.key||"",i.value=n.value,e.key&&(i.id=`voc-form-${e.key}-${s}`),Array.isArray(r)?r.includes(n.value)&&(i.checked=!0):r!==void 0?r===n.value&&(i.checked=!0):e.default===n.value&&(i.checked=!0),m.appendChild(i),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"),o&&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"),o&&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),k(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}d.fields.forEach(e=>{if(e.type==="row"&&"items"in e){let l=document.createElement("div");if(l.className="voc-form-group voc-form-row",o&&l.classList.add("mb-3"),e.label){let r=document.createElement("h3");r.className="voc-form-row-label",r.textContent=e.label,l.appendChild(r)}if(e.description){let r=document.createElement("p");r.className="voc-form-row-description",r.textContent=e.description,l.appendChild(r)}let E=document.createElement("div");E.className="voc-form-row-items "+(e.direction||"horizontal"),l.appendChild(E),e.items.forEach(r=>{let y=F(r);y&&E.appendChild(y)}),a.appendChild(l)}else{let l=F(e);l&&a.appendChild(l)}});let x=document.createElement("button");return x.type="submit",x.className="voc-form-submit",x.textContent="Submit",o&&x.classList.add("btn","btn-primary"),a.appendChild(x),a}let c=v();p[0].appendChild(c);let f=d?.submit_config??u?.submit_config??null;return f&&c.addEventListener("submit",o=>{o.preventDefault(),C(c,f,(a,h)=>{u?.onSubmitResult&&u.onSubmitResult(a,h)})}),{element:c,validate:()=>{let o=[];return c.querySelectorAll(":invalid").forEach(h=>{let k=h.validationMessage;o.push({element:h,message:k})}),o},getValues:()=>L(c),onSubmit:o=>{c.addEventListener("submit",a=>{a.preventDefault();let h=L(c);o(h,a)})}}}var w={renderForm:T};var O=w,j={FormsLib:w};export{O as FormsLib,j as default};
|