formique 1.0.1 → 1.0.2

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.
Files changed (3) hide show
  1. package/README.md +528 -0
  2. package/package.json +7 -6
  3. package/formique.js +0 -2939
package/README.md ADDED
@@ -0,0 +1,528 @@
1
+ # Formique
2
+
3
+ <!-- <img src="https://github.com/thincmedia/anyGridJs/blob/main/images/anyGridJs_Example.png" alt="anyGridJs Example"> -->
4
+
5
+
6
+ Formique: A lightweight, declarative JavaScript library for generating forms. Suited for vanilla js needs and Semantq JS framework. Formique simplifies the process of creating and managing forms with a variety of input types, built-in validation, and customizable attributes. Open-source &amp; MIT licensed.
7
+
8
+ ## Key Features
9
+
10
+ - Declarative Syntax: Define forms using a simple and intuitive schema.
11
+ - Wide Range of Inputs: Supports text, email, number, password, date, time, file uploads, and more.
12
+ - Validation and Attributes: Easily specify validation rules and attributes for each form field.
13
+ - Dynamic Form Generation: Generate forms dynamically based on your schema.
14
+ - Framework Agnostic: Currently works Semantq and vanilla JS.
15
+
16
+
17
+
18
+ ## Why Choose Formique?
19
+
20
+ - Vanilla JS: No dependencies; works seamlessly with vanilla JS or any JS framework.
21
+ - Lightweight: Minimal footprint optimized for performance.
22
+ - Customizable: Adapt the library to fit your project's unique needs.
23
+ - Declarative: Define forms with a straightforward schema for better readability and maintainability.
24
+
25
+
26
+ ## Form Input Types Covered
27
+
28
+ - Text: ```html <input type="text"> ```
29
+ - Email: ```html <input type="email"> ```
30
+ - Number: ```html <input type="number"> ```
31
+ - Password: ```html <input type="password"> ```
32
+ - Telephone: ```html <input type="tel"> ```
33
+ - Date: ```html <input type="date"> ```
34
+ - Time: ```html <input type="time"> ```
35
+ - Datetime-local: ```html <input type="datetime-local"> ```
36
+ - Month: ```html <input type="month"> ```
37
+ - Week: ```html <input type="week"> ```
38
+ - URL: ```html <input type="url"> ```
39
+ - Search: ```html <input type="search"> ```
40
+ - Color: ```html <input type="color"> ```
41
+ - File: ```html <input type="file"> ```
42
+ - Hidden: ```html <input type="hidden"> ```
43
+ - Image: ```html <input type="image"> ```
44
+ - Textarea: ```html <textarea> ```
45
+ - Radio: ```html <input type="radio"> ```
46
+ - Checkbox: ```html <input type="checkbox"> ```
47
+ - Select (Single & Multiple): ```html <select> ```
48
+ - Submit: ```html <input type="submit"> ```
49
+
50
+
51
+
52
+
53
+ # How to Write Form Schema
54
+
55
+ The form schema is an array of field definitions. Each field is defined by an array containing:
56
+
57
+ ## Input Definition
58
+ - **Type**: The input type (e.g., 'text', 'email', 'radio').
59
+ - **Name**: The name attribute for the input.
60
+ - **Label**: The label for the input.
61
+
62
+ ## Input Validation
63
+ - **Validation**: Object specifying validation rules. This can include:
64
+ - **Required**: Boolean to specify if the field is mandatory.
65
+ - Example: `required: true`
66
+ - **MinLength**: Specifies the minimum number of characters allowed.
67
+ - Example: `minlength: 5`
68
+ - **MaxLength**: Specifies the maximum number of characters allowed.
69
+ - Example: `maxlength: 50`
70
+ - **Pattern**: A regex pattern the input must match.
71
+ - Example: `pattern: /^[A-Za-z0-9]+$/`
72
+ - **Custom**: Function for custom validation logic.
73
+ - Example: `custom: value => value.startsWith('A')`
74
+
75
+ ## Input Attributes
76
+ - **Attributes**: Object specifying additional attributes like `id`, `class`, `style`, etc.
77
+ - Example: `attributes: { id: 'username', class: 'form-control' }`
78
+
79
+ ## Binding
80
+ - **Binding**: Optional binding syntax for dynamic data. It can use `bind:value` or `::inputName`.
81
+ - Example: `binding: 'bind:value'` or `binding: '::username'`
82
+
83
+ ## Options
84
+ - **Options**: For select, radio, and checkbox inputs. This is an array of options, each with a `value` and `label`.
85
+ - Example: `options: [{ value: 'male', label: 'Male' }, { value: 'female', label: 'Female' }]`
86
+
87
+
88
+
89
+ ## Usage Example
90
+
91
+
92
+ There are two ways to use install Formique in your project
93
+
94
+ ## Option A: Clone the GitHub repo:
95
+
96
+ 1. go to your terminal and run:
97
+
98
+ ```bash
99
+ git clone https://github.com/Gugulethu-Nyoni/formique.git
100
+
101
+ ````
102
+ 2. now you can use anygridjs this way in your html file
103
+
104
+ ```html
105
+
106
+ <link rel="stylesheet" href="./formique/formique.css">
107
+
108
+ ```
109
+
110
+ ```html
111
+
112
+ <script type="module">
113
+
114
+ import { Formique } from './formique/formique.js';
115
+
116
+ </script>
117
+
118
+ ```
119
+
120
+
121
+ ## Option B: Via npm
122
+
123
+ ### Installation
124
+
125
+
126
+ 1. go to your terminal and run:
127
+
128
+ ```bash
129
+
130
+ npm install formique
131
+
132
+ ````
133
+
134
+ 2. now you can use formique this way in your html file
135
+
136
+ ```html
137
+
138
+ <link rel="stylesheet" href="./node_modules/formique/formique.css">
139
+
140
+ ```
141
+
142
+ ```html
143
+
144
+ <script type="module">
145
+
146
+ import { Formique } from './node_modules/formique/formique.js';
147
+
148
+ // rest of js code (app.js) can come here e.g. data object, column definition etc ( see below)
149
+
150
+ </script>
151
+
152
+ ```
153
+
154
+
155
+ # Example Usage
156
+
157
+ ## Form Schema
158
+
159
+ Here's an example of a form schema that defines various input fields with validation, attributes, options, and binding syntax:
160
+
161
+ ```javascript
162
+ const formSchema = [
163
+ // Text Input Field
164
+ [
165
+ 'text',
166
+ 'firstName',
167
+ 'First Name',
168
+ { minlength: 2, maxlength: 5, required: true, disabled: true }, // Validation options
169
+ { value: "John", id: 'firstNameInput', class: 'form-input', style: 'width: 100%;', oninput: "incrementer()" }, // Attributes
170
+ '::firstName' // Binding syntax
171
+ ],
172
+
173
+ // URL Input Field
174
+ [
175
+ 'url',
176
+ 'websiteUrl',
177
+ 'Website URL',
178
+ { required: true }, // Validation options
179
+ { id: 'websiteUrlInput', class: 'form-control', style: 'width: 100%;' }, // Attributes
180
+ 'bind:value' // Binding syntax
181
+ ],
182
+
183
+ // Radio Input Field
184
+ [
185
+ 'radio',
186
+ 'gender',
187
+ 'Gender',
188
+ { required: true }, // Validation options
189
+ { id: 'genderRadio', class: 'form-radio-input', style: 'margin-left: 1rem;', onchange: 'actioner()' }, // Attributes
190
+ '::gender', // Binding syntax
191
+ [
192
+ { value: 'male', label: 'Male' }, // Options
193
+ { value: 'female', label: 'Female' },
194
+ { value: 'other', label: 'Other' }
195
+ ]
196
+ ],
197
+
198
+ // Checkbox Input Field
199
+ [
200
+ 'checkbox',
201
+ 'preferences',
202
+ 'Preferences',
203
+ { required: true }, // Validation options
204
+ { id: 'preferencesCheckbox', class: 'form-checkbox-input', style: 'margin-left: 1rem;', onchange: 'submit' }, // Attributes
205
+ '::preferences', // Binding syntax
206
+ [
207
+ { value: 'news', label: 'Newsletter' }, // Options
208
+ { value: 'updates', label: 'Product Updates' },
209
+ { value: 'offers', label: 'Special Offers' }
210
+ ]
211
+ ],
212
+
213
+ // Single Select Input Field
214
+ [
215
+ 'singleSelect',
216
+ 'colors',
217
+ 'Colors',
218
+ { required: true }, // Validation options
219
+ { id: 'colorsSelect', class: 'form-select-input', style: 'margin-left: 1rem;', onchange: 'trigger' }, // Attributes
220
+ '::colors', // Binding syntax
221
+ [
222
+ { value: 'red', label: 'Red' }, // Options
223
+ { value: 'green', label: 'Green' },
224
+ { value: 'blue', label: 'Blue', selected: true }
225
+ ]
226
+ ],
227
+
228
+ // Multiple Select Input Field
229
+ [
230
+ 'multipleSelect', // Type of field
231
+ 'colors', // Name/identifier of the field
232
+ 'Colors', // Label of the field
233
+ { required: true, min: 2, max: 3 }, // Validation options
234
+ { id: 'colorsSelect', class: 'form-select-input', style: 'margin-left: 1rem;', onchange: 'alerter' }, // Attributes
235
+ '::colors', // Binding syntax
236
+ [
237
+ { value: 'red', label: 'Red' }, // Options
238
+ { value: 'green', label: 'Green' },
239
+ { value: 'blue', label: 'Blue' },
240
+ { value: 'yellow', label: 'Yellow' }
241
+ ]
242
+ ],
243
+
244
+ // Submit Button
245
+ [
246
+ 'submit',
247
+ 'submitButton',
248
+ 'Submit',
249
+ { required: true }, // Validation options
250
+ { id: 'submitBtn', class: 'form-submit-btn', style: 'margin-top: 1rem;' } // Attributes
251
+ ]
252
+ ];
253
+
254
+ ```
255
+
256
+ # Invoking the Formique Class
257
+
258
+ To create a form using Formique, you need to define the form parameters and schema. Below is an example of how to invoke the class with basic parameters, followed by a full list of possible parameters.
259
+
260
+ ## Basic Form Parameters
261
+
262
+ ```javascript
263
+ const formParams = {
264
+ method: 'post', // HTTP method (e.g., 'get', 'post')
265
+ action: 'submit.js', // Form submission URL
266
+ id: 'myForm', // Unique identifier for the form
267
+ class: 'form', // CSS class for styling
268
+ semantq: true, // Whether to use semantic HTML elements
269
+ style: 'width: 100%; font-size: 14px;' // Inline CSS styling
270
+ };
271
+
272
+ const form = new Formique(formParams, formSchema);
273
+ const formHTML = form.renderFormHTML();
274
+ console.log(formHTML);
275
+ ```
276
+
277
+
278
+
279
+ # Full List of Possible Form Parameters
280
+
281
+ ```javascript
282
+ const formParams = {
283
+ method: 'post', // HTTP method (e.g., 'get', 'post')
284
+ action: 'submit.js', // Form submission URL
285
+ id: 'myForm', // Unique identifier for the form
286
+ class: 'form', // CSS class for styling
287
+ semantq: true, // if true it enables Semantq syntax sugar: i.e. attribute: onchange: 'handlerFunction' will be transformed to: @change={handlerFunction} if false or not defined completely the output would be regular html e.g.: onchange="handlerFunction()"
288
+ style: 'width: 100%; font-size: 14px;', // Inline CSS styling
289
+ enctype: 'multipart/form-data', // Encoding type for file uploads
290
+ target: '_blank', // Where to open the form result (e.g., '_self', '_blank')
291
+ novalidate: true, // Disable form validation
292
+ accept_charset: 'UTF-8' // this will be transformed to: accept-charset: 'UTF-8' Character set for form data
293
+ };
294
+ ```
295
+
296
+ ## Explanation of Parameters
297
+
298
+ - **method**: Specifies the HTTP method to use when submitting the form ('get', 'post').
299
+ - **action**: The URL where the form data will be submitted.
300
+ - **id**: A unique identifier for the form.
301
+ - **class**: CSS class names applied to the form for styling purposes.
302
+ - **semantq**: Boolean value indicating whether to use semantic HTML elements (e.g., `<fieldset>`, `<legend>`).
303
+ - **style**: Inline CSS styling applied directly to the form element.
304
+ - **enctype**: Specifies how the form data should be encoded when submitted ('application/x-www-form-urlencoded', 'multipart/form-data', 'text/plain').
305
+ - **target**: Specifies where to display the response after submitting the form ('_self', '_blank', '_parent', '_top').
306
+ - **novalidate**: Disables form validation when set to true.
307
+ - **accept_charset**: Specifies the character encoding used for form data submission.
308
+
309
+ By customizing these parameters, you can control various aspects of the form's behavior and appearance.
310
+
311
+
312
+
313
+ ## HTML
314
+
315
+ In your html, place this markup as placegolder for where the form will be rendered.
316
+
317
+ ```html
318
+
319
+ <div id="formique"></div>
320
+
321
+ ```
322
+
323
+ ## Example HTML Output
324
+
325
+ ```html
326
+ <div id="formique">
327
+ <form
328
+ method="post"
329
+ action="submit.js"
330
+ id="myForm"
331
+ class="form"
332
+ semantq
333
+ style="width: 100%; font-size: 14px;"
334
+ >
335
+ <div class="input-block">
336
+ <label for="firstNameInput">First Name</label>
337
+ <input
338
+ type="text"
339
+ name="firstName"
340
+ bind:value="firstName"
341
+ id="firstNameInput"
342
+ value="John"
343
+ class="form-input"
344
+ style="width: 100%;"
345
+ @input={incrementer}
346
+ minlength="2"
347
+ maxlength="5"
348
+ required
349
+ disabled
350
+ />
351
+ </div>
352
+ <div class="input-block">
353
+ <label for="websiteUrlInput">Website URL</label>
354
+ <input
355
+ type="url"
356
+ name="websiteUrl"
357
+ bind:value="websiteUrl"
358
+ id="websiteUrlInput"
359
+ class="form-control"
360
+ style="width: 100%;"
361
+ required
362
+ />
363
+ </div>
364
+ <fieldset class="radio-group">
365
+ <legend>Gender</legend>
366
+ <div>
367
+ <input
368
+ type="radio"
369
+ name="gender"
370
+ value="male"
371
+ bind:value="gender"
372
+ class="form-radio-input"
373
+ style="margin-left: 1rem;"
374
+ @change={actioner}
375
+ id="genderRadio-male"
376
+ />
377
+ <label for="genderRadio-male">Male</label>
378
+ </div>
379
+ <div>
380
+ <input
381
+ type="radio"
382
+ name="gender"
383
+ value="female"
384
+ bind:value="gender"
385
+ class="form-radio-input"
386
+ style="margin-left: 1rem;"
387
+ @change={actioner}
388
+ id="genderRadio-female"
389
+ />
390
+ <label for="genderRadio-female">Female</label>
391
+ </div>
392
+ <div>
393
+ <input
394
+ type="radio"
395
+ name="gender"
396
+ value="other"
397
+ bind:value="gender"
398
+ class="form-radio-input"
399
+ style="margin-left: 1rem;"
400
+ @change={actioner}
401
+ id="genderRadio-other"
402
+ />
403
+ <label for="genderRadio-other">Other</label>
404
+ </div>
405
+ </fieldset>
406
+ <fieldset class="checkbox-group">
407
+ <legend>Preferences</legend>
408
+ <div>
409
+ <input
410
+ type="checkbox"
411
+ name="preferences"
412
+ value="news"
413
+ bind:checked="preferences"
414
+ class="form-checkbox-input"
415
+ style="margin-left: 1rem;"
416
+ @change={submit}
417
+ id="preferencesCheckbox-news"
418
+ />
419
+ <label for="preferencesCheckbox-news">Newsletter</label>
420
+ </div>
421
+ <div>
422
+ <input
423
+ type="checkbox"
424
+ name="preferences"
425
+ value="updates"
426
+ bind:checked="preferences"
427
+ class="form-checkbox-input"
428
+ style="margin-left: 1rem;"
429
+ @change={submit}
430
+ id="preferencesCheckbox-updates"
431
+ />
432
+ <label for="preferencesCheckbox-updates">Product Updates</label>
433
+ </div>
434
+ <div>
435
+ <input
436
+ type="checkbox"
437
+ name="preferences"
438
+ value="offers"
439
+ bind:checked="preferences"
440
+ class="form-checkbox-input"
441
+ style="margin-left: 1rem;"
442
+ @change={submit}
443
+ id="preferencesCheckbox-offers"
444
+ />
445
+ <label for="preferencesCheckbox-offers">Special Offers</label>
446
+ </div>
447
+ </fieldset>
448
+ <fieldset class="form-select">
449
+ <label for="colorsSelect">Colors</label>
450
+ <select
451
+ name="colors"
452
+ bind:value="colors"
453
+ id="colorsSelect"
454
+ class="form-select-input"
455
+ style="margin-left: 1rem;"
456
+ @change={trigger}
457
+ required
458
+ >
459
+ <option value="">Choose an option</option>
460
+ <option value="red">Red</option>
461
+ <option value="green">Green</option>
462
+ <option value="blue" selected>Blue</option>
463
+ </select>
464
+ </fieldset>
465
+ <fieldset class="form-select">
466
+ <label for="colorsSelect">Colors</label>
467
+ <select
468
+ name="colors"
469
+ bind:value="colors"
470
+ id="colorsSelect"
471
+ class="form-select-input"
472
+ style="margin-left: 1rem;"
473
+ @change={alerter}
474
+ required
475
+ multiple
476
+ >
477
+ <option value="red">Red</option>
478
+ <option value="green">Green</option>
479
+ <option value="blue">Blue</option>
480
+ <option value="yellow">Yellow</option>
481
+ </select>
482
+ </fieldset>
483
+ <input
484
+ type="submit"
485
+ id="submitBtn"
486
+ value="Submit"
487
+ class="form-submit-btn"
488
+ style="margin-top: 1rem;"
489
+ />
490
+ </form>
491
+ </div>
492
+ ```
493
+
494
+ ## Styling the Form
495
+
496
+ Formique provides a set of CSS classes to facilitate the styling of various form elements. The default class names for different form components are as follows:
497
+
498
+ - **Wrapper for Input Elements:** `input-block`
499
+ - **Input Fields:** `form-input`
500
+ - **Radio Button Groups:** `radio-group`
501
+ - **Checkbox Groups:** `checkbox-group`
502
+ - **Select Dropdowns:** `form-select`
503
+
504
+ These classes are predefined in the `formique.css` stylesheet. Developers can either use this stylesheet for consistent styling or create their own custom CSS based on these class names to suit their design preferences.
505
+
506
+ ### Customizing Styles
507
+
508
+ 1. **Using Default Styles:** Apply the `formique.css` stylesheet to leverage the default styling provided by Formique.
509
+ 2. **Custom CSS:** If you prefer custom styling, you can override the default styles by defining your own CSS rules for the above class names.
510
+
511
+ ### Inline Styling
512
+
513
+ In addition to external stylesheets, individual form elements can be styled directly via attributes specified in the form schema. This allows for precise control over the appearance of each element without needing additional CSS files.
514
+
515
+ By utilizing these options, you can easily adapt the look and feel of your form to meet your project's design requirements.
516
+
517
+
518
+ ## Contribute
519
+
520
+ Formique is an open-source project. Contributions, issues, and feature requests are welcome!
521
+
522
+ ## License
523
+
524
+ Formique is licensed under the MIT License.
525
+
526
+ ## Keywords
527
+
528
+ Javascript datatables.
package/package.json CHANGED
@@ -1,13 +1,14 @@
1
1
  {
2
2
  "name": "formique",
3
- "version": "1.0.1",
4
- "description": "Formique JavaScript library",
5
- "main": "formique.js",
3
+ "version": "1.0.2",
4
+ "description": "Formique Declarative Form Syntax JavaScript Library",
5
+ "main": "dist/formique.umd.js",
6
+ "module": "dist/formique.esm.js",
6
7
  "files": [
7
- "formique.js"
8
+ "dist/*",
9
+ "README.md"
8
10
  ],
9
- "scripts": {},
10
11
  "keywords": [],
11
12
  "author": "",
12
13
  "license": "ISC"
13
- }
14
+ }