react-form-dto 0.0.5 → 0.0.7
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 +246 -49
- package/dist/examples/profileForm.i18n.d.ts +2 -0
- package/dist/types/dto.d.ts +14 -9
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -1,41 +1,49 @@
|
|
|
1
1
|
# React Form DTO
|
|
2
2
|
|
|
3
|
-
|
|
4
|
-
|
|
3
|
+
**Schema-first, DTO-driven form builder for React and Material UI (MUI v7)**
|
|
4
|
+
|
|
5
|
+
React Form DTO helps you build complex, responsive, and accessible forms using declarative JSON/TypeScript DTOs instead of verbose JSX. It is designed for enterprise-grade applications, internal tools, admin panels, and workflows where forms are dynamic, configurable, and data-driven.
|
|
5
6
|
|
|
6
7
|
---
|
|
7
8
|
|
|
8
|
-
##
|
|
9
|
+
## Why React Form DTO?
|
|
9
10
|
|
|
10
|
-
-
|
|
11
|
-
- **Material UI integration**: Uses MUI v7 components for consistent design and accessibility.
|
|
12
|
-
- **Responsive grid layout**: 12-column system mapped to MUI’s `Grid` with `size` props.
|
|
13
|
-
- **Conditional rendering**: Show/hide fields or sections based on other field values.
|
|
14
|
-
- **Custom renderers**: Override default MUI inputs with your own components.
|
|
15
|
-
- **TypeScript support**: Strongly typed DTOs for safety and autocompletion.
|
|
16
|
-
- **Composable architecture**: `FormBuilder`, `Section`, and `Field` components.
|
|
17
|
-
- **Built-in validation**: Use `useFormBuilder` hook and validation utilities for field and form validation.
|
|
11
|
+
Most form libraries focus on low-level state management. React Form DTO operates at a **higher abstraction level**.
|
|
18
12
|
|
|
19
|
-
|
|
13
|
+
**Use this library when:**
|
|
20
14
|
|
|
21
|
-
|
|
15
|
+
- Your forms are generated from backend schemas or configurations
|
|
16
|
+
- You want to avoid duplicating UI logic across applications
|
|
17
|
+
- You need imperative control over form state (wizards, modals, async flows)
|
|
18
|
+
- Your project is built on Material UI
|
|
22
19
|
|
|
23
|
-
|
|
20
|
+
**Key advantages:**
|
|
24
21
|
|
|
25
|
-
|
|
22
|
+
- 📄 **DTO-first design** – define forms entirely in JSON or TypeScript
|
|
23
|
+
- 🎨 **Material UI v7 native** – consistent design & accessibility
|
|
24
|
+
- 🧱 **Composable architecture** – Form → Section → Field
|
|
25
|
+
- 🎯 **Imperative API** – programmatic access via refs
|
|
26
|
+
- 🔀 **Conditional rendering** – show/hide fields dynamically
|
|
27
|
+
- 🧩 **Custom renderers** – plug in your own components
|
|
28
|
+
- 🛡️ **Strong TypeScript typing** – predictable, safe APIs
|
|
26
29
|
|
|
27
30
|
---
|
|
28
31
|
|
|
29
|
-
##
|
|
32
|
+
## How It Compares
|
|
30
33
|
|
|
31
|
-
|
|
32
|
-
|
|
34
|
+
| Feature | React Form DTO | React Hook Form | Formik |
|
|
35
|
+
|------|---------------|----------------|--------|
|
|
36
|
+
| Schema/DTO driven | ✅ Native | ❌ Manual | ❌ Manual |
|
|
37
|
+
| MUI-first | ✅ Yes | ⚠️ Partial | ⚠️ Partial |
|
|
38
|
+
| Imperative API | ✅ First-class | ⚠️ Limited | ⚠️ Limited |
|
|
39
|
+
| Large dynamic forms | ✅ Excellent | ⚠️ Medium | ❌ Poor |
|
|
40
|
+
| Boilerplate | ✅ Minimal | ❌ High | ❌ High |
|
|
33
41
|
|
|
34
|
-
|
|
42
|
+
> React Form DTO is **not a replacement** for React Hook Form. It is a higher-level abstraction for schema-driven UI generation.
|
|
35
43
|
|
|
36
|
-
|
|
44
|
+
---
|
|
37
45
|
|
|
38
|
-
|
|
46
|
+
## Installation
|
|
39
47
|
|
|
40
48
|
```bash
|
|
41
49
|
git clone https://github.com/Shakir-Afridi/react-form-dto.git
|
|
@@ -43,16 +51,15 @@ cd react-form-dto
|
|
|
43
51
|
npm install
|
|
44
52
|
```
|
|
45
53
|
|
|
46
|
-
|
|
47
|
-
> - Node.js >= 18
|
|
48
|
-
> - React >= 19
|
|
49
|
-
> - Material UI >= 7
|
|
54
|
+
### Requirements
|
|
50
55
|
|
|
51
|
-
|
|
56
|
+
- Node.js >= 18
|
|
57
|
+
- React >= 19
|
|
58
|
+
- Material UI >= 7
|
|
52
59
|
|
|
53
|
-
|
|
60
|
+
---
|
|
54
61
|
|
|
55
|
-
|
|
62
|
+
## Minimal Example
|
|
56
63
|
|
|
57
64
|
```tsx
|
|
58
65
|
import { FormBuilder, type FormBuilderHandle } from 'react-form-dto';
|
|
@@ -79,11 +86,9 @@ return (
|
|
|
79
86
|
);
|
|
80
87
|
```
|
|
81
88
|
|
|
82
|
-
Refer to the documentation and examples for DTO structure and customization.
|
|
83
|
-
|
|
84
89
|
---
|
|
85
90
|
|
|
86
|
-
|
|
91
|
+
## 📋 Example Form rendered
|
|
87
92
|
|
|
88
93
|

