formique 1.0.2 → 1.0.4
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 +170 -79
- package/package.json +2 -2
package/README.md
CHANGED
|
@@ -3,7 +3,17 @@
|
|
|
3
3
|
<!-- <img src="https://github.com/thincmedia/anyGridJs/blob/main/images/anyGridJs_Example.png" alt="anyGridJs Example"> -->
|
|
4
4
|
|
|
5
5
|
|
|
6
|
-
Formique: A lightweight, declarative JavaScript library for generating forms. Suited for vanilla js
|
|
6
|
+
Formique: A lightweight, declarative JavaScript syntax library for generating WCAG acceAccessibility compliant forms. Suited for vanilla js 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 & MIT licensed.
|
|
7
|
+
|
|
8
|
+
|
|
9
|
+
## Accessibility Compliance
|
|
10
|
+
|
|
11
|
+
Formique is designed with a laser focus on usability and accessibility, ensuring that the generated form HTML markup meets the highest standards of web accessibility, such as the Web Content Accessibility Guidelines (WCAG) 2.1.
|
|
12
|
+
|
|
13
|
+
With minimal declarative form input definitions, Formique takes care of the rest to ensure the final markup is comprehensive enough to meet [official](https://www.w3.org/WAI/tutorials/forms/) usability and accessibility standards.
|
|
14
|
+
|
|
15
|
+
For more information on the Web Content (Forms) Accessibility Guidelines (WCAG), you can visit the [W3C website](https://www.w3.org/WAI/tutorials/forms/).
|
|
16
|
+
|
|
7
17
|
|
|
8
18
|
## Key Features
|
|
9
19
|
|
|
@@ -17,10 +27,10 @@ Formique: A lightweight, declarative JavaScript library for generating forms. Su
|
|
|
17
27
|
|
|
18
28
|
## Why Choose Formique?
|
|
19
29
|
|
|
20
|
-
- Vanilla JS: No dependencies; works seamlessly with vanilla JS
|
|
30
|
+
- Vanilla JS: No dependencies; works seamlessly with vanilla JS and Semantq JS framework.(More frameworks to be added)
|
|
21
31
|
- Lightweight: Minimal footprint optimized for performance.
|
|
22
32
|
- Customizable: Adapt the library to fit your project's unique needs.
|
|
23
|
-
- Declarative:
|
|
33
|
+
- Declarative: Write your forms in JavaScript and define forms with a concise schema for better readability and maintainability.
|
|
24
34
|
|
|
25
35
|
|
|
26
36
|
## Form Input Types Covered
|
|
@@ -53,12 +63,29 @@ Formique: A lightweight, declarative JavaScript library for generating forms. Su
|
|
|
53
63
|
# How to Write Form Schema
|
|
54
64
|
|
|
55
65
|
The form schema is an array of field definitions. Each field is defined by an array containing:
|
|
66
|
+
- Input definition (required)
|
|
67
|
+
- Input validation (optional)
|
|
68
|
+
- Input attributes (optional)
|
|
69
|
+
- Binding syntax (optional)
|
|
70
|
+
- Options (applicable to single select, multiple select, radio and check box inputs)
|
|
56
71
|
|
|
57
72
|
## Input Definition
|
|
58
73
|
- **Type**: The input type (e.g., 'text', 'email', 'radio').
|
|
59
|
-
- **Name**: The name attribute for the input.
|
|
74
|
+
- **Name**: The name attribute for the input.
|
|
60
75
|
- **Label**: The label for the input.
|
|
61
76
|
|
|
77
|
+
You don't need to use the type, name and label keys to define these parameters.
|
|
78
|
+
**Example Input Definition:**
|
|
79
|
+
|
|
80
|
+
`{ 'text', 'firstname', 'First Name' }`
|
|
81
|
+
|
|
82
|
+
In the example above:
|
|
83
|
+
- The first item (text) defines the type of the input - this will yield: `<input type="text"`
|
|
84
|
+
- The second item (firstname) defines the name value of the input - this will yield: `<input name="firstname"`
|
|
85
|
+
- The third item (First Name) defines the Label value- this will yield: `<label for="firstname">First Name</label>`
|
|
86
|
+
|
|
87
|
+
|
|
88
|
+
|
|
62
89
|
## Input Validation
|
|
63
90
|
- **Validation**: Object specifying validation rules. This can include:
|
|
64
91
|
- **Required**: Boolean to specify if the field is mandatory.
|
|
@@ -68,95 +95,174 @@ The form schema is an array of field definitions. Each field is defined by an ar
|
|
|
68
95
|
- **MaxLength**: Specifies the maximum number of characters allowed.
|
|
69
96
|
- Example: `maxlength: 50`
|
|
70
97
|
- **Pattern**: A regex pattern the input must match.
|
|
71
|
-
- Example: `pattern: /^[A-Za-z0-9]
|
|
72
|
-
|
|
73
|
-
|
|
98
|
+
- Example: `pattern: "/^[A-Za-z0-9]+$/"`
|
|
99
|
+
|
|
100
|
+
**Formique will filter out any invalid validation defined and throw warnings on the browser console.E.g. you define min and max validations for a text field Formique will filter these out.**
|
|
101
|
+
|
|
74
102
|
|
|
75
103
|
## Input Attributes
|
|
76
104
|
- **Attributes**: Object specifying additional attributes like `id`, `class`, `style`, etc.
|
|
77
|
-
- Example: `
|
|
105
|
+
- Example: `{ id: 'username', class: 'form-input', style: 'font-size: 13px;' }`
|
|
78
106
|
|
|
79
107
|
## Binding
|
|
80
108
|
- **Binding**: Optional binding syntax for dynamic data. It can use `bind:value` or `::inputName`.
|
|
81
|
-
- Example: `
|
|
109
|
+
- Example: `'bind:value'` or `'::inputName'` - inputName must be the value defined as the input name (second item) in the input definition object.
|
|
82
110
|
|
|
83
111
|
## Options
|
|
84
|
-
- **Options**: For
|
|
85
|
-
- Example: `
|
|
112
|
+
- **Options**: For singleSelect,, multipleSelect, radio, and checkbox inputs. This is an array of options, each with a `value` and `label`.
|
|
113
|
+
- Example: `[{ value: 'male', label: 'Male' }, { value: 'female', label: 'Female' }]`
|
|
86
114
|
|
|
115
|
+
### Selected Options
|
|
116
|
+
For fields like singleSelect and multipleSelect you can also define default or pre selected options this way:
|
|
87
117
|
|
|
118
|
+
`[{ value: 'red', label: 'Red' }, { value: 'blue', label: 'Blue', selected: true }]`
|
|
88
119
|
|
|
89
|
-
|
|
120
|
+
In the example given: the blue option will be selected by default.
|
|
90
121
|
|
|
91
122
|
|
|
92
|
-
|
|
123
|
+
## Installation
|
|
93
124
|
|
|
94
|
-
|
|
125
|
+
There are two ways to install and use Formique in your project:
|
|
95
126
|
|
|
96
|
-
|
|
127
|
+
## Option A: Use Formique as a Universal Module Definition (UMD) Module
|
|
97
128
|
|
|
98
|
-
|
|
99
|
-
git clone https://github.com/Gugulethu-Nyoni/formique.git
|
|
129
|
+
1. Include the CSS and JavaScript in the head section of your HTML file:
|
|
100
130
|
|
|
101
|
-
|
|
102
|
-
|
|
131
|
+
```html
|
|
132
|
+
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/formique-css@1.0.1/formique.min.css">
|
|
133
|
+
```
|
|
103
134
|
|
|
104
|
-
```html
|
|
135
|
+
2. Insert the js script tag just before the closing tag ```html </body> ``` of your html file.
|
|
105
136
|
|
|
106
|
-
|
|
137
|
+
```html
|
|
138
|
+
<script src="https://cdn.jsdelivr.net/npm/formique@1.0.1/formique.umd.js"></script>
|
|
139
|
+
```
|
|
140
|
+
## Usage Example
|
|
107
141
|
|
|
108
|
-
|
|
142
|
+
1. Define the form container somewhere in the html body:
|
|
109
143
|
|
|
110
144
|
```html
|
|
111
|
-
|
|
112
|
-
<script type="module">
|
|
113
|
-
|
|
114
|
-
import { Formique } from './formique/formique.js';
|
|
115
|
-
|
|
116
|
-
</script>
|
|
117
|
-
|
|
145
|
+
<div id="formique"></div>
|
|
118
146
|
```
|
|
119
147
|
|
|
120
148
|
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
### Installation
|
|
124
|
-
|
|
149
|
+
2. Define your form parameters (formParams), form schema (formSchema) and then initialize Formique in script which should go below this script tag:
|
|
125
150
|
|
|
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
151
|
|
|
136
152
|
```html
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
153
|
+
<script src="https://cdn.jsdelivr.net/npm/formique@1.0.1/formique.umd.js"></script>
|
|
154
|
+
|
|
155
|
+
<script>
|
|
156
|
+
const formParams = {
|
|
157
|
+
method: 'post',
|
|
158
|
+
action: 'submit.js',
|
|
159
|
+
id: 'myForm',
|
|
160
|
+
class: 'form',
|
|
161
|
+
semantq: true,
|
|
162
|
+
style: 'width: 100%; font-size: 14px;'
|
|
163
|
+
};
|
|
164
|
+
|
|
165
|
+
const formSchema = [
|
|
166
|
+
['text', 'name', 'Name', { required: true }, {}, ''],
|
|
167
|
+
['email', 'email', 'Email', { required: true }, {}, ''],
|
|
168
|
+
[
|
|
169
|
+
'singleSelect', 'diet', 'Dietary Requirements', {required: true}, {}, '',
|
|
170
|
+
[
|
|
171
|
+
{value: 'gluten-free', label: 'Gluten-free'},
|
|
172
|
+
{value: 'dairy-free', label: 'Dairy-free'},
|
|
173
|
+
{value: 'keto', label: 'Ketogenic'},
|
|
174
|
+
{value: 'low-carb', label: 'Low-carb'},
|
|
175
|
+
{value: 'pescatarian', label: 'Pescatarian'},
|
|
176
|
+
{value: 'halal', label: 'Halal'},
|
|
177
|
+
{value: 'kosher', label: 'Kosher'},
|
|
178
|
+
{value: 'vegetarian', label: 'Vegetarian'},
|
|
179
|
+
{value: 'lacto-ovo-vegetarian', label: 'Lacto-ovo-vegetarian'},
|
|
180
|
+
{value: 'raw-food', label: 'Raw food'},
|
|
181
|
+
{value: 'macrobiotic', label: 'Macrobiotic'},
|
|
182
|
+
{value: 'flexitarian', label: 'Flexitarian'}
|
|
183
|
+
]
|
|
184
|
+
],
|
|
185
|
+
['submit', 'submitButton', 'Submit', {}, {}, '']
|
|
186
|
+
];
|
|
187
|
+
|
|
188
|
+
|
|
189
|
+
(function(formParams, formSchema) {
|
|
190
|
+
const form = new Formique(formParams, formSchema);
|
|
191
|
+
const formHTML = form.renderFormHTML();
|
|
192
|
+
})(formParams, formSchema);
|
|
193
|
+
|
|
194
|
+
</script>
|
|
140
195
|
```
|
|
141
196
|
|
|
142
|
-
|
|
197
|
+
## Option B: Use Formique as an ESM Module
|
|
143
198
|
|
|
144
|
-
|
|
199
|
+
1. Install Formique via npm:
|
|
145
200
|
|
|
146
|
-
|
|
201
|
+
```bash
|
|
202
|
+
npm install formique
|
|
203
|
+
```
|
|
147
204
|
|
|
148
|
-
|
|
205
|
+
2. Include the CSS and import Formique in the head section of your HTML file:
|
|
149
206
|
|
|
150
|
-
|
|
207
|
+
```html
|
|
208
|
+
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/formique-css@1.0.1/formique.min.css">
|
|
209
|
+
```
|
|
210
|
+
3. Define form container somewhere in the html body:
|
|
151
211
|
|
|
212
|
+
```html
|
|
213
|
+
<div id="formique"></div>
|
|
152
214
|
```
|
|
153
215
|
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
216
|
+
4. Define your form parameters (formParams), form schema (formSchema) and then initialize Formique in script which should go before the ```html </body> ``` tag.
|
|
217
|
+
|
|
218
|
+
```html
|
|
219
|
+
<script type="module">
|
|
220
|
+
import Formique from 'formique';
|
|
221
|
+
|
|
222
|
+
const formParams = {
|
|
223
|
+
method: 'post',
|
|
224
|
+
action: 'submit.js',
|
|
225
|
+
id: 'myForm',
|
|
226
|
+
class: 'form',
|
|
227
|
+
semantq: true,
|
|
228
|
+
style: 'width: 100%; font-size: 14px;'
|
|
229
|
+
};
|
|
230
|
+
|
|
231
|
+
const formSchema = [
|
|
232
|
+
['text', 'name', 'Name', { required: true }, {}, ''],
|
|
233
|
+
['email', 'email', 'Email', { required: true }, {}, ''],
|
|
234
|
+
[
|
|
235
|
+
'singleSelect', 'diet', 'Dietary Requirements', {required: true}, {}, '',
|
|
236
|
+
[
|
|
237
|
+
{value: 'gluten-free', label: 'Gluten-free'},
|
|
238
|
+
{value: 'dairy-free', label: 'Dairy-free'},
|
|
239
|
+
{value: 'keto', label: 'Ketogenic'},
|
|
240
|
+
{value: 'low-carb', label: 'Low-carb'},
|
|
241
|
+
{value: 'pescatarian', label: 'Pescatarian'},
|
|
242
|
+
{value: 'halal', label: 'Halal'},
|
|
243
|
+
{value: 'kosher', label: 'Kosher'},
|
|
244
|
+
{value: 'vegetarian', label: 'Vegetarian'},
|
|
245
|
+
{value: 'lacto-ovo-vegetarian', label: 'Lacto-ovo-vegetarian'},
|
|
246
|
+
{value: 'raw-food', label: 'Raw food'},
|
|
247
|
+
{value: 'macrobiotic', label: 'Macrobiotic'},
|
|
248
|
+
{value: 'flexitarian', label: 'Flexitarian'}
|
|
249
|
+
]
|
|
250
|
+
],
|
|
251
|
+
['submit', 'submitButton', 'Submit', {}, {}, ''],
|
|
252
|
+
];
|
|
253
|
+
|
|
254
|
+
const form = new Formique(formParams, formSchema);
|
|
255
|
+
const formHTML = form.renderFormHTML();
|
|
256
|
+
|
|
257
|
+
</script>
|
|
258
|
+
```
|
|
259
|
+
|
|
260
|
+
Formique can be used as either a UMD module with a simple `<script>` tag or as an ESM module via npm.
|
|
261
|
+
|
|
262
|
+
# Form Schema Example
|
|
263
|
+
|
|
264
|
+
|
|
265
|
+
Here's an example of a form schema that defines various input fields with validation, attributes, binding syntax and options:
|
|
160
266
|
|
|
161
267
|
```javascript
|
|
162
268
|
const formSchema = [
|
|
@@ -269,13 +375,9 @@ const formParams = {
|
|
|
269
375
|
style: 'width: 100%; font-size: 14px;' // Inline CSS styling
|
|
270
376
|
};
|
|
271
377
|
|
|
272
|
-
const form = new Formique(formParams, formSchema);
|
|
273
|
-
const formHTML = form.renderFormHTML();
|
|
274
|
-
console.log(formHTML);
|
|
275
378
|
```
|
|
276
379
|
|
|
277
380
|
|
|
278
|
-
|
|
279
381
|
# Full List of Possible Form Parameters
|
|
280
382
|
|
|
281
383
|
```javascript
|
|
@@ -299,7 +401,7 @@ const formParams = {
|
|
|
299
401
|
- **action**: The URL where the form data will be submitted.
|
|
300
402
|
- **id**: A unique identifier for the form.
|
|
301
403
|
- **class**: CSS class names applied to the form for styling purposes.
|
|
302
|
-
- **semantq**: Boolean value
|
|
404
|
+
- **semantq**: Boolean value to add syntax sugar in the Semantq JS Framework use case. If set event handler attributes will be rendered in the format: @click={incrementer}
|
|
303
405
|
- **style**: Inline CSS styling applied directly to the form element.
|
|
304
406
|
- **enctype**: Specifies how the form data should be encoded when submitted ('application/x-www-form-urlencoded', 'multipart/form-data', 'text/plain').
|
|
305
407
|
- **target**: Specifies where to display the response after submitting the form ('_self', '_blank', '_parent', '_top').
|
|
@@ -310,15 +412,6 @@ By customizing these parameters, you can control various aspects of the form's b
|
|
|
310
412
|
|
|
311
413
|
|
|
312
414
|
|
|
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
415
|
|
|
323
416
|
## Example HTML Output
|
|
324
417
|
|
|
@@ -495,24 +588,22 @@ In your html, place this markup as placegolder for where the form will be render
|
|
|
495
588
|
|
|
496
589
|
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
590
|
|
|
498
|
-
- **Wrapper for Input Elements:** `input-block`
|
|
591
|
+
- **Wrapper (div) for Input Elements:** `input-block`
|
|
499
592
|
- **Input Fields:** `form-input`
|
|
500
593
|
- **Radio Button Groups:** `radio-group`
|
|
501
594
|
- **Checkbox Groups:** `checkbox-group`
|
|
502
595
|
- **Select Dropdowns:** `form-select`
|
|
503
596
|
|
|
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.
|
|
597
|
+
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. Also, Formique implements these class names by default. The input class can be overidden by defining your preferred class names in the input attributes object e.g.
|
|
505
598
|
|
|
506
|
-
|
|
599
|
+
```javascript
|
|
600
|
+
{ class: 'form-control' }
|
|
601
|
+
```
|
|
507
602
|
|
|
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
603
|
|
|
511
604
|
### Inline Styling
|
|
512
605
|
|
|
513
|
-
In addition to external stylesheets, individual form elements can be styled directly via attributes specified in the form schema. This allows for
|
|
514
|
-
|
|
515
|
-
By utilizing these options, you can easily adapt the look and feel of your form to meet your project's design requirements.
|
|
606
|
+
In addition to external stylesheets, individual form elements can be styled directly via attributes specified in the form schema. This allows for fine grained control over the appearance of each element.
|
|
516
607
|
|
|
517
608
|
|
|
518
609
|
## Contribute
|
|
@@ -525,4 +616,4 @@ Formique is licensed under the MIT License.
|
|
|
525
616
|
|
|
526
617
|
## Keywords
|
|
527
618
|
|
|
528
|
-
Javascript
|
|
619
|
+
Javascript forms, declarative form syntax, js form library, formique
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "formique",
|
|
3
|
-
"version": "1.0.
|
|
3
|
+
"version": "1.0.4",
|
|
4
4
|
"description": "Formique Declarative Form Syntax JavaScript Library",
|
|
5
5
|
"main": "dist/formique.umd.js",
|
|
6
6
|
"module": "dist/formique.esm.js",
|
|
@@ -11,4 +11,4 @@
|
|
|
11
11
|
"keywords": [],
|
|
12
12
|
"author": "",
|
|
13
13
|
"license": "ISC"
|
|
14
|
-
}
|
|
14
|
+
}
|