react-form-dto 0.0.1
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/LICENSE +21 -0
- package/README.md +228 -0
- package/dist/index.d.ts +1 -0
- package/dist/react-form-dto.es.js +20863 -0
- package/dist/react-form-dto.umd.js +166 -0
- package/dist/vite.svg +1 -0
- package/package.json +66 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2025 Shakir Ullah
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,228 @@
|
|
|
1
|
+
# React Form DTO
|
|
2
|
+
|
|
3
|
+
A **dynamic, DTO-driven form builder** built with **React, TypeScript, and Material UI (MUI v7)**.
|
|
4
|
+
This library lets you define forms declaratively using JSON DTOs (`FormDTO`, `SectionDTO`, `FieldDTO`) and automatically renders responsive, accessible forms with MUI components.
|
|
5
|
+
|
|
6
|
+
---
|
|
7
|
+
|
|
8
|
+
## ✨ Features
|
|
9
|
+
|
|
10
|
+
- **DTO-driven**: Define forms entirely in JSON/TypeScript objects.
|
|
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.
|
|
18
|
+
|
|
19
|
+
---
|
|
20
|
+
|
|
21
|
+
## 📦 Installation
|
|
22
|
+
|
|
23
|
+
Clone the repo and install dependencies:
|
|
24
|
+
|
|
25
|
+
```bash
|
|
26
|
+
git clone https://github.com/Shakir-Afridi/react-form-dto.git
|
|
27
|
+
cd react-form-dto
|
|
28
|
+
npm install
|
|
29
|
+
```
|
|
30
|
+
|
|
31
|
+
> **Requirements:**
|
|
32
|
+
> - Node.js >= 18
|
|
33
|
+
> - React >= 19
|
|
34
|
+
> - Material UI >= 7
|
|
35
|
+
|
|
36
|
+
---
|
|
37
|
+
|
|
38
|
+
## 🏗️ Usage
|
|
39
|
+
|
|
40
|
+
Import and use the form builder in your React app:
|
|
41
|
+
|
|
42
|
+
```tsx
|
|
43
|
+
import { FormBuilder, type FormBuilderHandle } from 'react-form-dto';
|
|
44
|
+
import { useRef } from 'react';
|
|
45
|
+
import { myFormDTO } from './myFormDTO';
|
|
46
|
+
|
|
47
|
+
const formRef = useRef<FormBuilderHandle>(null);
|
|
48
|
+
|
|
49
|
+
const handleSubmit = (e) => {
|
|
50
|
+
e.preventDefault();
|
|
51
|
+
if (!formRef.current) return;
|
|
52
|
+
const values = formRef.current.getValues();
|
|
53
|
+
const errors = formRef.current.validateAll();
|
|
54
|
+
formRef.current.handleChange?.("firstName", "NewName");
|
|
55
|
+
const firstNameErrors = formRef.current.validateField("firstName");
|
|
56
|
+
const allErrors = formRef.current.getErrors();
|
|
57
|
+
};
|
|
58
|
+
|
|
59
|
+
return (
|
|
60
|
+
<form onSubmit={handleSubmit}>
|
|
61
|
+
<FormBuilder ref={formRef} dto={myFormDTO} />
|
|
62
|
+
<button type="submit">Submit</button>
|
|
63
|
+
</form>
|
|
64
|
+
);
|
|
65
|
+
```
|
|
66
|
+
|
|
67
|
+
Refer to the documentation and examples for DTO structure and customization.
|
|
68
|
+
|
|
69
|
+
---
|
|
70
|
+
|
|
71
|
+
### 📋 Example
|
|
72
|
+
|
|
73
|
+