|
|
89
94
|
|
|
@@ -187,28 +192,187 @@ const profileForm: FormDTO = {
|
|
|
187
192
|
|
|
188
193
|
---
|
|
189
194
|
|
|
190
|
-
|
|
195
|
+
## Supported Field Types
|
|
196
|
+
|
|
197
|
+
### Text Inputs
|
|
198
|
+
|
|
199
|
+
- `text`
|
|
200
|
+
- `number`
|
|
201
|
+
- `date`
|
|
202
|
+
- `email`
|
|
203
|
+
- `password`
|
|
204
|
+
|
|
205
|
+
### Selection Inputs
|
|
191
206
|
|
|
192
|
-
-
|
|
193
|
-
-
|
|
194
|
-
-
|
|
195
|
-
- **AutoCompleteField**: single autocomplete
|
|
196
|
-
- **MultiAutoCompleteField**: multi-select autocomplete
|
|
207
|
+
- `select`
|
|
208
|
+
- `autocomplete`
|
|
209
|
+
- `multi-autocomplete`
|
|
197
210
|
|
|
198
|
-
|
|
211
|
+
### Boolean Inputs
|
|
212
|
+
|
|
213
|
+
- `checkbox`
|
|
214
|
+
|
|
215
|
+
---
|
|
216
|
+
|
|
217
|
+
## 🌍 Internationalization (I18n)
|
|
218
|
+
|
|
219
|
+
React Form DTO has built-in support for multi-language forms through `I18nText` and `I18nOption` types.
|
|
220
|
+
|
|
221
|
+
### Using I18nText
|
|
222
|
+
|
|
223
|
+
Any text property (`label`, `placeholder`, `title`, `description`, validation messages) can be either a plain string or a locale map:
|
|
199
224
|
|
|
200
225
|
```tsx
|
|
226
|
+
// Simple string (single language)
|
|
201
227
|
{
|
|
202
|
-
|
|
203
|
-
renderer: MyCustomComponent
|
|
228
|
+
label: "First Name"
|
|
204
229
|
}
|
|
230
|
+
|
|
231
|
+
// Multi-language support
|
|
232
|
+
{
|
|
233
|
+
label: {
|
|
234
|
+
en: "First Name",
|
|
235
|
+
fr: "Prénom",
|
|
236
|
+
es: "Nombre",
|
|
237
|
+
de: "Vorname"
|
|
238
|
+
}
|
|
239
|
+
}
|
|
240
|
+
```
|
|
241
|
+
|
|
242
|
+
### Using I18nOption for Selections
|
|
243
|
+
|
|
244
|
+
For select, autocomplete, and multi-autocomplete fields, you can use `I18nOption` objects to provide translatable labels while maintaining stable values:
|
|
245
|
+
|
|
246
|
+
```tsx
|
|
247
|
+
{
|
|
248
|
+
id: "country",
|
|
249
|
+
type: "select",
|
|
250
|
+
label: { en: "Country", fr: "Pays" },
|
|
251
|
+
options: [
|
|
252
|
+
{
|
|
253
|
+
value: "us",
|
|
254
|
+
label: { en: "United States", fr: "États-Unis" }
|
|
255
|
+
},
|
|
256
|
+
{
|
|
257
|
+
value: "fr",
|
|
258
|
+
label: { en: "France", fr: "France" }
|
|
259
|
+
},
|
|
260
|
+
{
|
|
261
|
+
value: "de",
|
|
262
|
+
label: { en: "Germany", fr: "Allemagne" }
|
|
263
|
+
}
|
|
264
|
+
]
|
|
265
|
+
}
|
|
266
|
+
```
|
|
267
|
+
|
|
268
|
+
**Backward Compatibility:** Simple string arrays still work:
|
|
269
|
+
|
|
270
|
+
```tsx
|
|
271
|
+
options: ["USA", "France", "Germany"] // Still supported
|
|
205
272
|
```
|
|
206
273
|
|
|
207
|
-
### Validation
|
|
274
|
+
### I18n Validation Messages
|
|
208
275
|
|
|
209
|
-
|
|
276
|
+
Validation messages also support I18n:
|
|
210
277
|
|
|
211
278
|
```tsx
|
|
279
|
+
validations: {
|
|
280
|
+
required: {
|
|
281
|
+
en: "This field is required",
|
|
282
|
+
fr: "Ce champ est obligatoire",
|
|
283
|
+
es: "Este campo es obligatorio"
|
|
284
|
+
},
|
|
285
|
+
validate: (value) => value.length < 2
|
|
286
|
+
? {
|
|
287
|
+
en: "Minimum 2 characters",
|
|
288
|
+
fr: "Minimum 2 caractères"
|
|
289
|
+
}
|
|
290
|
+
: null
|
|
291
|
+
}
|
|
292
|
+
```
|
|
293
|
+
|
|
294
|
+
### Complete I18n Example
|
|
295
|
+
|
|
296
|
+
```tsx
|
|
297
|
+
const multilingualForm: FormDTO = {
|
|
298
|
+
title: {
|
|
299
|
+
en: "User Registration",
|
|
300
|
+
fr: "Inscription de l'utilisateur",
|
|
301
|
+
es: "Registro de Usuario"
|
|
302
|
+
},
|
|
303
|
+
description: {
|
|
304
|
+
en: "Please fill in your details",
|
|
305
|
+
fr: "Veuillez remplir vos coordonnées",
|
|
306
|
+
es: "Por favor complete sus datos"
|
|
307
|
+
},
|
|
308
|
+
sections: [
|
|
309
|
+
{
|
|
310
|
+
id: "personal",
|
|
311
|
+
heading: {
|
|
312
|
+
en: "Personal Information",
|
|
313
|
+
fr: "Informations personnelles",
|
|
314
|
+
es: "Información Personal"
|
|
315
|
+
},
|
|
316
|
+
fields: [
|
|
317
|
+
{
|
|
318
|
+
id: "title",
|
|
319
|
+
type: "select",
|
|
320
|
+
label: { en: "Title", fr: "Titre", es: "Título" },
|
|
321
|
+
options: [
|
|
322
|
+
{ value: "mr", label: { en: "Mr", fr: "M.", es: "Sr." } },
|
|
323
|
+
{ value: "ms", label: { en: "Ms", fr: "Mme", es: "Sra." } },
|
|
324
|
+
{ value: "dr", label: { en: "Dr", fr: "Dr", es: "Dr." } }
|
|
325
|
+
]
|
|
326
|
+
},
|
|
327
|
+
{
|
|
328
|
+
id: "firstName",
|
|
329
|
+
type: "text",
|
|
330
|
+
label: { en: "First Name", fr: "Prénom", es: "Nombre" },
|
|
331
|
+
placeholder: {
|
|
332
|
+
en: "Enter your first name",
|
|
333
|
+
fr: "Entrez votre prénom",
|
|
334
|
+
es: "Ingrese su nombre"
|
|
335
|
+
},
|
|
336
|
+
validations: {
|
|
337
|
+
required: {
|
|
338
|
+
en: "First name is required",
|
|
339
|
+
fr: "Le prénom est obligatoire",
|
|
340
|
+
es: "El nombre es obligatorio"
|
|
341
|
+
}
|
|
342
|
+
}
|
|
343
|
+
}
|
|
344
|
+
]
|
|
345
|
+
}
|
|
346
|
+
]
|
|
347
|
+
};
|
|
348
|
+
```
|
|
349
|
+
|
|
350
|
+
> **Note:** The library provides the I18n structure. You'll need to implement locale selection and text resolution in your application layer.
|
|
351
|
+
|
|
352
|
+
---
|
|
353
|
+
|
|
354
|
+
## Custom Field Renderers
|
|
355
|
+
|
|
356
|
+
Override any default renderer by supplying your own component:
|
|
357
|
+
|
|
358
|
+
```ts
|
|
359
|
+
{
|
|
360
|
+
id: "salary",
|
|
361
|
+
type: "number",
|
|
362
|
+
label: "Salary",
|
|
363
|
+
renderer: CurrencyInput
|
|
364
|
+
}
|
|
365
|
+
```
|
|
366
|
+
|
|
367
|
+
This makes the library extensible without modifying core logic.
|
|
368
|
+
|
|
369
|
+
---
|
|
370
|
+
|
|
371
|
+
## Validation API
|
|
372
|
+
|
|
373
|
+
Validation is handled through the `useFormBuilder` hook and the imperative form handle.
|
|
374
|
+
|
|
375
|
+
```ts
|
|
212
376
|
const {
|
|
213
377
|
handleChange, // Function to update a field value: (id, value) => void
|
|
214
378
|
validateAll, // Function to validate all fields: () => Record<string, string[]>
|
|
@@ -218,13 +382,42 @@ const {
|
|
|
218
382
|
} = useFormBuilder(myFormDTO);
|
|
219
383
|
```
|
|
220
384
|
|
|
221
|
-
|
|
222
|
-
- `validateAll()`: Validate all fields and return errors.
|
|
223
|
-
- `validateField(id)`: Validate a specific field and return errors for that field.
|
|
224
|
-
- `getValues()`: Get all form values.
|
|
225
|
-
- `getErrors()`: Get all form errors.
|
|
385
|
+
### Validation Rules
|
|
226
386
|
|
|
227
|
-
|
|
387
|
+
```ts
|
|
388
|
+
validations: {
|
|
389
|
+
required: "This field is required",
|
|
390
|
+
validate: (value) => value.length < 2 ? "Minimum 2 characters" : null
|
|
391
|
+
}
|
|
392
|
+
```
|
|
393
|
+
|
|
394
|
+
> Recommendation: Standardize validation return values to `string[]` for predictable handling in large applications.
|
|
395
|
+
|
|
396
|
+
---
|
|
397
|
+
|
|
398
|
+
## Documentation & Demo
|
|
399
|
+
|
|
400
|
+
- 📘 **Documentation:** See full DTO reference, APIs, and advanced examples [Documentation](https://shakir-afridi.github.io/react-form-dto/docs)
|
|
401
|
+
- 📗 **Storybook:** Interactive component playground and live demos [Live Demo](https://shakir-afridi.github.io/react-form-dto/storybook)
|
|
402
|
+
|
|
403
|
+
---
|
|
404
|
+
|
|
405
|
+
## Ideal Use Cases
|
|
406
|
+
|
|
407
|
+
- Admin dashboards
|
|
408
|
+
- Internal enterprise tools
|
|
409
|
+
- Multi-step onboarding flows
|
|
410
|
+
- Config-driven forms from APIs
|
|
411
|
+
- Rapid UI scaffolding for MUI projects
|
|
412
|
+
|
|
413
|
+
---
|
|
414
|
+
|
|
415
|
+
## Roadmap (Suggested)
|
|
416
|
+
|
|
417
|
+
- Field registry API (`registerFieldType`)
|
|
418
|
+
- Async validation support
|
|
419
|
+
- Form-level conditional logic
|
|
420
|
+
- Schema import (JSON Schema / OpenAPI)
|
|
228
421
|
|
|
229
422
|
---
|
|
230
423
|
|
|
@@ -240,4 +433,8 @@ Refer to the documentation and examples for DTO structure and customization.
|
|
|
240
433
|
|
|
241
434
|
## 📜 License
|
|
242
435
|
|
|
243
|
-
MIT
|
|
436
|
+
MIT
|
|
437
|
+
|
|
438
|
+
---
|
|
439
|
+
|
|
440
|
+
**React Form DTO — Schema-first forms for Material UI**
|
package/dist/types/dto.d.ts
CHANGED
|
@@ -5,13 +5,18 @@ export type LayoutDTO = {
|
|
|
5
5
|
align?: "start" | "center" | "end" | "stretch";
|
|
6
6
|
justify?: "start" | "center" | "end" | "space-between";
|
|
7
7
|
};
|
|
8
|
+
export type I18nText = string | Record<string, string>;
|
|
9
|
+
export type I18nOption = {
|
|
10
|
+
value: string;
|
|
11
|
+
label: I18nText;
|
|
12
|
+
};
|
|
8
13
|
export type InputType = "text" | "email" | "password" | "textarea" | "number" | "select" | "checkbox" | "date" | "autocomplete" | "multi-autocomplete" | "radio";
|
|
9
14
|
export type FieldDTO = {
|
|
10
15
|
id: string;
|
|
11
16
|
type: InputType;
|
|
12
|
-
label:
|
|
13
|
-
placeholder?:
|
|
14
|
-
options?:
|
|
17
|
+
label: I18nText;
|
|
18
|
+
placeholder?: I18nText;
|
|
19
|
+
options?: Array<I18nOption | I18nText>;
|
|
15
20
|
rows?: number;
|
|
16
21
|
disabled?: boolean;
|
|
17
22
|
defaultValue?: any;
|
|
@@ -20,27 +25,27 @@ export type FieldDTO = {
|
|
|
20
25
|
};
|
|
21
26
|
export type SectionDTO = {
|
|
22
27
|
id: string;
|
|
23
|
-
heading?:
|
|
28
|
+
heading?: I18nText;
|
|
24
29
|
headingFontSize?: number;
|
|
25
|
-
description?:
|
|
30
|
+
description?: I18nText;
|
|
26
31
|
descriptionFontSize?: number;
|
|
27
32
|
layout?: LayoutDTO;
|
|
28
33
|
fields: FieldDTO[];
|
|
29
34
|
};
|
|
30
35
|
export type FormDTO = {
|
|
31
|
-
title?:
|
|
36
|
+
title?: I18nText;
|
|
32
37
|
titleFontSize?: number;
|
|
33
|
-
description?:
|
|
38
|
+
description?: I18nText;
|
|
34
39
|
descriptionFontSize?: number;
|
|
35
40
|
layout?: LayoutDTO;
|
|
36
41
|
sections: SectionDTO[];
|
|
37
42
|
};
|
|
38
43
|
export type Validations = {
|
|
39
|
-
required?: boolean |
|
|
44
|
+
required?: boolean | I18nText;
|
|
40
45
|
min?: number;
|
|
41
46
|
max?: number;
|
|
42
47
|
minLength?: number;
|
|
43
48
|
maxLength?: number;
|
|
44
49
|
pattern?: RegExp;
|
|
45
|
-
validate?: (value: any) =>
|
|
50
|
+
validate?: (value: any) => I18nText | null;
|
|
46
51
|
};
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "react-form-dto",
|
|
3
3
|
"description": "A React library for building forms using DTOs with MUI and TypeScript.",
|
|
4
|
-
"version": "0.0.
|
|
4
|
+
"version": "0.0.7",
|
|
5
5
|
"main": "dist/react-form-dto.umd.js",
|
|
6
6
|
"module": "dist/react-form-dto.es.js",
|
|
7
7
|
"types": "dist/index.d.ts",
|