autoforma 1.0.36 → 1.0.38
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 +157 -157
- package/dist/components/AutoForm.d.ts +3 -3
- package/dist/index.cjs.js +15 -55
- package/dist/index.d.ts +1 -0
- package/dist/index.es.js +3590 -12395
- package/dist/types/field.d.ts +2 -1
- package/package.json +11 -9
package/README.md
CHANGED
|
@@ -1,157 +1,157 @@
|
|
|
1
|
-
|
|
2
|
-
# AutoForm
|
|
3
|
-
|
|
4
|
-
AutoForm is a dynamic form builder component for React applications, using **Mantine**. It generates forms dynamically based on a schema definition. This allows for faster form creation and less repetitive code when working with forms in your React applications.
|
|
5
|
-
|
|
6
|
-
## Features
|
|
7
|
-
|
|
8
|
-
- **Dynamic Form Rendering**: AutoForm renders fields based on the provided schema, making form generation easy.
|
|
9
|
-
- **Field Types Support**: Supports various input types like `text`, `number`, `password`, `select`, `checkbox`, etc.
|
|
10
|
-
- **Validation Support**: Integrates with form validation mechanisms.
|
|
11
|
-
- **Custom Layout**: Supports custom layout rendering via the `container` and `fieldContainer` props.
|
|
12
|
-
- **Read-Only Mode**: Option to set the form in read-only mode.
|
|
13
|
-
- **Field Customization**: Easily customize field rendering for different input types.
|
|
14
|
-
- **Conditional Rendering**: Hide fields or conditionally render based on certain conditions.
|
|
15
|
-
|
|
16
|
-
## Installation
|
|
17
|
-
|
|
18
|
-
To install `AutoForm`, you can use npm or yarn:
|
|
19
|
-
|
|
20
|
-
```bash
|
|
21
|
-
npm install @mantine/core react
|
|
22
|
-
```
|
|
23
|
-
|
|
24
|
-
## Usage
|
|
25
|
-
|
|
26
|
-
### Example Usage
|
|
27
|
-
|
|
28
|
-
```tsx
|
|
29
|
-
import React from 'react';
|
|
30
|
-
import AutoForm from './AutoForm'; // Path to your AutoForm component
|
|
31
|
-
import { Button, Grid, Group } from '@mantine/core';
|
|
32
|
-
|
|
33
|
-
const schema = [
|
|
34
|
-
{ name: 'firstName', label: 'First Name', type: 'text' },
|
|
35
|
-
{ name: 'age', label: 'Age', type: 'number' },
|
|
36
|
-
{ name: 'subscribe', label: 'Subscribe?', type: 'checkbox' },
|
|
37
|
-
{ name: 'role', label: 'Role', type: 'select', options: ['User', 'Admin'] },
|
|
38
|
-
];
|
|
39
|
-
|
|
40
|
-
const MyForm = () => {
|
|
41
|
-
const onSubmit = (values) => {
|
|
42
|
-
alert(JSON.stringify(values, null, 2));
|
|
43
|
-
};
|
|
44
|
-
|
|
45
|
-
return (
|
|
46
|
-
<AutoForm
|
|
47
|
-
schema={schema}
|
|
48
|
-
onSubmit={onSubmit}
|
|
49
|
-
container={(form, onSubmit, readOnly) => (
|
|
50
|
-
<>
|
|
51
|
-
<Grid>{form}</Grid>
|
|
52
|
-
{!readOnly && (
|
|
53
|
-
<Group justify="flex-end">
|
|
54
|
-
<Button onClick={onSubmit}>Submit</Button>
|
|
55
|
-
</Group>
|
|
56
|
-
)}
|
|
57
|
-
</>
|
|
58
|
-
)}
|
|
59
|
-
/>
|
|
60
|
-
);
|
|
61
|
-
};
|
|
62
|
-
```
|
|
63
|
-
|
|
64
|
-
### Props
|
|
65
|
-
|
|
66
|
-
| Prop | Type | Description |
|
|
67
|
-
|------------------|--------------------------------------------------------------------------------|-----------------------------------------------------------------------------|
|
|
68
|
-
| `schema` | `FieldSchema[]` | List of field definitions to render. Each field specifies `name`, `type`, etc. |
|
|
69
|
-
| `values` | `Record<string, any>` | Initial values for the form fields. Optional. |
|
|
70
|
-
| `onSubmit` | `(values: Record<string, any>) => void` | Callback triggered on form submission with current form values. |
|
|
71
|
-
| `container` | `(form: ReactNode, onSubmit: () => void, readOnly?: true) => ReactNode` | Custom render wrapper around the form and submit button. |
|
|
72
|
-
| `fieldContainer` | `(field: ReactNode, fieldSchema: FieldSchema) => ReactNode` | Optional. Custom layout for each field (e.g., grid wrappers). |
|
|
73
|
-
| `customRender` | `FieldRenderCustomRender` | Optional. Override how specific field types are rendered. |
|
|
74
|
-
| `validate` | `FormValidateInput<Record<string, any>>` | Optional validation logic. Uses Mantine-compatible validation. |
|
|
75
|
-
| `readOnly` | `true` | Optional. When true, disables all inputs (read-only mode). |
|
|
76
|
-
| `onFieldChange` | `(name: string, value: any, values: Record<string, any>) => Record<string, any>` | Optional. Modify or transform values when a field changes. |
|
|
77
|
-
|
|
78
|
-
### Custom Layout
|
|
79
|
-
|
|
80
|
-
You can customize the layout of the entire form or specific fields by using the `container` and `fieldContainer` props.
|
|
81
|
-
|
|
82
|
-
#### Example: Customizing the Form Layout
|
|
83
|
-
|
|
84
|
-
```tsx
|
|
85
|
-
const customLayout = (form, onSubmit, readOnly) => (
|
|
86
|
-
<>
|
|
87
|
-
<div className="form-container">{form}</div>
|
|
88
|
-
{!readOnly && <button onClick={onSubmit}>Submit</button>}
|
|
89
|
-
</>
|
|
90
|
-
);
|
|
91
|
-
```
|
|
92
|
-
|
|
93
|
-
#### Example: Customizing Field Layout
|
|
94
|
-
|
|
95
|
-
```tsx
|
|
96
|
-
const customFieldLayout = (field, fieldSchema) => (
|
|
97
|
-
<div className="custom-field">{field}</div>
|
|
98
|
-
);
|
|
99
|
-
```
|
|
100
|
-
|
|
101
|
-
### Validation
|
|
102
|
-
|
|
103
|
-
AutoForm supports validation through the `validate` prop. You can pass a validation function that returns an object of validation errors.
|
|
104
|
-
|
|
105
|
-
Example:
|
|
106
|
-
|
|
107
|
-
```tsx
|
|
108
|
-
const validateForm = (values) => {
|
|
109
|
-
const errors = {};
|
|
110
|
-
if (!values.firstName) errors.firstName = 'First name is required';
|
|
111
|
-
if (!values.age) errors.age = 'Age is required';
|
|
112
|
-
return errors;
|
|
113
|
-
};
|
|
114
|
-
|
|
115
|
-
<AutoForm
|
|
116
|
-
schema={schema}
|
|
117
|
-
validate={validateForm}
|
|
118
|
-
onSubmit={(values) => console.log(values)}
|
|
119
|
-
container={(form, onSubmit) => (
|
|
120
|
-
<>
|
|
121
|
-
<Grid>{form}</Grid>
|
|
122
|
-
<Button onClick={onSubmit}>Submit</Button>
|
|
123
|
-
</>
|
|
124
|
-
)}
|
|
125
|
-
/>
|
|
126
|
-
```
|
|
127
|
-
|
|
128
|
-
### Hide Fields
|
|
129
|
-
|
|
130
|
-
To hide a field from rendering, you can set the `hidden` property in the schema.
|
|
131
|
-
|
|
132
|
-
Example:
|
|
133
|
-
|
|
134
|
-
```tsx
|
|
135
|
-
const schema = [
|
|
136
|
-
{ name: 'firstName', label: 'First Name', type: 'text' },
|
|
137
|
-
{ name: 'internalCode', label: 'Internal Code', type: 'text', hidden: true },
|
|
138
|
-
];
|
|
139
|
-
```
|
|
140
|
-
|
|
141
|
-
This will ensure the "Internal Code" field is not rendered.
|
|
142
|
-
|
|
143
|
-
---
|
|
144
|
-
|
|
145
|
-
## Contributing
|
|
146
|
-
|
|
147
|
-
1. Fork the repository
|
|
148
|
-
2. Create a new branch (`git checkout -b feature-branch`)
|
|
149
|
-
3. Commit your changes (`git commit -am 'Add feature'`)
|
|
150
|
-
4. Push to the branch (`git push origin feature-branch`)
|
|
151
|
-
5. Create a new Pull Request
|
|
152
|
-
|
|
153
|
-
---
|
|
154
|
-
|
|
155
|
-
## License
|
|
156
|
-
|
|
157
|
-
Distributed under the MIT License. See `LICENSE` for more information.
|
|
1
|
+
|
|
2
|
+
# AutoForm
|
|
3
|
+
|
|
4
|
+
AutoForm is a dynamic form builder component for React applications, using **Mantine**. It generates forms dynamically based on a schema definition. This allows for faster form creation and less repetitive code when working with forms in your React applications.
|
|
5
|
+
|
|
6
|
+
## Features
|
|
7
|
+
|
|
8
|
+
- **Dynamic Form Rendering**: AutoForm renders fields based on the provided schema, making form generation easy.
|
|
9
|
+
- **Field Types Support**: Supports various input types like `text`, `number`, `password`, `select`, `checkbox`, etc.
|
|
10
|
+
- **Validation Support**: Integrates with form validation mechanisms.
|
|
11
|
+
- **Custom Layout**: Supports custom layout rendering via the `container` and `fieldContainer` props.
|
|
12
|
+
- **Read-Only Mode**: Option to set the form in read-only mode.
|
|
13
|
+
- **Field Customization**: Easily customize field rendering for different input types.
|
|
14
|
+
- **Conditional Rendering**: Hide fields or conditionally render based on certain conditions.
|
|
15
|
+
|
|
16
|
+
## Installation
|
|
17
|
+
|
|
18
|
+
To install `AutoForm`, you can use npm or yarn:
|
|
19
|
+
|
|
20
|
+
```bash
|
|
21
|
+
npm install @mantine/core react
|
|
22
|
+
```
|
|
23
|
+
|
|
24
|
+
## Usage
|
|
25
|
+
|
|
26
|
+
### Example Usage
|
|
27
|
+
|
|
28
|
+
```tsx
|
|
29
|
+
import React from 'react';
|
|
30
|
+
import AutoForm from './AutoForm'; // Path to your AutoForm component
|
|
31
|
+
import { Button, Grid, Group } from '@mantine/core';
|
|
32
|
+
|
|
33
|
+
const schema = [
|
|
34
|
+
{ name: 'firstName', label: 'First Name', type: 'text' },
|
|
35
|
+
{ name: 'age', label: 'Age', type: 'number' },
|
|
36
|
+
{ name: 'subscribe', label: 'Subscribe?', type: 'checkbox' },
|
|
37
|
+
{ name: 'role', label: 'Role', type: 'select', options: ['User', 'Admin'] },
|
|
38
|
+
];
|
|
39
|
+
|
|
40
|
+
const MyForm = () => {
|
|
41
|
+
const onSubmit = (values) => {
|
|
42
|
+
alert(JSON.stringify(values, null, 2));
|
|
43
|
+
};
|
|
44
|
+
|
|
45
|
+
return (
|
|
46
|
+
<AutoForm
|
|
47
|
+
schema={schema}
|
|
48
|
+
onSubmit={onSubmit}
|
|
49
|
+
container={(form, onSubmit, readOnly) => (
|
|
50
|
+
<>
|
|
51
|
+
<Grid>{form}</Grid>
|
|
52
|
+
{!readOnly && (
|
|
53
|
+
<Group justify="flex-end">
|
|
54
|
+
<Button onClick={onSubmit}>Submit</Button>
|
|
55
|
+
</Group>
|
|
56
|
+
)}
|
|
57
|
+
</>
|
|
58
|
+
)}
|
|
59
|
+
/>
|
|
60
|
+
);
|
|
61
|
+
};
|
|
62
|
+
```
|
|
63
|
+
|
|
64
|
+
### Props
|
|
65
|
+
|
|
66
|
+
| Prop | Type | Description |
|
|
67
|
+
|------------------|--------------------------------------------------------------------------------|-----------------------------------------------------------------------------|
|
|
68
|
+
| `schema` | `FieldSchema[]` | List of field definitions to render. Each field specifies `name`, `type`, etc. |
|
|
69
|
+
| `values` | `Record<string, any>` | Initial values for the form fields. Optional. |
|
|
70
|
+
| `onSubmit` | `(values: Record<string, any>) => void` | Callback triggered on form submission with current form values. |
|
|
71
|
+
| `container` | `(form: ReactNode, onSubmit: () => void, readOnly?: true) => ReactNode` | Custom render wrapper around the form and submit button. |
|
|
72
|
+
| `fieldContainer` | `(field: ReactNode, fieldSchema: FieldSchema) => ReactNode` | Optional. Custom layout for each field (e.g., grid wrappers). |
|
|
73
|
+
| `customRender` | `FieldRenderCustomRender` | Optional. Override how specific field types are rendered. |
|
|
74
|
+
| `validate` | `FormValidateInput<Record<string, any>>` | Optional validation logic. Uses Mantine-compatible validation. |
|
|
75
|
+
| `readOnly` | `true` | Optional. When true, disables all inputs (read-only mode). |
|
|
76
|
+
| `onFieldChange` | `(name: string, value: any, values: Record<string, any>) => Record<string, any>` | Optional. Modify or transform values when a field changes. |
|
|
77
|
+
|
|
78
|
+
### Custom Layout
|
|
79
|
+
|
|
80
|
+
You can customize the layout of the entire form or specific fields by using the `container` and `fieldContainer` props.
|
|
81
|
+
|
|
82
|
+
#### Example: Customizing the Form Layout
|
|
83
|
+
|
|
84
|
+
```tsx
|
|
85
|
+
const customLayout = (form, onSubmit, readOnly) => (
|
|
86
|
+
<>
|
|
87
|
+
<div className="form-container">{form}</div>
|
|
88
|
+
{!readOnly && <button onClick={onSubmit}>Submit</button>}
|
|
89
|
+
</>
|
|
90
|
+
);
|
|
91
|
+
```
|
|
92
|
+
|
|
93
|
+
#### Example: Customizing Field Layout
|
|
94
|
+
|
|
95
|
+
```tsx
|
|
96
|
+
const customFieldLayout = (field, fieldSchema) => (
|
|
97
|
+
<div className="custom-field">{field}</div>
|
|
98
|
+
);
|
|
99
|
+
```
|
|
100
|
+
|
|
101
|
+
### Validation
|
|
102
|
+
|
|
103
|
+
AutoForm supports validation through the `validate` prop. You can pass a validation function that returns an object of validation errors.
|
|
104
|
+
|
|
105
|
+
Example:
|
|
106
|
+
|
|
107
|
+
```tsx
|
|
108
|
+
const validateForm = (values) => {
|
|
109
|
+
const errors = {};
|
|
110
|
+
if (!values.firstName) errors.firstName = 'First name is required';
|
|
111
|
+
if (!values.age) errors.age = 'Age is required';
|
|
112
|
+
return errors;
|
|
113
|
+
};
|
|
114
|
+
|
|
115
|
+
<AutoForm
|
|
116
|
+
schema={schema}
|
|
117
|
+
validate={validateForm}
|
|
118
|
+
onSubmit={(values) => console.log(values)}
|
|
119
|
+
container={(form, onSubmit) => (
|
|
120
|
+
<>
|
|
121
|
+
<Grid>{form}</Grid>
|
|
122
|
+
<Button onClick={onSubmit}>Submit</Button>
|
|
123
|
+
</>
|
|
124
|
+
)}
|
|
125
|
+
/>
|
|
126
|
+
```
|
|
127
|
+
|
|
128
|
+
### Hide Fields
|
|
129
|
+
|
|
130
|
+
To hide a field from rendering, you can set the `hidden` property in the schema.
|
|
131
|
+
|
|
132
|
+
Example:
|
|
133
|
+
|
|
134
|
+
```tsx
|
|
135
|
+
const schema = [
|
|
136
|
+
{ name: 'firstName', label: 'First Name', type: 'text' },
|
|
137
|
+
{ name: 'internalCode', label: 'Internal Code', type: 'text', hidden: true },
|
|
138
|
+
];
|
|
139
|
+
```
|
|
140
|
+
|
|
141
|
+
This will ensure the "Internal Code" field is not rendered.
|
|
142
|
+
|
|
143
|
+
---
|
|
144
|
+
|
|
145
|
+
## Contributing
|
|
146
|
+
|
|
147
|
+
1. Fork the repository
|
|
148
|
+
2. Create a new branch (`git checkout -b feature-branch`)
|
|
149
|
+
3. Commit your changes (`git commit -am 'Add feature'`)
|
|
150
|
+
4. Push to the branch (`git push origin feature-branch`)
|
|
151
|
+
5. Create a new Pull Request
|
|
152
|
+
|
|
153
|
+
---
|
|
154
|
+
|
|
155
|
+
## License
|
|
156
|
+
|
|
157
|
+
Distributed under the MIT License. See `LICENSE` for more information.
|
|
@@ -1,7 +1,7 @@
|
|
|
1
|
-
import { default as React } from 'react';
|
|
2
|
-
import { FormValidateInput } from '@mantine/form';
|
|
3
1
|
import { FieldRenderCustomRender } from '../types/custom-render';
|
|
4
2
|
import { FieldSchema } from '../types/field';
|
|
3
|
+
import { FormValidateInput } from '@mantine/form';
|
|
4
|
+
import { default as React } from 'react';
|
|
5
5
|
export interface AutoFormProps {
|
|
6
6
|
values?: Record<string, any>;
|
|
7
7
|
schema: FieldSchema[];
|
|
@@ -11,7 +11,7 @@ export interface AutoFormProps {
|
|
|
11
11
|
customRender?: FieldRenderCustomRender;
|
|
12
12
|
validate?: FormValidateInput<Record<string, any>>;
|
|
13
13
|
readOnly?: true;
|
|
14
|
-
onFieldChange?: (name: string,
|
|
14
|
+
onFieldChange?: (name: string, oldValue: any, newValue: any, values: Record<string, any>) => Record<string, any> | Promise<Record<string, any>>;
|
|
15
15
|
}
|
|
16
16
|
export declare const validateRequiredFields: (schema: FieldSchema[], values: Record<string, any>, parentPath?: string) => Record<string, string>;
|
|
17
17
|
declare const AutoForm: React.FC<AutoFormProps>;
|