ynotsoft-dynamic-form 1.0.39 → 1.0.41

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 DELETED
@@ -1,692 +0,0 @@
1
- # DynamicForm Component - Complete Field Reference
2
-
3
- ## Install
4
-
5
- ```
6
- npm install ynotsoft-dynamic-form
7
-
8
- # install peer dependencies
9
- npm install react-hot-toast dayjs react-select react-day-picker dompurify @radix-ui/react-label @radix-ui/react-popover @radix-ui/react-radio-group @radix-ui/react-select @radix-ui/react-separator @heroicons/react
10
-
11
- ```
12
-
13
- ### Build the Library
14
-
15
- From the root of the library (dynamic-form/):
16
-
17
- ```
18
- npm run build
19
- npm link
20
- ```
21
-
22
- ### Link It Inside the Example App
23
-
24
- Now connect the example app to the linked library:
25
-
26
- ```
27
- cd ../example
28
- npm link ynotsoft-dynamic-form
29
- ```
30
-
31
- ### Start the Example App
32
-
33
- ```
34
- npm run dev
35
- ```
36
-
37
- ### TODO:
38
-
39
- - remove radixui as dependency, maybe replace with ShadCN and maintain it ourselves
40
-
41
- ## Overview
42
-
43
- The DynamicForm component provides a flexible, declarative way to build forms with various field types, validation, conditional logic, and styling options.
44
-
45
- ## Basic Usage
46
-
47
- ```javascript
48
- import DynamicForm from "./lib/DynamicForm/DynamicForm";
49
-
50
- const formDefinition = {
51
- fields: [
52
- // Field definitions here
53
- ],
54
- };
55
-
56
- <DynamicForm
57
- formDefinition={formDefinition}
58
- defaultValues={{ name: "John Doe" }}
59
- sendFormValues={(values) => console.log(values)}
60
- onFieldsChange={(values) => console.log("Changed:", values)}
61
- />;
62
- ```
63
-
64
- ---
65
-
66
- ## Field Types
67
-
68
- ### 1. Header Field
69
-
70
- Used for section titles and form organization.
71
-
72
- ```javascript
73
- {
74
- type: 'header',
75
- label: 'Personal Information',
76
- size: 'xl', // 'sm' | 'md' | 'lg' | 'xl' | '2xl' | '3xl' | '4xl'
77
- align: 'left', // 'left' | 'center' | 'right'
78
- underline: true // Boolean - adds bottom border
79
- }
80
- ```
81
-
82
- ### 2. Input Field
83
-
84
- Standard text input with shadcn/ui styling.
85
-
86
- ```javascript
87
- {
88
- name: 'fullName',
89
- label: 'Full Name',
90
- type: 'input',
91
- required: true,
92
- placeholder: 'Enter your name',
93
- value: 'John Doe',
94
- disabled: false,
95
- maxLength: 100,
96
- validate: (value) => {
97
- if (value.length < 2) return 'Name must be at least 2 characters';
98
- return null;
99
- }
100
- }
101
- ```
102
-
103
- ### 3. Email Field
104
-
105
- Email input with validation and shadcn/ui styling.
106
-
107
- ```javascript
108
- {
109
- name: 'email',
110
- label: 'Email Address',
111
- type: 'email',
112
- required: true,
113
- placeholder: 'you@example.com',
114
- value: 'john@example.com'
115
- }
116
- ```
117
-
118
- ### 4. TextArea Field
119
-
120
- Multi-line text input.
121
-
122
- ```javascript
123
- {
124
- name: 'description',
125
- label: 'Description',
126
- type: 'textarea',
127
- required: false,
128
- placeholder: 'Enter description...',
129
- rows: 4,
130
- maxLength: 500,
131
- showCharCount: true,
132
- value: 'Initial description'
133
- }
134
- ```
135
-
136
- ### 5. Select Field
137
-
138
- Dropdown selection with single choice.
139
-
140
- ```javascript
141
- {
142
- name: 'country',
143
- label: 'Country',
144
- type: 'select',
145
- required: true,
146
- value: 'us',
147
- options: [
148
- { value: '', label: 'Select a country' },
149
- { value: 'us', label: 'United States' },
150
- { value: 'uk', label: 'United Kingdom' },
151
- { value: 'ca', label: 'Canada' }
152
- ],
153
- // Dynamic options from API
154
- optionsUrl: '/api/countries',
155
- dependsOn: 'region' // Reload when 'region' changes
156
- }
157
- ```
158
-
159
- ### 6. MultiSelect Field
160
-
161
- Multiple selection dropdown.
162
-
163
- ```javascript
164
- {
165
- name: 'interests',
166
- label: 'Interests',
167
- type: 'multiselect',
168
- required: true,
169
- value: ['sports', 'tech'],
170
- options: [
171
- { value: 'sports', label: 'Sports' },
172
- { value: 'music', label: 'Music' },
173
- { value: 'tech', label: 'Technology' },
174
- { value: 'travel', label: 'Travel' }
175
- ],
176
- validate: (value) => {
177
- if (value && value.length > 3) return 'Select up to 3 interests';
178
- return null;
179
- }
180
- }
181
- ```
182
-
183
- ### 7. Checkbox Field
184
-
185
- Single checkbox with flexible layouts and card styling.
186
-
187
- ```javascript
188
- {
189
- name: 'agreeTerms',
190
- label: 'I agree to terms',
191
- type: 'checkbox',
192
- required: true,
193
- value: false,
194
- description: 'By checking this, you agree to our terms and conditions',
195
-
196
- // Layout options
197
- layout: 'inline', // 'inline' | 'stacked' | 'default'
198
-
199
- // Card container styling
200
- containerStyle: 'card', // Wraps in bordered card
201
- color: 'blue', // 'green' | 'blue' | 'red' | 'yellow' | 'purple' | 'indigo' | 'gray' | 'pink' | 'orange'
202
- }
203
- ```
204
-
205
- **Checkbox Layouts:**
206
-
207
- - `default`: Standard checkbox with label above
208
- - `inline`: Checkbox and label side-by-side
209
- - `stacked`: Checkbox, label, and description stacked vertically
210
-
211
- ### 8. Radio Group Field
212
-
213
- Single selection from multiple options using Radix UI.
214
-
215
- ```javascript
216
- {
217
- name: 'paymentMethod',
218
- label: 'Payment Method',
219
- type: 'radiogroup',
220
- required: true,
221
- value: 'card',
222
- options: [
223
- { value: 'card', label: 'Credit Card' },
224
- { value: 'paypal', label: 'PayPal' },
225
- { value: 'bank', label: 'Bank Transfer' }
226
- ],
227
-
228
- // Layout options
229
- inline: true, // Display options horizontally
230
-
231
- // Color variants
232
- color: 'blue', // 'green' | 'blue' | 'red' | 'yellow' | 'purple' | 'indigo' | 'gray' | 'pink' | 'orange'
233
-
234
- // Card container styling
235
- containerStyle: 'card',
236
- color: 'green'
237
- }
238
- ```
239
-
240
- ### 9. Date Picker Field
241
-
242
- Single date selection with shadcn/ui calendar.
243
-
244
- ```javascript
245
- {
246
- name: 'birthDate',
247
- label: 'Birth Date',
248
- type: 'date',
249
- required: true,
250
- placeholder: 'Select date',
251
- value: new Date('1990-01-01')
252
- }
253
- ```
254
-
255
- **Features:**
256
-
257
- - Year/month dropdown selectors
258
- - Clear and Done buttons
259
- - Blue highlight for selected date
260
- - Popover interface
261
-
262
- ### 10. Date Range Picker Field
263
-
264
- Select date ranges (from/to).
265
-
266
- ```javascript
267
- {
268
- name: 'projectDates',
269
- label: 'Project Timeline',
270
- type: 'dateRange',
271
- required: true,
272
- placeholder: 'Select date range',
273
- value: {
274
- from: new Date('2025-01-01'),
275
- to: new Date('2025-12-31')
276
- }
277
- }
278
- ```
279
-
280
- ### 11. Time Field
281
-
282
- Time picker with AM/PM selection.
283
-
284
- ```javascript
285
- {
286
- name: 'appointmentTime',
287
- label: 'Appointment Time',
288
- type: 'time',
289
- required: true,
290
- placeholder: 'Select time',
291
- value: '03:45 PM'
292
- }
293
- ```
294
-
295
- **Features:**
296
-
297
- - Hour/minute spinners
298
- - AM/PM toggle buttons
299
- - Clear and Done buttons
300
- - Format: "HH:MM AM/PM"
301
-
302
- ### 12. Date Time Picker Field
303
-
304
- Combined date and time selection.
305
-
306
- ```javascript
307
- {
308
- name: 'meetingDateTime',
309
- label: 'Meeting Date & Time',
310
- type: 'dayTimePicker',
311
- required: true,
312
- value: new Date('2025-10-17T15:30:00')
313
- }
314
- ```
315
-
316
- ### 13. File Upload Field
317
-
318
- Single or multiple file uploads.
319
-
320
- ```javascript
321
- {
322
- name: 'documents',
323
- label: 'Upload Documents',
324
- type: 'file', // or 'multifile' for multiple
325
- required: true,
326
- accept: '.pdf,.doc,.docx,.jpg,.jpeg,.png',
327
- maxSize: 5 * 1024 * 1024, // 5 MB
328
- multiple: false
329
- }
330
- ```
331
-
332
- ### 14. Hidden Field
333
-
334
- Store hidden values in the form.
335
-
336
- ```javascript
337
- {
338
- name: 'userId',
339
- type: 'hidden',
340
- value: '12345'
341
- }
342
- ```
343
-
344
- ### 15. HTML/Literal Field
345
-
346
- Display rich HTML content (non-editable).
347
-
348
- ```javascript
349
- {
350
- type: 'litertext',
351
- content: '<div class="alert">Important notice here</div>'
352
- }
353
- ```
354
-
355
- ### 16. Alert Message Field
356
-
357
- Display contextual alert messages with icons (info, success, warning, error).
358
-
359
- ```javascript
360
- {
361
- type: 'alert',
362
- variant: 'info', // 'info' | 'success' | 'warning' | 'error' | 'danger'
363
- message: 'This is an informational message'
364
- }
365
-
366
- // Success alert
367
- {
368
- type: 'alert',
369
- variant: 'success',
370
- message: 'Your form was submitted successfully!'
371
- }
372
-
373
- // Warning alert
374
- {
375
- type: 'alert',
376
- variant: 'warning',
377
- message: 'Please review your information before submitting'
378
- }
379
-
380
- // Error alert
381
- {
382
- type: 'alert',
383
- variant: 'error',
384
- message: 'There was an error processing your request'
385
- }
386
- ```
387
-
388
- **Features:**
389
-
390
- - Color-coded backgrounds (blue, green, yellow, red)
391
- - Icon indicators for each variant
392
- - Clean, accessible design
393
- - No user interaction required (display only)
394
-
395
- ### 17. Line Break Field
396
-
397
- Add visual spacing between sections.
398
-
399
- ```javascript
400
- {
401
- type: "linebreak";
402
- }
403
- ```
404
-
405
- ---
406
-
407
- ## Global Field Format Options
408
-
409
- Apply consistent styling to all fields (except header, html, linebreak, hidden, alert):
410
-
411
- ```javascript
412
- {
413
- name: 'email',
414
- label: 'Email',
415
- type: 'email',
416
-
417
- // Card container
418
- containerStyle: 'card',
419
- color: 'blue', // Card border/accent color
420
-
421
- // Layout for checkbox/radio
422
- layout: 'inline', // or 'stacked' | 'default'
423
- inline: true, // For radio groups
424
- }
425
- ```
426
-
427
- ---
428
-
429
- ## Advanced Features
430
-
431
- ### Conditional Display
432
-
433
- Show/hide fields based on other values:
434
-
435
- ```javascript
436
- {
437
- name: 'state',
438
- label: 'State',
439
- type: 'select',
440
- showIf: (values) => values.country === 'us',
441
- options: [...]
442
- }
443
- ```
444
-
445
- ### Conditional Disable
446
-
447
- Disable fields based on conditions:
448
-
449
- ```javascript
450
- {
451
- name: 'billingAddress',
452
- label: 'Billing Address',
453
- type: 'input',
454
- disabled: (values) => values.sameAsShipping === true
455
- }
456
- ```
457
-
458
- ### Custom Validation
459
-
460
- Field-level validation functions:
461
-
462
- ```javascript
463
- {
464
- name: 'password',
465
- label: 'Password',
466
- type: 'input',
467
- validate: (value) => {
468
- if (value.length < 8) return 'Password must be at least 8 characters';
469
- if (!/[A-Z]/.test(value)) return 'Must contain uppercase letter';
470
- if (!/[0-9]/.test(value)) return 'Must contain a number';
471
- return null;
472
- }
473
- }
474
- ```
475
-
476
- ### Dynamic Options
477
-
478
- Load options from API:
479
-
480
- ```javascript
481
- {
482
- name: 'city',
483
- label: 'City',
484
- type: 'select',
485
- optionsUrl: '/api/cities',
486
- dependsOn: 'state', // Reload when state changes
487
- }
488
- ```
489
-
490
- ---
491
-
492
- ## Complete Example
493
-
494
- ```javascript
495
- const formDefinition = {
496
- fields: [
497
- // Section Header
498
- {
499
- type: "header",
500
- label: "Personal Information",
501
- size: "xl",
502
- underline: true,
503
- },
504
-
505
- // Alert Message
506
- {
507
- type: "alert",
508
- variant: "info",
509
- message:
510
- "Please provide accurate information. All fields marked with * are required.",
511
- },
512
-
513
- // Input Field
514
- {
515
- name: "fullName",
516
- label: "Full Name",
517
- type: "input",
518
- required: true,
519
- placeholder: "John Doe",
520
- containerStyle: "card",
521
- color: "blue",
522
- },
523
-
524
- // Email Field
525
- {
526
- name: "email",
527
- label: "Email",
528
- type: "email",
529
- required: true,
530
- placeholder: "you@example.com",
531
- },
532
-
533
- // Date Picker
534
- {
535
- name: "birthDate",
536
- label: "Birth Date",
537
- type: "date",
538
- required: true,
539
- placeholder: "Select your birth date",
540
- },
541
-
542
- // Time Picker
543
- {
544
- name: "preferredTime",
545
- label: "Preferred Contact Time",
546
- type: "time",
547
- placeholder: "Select time",
548
- },
549
-
550
- // Section Header
551
- {
552
- type: "header",
553
- label: "Preferences",
554
- size: "lg",
555
- underline: true,
556
- },
557
-
558
- // Radio Group
559
- {
560
- name: "contactMethod",
561
- label: "Preferred Contact Method",
562
- type: "radiogroup",
563
- required: true,
564
- value: "email",
565
- inline: true,
566
- color: "green",
567
- options: [
568
- { value: "email", label: "Email" },
569
- { value: "phone", label: "Phone" },
570
- { value: "sms", label: "SMS" },
571
- ],
572
- },
573
-
574
- // MultiSelect
575
- {
576
- name: "interests",
577
- label: "Areas of Interest",
578
- type: "multiselect",
579
- value: [],
580
- options: [
581
- { value: "sports", label: "Sports" },
582
- { value: "tech", label: "Technology" },
583
- { value: "travel", label: "Travel" },
584
- { value: "food", label: "Food & Dining" },
585
- ],
586
- },
587
-
588
- // Checkbox
589
- {
590
- name: "newsletter",
591
- label: "Subscribe to newsletter",
592
- type: "checkbox",
593
- layout: "inline",
594
- description: "Get weekly updates and special offers",
595
- containerStyle: "card",
596
- color: "purple",
597
- },
598
-
599
- // TextArea
600
- {
601
- name: "comments",
602
- label: "Additional Comments",
603
- type: "textarea",
604
- rows: 4,
605
- maxLength: 500,
606
- showCharCount: true,
607
- placeholder: "Any additional information...",
608
- },
609
-
610
- // File Upload
611
- {
612
- name: "documents",
613
- label: "Upload Documents",
614
- type: "file",
615
- accept: ".pdf,.doc,.docx",
616
- maxSize: 5 * 1024 * 1024,
617
- },
618
- ],
619
- };
620
-
621
- // Usage
622
- <DynamicForm
623
- formDefinition={formDefinition}
624
- defaultValues={{
625
- fullName: "John Doe",
626
- email: "john@example.com",
627
- contactMethod: "email",
628
- }}
629
- sendFormValues={(values) => {
630
- console.log("Form submitted:", values);
631
- }}
632
- onFieldsChange={(values) => {
633
- console.log("Form changed:", values);
634
- }}
635
- />;
636
- ```
637
-
638
- ---
639
-
640
- ## Styling Reference
641
-
642
- ### Card Container Colors
643
-
644
- Available for `containerStyle='card'`:
645
-
646
- - `green` - Green border and accent
647
- - `blue` - Blue border and accent
648
- - `red` - Red border and accent
649
- - `yellow` - Yellow border and accent
650
- - `purple` - Purple border and accent
651
- - `indigo` - Indigo border and accent
652
- - `gray` - Gray border and accent
653
- - `pink` - Pink border and accent
654
- - `orange` - Orange border and accent
655
-
656
- ### Header Sizes
657
-
658
- - `sm` - Small header
659
- - `md` - Medium header
660
- - `lg` - Large header
661
- - `xl` - Extra large (default)
662
- - `2xl` - 2X large
663
- - `3xl` - 3X large
664
- - `4xl` - 4X large
665
-
666
- ### Layout Options (Checkbox/Radio)
667
-
668
- - `default` - Standard vertical layout
669
- - `inline` - Horizontal with label beside control
670
- - `stacked` - Vertical with description below
671
-
672
- ---
673
-
674
- ## Props
675
-
676
- ### DynamicForm Props
677
-
678
- - `formDefinition` - Object containing field definitions
679
- - `defaultValues` - Initial form values
680
- - `sendFormValues` - Callback when form is submitted
681
- - `onFieldsChange` - Callback when any field changes
682
- - `children` - Additional content (e.g., submit button)
683
-
684
- ---
685
-
686
- ## Notes
687
-
688
- - All fields support `required`, `disabled`, `showIf` properties
689
- - Fields are automatically validated on blur and submit
690
- - Error messages display below invalid fields
691
- - Form values are managed internally with React state
692
- - shadcn/ui components used for consistent styling