voc-lib-js 1.0.11 → 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 +108 -20
- package/dist/main.d.mts +8 -5
- package/dist/main.d.ts +8 -5
- package/dist/main.global.js +1 -1
- package/dist/main.js +1 -1
- package/dist/main.mjs +1 -1
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -76,32 +76,120 @@ onMounted(() => {
|
|
|
76
76
|
|
|
77
77
|
## EVENTS
|
|
78
78
|
|
|
79
|
-
|
|
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
|
-
//
|
|
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
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
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
|
|
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,10 +44,8 @@ interface RenderedForm {
|
|
|
39
44
|
element: HTMLElement;
|
|
40
45
|
message: string;
|
|
41
46
|
}>;
|
|
42
|
-
getValues: () =>
|
|
43
|
-
|
|
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;
|
package/dist/main.d.ts
CHANGED
|
@@ -9,7 +9,9 @@ type Option = {
|
|
|
9
9
|
};
|
|
10
10
|
type BaseField = {
|
|
11
11
|
label?: string;
|
|
12
|
-
key
|
|
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,10 +44,8 @@ interface RenderedForm {
|
|
|
39
44
|
element: HTMLElement;
|
|
40
45
|
message: string;
|
|
41
46
|
}>;
|
|
42
|
-
getValues: () =>
|
|
43
|
-
|
|
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;
|
package/dist/main.global.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
"use strict";var VocLibJs=(()=>{var
|
|
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
|
|
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
|
|
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};
|