@arturton/react-form-constructor 0.1.2
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 +160 -0
- package/dist/index.d.mts +76 -0
- package/dist/index.d.ts +76 -0
- package/dist/index.js +228 -0
- package/dist/index.mjs +199 -0
- package/package.json +52 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Artur Tonoyan
|
|
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,160 @@
|
|
|
1
|
+
# React Form Constructor
|
|
2
|
+
|
|
3
|
+
> Минималистичный конструктор форм для React на базе **react-hook-form**, с удобной декларацией полей, валидацией и масками ввода.
|
|
4
|
+
|
|
5
|
+
## Зачем это нужно
|
|
6
|
+
|
|
7
|
+
Когда форма состоит из набора однотипных полей, удобнее описывать её **данными**, а не JSX-шаблонами. Библиотека помогает:
|
|
8
|
+
|
|
9
|
+
- быстро собрать форму из массива полей;
|
|
10
|
+
- подключить валидацию `react-hook-form` без лишнего кода;
|
|
11
|
+
- использовать маски ввода через `react-number-format`;
|
|
12
|
+
- гибко стилизовать форму через классы;
|
|
13
|
+
- при необходимости перейти на «ручной» рендер через `children`.
|
|
14
|
+
|
|
15
|
+
## Установка
|
|
16
|
+
|
|
17
|
+
```bash
|
|
18
|
+
npm i react-form-constructor
|
|
19
|
+
```
|
|
20
|
+
|
|
21
|
+
## Быстрый старт
|
|
22
|
+
|
|
23
|
+
```tsx
|
|
24
|
+
import { FormLayout, type FormField } from "react-form-constructor";
|
|
25
|
+
|
|
26
|
+
type LoginForm = {
|
|
27
|
+
phone: string;
|
|
28
|
+
password: string;
|
|
29
|
+
};
|
|
30
|
+
|
|
31
|
+
const fields: FormField<LoginForm>[] = [
|
|
32
|
+
{
|
|
33
|
+
label: "Телефон",
|
|
34
|
+
placeholder: "+7 (___) ___-__-__",
|
|
35
|
+
key: "phone",
|
|
36
|
+
required: "Введите телефон",
|
|
37
|
+
maska: {
|
|
38
|
+
required: "Введите телефон",
|
|
39
|
+
format: "+7 (###) ###-##-##",
|
|
40
|
+
mask: "_",
|
|
41
|
+
},
|
|
42
|
+
},
|
|
43
|
+
{
|
|
44
|
+
label: "Пароль",
|
|
45
|
+
placeholder: "••••••••",
|
|
46
|
+
key: "password",
|
|
47
|
+
required: "Введите пароль",
|
|
48
|
+
minLength: { value: 6, message: "Минимум 6 символов" },
|
|
49
|
+
type: "password",
|
|
50
|
+
},
|
|
51
|
+
];
|
|
52
|
+
|
|
53
|
+
export function Login() {
|
|
54
|
+
return (
|
|
55
|
+
<FormLayout<LoginForm>
|
|
56
|
+
formData={fields}
|
|
57
|
+
funSubmit={(data) => console.log(data)}
|
|
58
|
+
formClass="form"
|
|
59
|
+
buttonClass="btn"
|
|
60
|
+
/>
|
|
61
|
+
);
|
|
62
|
+
}
|
|
63
|
+
```
|
|
64
|
+
|
|
65
|
+
## Как это работает
|
|
66
|
+
|
|
67
|
+
- **`FormLayout`** создаёт форму и сам регистрирует поля через `react-hook-form`.
|
|
68
|
+
- Каждое поле описывается типом `FormField<T>` и привязывается к ключу `keyof T`.
|
|
69
|
+
- Если передан `formData`, библиотека сама отрисует поля и кнопку отправки.
|
|
70
|
+
- Если `formData` не передан — можно рендерить **свои поля** через `children` и использовать `useFormContext()`.
|
|
71
|
+
|
|
72
|
+
## Пример с кастомной разметкой (children)
|
|
73
|
+
|
|
74
|
+
```tsx
|
|
75
|
+
import { FormLayout, useFormContext } from "react-form-constructor";
|
|
76
|
+
|
|
77
|
+
function CustomField() {
|
|
78
|
+
const { register, errors } = useFormContext<{ email: string }>();
|
|
79
|
+
|
|
80
|
+
return (
|
|
81
|
+
<div>
|
|
82
|
+
<input {...register("email", { required: "Введите email" })} />
|
|
83
|
+
{errors.email && <span>{errors.email.message}</span>}
|
|
84
|
+
</div>
|
|
85
|
+
);
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
export function CustomForm() {
|
|
89
|
+
return (
|
|
90
|
+
<FormLayout funSubmit={(data) => console.log(data)}>
|
|
91
|
+
<CustomField />
|
|
92
|
+
</FormLayout>
|
|
93
|
+
);
|
|
94
|
+
}
|
|
95
|
+
```
|
|
96
|
+
|
|
97
|
+
## Валидация
|
|
98
|
+
|
|
99
|
+
Поддерживаются стандартные правила `react-hook-form`:
|
|
100
|
+
|
|
101
|
+
- `required`
|
|
102
|
+
- `minLength`
|
|
103
|
+
- `maxLength`
|
|
104
|
+
- `pattern`
|
|
105
|
+
- `validate`
|
|
106
|
+
|
|
107
|
+
Пример:
|
|
108
|
+
|
|
109
|
+
```ts
|
|
110
|
+
{
|
|
111
|
+
key: "username",
|
|
112
|
+
label: "Имя",
|
|
113
|
+
required: "Обязательное поле",
|
|
114
|
+
minLength: { value: 3, message: "Минимум 3 символа" },
|
|
115
|
+
pattern: { value: /^[a-z]+$/i, message: "Только буквы" },
|
|
116
|
+
}
|
|
117
|
+
```
|
|
118
|
+
|
|
119
|
+
## Маски ввода
|
|
120
|
+
|
|
121
|
+
Поле с маской задаётся через `maska`:
|
|
122
|
+
|
|
123
|
+
```ts
|
|
124
|
+
{
|
|
125
|
+
key: "phone",
|
|
126
|
+
label: "Телефон",
|
|
127
|
+
maska: {
|
|
128
|
+
required: "Введите телефон",
|
|
129
|
+
format: "+7 (###) ###-##-##",
|
|
130
|
+
mask: "_",
|
|
131
|
+
},
|
|
132
|
+
}
|
|
133
|
+
```
|
|
134
|
+
|
|
135
|
+
## Стилизация
|
|
136
|
+
|
|
137
|
+
Доступны классы для быстрого оформления:
|
|
138
|
+
|
|
139
|
+
- `formClass` — класс формы
|
|
140
|
+
- `buttonClass` — класс кнопки
|
|
141
|
+
- `inputClass` — класс поля
|
|
142
|
+
- `labelClass` — класс лейбла
|
|
143
|
+
- `errorClass` — класс текста ошибки
|
|
144
|
+
|
|
145
|
+
## Публичный API
|
|
146
|
+
|
|
147
|
+
- `FormLayout` — рендерит форму по массиву `formData` или принимает `children`.
|
|
148
|
+
- `InputForm` — базовый элемент поля (экспортируется по умолчанию).
|
|
149
|
+
- `useFormContext` — доступ к `react-hook-form` контексту внутри ваших компонентов.
|
|
150
|
+
- Типы: `FormField`, `FormLayoutProps`, `FormContextValue`.
|
|
151
|
+
|
|
152
|
+
## Требования
|
|
153
|
+
|
|
154
|
+
- React `^18` или `^19`
|
|
155
|
+
- react-hook-form `^7`
|
|
156
|
+
- react-number-format `^5`
|
|
157
|
+
|
|
158
|
+
## Лицензия
|
|
159
|
+
|
|
160
|
+
MIT — см. файл LICENSE.
|
package/dist/index.d.mts
ADDED
|
@@ -0,0 +1,76 @@
|
|
|
1
|
+
import * as react_jsx_runtime from 'react/jsx-runtime';
|
|
2
|
+
import { Control, UseFormRegister, FieldErrors } from 'react-hook-form';
|
|
3
|
+
|
|
4
|
+
type FormField<T extends object = any> = {
|
|
5
|
+
label: string;
|
|
6
|
+
placeholder: string;
|
|
7
|
+
key: keyof T;
|
|
8
|
+
required?: string;
|
|
9
|
+
minLength?: {
|
|
10
|
+
value: number;
|
|
11
|
+
message: string;
|
|
12
|
+
};
|
|
13
|
+
maxLength?: {
|
|
14
|
+
value: number;
|
|
15
|
+
message: string;
|
|
16
|
+
};
|
|
17
|
+
pattern?: {
|
|
18
|
+
value: RegExp;
|
|
19
|
+
message: string;
|
|
20
|
+
};
|
|
21
|
+
validate?: any;
|
|
22
|
+
type?: string;
|
|
23
|
+
maska?: {
|
|
24
|
+
required: string;
|
|
25
|
+
format: string;
|
|
26
|
+
mask: string;
|
|
27
|
+
};
|
|
28
|
+
textarea?: boolean;
|
|
29
|
+
register?: {
|
|
30
|
+
[key: string]: any;
|
|
31
|
+
};
|
|
32
|
+
inputClass?: any;
|
|
33
|
+
errorClass?: any;
|
|
34
|
+
labelClass?: any;
|
|
35
|
+
};
|
|
36
|
+
type FormLayoutProps<T extends object = any> = {
|
|
37
|
+
funSubmit: (data: T) => void;
|
|
38
|
+
formClass?: string;
|
|
39
|
+
buttonClass?: string;
|
|
40
|
+
} & ({
|
|
41
|
+
formData: FormField<T>[];
|
|
42
|
+
children?: never;
|
|
43
|
+
} | {
|
|
44
|
+
formData?: never;
|
|
45
|
+
children: React.ReactNode;
|
|
46
|
+
});
|
|
47
|
+
|
|
48
|
+
declare function FormLayout<T extends object = any>({ formData, funSubmit, formClass, buttonClass, children, }: FormLayoutProps<T>): react_jsx_runtime.JSX.Element;
|
|
49
|
+
|
|
50
|
+
interface FormContextValue<T extends object = any> {
|
|
51
|
+
control: Control<T>;
|
|
52
|
+
register: UseFormRegister<T>;
|
|
53
|
+
errors: FieldErrors<T>;
|
|
54
|
+
}
|
|
55
|
+
declare function useFormContext<T extends object = any>(): FormContextValue<T>;
|
|
56
|
+
|
|
57
|
+
declare function InputForm({ register, type, placeholder, error, name, label, required, control, maska, inputClass, labelClass, errorClass, }: {
|
|
58
|
+
register: any;
|
|
59
|
+
type?: string;
|
|
60
|
+
placeholder?: string;
|
|
61
|
+
error: any;
|
|
62
|
+
name: string;
|
|
63
|
+
label?: string;
|
|
64
|
+
required?: string | boolean;
|
|
65
|
+
control?: any;
|
|
66
|
+
maska?: {
|
|
67
|
+
required: string;
|
|
68
|
+
format: string;
|
|
69
|
+
mask: string;
|
|
70
|
+
};
|
|
71
|
+
inputClass?: any;
|
|
72
|
+
labelClass?: any;
|
|
73
|
+
errorClass?: any;
|
|
74
|
+
}): react_jsx_runtime.JSX.Element;
|
|
75
|
+
|
|
76
|
+
export { type FormContextValue, type FormField, FormLayout, type FormLayoutProps, InputForm, useFormContext };
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,76 @@
|
|
|
1
|
+
import * as react_jsx_runtime from 'react/jsx-runtime';
|
|
2
|
+
import { Control, UseFormRegister, FieldErrors } from 'react-hook-form';
|
|
3
|
+
|
|
4
|
+
type FormField<T extends object = any> = {
|
|
5
|
+
label: string;
|
|
6
|
+
placeholder: string;
|
|
7
|
+
key: keyof T;
|
|
8
|
+
required?: string;
|
|
9
|
+
minLength?: {
|
|
10
|
+
value: number;
|
|
11
|
+
message: string;
|
|
12
|
+
};
|
|
13
|
+
maxLength?: {
|
|
14
|
+
value: number;
|
|
15
|
+
message: string;
|
|
16
|
+
};
|
|
17
|
+
pattern?: {
|
|
18
|
+
value: RegExp;
|
|
19
|
+
message: string;
|
|
20
|
+
};
|
|
21
|
+
validate?: any;
|
|
22
|
+
type?: string;
|
|
23
|
+
maska?: {
|
|
24
|
+
required: string;
|
|
25
|
+
format: string;
|
|
26
|
+
mask: string;
|
|
27
|
+
};
|
|
28
|
+
textarea?: boolean;
|
|
29
|
+
register?: {
|
|
30
|
+
[key: string]: any;
|
|
31
|
+
};
|
|
32
|
+
inputClass?: any;
|
|
33
|
+
errorClass?: any;
|
|
34
|
+
labelClass?: any;
|
|
35
|
+
};
|
|
36
|
+
type FormLayoutProps<T extends object = any> = {
|
|
37
|
+
funSubmit: (data: T) => void;
|
|
38
|
+
formClass?: string;
|
|
39
|
+
buttonClass?: string;
|
|
40
|
+
} & ({
|
|
41
|
+
formData: FormField<T>[];
|
|
42
|
+
children?: never;
|
|
43
|
+
} | {
|
|
44
|
+
formData?: never;
|
|
45
|
+
children: React.ReactNode;
|
|
46
|
+
});
|
|
47
|
+
|
|
48
|
+
declare function FormLayout<T extends object = any>({ formData, funSubmit, formClass, buttonClass, children, }: FormLayoutProps<T>): react_jsx_runtime.JSX.Element;
|
|
49
|
+
|
|
50
|
+
interface FormContextValue<T extends object = any> {
|
|
51
|
+
control: Control<T>;
|
|
52
|
+
register: UseFormRegister<T>;
|
|
53
|
+
errors: FieldErrors<T>;
|
|
54
|
+
}
|
|
55
|
+
declare function useFormContext<T extends object = any>(): FormContextValue<T>;
|
|
56
|
+
|
|
57
|
+
declare function InputForm({ register, type, placeholder, error, name, label, required, control, maska, inputClass, labelClass, errorClass, }: {
|
|
58
|
+
register: any;
|
|
59
|
+
type?: string;
|
|
60
|
+
placeholder?: string;
|
|
61
|
+
error: any;
|
|
62
|
+
name: string;
|
|
63
|
+
label?: string;
|
|
64
|
+
required?: string | boolean;
|
|
65
|
+
control?: any;
|
|
66
|
+
maska?: {
|
|
67
|
+
required: string;
|
|
68
|
+
format: string;
|
|
69
|
+
mask: string;
|
|
70
|
+
};
|
|
71
|
+
inputClass?: any;
|
|
72
|
+
labelClass?: any;
|
|
73
|
+
errorClass?: any;
|
|
74
|
+
}): react_jsx_runtime.JSX.Element;
|
|
75
|
+
|
|
76
|
+
export { type FormContextValue, type FormField, FormLayout, type FormLayoutProps, InputForm, useFormContext };
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,228 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __defProp = Object.defineProperty;
|
|
3
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
4
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
5
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
6
|
+
var __export = (target, all) => {
|
|
7
|
+
for (var name in all)
|
|
8
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
9
|
+
};
|
|
10
|
+
var __copyProps = (to, from, except, desc) => {
|
|
11
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
12
|
+
for (let key of __getOwnPropNames(from))
|
|
13
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
14
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
15
|
+
}
|
|
16
|
+
return to;
|
|
17
|
+
};
|
|
18
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
19
|
+
|
|
20
|
+
// src/index.ts
|
|
21
|
+
var index_exports = {};
|
|
22
|
+
__export(index_exports, {
|
|
23
|
+
FormLayout: () => FormLayout,
|
|
24
|
+
InputForm: () => InputForm_default,
|
|
25
|
+
useFormContext: () => useFormContext
|
|
26
|
+
});
|
|
27
|
+
module.exports = __toCommonJS(index_exports);
|
|
28
|
+
|
|
29
|
+
// src/FormLayout.tsx
|
|
30
|
+
var import_react_hook_form2 = require("react-hook-form");
|
|
31
|
+
|
|
32
|
+
// src/InputForm/layouts/mainLayout.tsx
|
|
33
|
+
var import_jsx_runtime = require("react/jsx-runtime");
|
|
34
|
+
function MainLayout({
|
|
35
|
+
children,
|
|
36
|
+
label,
|
|
37
|
+
required,
|
|
38
|
+
error,
|
|
39
|
+
name,
|
|
40
|
+
labelClass,
|
|
41
|
+
errorClass
|
|
42
|
+
}) {
|
|
43
|
+
return /* @__PURE__ */ (0, import_jsx_runtime.jsxs)("div", { children: [
|
|
44
|
+
/* @__PURE__ */ (0, import_jsx_runtime.jsxs)("label", { className: labelClass, children: [
|
|
45
|
+
label,
|
|
46
|
+
" ",
|
|
47
|
+
required ? /* @__PURE__ */ (0, import_jsx_runtime.jsx)("span", { children: "*" }) : null
|
|
48
|
+
] }),
|
|
49
|
+
children,
|
|
50
|
+
error?.[name] && /* @__PURE__ */ (0, import_jsx_runtime.jsx)("span", { className: errorClass, children: error?.[name]?.message })
|
|
51
|
+
] });
|
|
52
|
+
}
|
|
53
|
+
var mainLayout_default = MainLayout;
|
|
54
|
+
|
|
55
|
+
// src/InputForm/components/MaskedField.tsx
|
|
56
|
+
var import_react_hook_form = require("react-hook-form");
|
|
57
|
+
var import_react_number_format = require("react-number-format");
|
|
58
|
+
var import_jsx_runtime2 = require("react/jsx-runtime");
|
|
59
|
+
function MaskedField({
|
|
60
|
+
placeholder,
|
|
61
|
+
name,
|
|
62
|
+
control,
|
|
63
|
+
maska,
|
|
64
|
+
inputClass
|
|
65
|
+
}) {
|
|
66
|
+
return /* @__PURE__ */ (0, import_jsx_runtime2.jsx)(
|
|
67
|
+
import_react_hook_form.Controller,
|
|
68
|
+
{
|
|
69
|
+
name: name || "",
|
|
70
|
+
control,
|
|
71
|
+
rules: { required: maska.required },
|
|
72
|
+
render: ({ field }) => /* @__PURE__ */ (0, import_jsx_runtime2.jsx)(
|
|
73
|
+
import_react_number_format.PatternFormat,
|
|
74
|
+
{
|
|
75
|
+
...field,
|
|
76
|
+
format: maska.format,
|
|
77
|
+
mask: maska.mask,
|
|
78
|
+
placeholder: placeholder ? placeholder : "",
|
|
79
|
+
className: inputClass
|
|
80
|
+
}
|
|
81
|
+
)
|
|
82
|
+
}
|
|
83
|
+
);
|
|
84
|
+
}
|
|
85
|
+
var MaskedField_default = MaskedField;
|
|
86
|
+
|
|
87
|
+
// src/InputForm/components/InputField.tsx
|
|
88
|
+
var import_jsx_runtime3 = require("react/jsx-runtime");
|
|
89
|
+
function InputField({
|
|
90
|
+
register,
|
|
91
|
+
type,
|
|
92
|
+
placeholder,
|
|
93
|
+
inputClass,
|
|
94
|
+
name
|
|
95
|
+
}) {
|
|
96
|
+
return /* @__PURE__ */ (0, import_jsx_runtime3.jsx)(
|
|
97
|
+
"input",
|
|
98
|
+
{
|
|
99
|
+
id: name,
|
|
100
|
+
...register,
|
|
101
|
+
type: type ? type : "text",
|
|
102
|
+
placeholder: placeholder ? placeholder : "",
|
|
103
|
+
className: inputClass
|
|
104
|
+
}
|
|
105
|
+
);
|
|
106
|
+
}
|
|
107
|
+
var InputField_default = InputField;
|
|
108
|
+
|
|
109
|
+
// src/InputForm/InputForm.tsx
|
|
110
|
+
var import_jsx_runtime4 = require("react/jsx-runtime");
|
|
111
|
+
function InputForm({
|
|
112
|
+
register,
|
|
113
|
+
type,
|
|
114
|
+
placeholder,
|
|
115
|
+
error,
|
|
116
|
+
name,
|
|
117
|
+
label,
|
|
118
|
+
required,
|
|
119
|
+
control,
|
|
120
|
+
maska,
|
|
121
|
+
inputClass,
|
|
122
|
+
labelClass,
|
|
123
|
+
errorClass
|
|
124
|
+
}) {
|
|
125
|
+
const renderChildren = () => {
|
|
126
|
+
if (maska) {
|
|
127
|
+
return /* @__PURE__ */ (0, import_jsx_runtime4.jsx)(
|
|
128
|
+
MaskedField_default,
|
|
129
|
+
{
|
|
130
|
+
name,
|
|
131
|
+
placeholder,
|
|
132
|
+
control,
|
|
133
|
+
maska,
|
|
134
|
+
inputClass
|
|
135
|
+
}
|
|
136
|
+
);
|
|
137
|
+
} else {
|
|
138
|
+
return /* @__PURE__ */ (0, import_jsx_runtime4.jsx)(
|
|
139
|
+
InputField_default,
|
|
140
|
+
{
|
|
141
|
+
name,
|
|
142
|
+
type,
|
|
143
|
+
placeholder,
|
|
144
|
+
register,
|
|
145
|
+
inputClass
|
|
146
|
+
}
|
|
147
|
+
);
|
|
148
|
+
}
|
|
149
|
+
};
|
|
150
|
+
return /* @__PURE__ */ (0, import_jsx_runtime4.jsx)(
|
|
151
|
+
mainLayout_default,
|
|
152
|
+
{
|
|
153
|
+
label,
|
|
154
|
+
required,
|
|
155
|
+
error,
|
|
156
|
+
name,
|
|
157
|
+
labelClass,
|
|
158
|
+
errorClass,
|
|
159
|
+
children: renderChildren()
|
|
160
|
+
}
|
|
161
|
+
);
|
|
162
|
+
}
|
|
163
|
+
var InputForm_default = InputForm;
|
|
164
|
+
|
|
165
|
+
// src/FormContext.tsx
|
|
166
|
+
var import_react = require("react");
|
|
167
|
+
var FormContext = (0, import_react.createContext)(null);
|
|
168
|
+
var FormProvider = FormContext.Provider;
|
|
169
|
+
function useFormContext() {
|
|
170
|
+
const context = (0, import_react.useContext)(FormContext);
|
|
171
|
+
if (!context) {
|
|
172
|
+
throw new Error("useFormContext must be used within FormLayout");
|
|
173
|
+
}
|
|
174
|
+
return context;
|
|
175
|
+
}
|
|
176
|
+
|
|
177
|
+
// src/FormLayout.tsx
|
|
178
|
+
var import_jsx_runtime5 = require("react/jsx-runtime");
|
|
179
|
+
function FormLayout({
|
|
180
|
+
formData,
|
|
181
|
+
funSubmit,
|
|
182
|
+
formClass,
|
|
183
|
+
buttonClass,
|
|
184
|
+
children
|
|
185
|
+
}) {
|
|
186
|
+
const {
|
|
187
|
+
control,
|
|
188
|
+
register,
|
|
189
|
+
handleSubmit,
|
|
190
|
+
formState: { errors }
|
|
191
|
+
} = (0, import_react_hook_form2.useForm)();
|
|
192
|
+
const onSubmit = (data) => {
|
|
193
|
+
funSubmit(data);
|
|
194
|
+
};
|
|
195
|
+
return /* @__PURE__ */ (0, import_jsx_runtime5.jsx)(FormProvider, { value: { control, register, errors }, children: /* @__PURE__ */ (0, import_jsx_runtime5.jsxs)("form", { onSubmit: handleSubmit(onSubmit), className: formClass, children: [
|
|
196
|
+
formData ? formData.map((item) => /* @__PURE__ */ (0, import_jsx_runtime5.jsx)(
|
|
197
|
+
InputForm_default,
|
|
198
|
+
{
|
|
199
|
+
type: item.type,
|
|
200
|
+
placeholder: item.placeholder,
|
|
201
|
+
error: errors,
|
|
202
|
+
name: String(item.key),
|
|
203
|
+
label: item.label,
|
|
204
|
+
control,
|
|
205
|
+
maska: item.maska,
|
|
206
|
+
register: register(item.key, {
|
|
207
|
+
required: item.required,
|
|
208
|
+
minLength: item.minLength,
|
|
209
|
+
maxLength: item.maxLength,
|
|
210
|
+
pattern: item.pattern,
|
|
211
|
+
validate: item.validate
|
|
212
|
+
}),
|
|
213
|
+
required: item.required,
|
|
214
|
+
inputClass: item.inputClass,
|
|
215
|
+
errorClass: item.errorClass,
|
|
216
|
+
labelClass: item.labelClass
|
|
217
|
+
},
|
|
218
|
+
String(item.key)
|
|
219
|
+
)) : children,
|
|
220
|
+
/* @__PURE__ */ (0, import_jsx_runtime5.jsx)("button", { type: "submit", className: buttonClass, children: "\u041E\u0442\u043F\u0440\u0430\u0432\u0438\u0442\u044C" })
|
|
221
|
+
] }) });
|
|
222
|
+
}
|
|
223
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
224
|
+
0 && (module.exports = {
|
|
225
|
+
FormLayout,
|
|
226
|
+
InputForm,
|
|
227
|
+
useFormContext
|
|
228
|
+
});
|
package/dist/index.mjs
ADDED
|
@@ -0,0 +1,199 @@
|
|
|
1
|
+
// src/FormLayout.tsx
|
|
2
|
+
import { useForm } from "react-hook-form";
|
|
3
|
+
|
|
4
|
+
// src/InputForm/layouts/mainLayout.tsx
|
|
5
|
+
import { jsx, jsxs } from "react/jsx-runtime";
|
|
6
|
+
function MainLayout({
|
|
7
|
+
children,
|
|
8
|
+
label,
|
|
9
|
+
required,
|
|
10
|
+
error,
|
|
11
|
+
name,
|
|
12
|
+
labelClass,
|
|
13
|
+
errorClass
|
|
14
|
+
}) {
|
|
15
|
+
return /* @__PURE__ */ jsxs("div", { children: [
|
|
16
|
+
/* @__PURE__ */ jsxs("label", { className: labelClass, children: [
|
|
17
|
+
label,
|
|
18
|
+
" ",
|
|
19
|
+
required ? /* @__PURE__ */ jsx("span", { children: "*" }) : null
|
|
20
|
+
] }),
|
|
21
|
+
children,
|
|
22
|
+
error?.[name] && /* @__PURE__ */ jsx("span", { className: errorClass, children: error?.[name]?.message })
|
|
23
|
+
] });
|
|
24
|
+
}
|
|
25
|
+
var mainLayout_default = MainLayout;
|
|
26
|
+
|
|
27
|
+
// src/InputForm/components/MaskedField.tsx
|
|
28
|
+
import { Controller } from "react-hook-form";
|
|
29
|
+
import { PatternFormat } from "react-number-format";
|
|
30
|
+
import { jsx as jsx2 } from "react/jsx-runtime";
|
|
31
|
+
function MaskedField({
|
|
32
|
+
placeholder,
|
|
33
|
+
name,
|
|
34
|
+
control,
|
|
35
|
+
maska,
|
|
36
|
+
inputClass
|
|
37
|
+
}) {
|
|
38
|
+
return /* @__PURE__ */ jsx2(
|
|
39
|
+
Controller,
|
|
40
|
+
{
|
|
41
|
+
name: name || "",
|
|
42
|
+
control,
|
|
43
|
+
rules: { required: maska.required },
|
|
44
|
+
render: ({ field }) => /* @__PURE__ */ jsx2(
|
|
45
|
+
PatternFormat,
|
|
46
|
+
{
|
|
47
|
+
...field,
|
|
48
|
+
format: maska.format,
|
|
49
|
+
mask: maska.mask,
|
|
50
|
+
placeholder: placeholder ? placeholder : "",
|
|
51
|
+
className: inputClass
|
|
52
|
+
}
|
|
53
|
+
)
|
|
54
|
+
}
|
|
55
|
+
);
|
|
56
|
+
}
|
|
57
|
+
var MaskedField_default = MaskedField;
|
|
58
|
+
|
|
59
|
+
// src/InputForm/components/InputField.tsx
|
|
60
|
+
import { jsx as jsx3 } from "react/jsx-runtime";
|
|
61
|
+
function InputField({
|
|
62
|
+
register,
|
|
63
|
+
type,
|
|
64
|
+
placeholder,
|
|
65
|
+
inputClass,
|
|
66
|
+
name
|
|
67
|
+
}) {
|
|
68
|
+
return /* @__PURE__ */ jsx3(
|
|
69
|
+
"input",
|
|
70
|
+
{
|
|
71
|
+
id: name,
|
|
72
|
+
...register,
|
|
73
|
+
type: type ? type : "text",
|
|
74
|
+
placeholder: placeholder ? placeholder : "",
|
|
75
|
+
className: inputClass
|
|
76
|
+
}
|
|
77
|
+
);
|
|
78
|
+
}
|
|
79
|
+
var InputField_default = InputField;
|
|
80
|
+
|
|
81
|
+
// src/InputForm/InputForm.tsx
|
|
82
|
+
import { jsx as jsx4 } from "react/jsx-runtime";
|
|
83
|
+
function InputForm({
|
|
84
|
+
register,
|
|
85
|
+
type,
|
|
86
|
+
placeholder,
|
|
87
|
+
error,
|
|
88
|
+
name,
|
|
89
|
+
label,
|
|
90
|
+
required,
|
|
91
|
+
control,
|
|
92
|
+
maska,
|
|
93
|
+
inputClass,
|
|
94
|
+
labelClass,
|
|
95
|
+
errorClass
|
|
96
|
+
}) {
|
|
97
|
+
const renderChildren = () => {
|
|
98
|
+
if (maska) {
|
|
99
|
+
return /* @__PURE__ */ jsx4(
|
|
100
|
+
MaskedField_default,
|
|
101
|
+
{
|
|
102
|
+
name,
|
|
103
|
+
placeholder,
|
|
104
|
+
control,
|
|
105
|
+
maska,
|
|
106
|
+
inputClass
|
|
107
|
+
}
|
|
108
|
+
);
|
|
109
|
+
} else {
|
|
110
|
+
return /* @__PURE__ */ jsx4(
|
|
111
|
+
InputField_default,
|
|
112
|
+
{
|
|
113
|
+
name,
|
|
114
|
+
type,
|
|
115
|
+
placeholder,
|
|
116
|
+
register,
|
|
117
|
+
inputClass
|
|
118
|
+
}
|
|
119
|
+
);
|
|
120
|
+
}
|
|
121
|
+
};
|
|
122
|
+
return /* @__PURE__ */ jsx4(
|
|
123
|
+
mainLayout_default,
|
|
124
|
+
{
|
|
125
|
+
label,
|
|
126
|
+
required,
|
|
127
|
+
error,
|
|
128
|
+
name,
|
|
129
|
+
labelClass,
|
|
130
|
+
errorClass,
|
|
131
|
+
children: renderChildren()
|
|
132
|
+
}
|
|
133
|
+
);
|
|
134
|
+
}
|
|
135
|
+
var InputForm_default = InputForm;
|
|
136
|
+
|
|
137
|
+
// src/FormContext.tsx
|
|
138
|
+
import { createContext, useContext } from "react";
|
|
139
|
+
var FormContext = createContext(null);
|
|
140
|
+
var FormProvider = FormContext.Provider;
|
|
141
|
+
function useFormContext() {
|
|
142
|
+
const context = useContext(FormContext);
|
|
143
|
+
if (!context) {
|
|
144
|
+
throw new Error("useFormContext must be used within FormLayout");
|
|
145
|
+
}
|
|
146
|
+
return context;
|
|
147
|
+
}
|
|
148
|
+
|
|
149
|
+
// src/FormLayout.tsx
|
|
150
|
+
import { jsx as jsx5, jsxs as jsxs2 } from "react/jsx-runtime";
|
|
151
|
+
function FormLayout({
|
|
152
|
+
formData,
|
|
153
|
+
funSubmit,
|
|
154
|
+
formClass,
|
|
155
|
+
buttonClass,
|
|
156
|
+
children
|
|
157
|
+
}) {
|
|
158
|
+
const {
|
|
159
|
+
control,
|
|
160
|
+
register,
|
|
161
|
+
handleSubmit,
|
|
162
|
+
formState: { errors }
|
|
163
|
+
} = useForm();
|
|
164
|
+
const onSubmit = (data) => {
|
|
165
|
+
funSubmit(data);
|
|
166
|
+
};
|
|
167
|
+
return /* @__PURE__ */ jsx5(FormProvider, { value: { control, register, errors }, children: /* @__PURE__ */ jsxs2("form", { onSubmit: handleSubmit(onSubmit), className: formClass, children: [
|
|
168
|
+
formData ? formData.map((item) => /* @__PURE__ */ jsx5(
|
|
169
|
+
InputForm_default,
|
|
170
|
+
{
|
|
171
|
+
type: item.type,
|
|
172
|
+
placeholder: item.placeholder,
|
|
173
|
+
error: errors,
|
|
174
|
+
name: String(item.key),
|
|
175
|
+
label: item.label,
|
|
176
|
+
control,
|
|
177
|
+
maska: item.maska,
|
|
178
|
+
register: register(item.key, {
|
|
179
|
+
required: item.required,
|
|
180
|
+
minLength: item.minLength,
|
|
181
|
+
maxLength: item.maxLength,
|
|
182
|
+
pattern: item.pattern,
|
|
183
|
+
validate: item.validate
|
|
184
|
+
}),
|
|
185
|
+
required: item.required,
|
|
186
|
+
inputClass: item.inputClass,
|
|
187
|
+
errorClass: item.errorClass,
|
|
188
|
+
labelClass: item.labelClass
|
|
189
|
+
},
|
|
190
|
+
String(item.key)
|
|
191
|
+
)) : children,
|
|
192
|
+
/* @__PURE__ */ jsx5("button", { type: "submit", className: buttonClass, children: "\u041E\u0442\u043F\u0440\u0430\u0432\u0438\u0442\u044C" })
|
|
193
|
+
] }) });
|
|
194
|
+
}
|
|
195
|
+
export {
|
|
196
|
+
FormLayout,
|
|
197
|
+
InputForm_default as InputForm,
|
|
198
|
+
useFormContext
|
|
199
|
+
};
|
package/package.json
ADDED
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@arturton/react-form-constructor",
|
|
3
|
+
"version": "0.1.2",
|
|
4
|
+
"description": "Flexible form constructor with React Hook Form integration",
|
|
5
|
+
"main": "dist/index.js",
|
|
6
|
+
"module": "dist/index.mjs",
|
|
7
|
+
"types": "dist/index.d.ts",
|
|
8
|
+
"exports": {
|
|
9
|
+
".": {
|
|
10
|
+
"types": "./dist/index.d.ts",
|
|
11
|
+
"import": "./dist/index.mjs",
|
|
12
|
+
"require": "./dist/index.js"
|
|
13
|
+
}
|
|
14
|
+
},
|
|
15
|
+
"files": [
|
|
16
|
+
"dist",
|
|
17
|
+
"README.md",
|
|
18
|
+
"LICENSE"
|
|
19
|
+
],
|
|
20
|
+
"scripts": {
|
|
21
|
+
"build": "tsup src/index.ts --format cjs,esm --dts --external react,react-dom,react-hook-form,react-number-format,zod",
|
|
22
|
+
"dev": "tsup src/index.ts --format cjs,esm --dts --watch"
|
|
23
|
+
},
|
|
24
|
+
"keywords": [
|
|
25
|
+
"react",
|
|
26
|
+
"forms",
|
|
27
|
+
"form-builder",
|
|
28
|
+
"validation",
|
|
29
|
+
"hooks"
|
|
30
|
+
],
|
|
31
|
+
"repository": {
|
|
32
|
+
"type": "git",
|
|
33
|
+
"url": "git+https://github.com/ArturTonoyan/react-form-constructor.git"
|
|
34
|
+
},
|
|
35
|
+
"bugs": {
|
|
36
|
+
"url": "https://github.com/ArturTonoyan/react-form-constructor/issues"
|
|
37
|
+
},
|
|
38
|
+
"homepage": "https://github.com/ArturTonoyan/react-form-constructor#readme",
|
|
39
|
+
"peerDependencies": {
|
|
40
|
+
"react": "^18 || ^19",
|
|
41
|
+
"react-hook-form": "^7.0.0",
|
|
42
|
+
"react-number-format": "^5.4.4"
|
|
43
|
+
},
|
|
44
|
+
"license": "MIT",
|
|
45
|
+
"devDependencies": {
|
|
46
|
+
"@types/react": "^19.2.10",
|
|
47
|
+
"react-number-format": "^5.4.4"
|
|
48
|
+
},
|
|
49
|
+
"dependencies": {
|
|
50
|
+
"zod": "^3.22.0"
|
|
51
|
+
}
|
|
52
|
+
}
|