voc-lib-js 1.0.10 → 1.0.12

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 CHANGED
@@ -76,32 +76,120 @@ onMounted(() => {
76
76
 
77
77
  ## EVENTS
78
78
 
79
- We have a onSubmit event that when user clicked on the submit button it will return the data from the form.
79
+ The library emits an `onSubmit` event when the user submits the form. By default the library returns a `FormData` instance. If you prefer a plain object, call `getValues(false)` or convert the FormData yourself.
80
+
81
+ Basic example (FormData is returned by default):
80
82
 
81
83
  ```html
82
84
  <script src="./../../dist/main.global.js"></script>
83
85
  <script>
84
- const formRendered = VocLibJs.FormsLib.renderForm("#render-form");
86
+ const formRendered = VocLibJs.FormsLib.renderForm("#render-form", jsonObject);
85
87
 
86
- // Listen for the submit event
87
- // the onSubmit will return the form data in object
88
+ // onSubmit receives the same type returned by getValues() — FormData by default
88
89
  formRendered.onSubmit((data) => {
89
- console.log(data);
90
- /**
91
- * example output
92
- * {
93
- * "first_name": "John",
94
- * "last_name": "Doe",
95
- * "username": "johndoe",
96
- * "password": "password123",
97
- * "text_area": "This is a text area",
98
- * "email": "3ZJ3f@example.com",
99
- * "gender": "male",
100
- * "country": "usa"
101
- * }
102
- */
103
-
104
- // Do something with the data
90
+ // `data` is a FormData instance
91
+ console.log(data.get('first_name'));
92
+
93
+ // Example: read all values for a multi-value field
94
+ console.log(data.getAll('hobbies'));
95
+
96
+ // For file inputs, FormData contains File objects
97
+ const file = data.get('upload'); // single File or null
98
+ const files = data.getAll('upload'); // array of File objects
99
+
100
+ // If you want a plain object instead of FormData, call getValues(false)
101
+ const obj = formRendered.getValues(false);
102
+ console.log(obj);
105
103
  });
106
104
  </script>
107
105
  ```
106
+
107
+ Example output (converted to a plain object for readability):
108
+
109
+ ```js
110
+ {
111
+ first_name: 'John',
112
+ last_name: 'Doe',
113
+ username: 'johndoe',
114
+ email: 'john@example.com',
115
+ gender: 'male',
116
+ country: 'usa',
117
+ // file fields will contain File objects when using FormData
118
+ }
119
+ ```
120
+
121
+ Notes
122
+ - FormData is convenient for uploads (it serializes files and other fields for multipart/form-data POSTs).
123
+ - Use `getValues(false)` to receive a plain object where repeated keys become arrays and File objects are left as File instances.
124
+ - Placeholders and default values are supported via the field schema (`placeholder` and `default`). File inputs cannot be prefilled by the browser for security reasons.
125
+
126
+ # Options
127
+
128
+ ## Form Name (optional)
129
+
130
+ This will be the title of the form. If this property is provided it will show up in the top of the form as a title.
131
+
132
+ ```json
133
+ {
134
+ "form_name": "User Registration",
135
+ ...
136
+ }
137
+ ```
138
+
139
+ This will generate a h2 tag inside the form as an h2 tag.
140
+
141
+ ```html
142
+ <h2>User Registration</h2>
143
+ ```
144
+
145
+ ## Fields
146
+
147
+ This fields are the fields of the form. This is an array of objects.
148
+
149
+ example
150
+
151
+ ```json
152
+ {
153
+ ...
154
+ "fields": [
155
+ {
156
+ "label": "First Name",
157
+ "key": "first_name",
158
+ "type": "text",
159
+ "required": true
160
+ },
161
+ {
162
+ "label": "Last Name",
163
+ "key": "last_name",
164
+ "type": "text",
165
+ "required": true
166
+ },
167
+ ...
168
+ ]
169
+ }
170
+ ```
171
+
172
+ ### Field Options
173
+
174
+ - label: string - the label of the field
175
+ - key: string - the key of the field this will be used as the name of the input as well
176
+ - type: string - the type of the field
177
+ - text
178
+ - email
179
+ - password
180
+ - textarea
181
+ - radio
182
+ - select
183
+ - row - this will contain an array of fields. Row will be rendered in a horizontal layout.
184
+ - required: boolean
185
+ - validation: object
186
+ - hint_text: string - shows a small text just bellow the input field
187
+ - direction: string - horizontal or vertical - this mostly works when type is `row`, `radio`, or `checkbox` this will be the direction of the row.
188
+ - items: array - this will be used when type is `row`. the array contains object of fields.
189
+ - options: array - this will be used when type is `radio` or `select`. the array contains object of options.
190
+ - label: string - the label of the option
191
+ - value: string - the value of the option
192
+ - validation: object
193
+ - min_length: number
194
+ - max_length: number
195
+ - regex: string
package/dist/main.d.mts CHANGED
@@ -9,7 +9,9 @@ type Option = {
9
9
  };
10
10
  type BaseField = {
11
11
  label?: string;
12
- key?: string;
12
+ key: string;
13
+ placeholder?: string;
14
+ name?: string;
13
15
  type?: string;
14
16
  required?: boolean;
15
17
  validation?: Validation;
@@ -17,6 +19,9 @@ type BaseField = {
17
19
  direction?: "horizontal" | "vertical";
18
20
  options?: Option[];
19
21
  default?: string;
22
+ width?: string;
23
+ accept?: string;
24
+ multiple?: boolean;
20
25
  description?: string;
21
26
  };
22
27
  type RowField = {
@@ -39,14 +44,13 @@ interface RenderedForm {
39
44
  element: HTMLElement;
40
45
  message: string;
41
46
  }>;
42
- getValues: () => {
43
- [key: string]: FormDataEntryValue | FormDataEntryValue[];
44
- };
45
- onSubmit: (callback: (data: any, event: Event) => void) => void;
47
+ getValues: (asFormData?: boolean) => globalThis.FormData | Record<string, any>;
48
+ onSubmit: (callback: (data: globalThis.FormData | Record<string, any>, event: Event) => void) => void;
46
49
  }
47
50
  declare function renderForm(element: string | HTMLElement, data: FormData, options?: {
48
51
  onSubmit?: (result: any, error?: any) => void;
49
52
  enableAutoSubmit?: boolean;
53
+ style?: "bootstrap" | undefined | null;
50
54
  }): RenderedForm | null;
51
55
 
52
56
  declare const FormsLib: {
package/dist/main.d.ts CHANGED
@@ -9,7 +9,9 @@ type Option = {
9
9
  };
10
10
  type BaseField = {
11
11
  label?: string;
12
- key?: string;
12
+ key: string;
13
+ placeholder?: string;
14
+ name?: string;
13
15
  type?: string;
14
16
  required?: boolean;
15
17
  validation?: Validation;
@@ -17,6 +19,9 @@ type BaseField = {
17
19
  direction?: "horizontal" | "vertical";
18
20
  options?: Option[];
19
21
  default?: string;
22
+ width?: string;
23
+ accept?: string;
24
+ multiple?: boolean;
20
25
  description?: string;
21
26
  };
22
27
  type RowField = {
@@ -39,14 +44,13 @@ interface RenderedForm {
39
44
  element: HTMLElement;
40
45
  message: string;
41
46
  }>;
42
- getValues: () => {
43
- [key: string]: FormDataEntryValue | FormDataEntryValue[];
44
- };
45
- onSubmit: (callback: (data: any, event: Event) => void) => void;
47
+ getValues: (asFormData?: boolean) => globalThis.FormData | Record<string, any>;
48
+ onSubmit: (callback: (data: globalThis.FormData | Record<string, any>, event: Event) => void) => void;
46
49
  }
47
50
  declare function renderForm(element: string | HTMLElement, data: FormData, options?: {
48
51
  onSubmit?: (result: any, error?: any) => void;
49
52
  enableAutoSubmit?: boolean;
53
+ style?: "bootstrap" | undefined | null;
50
54
  }): RenderedForm | null;
51
55
 
52
56
  declare const FormsLib: {
@@ -1 +1 @@
1
- "use strict";var VocLibJs=(()=>{var k=Object.defineProperty;var L=Object.getOwnPropertyDescriptor;var F=Object.getOwnPropertyNames;var C=Object.prototype.hasOwnProperty;var T=(i,a)=>{for(var p in a)k(i,p,{get:a[p],enumerable:!0})},M=(i,a,p,d)=>{if(a&&typeof a=="object"||typeof a=="function")for(let u of F(a))!C.call(i,u)&&u!==p&&k(i,u,{get:()=>a[u],enumerable:!(d=L(a,u))||d.enumerable});return i};var H=i=>M(k({},"__esModule",{value:!0}),i);var N={};T(N,{FormsLib:()=>w,default:()=>q});function x(i){let a=new FormData(i),p={};return a.forEach((d,u)=>{p[u]=d}),p}function f(i,a,p){let d=[];if(typeof i=="string"?d=Array.from(document.querySelectorAll(i)):d=[i],d.length===0)return null;function u(){let s=document.createElement("form");s.classList.add("voc-form"),s.id=a.id;let y=document.createElement("h2");y.textContent=a.form_name,s.appendChild(y);function h(e,n){n&&(n.min_length!==void 0&&(e.minLength=n.min_length),n.max_length!==void 0&&(e.maxLength=n.max_length),n.regex&&(e instanceof HTMLInputElement?e.pattern=n.regex:e.dataset.pattern=n.regex))}function v(e){if(!e.key&&!e.label)return null;let n=document.createElement("div");if(n.className="voc-form-group",e.label){let t=document.createElement("label");t.classList.add("voc-form-label"),e.key?e.type==="radio"?t.htmlFor="":t.htmlFor=`voc-form-${e.key}`:t.htmlFor="",t.textContent=e.label+(e.required?" *":""),n.appendChild(t)}let l=null;switch(e.type){case"checkbox":{let t=document.createElement("div");if(t.className="voc-form-checkbox-group "+(e.direction||"vertical"),e.options)e.options.forEach((r,o)=>{let m=document.createElement("label");m.classList.add("voc-form-checkbox-label"),m.style.display=e.direction==="horizontal"?"inline-block":"block";let c=document.createElement("input");c.type="checkbox",c.classList.add("voc-form-checkbox"),c.name=e.key||"",c.value=r.value,e.key&&(c.id=`voc-form-${e.key}-${o}`),e.required&&(c.required=!0),e.default===r.value&&(c.checked=!0),m.appendChild(c),m.appendChild(document.createTextNode(r.label)),t.appendChild(m)});else{let r=document.createElement("label");r.classList.add("voc-form-checkbox-label");let o=document.createElement("input");o.type="checkbox",o.classList.add("voc-form-checkbox"),e.key&&(o.name=e.key,o.id=`voc-form-${e.key}`),e.required&&(o.required=!0),e.default&&(o.checked=!!e.default),r.appendChild(o),r.appendChild(document.createTextNode(e.label||"")),t.appendChild(r)}l=t;break}case"textarea":{let t=document.createElement("textarea");t.classList.add("voc-form-textarea"),e.key&&(t.name=e.key,t.id=`voc-form-${e.key}`),e.required&&(t.required=!0),h(t,e.validation),l=t;break}case"select":{let t=document.createElement("select");t.classList.add("voc-form-select"),e.key&&(t.name=e.key,t.id=`voc-form-${e.key}`),e.required&&(t.required=!0),e.options&&e.options.forEach(r=>{let o=document.createElement("option");o.value=r.value,o.textContent=r.label,e.default===r.value&&(o.selected=!0),t.appendChild(o)}),l=t;break}case"radio":{let t=document.createElement("div");t.className="voc-form-radio-group "+(e.direction||"vertical"),e.options&&e.options.forEach((r,o)=>{let m=document.createElement("label");m.classList.add("voc-form-radio-label"),m.style.display=e.direction==="horizontal"?"inline-block":"block";let c=document.createElement("input");c.type="radio",c.classList.add("voc-form-radio"),c.name=e.key||"",c.value=r.value,e.key&&(c.id=`voc-form-${e.key}-${o}`),m.appendChild(c),m.appendChild(document.createTextNode(r.label)),t.appendChild(m)}),l=t;break}default:{let t=document.createElement("input");t.type=e.type||"text",t.classList.add("voc-form-input"),e.key&&(t.name=e.key,t.id=`voc-form-${e.key}`),e.required&&(t.required=!0),h(t,e.validation),l=t;break}}if(l&&n.appendChild(l),e.hint_text){let t=document.createElement("small");t.className="voc-form-hint",t.textContent=e.hint_text,n.appendChild(t)}return n}a.fields.forEach(e=>{if(e.type==="row"&&"items"in e){let n=document.createElement("div");if(n.className="voc-form-row",e.label){let t=document.createElement("h3");t.className="voc-form-row-label",t.textContent=e.label,n.appendChild(t)}if(e.description){let t=document.createElement("p");t.className="voc-form-row-description",t.textContent=e.description,n.appendChild(t)}let l=document.createElement("div");l.className="voc-form-row-items "+(e.direction||"vertical"),n.appendChild(l),e.items.forEach(t=>{let r=v(t);r&&l.appendChild(r)}),s.appendChild(n)}else{let n=v(e);n&&s.appendChild(n)}});let E=document.createElement("button");return E.type="submit",E.className="voc-form-submit",E.textContent="Submit",s.appendChild(E),s}let b=u();return d[0].appendChild(b),{element:b,validate:()=>{let s=[];return b.querySelectorAll(":invalid").forEach(h=>{let v=h.validationMessage;s.push({element:h,message:v})}),s},getValues:()=>x(b),onSubmit:s=>{b.addEventListener("submit",y=>{y.preventDefault();let h=x(b);s(h,y)})}}}var g={renderForm:f};var w=g,q={FormsLib:g};return H(N);})();
1
+ "use strict";var VocLibJs=(()=>{var g=Object.defineProperty;var w=Object.getOwnPropertyDescriptor;var F=Object.getOwnPropertyNames;var C=Object.prototype.hasOwnProperty;var T=(m,s)=>{for(var b in s)g(m,b,{get:s[b],enumerable:!0})},M=(m,s,b,i)=>{if(s&&typeof s=="object"||typeof s=="function")for(let h of F(s))!C.call(m,h)&&h!==b&&g(m,h,{get:()=>s[h],enumerable:!(i=w(s,h))||i.enumerable});return m};var q=m=>M(g({},"__esModule",{value:!0}),m);var D={};T(D,{FormsLib:()=>H,default:()=>N});function k(m,s=!0){let b=new FormData(m);if(s)return b;let i={};return b.forEach((h,d)=>{let r=(h instanceof File,h);Object.prototype.hasOwnProperty.call(i,d)?Array.isArray(i[d])?i[d].push(r):i[d]=[i[d],r]:i[d]=r}),i}function x(m,s,b){let i=[];if(typeof m=="string"?i=Array.from(document.querySelectorAll(m)):i=[m],i.length===0)return null;function h(){let r=b?.style==="bootstrap",p=document.createElement("form");p.classList.add("voc-form"),r&&p.classList.add("needs-validation"),p.id=s.id;let y=document.createElement("h2");y.textContent=s.form_name,p.appendChild(y);function f(e,a){a&&(a.min_length!==void 0&&(e.minLength=a.min_length),a.max_length!==void 0&&(e.maxLength=a.max_length),a.regex&&(e instanceof HTMLInputElement?e.pattern=a.regex:e.dataset.pattern=a.regex))}function L(e){if(!e.key&&!e.label)return null;let a=document.createElement("div");if(a.className="voc-form-group",r&&a.classList.add("mb-3"),e.label){let t=document.createElement("label");t.classList.add("voc-form-label"),r&&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?" *":""),a.appendChild(t)}let u=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 l=document.createElement("label");l.classList.add("voc-form-checkbox-label"),r&&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"),r&&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),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"),r&&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}`)),r&&c.classList.add("form-check-input"),e.required&&(c.required=!0),e.default&&(c.checked=!!e.default),n.appendChild(c),n.appendChild(document.createTextNode(e.label||"")),t.appendChild(n)}u=t;break}case"textarea":{let t=document.createElement("textarea");t.classList.add("voc-form-textarea"),r&&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),e.default!==void 0&&e.default!==null&&(t.value=String(e.default)),e.width&&(t.style.width=e.width),e.required&&(t.required=!0),f(t,e.validation),u=t;break}case"select":{let t=document.createElement("select");if(t.classList.add("voc-form-select"),r&&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,e.default===n.value&&(c.selected=!0),t.appendChild(c)}),e.width&&(t.style.width=e.width),u=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 l=document.createElement("label");l.classList.add("voc-form-radio-label"),r&&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"),r&&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.default===n.value&&(o.checked=!0),l.appendChild(o),l.appendChild(document.createTextNode(n.label)),t.appendChild(l)}),u=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"),r&&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),u=t;break}default:{let t=document.createElement("input");t.type=e.type||"text",t.classList.add("voc-form-input"),r&&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),e.default!==void 0&&e.default!==null&&(t.value=String(e.default)),e.width&&(t.style.width=e.width),e.required&&(t.required=!0),f(t,e.validation),u=t;break}}if(u&&a.appendChild(u),e.hint_text){let t=document.createElement("small");t.className="voc-form-hint",t.textContent=e.hint_text,a.appendChild(t)}return a}s.fields.forEach(e=>{if(e.type==="row"&&"items"in e){let a=document.createElement("div");if(a.className="voc-form-group voc-form-row",r&&a.classList.add("mb-3"),e.label){let t=document.createElement("h3");t.className="voc-form-row-label",t.textContent=e.label,a.appendChild(t)}if(e.description){let t=document.createElement("p");t.className="voc-form-row-description",t.textContent=e.description,a.appendChild(t)}let u=document.createElement("div");u.className="voc-form-row-items "+(e.direction||"horizontal"),a.appendChild(u),e.items.forEach(t=>{let n=L(t);n&&u.appendChild(n)}),p.appendChild(a)}else{let a=L(e);a&&p.appendChild(a)}});let v=document.createElement("button");return v.type="submit",v.className="voc-form-submit",v.textContent="Submit",r&&v.classList.add("btn","btn-primary"),p.appendChild(v),p}let d=h();return i[0].appendChild(d),{element:d,validate:()=>{let r=[];return d.querySelectorAll(":invalid").forEach(y=>{let f=y.validationMessage;r.push({element:y,message:f})}),r},getValues:()=>k(d),onSubmit:r=>{d.addEventListener("submit",p=>{p.preventDefault();let y=k(d);r(y,p)})}}}var E={renderForm:x};var H=E,N={FormsLib:E};return q(D);})();
package/dist/main.js CHANGED
@@ -1 +1 @@
1
- "use strict";var k=Object.defineProperty;var L=Object.getOwnPropertyDescriptor;var F=Object.getOwnPropertyNames;var C=Object.prototype.hasOwnProperty;var T=(i,a)=>{for(var p in a)k(i,p,{get:a[p],enumerable:!0})},M=(i,a,p,d)=>{if(a&&typeof a=="object"||typeof a=="function")for(let u of F(a))!C.call(i,u)&&u!==p&&k(i,u,{get:()=>a[u],enumerable:!(d=L(a,u))||d.enumerable});return i};var H=i=>M(k({},"__esModule",{value:!0}),i);var N={};T(N,{FormsLib:()=>w,default:()=>q});module.exports=H(N);function x(i){let a=new FormData(i),p={};return a.forEach((d,u)=>{p[u]=d}),p}function f(i,a,p){let d=[];if(typeof i=="string"?d=Array.from(document.querySelectorAll(i)):d=[i],d.length===0)return null;function u(){let s=document.createElement("form");s.classList.add("voc-form"),s.id=a.id;let y=document.createElement("h2");y.textContent=a.form_name,s.appendChild(y);function h(e,n){n&&(n.min_length!==void 0&&(e.minLength=n.min_length),n.max_length!==void 0&&(e.maxLength=n.max_length),n.regex&&(e instanceof HTMLInputElement?e.pattern=n.regex:e.dataset.pattern=n.regex))}function v(e){if(!e.key&&!e.label)return null;let n=document.createElement("div");if(n.className="voc-form-group",e.label){let t=document.createElement("label");t.classList.add("voc-form-label"),e.key?e.type==="radio"?t.htmlFor="":t.htmlFor=`voc-form-${e.key}`:t.htmlFor="",t.textContent=e.label+(e.required?" *":""),n.appendChild(t)}let l=null;switch(e.type){case"checkbox":{let t=document.createElement("div");if(t.className="voc-form-checkbox-group "+(e.direction||"vertical"),e.options)e.options.forEach((r,o)=>{let m=document.createElement("label");m.classList.add("voc-form-checkbox-label"),m.style.display=e.direction==="horizontal"?"inline-block":"block";let c=document.createElement("input");c.type="checkbox",c.classList.add("voc-form-checkbox"),c.name=e.key||"",c.value=r.value,e.key&&(c.id=`voc-form-${e.key}-${o}`),e.required&&(c.required=!0),e.default===r.value&&(c.checked=!0),m.appendChild(c),m.appendChild(document.createTextNode(r.label)),t.appendChild(m)});else{let r=document.createElement("label");r.classList.add("voc-form-checkbox-label");let o=document.createElement("input");o.type="checkbox",o.classList.add("voc-form-checkbox"),e.key&&(o.name=e.key,o.id=`voc-form-${e.key}`),e.required&&(o.required=!0),e.default&&(o.checked=!!e.default),r.appendChild(o),r.appendChild(document.createTextNode(e.label||"")),t.appendChild(r)}l=t;break}case"textarea":{let t=document.createElement("textarea");t.classList.add("voc-form-textarea"),e.key&&(t.name=e.key,t.id=`voc-form-${e.key}`),e.required&&(t.required=!0),h(t,e.validation),l=t;break}case"select":{let t=document.createElement("select");t.classList.add("voc-form-select"),e.key&&(t.name=e.key,t.id=`voc-form-${e.key}`),e.required&&(t.required=!0),e.options&&e.options.forEach(r=>{let o=document.createElement("option");o.value=r.value,o.textContent=r.label,e.default===r.value&&(o.selected=!0),t.appendChild(o)}),l=t;break}case"radio":{let t=document.createElement("div");t.className="voc-form-radio-group "+(e.direction||"vertical"),e.options&&e.options.forEach((r,o)=>{let m=document.createElement("label");m.classList.add("voc-form-radio-label"),m.style.display=e.direction==="horizontal"?"inline-block":"block";let c=document.createElement("input");c.type="radio",c.classList.add("voc-form-radio"),c.name=e.key||"",c.value=r.value,e.key&&(c.id=`voc-form-${e.key}-${o}`),m.appendChild(c),m.appendChild(document.createTextNode(r.label)),t.appendChild(m)}),l=t;break}default:{let t=document.createElement("input");t.type=e.type||"text",t.classList.add("voc-form-input"),e.key&&(t.name=e.key,t.id=`voc-form-${e.key}`),e.required&&(t.required=!0),h(t,e.validation),l=t;break}}if(l&&n.appendChild(l),e.hint_text){let t=document.createElement("small");t.className="voc-form-hint",t.textContent=e.hint_text,n.appendChild(t)}return n}a.fields.forEach(e=>{if(e.type==="row"&&"items"in e){let n=document.createElement("div");if(n.className="voc-form-row",e.label){let t=document.createElement("h3");t.className="voc-form-row-label",t.textContent=e.label,n.appendChild(t)}if(e.description){let t=document.createElement("p");t.className="voc-form-row-description",t.textContent=e.description,n.appendChild(t)}let l=document.createElement("div");l.className="voc-form-row-items "+(e.direction||"vertical"),n.appendChild(l),e.items.forEach(t=>{let r=v(t);r&&l.appendChild(r)}),s.appendChild(n)}else{let n=v(e);n&&s.appendChild(n)}});let E=document.createElement("button");return E.type="submit",E.className="voc-form-submit",E.textContent="Submit",s.appendChild(E),s}let b=u();return d[0].appendChild(b),{element:b,validate:()=>{let s=[];return b.querySelectorAll(":invalid").forEach(h=>{let v=h.validationMessage;s.push({element:h,message:v})}),s},getValues:()=>x(b),onSubmit:s=>{b.addEventListener("submit",y=>{y.preventDefault();let h=x(b);s(h,y)})}}}var g={renderForm:f};var w=g,q={FormsLib:g};0&&(module.exports={FormsLib});
1
+ "use strict";var g=Object.defineProperty;var w=Object.getOwnPropertyDescriptor;var F=Object.getOwnPropertyNames;var C=Object.prototype.hasOwnProperty;var T=(m,s)=>{for(var b in s)g(m,b,{get:s[b],enumerable:!0})},M=(m,s,b,i)=>{if(s&&typeof s=="object"||typeof s=="function")for(let h of F(s))!C.call(m,h)&&h!==b&&g(m,h,{get:()=>s[h],enumerable:!(i=w(s,h))||i.enumerable});return m};var q=m=>M(g({},"__esModule",{value:!0}),m);var D={};T(D,{FormsLib:()=>H,default:()=>N});module.exports=q(D);function k(m,s=!0){let b=new FormData(m);if(s)return b;let i={};return b.forEach((h,d)=>{let r=(h instanceof File,h);Object.prototype.hasOwnProperty.call(i,d)?Array.isArray(i[d])?i[d].push(r):i[d]=[i[d],r]:i[d]=r}),i}function x(m,s,b){let i=[];if(typeof m=="string"?i=Array.from(document.querySelectorAll(m)):i=[m],i.length===0)return null;function h(){let r=b?.style==="bootstrap",p=document.createElement("form");p.classList.add("voc-form"),r&&p.classList.add("needs-validation"),p.id=s.id;let y=document.createElement("h2");y.textContent=s.form_name,p.appendChild(y);function f(e,a){a&&(a.min_length!==void 0&&(e.minLength=a.min_length),a.max_length!==void 0&&(e.maxLength=a.max_length),a.regex&&(e instanceof HTMLInputElement?e.pattern=a.regex:e.dataset.pattern=a.regex))}function L(e){if(!e.key&&!e.label)return null;let a=document.createElement("div");if(a.className="voc-form-group",r&&a.classList.add("mb-3"),e.label){let t=document.createElement("label");t.classList.add("voc-form-label"),r&&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?" *":""),a.appendChild(t)}let u=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 l=document.createElement("label");l.classList.add("voc-form-checkbox-label"),r&&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"),r&&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),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"),r&&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}`)),r&&c.classList.add("form-check-input"),e.required&&(c.required=!0),e.default&&(c.checked=!!e.default),n.appendChild(c),n.appendChild(document.createTextNode(e.label||"")),t.appendChild(n)}u=t;break}case"textarea":{let t=document.createElement("textarea");t.classList.add("voc-form-textarea"),r&&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),e.default!==void 0&&e.default!==null&&(t.value=String(e.default)),e.width&&(t.style.width=e.width),e.required&&(t.required=!0),f(t,e.validation),u=t;break}case"select":{let t=document.createElement("select");if(t.classList.add("voc-form-select"),r&&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,e.default===n.value&&(c.selected=!0),t.appendChild(c)}),e.width&&(t.style.width=e.width),u=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 l=document.createElement("label");l.classList.add("voc-form-radio-label"),r&&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"),r&&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.default===n.value&&(o.checked=!0),l.appendChild(o),l.appendChild(document.createTextNode(n.label)),t.appendChild(l)}),u=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"),r&&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),u=t;break}default:{let t=document.createElement("input");t.type=e.type||"text",t.classList.add("voc-form-input"),r&&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),e.default!==void 0&&e.default!==null&&(t.value=String(e.default)),e.width&&(t.style.width=e.width),e.required&&(t.required=!0),f(t,e.validation),u=t;break}}if(u&&a.appendChild(u),e.hint_text){let t=document.createElement("small");t.className="voc-form-hint",t.textContent=e.hint_text,a.appendChild(t)}return a}s.fields.forEach(e=>{if(e.type==="row"&&"items"in e){let a=document.createElement("div");if(a.className="voc-form-group voc-form-row",r&&a.classList.add("mb-3"),e.label){let t=document.createElement("h3");t.className="voc-form-row-label",t.textContent=e.label,a.appendChild(t)}if(e.description){let t=document.createElement("p");t.className="voc-form-row-description",t.textContent=e.description,a.appendChild(t)}let u=document.createElement("div");u.className="voc-form-row-items "+(e.direction||"horizontal"),a.appendChild(u),e.items.forEach(t=>{let n=L(t);n&&u.appendChild(n)}),p.appendChild(a)}else{let a=L(e);a&&p.appendChild(a)}});let v=document.createElement("button");return v.type="submit",v.className="voc-form-submit",v.textContent="Submit",r&&v.classList.add("btn","btn-primary"),p.appendChild(v),p}let d=h();return i[0].appendChild(d),{element:d,validate:()=>{let r=[];return d.querySelectorAll(":invalid").forEach(y=>{let f=y.validationMessage;r.push({element:y,message:f})}),r},getValues:()=>k(d),onSubmit:r=>{d.addEventListener("submit",p=>{p.preventDefault();let y=k(d);r(y,p)})}}}var E={renderForm:x};var H=E,N={FormsLib:E};0&&(module.exports={FormsLib});
package/dist/main.mjs CHANGED
@@ -1 +1 @@
1
- function k(p){let h=new FormData(p),v={};return h.forEach((m,E)=>{v[E]=m}),v}function g(p,h,v){let m=[];if(typeof p=="string"?m=Array.from(document.querySelectorAll(p)):m=[p],m.length===0)return null;function E(){let c=document.createElement("form");c.classList.add("voc-form"),c.id=h.id;let u=document.createElement("h2");u.textContent=h.form_name,c.appendChild(u);function l(e,n){n&&(n.min_length!==void 0&&(e.minLength=n.min_length),n.max_length!==void 0&&(e.maxLength=n.max_length),n.regex&&(e instanceof HTMLInputElement?e.pattern=n.regex:e.dataset.pattern=n.regex))}function b(e){if(!e.key&&!e.label)return null;let n=document.createElement("div");if(n.className="voc-form-group",e.label){let t=document.createElement("label");t.classList.add("voc-form-label"),e.key?e.type==="radio"?t.htmlFor="":t.htmlFor=`voc-form-${e.key}`:t.htmlFor="",t.textContent=e.label+(e.required?" *":""),n.appendChild(t)}let i=null;switch(e.type){case"checkbox":{let t=document.createElement("div");if(t.className="voc-form-checkbox-group "+(e.direction||"vertical"),e.options)e.options.forEach((r,o)=>{let s=document.createElement("label");s.classList.add("voc-form-checkbox-label"),s.style.display=e.direction==="horizontal"?"inline-block":"block";let a=document.createElement("input");a.type="checkbox",a.classList.add("voc-form-checkbox"),a.name=e.key||"",a.value=r.value,e.key&&(a.id=`voc-form-${e.key}-${o}`),e.required&&(a.required=!0),e.default===r.value&&(a.checked=!0),s.appendChild(a),s.appendChild(document.createTextNode(r.label)),t.appendChild(s)});else{let r=document.createElement("label");r.classList.add("voc-form-checkbox-label");let o=document.createElement("input");o.type="checkbox",o.classList.add("voc-form-checkbox"),e.key&&(o.name=e.key,o.id=`voc-form-${e.key}`),e.required&&(o.required=!0),e.default&&(o.checked=!!e.default),r.appendChild(o),r.appendChild(document.createTextNode(e.label||"")),t.appendChild(r)}i=t;break}case"textarea":{let t=document.createElement("textarea");t.classList.add("voc-form-textarea"),e.key&&(t.name=e.key,t.id=`voc-form-${e.key}`),e.required&&(t.required=!0),l(t,e.validation),i=t;break}case"select":{let t=document.createElement("select");t.classList.add("voc-form-select"),e.key&&(t.name=e.key,t.id=`voc-form-${e.key}`),e.required&&(t.required=!0),e.options&&e.options.forEach(r=>{let o=document.createElement("option");o.value=r.value,o.textContent=r.label,e.default===r.value&&(o.selected=!0),t.appendChild(o)}),i=t;break}case"radio":{let t=document.createElement("div");t.className="voc-form-radio-group "+(e.direction||"vertical"),e.options&&e.options.forEach((r,o)=>{let s=document.createElement("label");s.classList.add("voc-form-radio-label"),s.style.display=e.direction==="horizontal"?"inline-block":"block";let a=document.createElement("input");a.type="radio",a.classList.add("voc-form-radio"),a.name=e.key||"",a.value=r.value,e.key&&(a.id=`voc-form-${e.key}-${o}`),s.appendChild(a),s.appendChild(document.createTextNode(r.label)),t.appendChild(s)}),i=t;break}default:{let t=document.createElement("input");t.type=e.type||"text",t.classList.add("voc-form-input"),e.key&&(t.name=e.key,t.id=`voc-form-${e.key}`),e.required&&(t.required=!0),l(t,e.validation),i=t;break}}if(i&&n.appendChild(i),e.hint_text){let t=document.createElement("small");t.className="voc-form-hint",t.textContent=e.hint_text,n.appendChild(t)}return n}h.fields.forEach(e=>{if(e.type==="row"&&"items"in e){let n=document.createElement("div");if(n.className="voc-form-row",e.label){let t=document.createElement("h3");t.className="voc-form-row-label",t.textContent=e.label,n.appendChild(t)}if(e.description){let t=document.createElement("p");t.className="voc-form-row-description",t.textContent=e.description,n.appendChild(t)}let i=document.createElement("div");i.className="voc-form-row-items "+(e.direction||"vertical"),n.appendChild(i),e.items.forEach(t=>{let r=b(t);r&&i.appendChild(r)}),c.appendChild(n)}else{let n=b(e);n&&c.appendChild(n)}});let y=document.createElement("button");return y.type="submit",y.className="voc-form-submit",y.textContent="Submit",c.appendChild(y),c}let d=E();return m[0].appendChild(d),{element:d,validate:()=>{let c=[];return d.querySelectorAll(":invalid").forEach(l=>{let b=l.validationMessage;c.push({element:l,message:b})}),c},getValues:()=>k(d),onSubmit:c=>{d.addEventListener("submit",u=>{u.preventDefault();let l=k(d);c(l,u)})}}}var x={renderForm:g};var H=x,w={FormsLib:x};export{H as FormsLib,w as default};
1
+ function g(u,h=!0){let v=new FormData(u);if(h)return v;let l={};return v.forEach((b,i)=>{let r=(b instanceof File,b);Object.prototype.hasOwnProperty.call(l,i)?Array.isArray(l[i])?l[i].push(r):l[i]=[l[i],r]:l[i]=r}),l}function L(u,h,v){let l=[];if(typeof u=="string"?l=Array.from(document.querySelectorAll(u)):l=[u],l.length===0)return null;function b(){let r=v?.style==="bootstrap",m=document.createElement("form");m.classList.add("voc-form"),r&&m.classList.add("needs-validation"),m.id=h.id;let p=document.createElement("h2");p.textContent=h.form_name,m.appendChild(p);function f(e,a){a&&(a.min_length!==void 0&&(e.minLength=a.min_length),a.max_length!==void 0&&(e.maxLength=a.max_length),a.regex&&(e instanceof HTMLInputElement?e.pattern=a.regex:e.dataset.pattern=a.regex))}function E(e){if(!e.key&&!e.label)return null;let a=document.createElement("div");if(a.className="voc-form-group",r&&a.classList.add("mb-3"),e.label){let t=document.createElement("label");t.classList.add("voc-form-label"),r&&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?" *":""),a.appendChild(t)}let d=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 s=document.createElement("label");s.classList.add("voc-form-checkbox-label"),r&&s.classList.add("form-check"),s.style.display=e.direction==="horizontal"?"inline-block":"block",e.width&&(s.style.width=e.width);let o=document.createElement("input");o.type="checkbox",o.classList.add("voc-form-checkbox"),r&&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),e.default===n.value&&(o.checked=!0),s.appendChild(o),s.appendChild(document.createTextNode(n.label)),t.appendChild(s)});else{let n=document.createElement("label");n.classList.add("voc-form-checkbox-label"),r&&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}`)),r&&c.classList.add("form-check-input"),e.required&&(c.required=!0),e.default&&(c.checked=!!e.default),n.appendChild(c),n.appendChild(document.createTextNode(e.label||"")),t.appendChild(n)}d=t;break}case"textarea":{let t=document.createElement("textarea");t.classList.add("voc-form-textarea"),r&&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),e.default!==void 0&&e.default!==null&&(t.value=String(e.default)),e.width&&(t.style.width=e.width),e.required&&(t.required=!0),f(t,e.validation),d=t;break}case"select":{let t=document.createElement("select");if(t.classList.add("voc-form-select"),r&&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,e.default===n.value&&(c.selected=!0),t.appendChild(c)}),e.width&&(t.style.width=e.width),d=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 s=document.createElement("label");s.classList.add("voc-form-radio-label"),r&&s.classList.add("form-check"),s.style.display=e.direction==="horizontal"?"inline-block":"block",e.width&&(s.style.width=e.width);let o=document.createElement("input");o.type="radio",o.classList.add("voc-form-radio"),r&&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.default===n.value&&(o.checked=!0),s.appendChild(o),s.appendChild(document.createTextNode(n.label)),t.appendChild(s)}),d=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"),r&&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),d=t;break}default:{let t=document.createElement("input");t.type=e.type||"text",t.classList.add("voc-form-input"),r&&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),e.default!==void 0&&e.default!==null&&(t.value=String(e.default)),e.width&&(t.style.width=e.width),e.required&&(t.required=!0),f(t,e.validation),d=t;break}}if(d&&a.appendChild(d),e.hint_text){let t=document.createElement("small");t.className="voc-form-hint",t.textContent=e.hint_text,a.appendChild(t)}return a}h.fields.forEach(e=>{if(e.type==="row"&&"items"in e){let a=document.createElement("div");if(a.className="voc-form-group voc-form-row",r&&a.classList.add("mb-3"),e.label){let t=document.createElement("h3");t.className="voc-form-row-label",t.textContent=e.label,a.appendChild(t)}if(e.description){let t=document.createElement("p");t.className="voc-form-row-description",t.textContent=e.description,a.appendChild(t)}let d=document.createElement("div");d.className="voc-form-row-items "+(e.direction||"horizontal"),a.appendChild(d),e.items.forEach(t=>{let n=E(t);n&&d.appendChild(n)}),m.appendChild(a)}else{let a=E(e);a&&m.appendChild(a)}});let y=document.createElement("button");return y.type="submit",y.className="voc-form-submit",y.textContent="Submit",r&&y.classList.add("btn","btn-primary"),m.appendChild(y),m}let i=b();return l[0].appendChild(i),{element:i,validate:()=>{let r=[];return i.querySelectorAll(":invalid").forEach(p=>{let f=p.validationMessage;r.push({element:p,message:f})}),r},getValues:()=>g(i),onSubmit:r=>{i.addEventListener("submit",m=>{m.preventDefault();let p=g(i);r(p,m)})}}}var k={renderForm:L};var q=k,H={FormsLib:k};export{q as FormsLib,H as default};
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "voc-lib-js",
3
- "version": "1.0.10",
3
+ "version": "1.0.12",
4
4
  "description": "A JavaScript library for VocPhone",
5
5
  "main": "./dist/main.cjs",
6
6
  "module": "./dist/main.mjs",