|
|
74
|
+
|
|
75
|
+
The form in the image above is generated from this DTO.
|
|
76
|
+
|
|
77
|
+
```tsx
|
|
78
|
+
const profileForm: FormDTO = {
|
|
79
|
+
title: "User Profile",
|
|
80
|
+
description: "Fill out your personal information",
|
|
81
|
+
layout: { columns: 12, gap: "1rem" }, // global form layout
|
|
82
|
+
sections: [
|
|
83
|
+
{
|
|
84
|
+
id: "personal",
|
|
85
|
+
heading: "Personal Information",
|
|
86
|
+
description: "Basic details about you",
|
|
87
|
+
layout: { columns: 12, gap: "1rem" }, // section layout
|
|
88
|
+
fields: [
|
|
89
|
+
{
|
|
90
|
+
id: "title",
|
|
91
|
+
type: "select",
|
|
92
|
+
label: "Title",
|
|
93
|
+
placeholder: "Select your title",
|
|
94
|
+
options: ["Mr", "Ms", "Dr", "Prof"],
|
|
95
|
+
layout: { col: 4 },
|
|
96
|
+
},
|
|
97
|
+
{
|
|
98
|
+
id: "firstName",
|
|
99
|
+
type: "text",
|
|
100
|
+
label: "First Name",
|
|
101
|
+
layout: { col: 4 },
|
|
102
|
+
},
|
|
103
|
+
{
|
|
104
|
+
id: "lastName",
|
|
105
|
+
type: "text",
|
|
106
|
+
label: "Last Name",
|
|
107
|
+
layout: { col: 4 },
|
|
108
|
+
},
|
|
109
|
+
{
|
|
110
|
+
id: "age",
|
|
111
|
+
type: "number",
|
|
112
|
+
label: "Age",
|
|
113
|
+
layout: { col: 6 },
|
|
114
|
+
},
|
|
115
|
+
{
|
|
116
|
+
id: "dob",
|
|
117
|
+
type: "date",
|
|
118
|
+
label: "Date of Birth",
|
|
119
|
+
layout: { col: 6 },
|
|
120
|
+
},
|
|
121
|
+
{
|
|
122
|
+
id: "skills",
|
|
123
|
+
type: "multi-autocomplete",
|
|
124
|
+
label: "Skills",
|
|
125
|
+
placeholder: "Select your skills",
|
|
126
|
+
options: [
|
|
127
|
+
"React",
|
|
128
|
+
"TypeScript",
|
|
129
|
+
"Node.js",
|
|
130
|
+
"GraphQL",
|
|
131
|
+
"Docker",
|
|
132
|
+
],
|
|
133
|
+
layout: { col: 12 },
|
|
134
|
+
validations: {
|
|
135
|
+
required: "Select at least one skill",
|
|
136
|
+
validate: (val: string[]) =>
|
|
137
|
+
val && val.length < 2
|
|
138
|
+
? "Pick at least 2 skills"
|
|
139
|
+
: null,
|
|
140
|
+
},
|
|
141
|
+
},
|
|
142
|
+
],
|
|
143
|
+
},
|
|
144
|
+
{
|
|
145
|
+
id: "contact",
|
|
146
|
+
heading: "Contact Information",
|
|
147
|
+
layout: { columns: 12 },
|
|
148
|
+
fields: [
|
|
149
|
+
{
|
|
150
|
+
id: "email",
|
|
151
|
+
type: "email",
|
|
152
|
+
label: "Email",
|
|
153
|
+
validations: {
|
|
154
|
+
required: "Email is required",
|
|
155
|
+
pattern: /^[^\s@]+@[^\s@]+\.[^\s@]+$/,
|
|
156
|
+
},
|
|
157
|
+
},
|
|
158
|
+
{ id: "phone", type: "text", label: "Phone Number" },
|
|
159
|
+
{
|
|
160
|
+
id: "country",
|
|
161
|
+
type: "autocomplete",
|
|
162
|
+
label: "Country",
|
|
163
|
+
placeholder: "Select a country",
|
|
164
|
+
options: ["Pakistan", "India", "USA", "UK", "Germany"],
|
|
165
|
+
layout: { col: 6 },
|
|
166
|
+
},
|
|
167
|
+
],
|
|
168
|
+
},
|
|
169
|
+
],
|
|
170
|
+
};
|
|
171
|
+
```
|
|
172
|
+
|
|
173
|
+
---
|
|
174
|
+
|
|
175
|
+
### Supported Field Types & Renderers
|
|
176
|
+
|
|
177
|
+
- **TextInput**: text, number, date
|
|
178
|
+
- **SelectInput**: select/dropdown
|
|
179
|
+
- **CheckBoxInput**: boolean/checkbox
|
|
180
|
+
- **AutoCompleteField**: single autocomplete
|
|
181
|
+
- **MultiAutoCompleteField**: multi-select autocomplete
|
|
182
|
+
|
|
183
|
+
You can provide a custom renderer for any field via the DTO:
|
|
184
|
+
|
|
185
|
+
```tsx
|
|
186
|
+
{
|
|
187
|
+
// ...existing field DTO...
|
|
188
|
+
renderer: MyCustomComponent
|
|
189
|
+
}
|
|
190
|
+
```
|
|
191
|
+
|
|
192
|
+
### Validation
|
|
193
|
+
|
|
194
|
+
The `useFormBuilder` hook provides the following API for managing form state and validation:
|
|
195
|
+
|
|
196
|
+
```tsx
|
|
197
|
+
const {
|
|
198
|
+
handleChange, // Function to update a field value: (id, value) => void
|
|
199
|
+
validateAll, // Function to validate all fields: () => Record<string, string[]>
|
|
200
|
+
getValues, // Function to get all current form values: () => Record<string, any>
|
|
201
|
+
getErrors, // Function to get all current form errors: () => Record<string, string | null>
|
|
202
|
+
validateField, // Function to validate a specific field: (id) => string[]
|
|
203
|
+
} = useFormBuilder(myFormDTO);
|
|
204
|
+
```
|
|
205
|
+
|
|
206
|
+
- `handleChange(id, value)`: Update a field value.
|
|
207
|
+
- `validateAll()`: Validate all fields and return errors.
|
|
208
|
+
- `validateField(id)`: Validate a specific field and return errors for that field.
|
|
209
|
+
- `getValues()`: Get all form values.
|
|
210
|
+
- `getErrors()`: Get all form errors.
|
|
211
|
+
|
|
212
|
+
Refer to the documentation and examples for DTO structure and customization.
|
|
213
|
+
|
|
214
|
+
---
|
|
215
|
+
|
|
216
|
+
## 🤝 Contributing
|
|
217
|
+
|
|
218
|
+
- Fork the repo
|
|
219
|
+
- Create a feature branch (`git checkout -b feature/my-feature`)
|
|
220
|
+
- Commit changes (`git commit -m "Add my feature"`)
|
|
221
|
+
- Push to branch (`git push origin feature/my-feature`)
|
|
222
|
+
- Open a Pull Request
|
|
223
|
+
|
|
224
|
+
---
|
|
225
|
+
|
|
226
|
+
## 📜 License
|
|
227
|
+
|
|
228
|
+
MIT License © 2025 Shakir Ullah
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {}
|