indicator-ui 0.1.48 → 0.1.49
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/dist/index.js +259 -112
- package/dist/index.js.map +1 -1
- package/dist/types/src/hooks/forms/classes/index.d.ts +19 -9
- package/dist/types/src/hooks/forms/lib/scheme.d.ts +7 -4
- package/dist/types/src/hooks/forms/lib/utils.d.ts +1 -1
- package/dist/types/src/hooks/forms/types/formTypes.d.ts +8 -5
- package/dist/types/src/hooks/forms/types/scheme.d.ts +7 -3
- package/dist/types/src/hooks/forms/useForm.d.ts +51 -61
- package/package.json +1 -1
|
@@ -1,13 +1,23 @@
|
|
|
1
|
-
import { FormErrorsType, FormErrorType, FormPath, FormValidateFunItemType, FormValidateFunListType, FormValidateFunType, FormValue
|
|
1
|
+
import { ExtendFormPath, ExtendFormValue, FormErrorsType, FormErrorType, FormPath, FormValidateFunItemType, FormValidateFunListType, FormValidateFunType, FormValue } from "../types";
|
|
2
2
|
export declare class Form<T> {
|
|
3
3
|
private data;
|
|
4
|
+
private defaultValues;
|
|
4
5
|
constructor(data?: T | undefined);
|
|
5
6
|
getFormData(): T | undefined;
|
|
6
7
|
setFormData(data: T | undefined): void;
|
|
7
8
|
get formData(): T | undefined;
|
|
8
9
|
set formData(data: T | undefined);
|
|
9
|
-
|
|
10
|
-
|
|
10
|
+
hasDefaultValue<const P extends FormPath<T>>(path: P): boolean;
|
|
11
|
+
getDefaultValue<const P extends FormPath<T>>(path: P): {
|
|
12
|
+
default: ExtendFormValue<T | undefined, P>;
|
|
13
|
+
} | undefined;
|
|
14
|
+
setDefaultValue<const P extends ExtendFormPath<T>>(path: P, value: ExtendFormValue<T, P>): void;
|
|
15
|
+
deleteDefaultValue<const P extends ExtendFormPath<T>>(path: P): void;
|
|
16
|
+
clearDefaultValues(): void;
|
|
17
|
+
applyDefaultValue<const P extends ExtendFormPath<T>>(path: P): void;
|
|
18
|
+
applyDefaultValues(): void;
|
|
19
|
+
setField<const P extends FormPath<T>>(path: P, value: FormValue<T, P> | undefined): void;
|
|
20
|
+
getField<const P extends FormPath<T>>(path: P): FormValue<T, P> | undefined;
|
|
11
21
|
clear(): void;
|
|
12
22
|
}
|
|
13
23
|
export declare class FormError<T> {
|
|
@@ -32,12 +42,12 @@ export declare class FormValidateFun<T> {
|
|
|
32
42
|
private getValueForValidate;
|
|
33
43
|
getFormValidateFunctions(): FormValidateFunType<T>;
|
|
34
44
|
setFormValidateFunctions(newValue: FormValidateFunType<T>): void;
|
|
35
|
-
getValidateFunctions<const P extends
|
|
36
|
-
setValidateFunctions<const P extends
|
|
37
|
-
deleteValidateFunctions<const P extends
|
|
38
|
-
addValidateFunctions<const P extends
|
|
39
|
-
validate<const P extends
|
|
40
|
-
isValid<const P extends
|
|
45
|
+
getValidateFunctions<const P extends ExtendFormPath<T>>(path: P): FormValidateFunListType<ExtendFormValue<T, P>>;
|
|
46
|
+
setValidateFunctions<const P extends ExtendFormPath<T>>(path: P, func: FormValidateFunListType<ExtendFormValue<T, P>>): void;
|
|
47
|
+
deleteValidateFunctions<const P extends ExtendFormPath<T>>(chain: P): void;
|
|
48
|
+
addValidateFunctions<const P extends ExtendFormPath<T>>(chain: P, fun: FormValidateFunItemType<ExtendFormValue<T, P>>): void;
|
|
49
|
+
validate<const P extends ExtendFormPath<T>>(path: P): Promise<ValidateResArrayType<T>>;
|
|
50
|
+
isValid<const P extends ExtendFormPath<T>>(path: P): Promise<boolean>;
|
|
41
51
|
validateAllFields(): Promise<ValidateResArrayType<T>>;
|
|
42
52
|
isFormValid(): Promise<boolean>;
|
|
43
53
|
}
|
|
@@ -1,13 +1,16 @@
|
|
|
1
|
-
import { FieldConfigType, FormPath, FormSchemeType,
|
|
1
|
+
import { ExtendFormPath, ExtendFormValue, FieldConfigType, FormDefaultValues, FormPath, FormSchemeType, GetProcessArrayFieldValue } from '../types';
|
|
2
2
|
import { Form, FormValidateFun } from '../classes';
|
|
3
3
|
type PropsOptionsType<T> = {
|
|
4
4
|
form: Form<T>;
|
|
5
5
|
formValidateFun: FormValidateFun<T>;
|
|
6
6
|
};
|
|
7
7
|
/** Полностью заменяет валидаторы поля */
|
|
8
|
-
export declare function processValidators<T, F>(path: FormPath<T>,
|
|
8
|
+
export declare function processValidators<T, F>(path: FormPath<T>, _config: FieldConfigType<T, F>, options: PropsOptionsType<T>): void;
|
|
9
9
|
/** Полностью заменяет валидаторы полей */
|
|
10
10
|
export declare function processScheme<T>(scheme: FormSchemeType<T>, options: PropsOptionsType<T>): void;
|
|
11
|
-
export declare function
|
|
12
|
-
export declare function
|
|
11
|
+
export declare function getArrayExtendBasePath<T, P extends ExtendFormPath<T>>(path: P): GetProcessArrayFieldValue<T, P> | undefined;
|
|
12
|
+
export declare function getArrayExtendPath<T, P extends ExtendFormPath<T>>(path: P): GetProcessArrayFieldValue<T, P> | undefined;
|
|
13
|
+
export declare function getExtendPath<T, P extends FormPath<T>>(path: P): ExtendFormPath<T>;
|
|
14
|
+
export declare function getSortedExtendsPaths<T>(defaultValues: FormDefaultValues<T>): ExtendFormPath<T>[];
|
|
15
|
+
export declare function setExtendValue<T, P extends ExtendFormPath<T>>(formData: T, path: P, value: ExtendFormValue<T, P>): T | undefined;
|
|
13
16
|
export {};
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
export declare function copyData(data:
|
|
1
|
+
export declare function copyData<T>(data: T): T;
|
|
2
2
|
export declare function getBasePath(path: string): string | null;
|
|
3
3
|
export declare function getBaseRest(path: string): string | null;
|
|
4
4
|
export declare function getArrayIndex(path: string): number | null;
|
|
@@ -37,15 +37,18 @@ export type FormErrorsType<T> = Array<{
|
|
|
37
37
|
}[FormPath<T>]>;
|
|
38
38
|
type GetOnlyArrayField<P> = P extends string ? Extract<P, `${string}]`> : never;
|
|
39
39
|
export type ProcessArrayFields<P> = GetOnlyArrayField<P> extends infer T extends string ? T extends `${infer B}[${number}]` ? `${B}[*]` : never : never;
|
|
40
|
+
export type ExtendFormPath<T> = FormPath<T> | ProcessArrayFields<FormPath<T>>;
|
|
40
41
|
export type GetProcessArrayFieldValue<T, P> = P extends `${infer Rest}[*]` ? Rest extends FormPath<T> ? FormValue<T, Rest> extends infer Arr extends any[] ? Arr[number] : never : never : never;
|
|
41
|
-
export type
|
|
42
|
-
export type GetFormValidateFunValueType<T, P> = GetProcessArrayFieldValue<T, P> | (P extends FormPath<T> ? FormValue<T, P> : never);
|
|
42
|
+
export type ExtendFormValue<T, P> = GetProcessArrayFieldValue<T, P> | (P extends FormPath<T> ? FormValue<T, P> : never);
|
|
43
43
|
export type FormValidateFunItemType<T> = (value: T | undefined) => Promise<FormErrorType> | FormErrorType;
|
|
44
44
|
export type FormValidateFunListType<T> = Array<FormValidateFunItemType<T>>;
|
|
45
45
|
export type FormValidateFunType<T> = Array<{
|
|
46
|
-
[K in
|
|
46
|
+
[K in ExtendFormPath<T>]: {
|
|
47
47
|
path: K;
|
|
48
|
-
fun: FormValidateFunListType<
|
|
48
|
+
fun: FormValidateFunListType<ExtendFormValue<T, K>>;
|
|
49
49
|
};
|
|
50
|
-
}[
|
|
50
|
+
}[ExtendFormPath<T>]>;
|
|
51
|
+
export type FormDefaultValues<T> = {
|
|
52
|
+
[P in ExtendFormPath<T>]?: ExtendFormValue<T, P>;
|
|
53
|
+
};
|
|
51
54
|
export {};
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { FormErrorType, FormPath, FormValidateFunItemType,
|
|
1
|
+
import { FormErrorType, FormPath, FormValidateFunItemType, ExtendFormPath, ExtendFormValue } from './formTypes';
|
|
2
2
|
export type StringFieldValidators = Partial<{
|
|
3
3
|
/** Минимальная длина строки */
|
|
4
4
|
minStr: number;
|
|
@@ -70,6 +70,10 @@ export type BaseFieldValidators<T, F> = Partial<{
|
|
|
70
70
|
priory?: number;
|
|
71
71
|
};
|
|
72
72
|
}>;
|
|
73
|
+
export type DefaultValueConfig<T, F> = Partial<{
|
|
74
|
+
/** Значение по-умолчанию */
|
|
75
|
+
default: F;
|
|
76
|
+
}>;
|
|
73
77
|
export type ExtendsValidatorOptions<T> = {
|
|
74
78
|
/** Значение валидотора */
|
|
75
79
|
setting: T;
|
|
@@ -87,7 +91,7 @@ type ProcessValidator<T> = T extends Record<string, unknown> ? {
|
|
|
87
91
|
[K in keyof T]: T[K] | ValidatorOption<T[K]>;
|
|
88
92
|
} : never;
|
|
89
93
|
type GetValidatorValue<V> = V extends ExtendsValidatorOptions<infer T> ? T : never;
|
|
90
|
-
export type FieldConfigType<T, F> = BaseFieldValidators<T, F> & ProcessValidator<ArrayFieldValidators> & ProcessValidator<FileFieldValidators> & ProcessValidator<StringFieldValidators> & ProcessValidator<DateTimeFieldValidators> & ProcessValidator<NumberFieldValidators>;
|
|
94
|
+
export type FieldConfigType<T, F> = DefaultValueConfig<T, F> & BaseFieldValidators<T, F> & ProcessValidator<ArrayFieldValidators> & ProcessValidator<FileFieldValidators> & ProcessValidator<StringFieldValidators> & ProcessValidator<DateTimeFieldValidators> & ProcessValidator<NumberFieldValidators>;
|
|
91
95
|
/**
|
|
92
96
|
* Тип схемы валидации формы.
|
|
93
97
|
*
|
|
@@ -136,7 +140,7 @@ export type FieldConfigType<T, F> = BaseFieldValidators<T, F> & ProcessValidator
|
|
|
136
140
|
* @template T — тип данных формы (например, интерфейс или тип объекта формы).
|
|
137
141
|
*/
|
|
138
142
|
export type FormSchemeType<T> = Partial<T> extends infer Form ? {
|
|
139
|
-
[K in
|
|
143
|
+
[K in ExtendFormPath<Form>]?: FieldConfigType<Form, ExtendFormValue<Form, K>>;
|
|
140
144
|
} : never;
|
|
141
145
|
export type FieldPropsType<V> = {
|
|
142
146
|
value?: V;
|
|
@@ -1,9 +1,13 @@
|
|
|
1
|
-
import
|
|
1
|
+
import React from "react";
|
|
2
|
+
import { ExtendFormPath, FieldPropsType, FormErrorsType, FormErrorType } from "../../hooks";
|
|
2
3
|
import { FormPath, FormSchemeType, FormValue } from "./types";
|
|
3
|
-
type PropsType<T> = [
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
4
|
+
type PropsType<T, Form> = [
|
|
5
|
+
{
|
|
6
|
+
initFormData?: T;
|
|
7
|
+
scheme?: FormSchemeType<T>;
|
|
8
|
+
onSubmit?: (data: Form) => void | Promise<void>;
|
|
9
|
+
}
|
|
10
|
+
];
|
|
7
11
|
/**
|
|
8
12
|
* Хук `useForm` — универсальное решение для управления и валидации форм с полной статической типизацией.
|
|
9
13
|
*
|
|
@@ -71,10 +75,13 @@ type PropsType<T> = [{
|
|
|
71
75
|
* };
|
|
72
76
|
*
|
|
73
77
|
* export function FormPage() {
|
|
74
|
-
* const { register, formData, errors, isFormValid } = useForm<TestFormType>({
|
|
78
|
+
* const { register, registerForm, formData, errors, isFormValid } = useForm<TestFormType>({
|
|
79
|
+
* scheme,
|
|
80
|
+
* onSubmit: (data) => console.log('Submit data:', data),
|
|
81
|
+
* });
|
|
75
82
|
*
|
|
76
83
|
* return (
|
|
77
|
-
* <
|
|
84
|
+
* <form {...registerForm()}>
|
|
78
85
|
* <FormField
|
|
79
86
|
* {...register('baseInput', { required: { setting: true, message: 'Обязательное поле' } })}
|
|
80
87
|
* label="base input"
|
|
@@ -105,8 +112,9 @@ type PropsType<T> = [{
|
|
|
105
112
|
* hint="Select с выбором нескольких элементов"
|
|
106
113
|
* />
|
|
107
114
|
*
|
|
108
|
-
* <button
|
|
109
|
-
*
|
|
115
|
+
* <button type="submit">Отправить</button>
|
|
116
|
+
* <button type="reset">Сбросить</button>
|
|
117
|
+
* </form>
|
|
110
118
|
* );
|
|
111
119
|
* }
|
|
112
120
|
* ```
|
|
@@ -132,6 +140,34 @@ type PropsType<T> = [{
|
|
|
132
140
|
* | `clearForm()` | `() => void` | Сбрасывает все значения формы |
|
|
133
141
|
* | `clearErrors()` | `() => void` | Очищает все ошибки |
|
|
134
142
|
* | `register(path, config?)` | `(path, config?) => FieldPropsType` | Регистрирует поле и возвращает пропсы для компонента |
|
|
143
|
+
* | `registerForm()` | `() => Pick<React.ComponentProps<'form'>, 'onSubmit' | 'onReset' | 'noValidate'>` | Возвращает обработчики и настройки для `<form>` |
|
|
144
|
+
*
|
|
145
|
+
* ---
|
|
146
|
+
* ### 🧠 `registerForm`
|
|
147
|
+
*
|
|
148
|
+
* Функция `registerForm` позволяет удобно подключить форму к системе валидации и управления состоянием,
|
|
149
|
+
* не теряя контроль над поведением `onSubmit` и `onReset`.
|
|
150
|
+
*
|
|
151
|
+
* #### Пример:
|
|
152
|
+
* ```tsx
|
|
153
|
+
* const { register, registerForm } = useForm<MyFormType>({
|
|
154
|
+
* onSubmit: (data) => console.log('Отправлено:', data),
|
|
155
|
+
* });
|
|
156
|
+
*
|
|
157
|
+
* return (
|
|
158
|
+
* <form {...registerForm()}>
|
|
159
|
+
* <FormField {...register('username')} label="Имя" />
|
|
160
|
+
* <FormField {...register('email')} label="Email" />
|
|
161
|
+
* <button type="submit">Отправить</button>
|
|
162
|
+
* <button type="reset">Сбросить</button>
|
|
163
|
+
* </form>
|
|
164
|
+
* );
|
|
165
|
+
* ```
|
|
166
|
+
*
|
|
167
|
+
* #### Поведение:
|
|
168
|
+
* - `onSubmit`: выполняет валидацию всей формы (`isFormValid()`), при успехе вызывает переданный `onSubmit`;
|
|
169
|
+
* - `onReset`: предотвращает нативный сброс, очищает значения (`clearForm()`) и ошибки (`clearErrors()`);
|
|
170
|
+
* - `noValidate: true`: отключает нативную HTML5-валидацию, чтобы не мешала кастомной логике.
|
|
135
171
|
*
|
|
136
172
|
* ---
|
|
137
173
|
* @template Form — объект, описывающий форму.
|
|
@@ -140,58 +176,11 @@ type PropsType<T> = [{
|
|
|
140
176
|
* @param {Object} [params]
|
|
141
177
|
* @param {T} [params.initFormData] — начальные данные формы.
|
|
142
178
|
* @param {FormSchemeType<T>} [params.scheme] — схема валидации формы.
|
|
179
|
+
* @param {(data: Form) => void | Promise<void>} [params.onSubmit] — колбэк, вызываемый при успешной отправке формы.
|
|
143
180
|
*
|
|
144
|
-
* @returns {
|
|
145
|
-
* /** Текущее состояние формы (реактивное) *\/
|
|
146
|
-
* formData: T;
|
|
147
|
-
*
|
|
148
|
-
* /** Установка полного состояния формы *\/
|
|
149
|
-
* setFormData: (data: T) => void;
|
|
150
|
-
*
|
|
151
|
-
* /** Получение текущего состояния формы *\/
|
|
152
|
-
* getFormData: () => T;
|
|
153
|
-
*
|
|
154
|
-
* /** Все ошибки формы *\/
|
|
155
|
-
* errors: FormErrorsType<T>;
|
|
156
|
-
*
|
|
157
|
-
* /** Замена всех ошибок формы *\/
|
|
158
|
-
* setErrors: (errors: FormErrorsType<T>) => void;
|
|
159
|
-
*
|
|
160
|
-
* /** Получение значения поля *\/
|
|
161
|
-
* getField: <P extends FormPath<T>>(path: P) => FormValue<T, P>;
|
|
162
|
-
*
|
|
163
|
-
* /** Установка значения поля *\/
|
|
164
|
-
* setField: <P extends FormPath<T>>(path: P, value: FormValue<T, P> | undefined) => void;
|
|
165
|
-
*
|
|
166
|
-
* /** Получение ошибки конкретного поля *\/
|
|
167
|
-
* getError: <P extends FormPath<T>>(path: P) => FormErrorType;
|
|
168
|
-
*
|
|
169
|
-
* /** Установка ошибки поля вручную *\/
|
|
170
|
-
* setError: <P extends FormPath<T>>(path: P, error: FormErrorType) => void;
|
|
171
|
-
*
|
|
172
|
-
* /** Проверка конкретного поля с обновлением `errors` *\/
|
|
173
|
-
* highlightField: <P extends GetFormValidateFunPathType<T>>(path: P) => Promise<void>;
|
|
174
|
-
*
|
|
175
|
-
* /** Проверка всех полей формы с обновлением `errors` *\/
|
|
176
|
-
* highlightFormErrors: () => Promise<void>;
|
|
177
|
-
*
|
|
178
|
-
* /** Проверка валидности конкретного поля (без обновления `errors`) *\/
|
|
179
|
-
* isFieldValid: <P extends GetFormValidateFunPathType<T>>(path: P) => boolean;
|
|
180
|
-
*
|
|
181
|
-
* /** Проверка валидности всей формы (без обновления `errors`) *\/
|
|
182
|
-
* isFormValid: () => boolean;
|
|
183
|
-
*
|
|
184
|
-
* /** Сброс всех значений формы *\/
|
|
185
|
-
* clearForm: () => void;
|
|
186
|
-
*
|
|
187
|
-
* /** Очистка всех ошибок *\/
|
|
188
|
-
* clearErrors: () => void;
|
|
189
|
-
*
|
|
190
|
-
* /** Регистрация поля с привязкой валидации и значений *\/
|
|
191
|
-
* register: <P extends FormPath<T>>(path: P, config?: FormSchemeType<T>[P]) => FieldPropsType<FormValue<T, P>>;
|
|
192
|
-
* }}
|
|
181
|
+
* @returns {object} Объект с методами и состоянием формы (см. таблицу выше).
|
|
193
182
|
*/
|
|
194
|
-
export declare function useForm<Form, T extends Partial<Form> = Partial<Form>>(...args: PropsType<T>): {
|
|
183
|
+
export declare function useForm<Form, T extends Partial<Form> = Partial<Form>>(...args: PropsType<T, Form>): {
|
|
195
184
|
formData: T | undefined;
|
|
196
185
|
setFormData: (formData: T) => void;
|
|
197
186
|
getFormData: () => T | undefined;
|
|
@@ -201,12 +190,13 @@ export declare function useForm<Form, T extends Partial<Form> = Partial<Form>>(.
|
|
|
201
190
|
setField: <P extends FormPath<T>>(path: P, value: FormValue<T, P> | undefined) => void;
|
|
202
191
|
getError: <P extends FormPath<T>>(path: P) => FormErrorType | undefined;
|
|
203
192
|
setError: <P extends FormPath<T>>(path: P, error: FormErrorType) => void;
|
|
204
|
-
highlightField: <P extends
|
|
193
|
+
highlightField: <P extends ExtendFormPath<T>>(path: P) => Promise<void>;
|
|
205
194
|
highlightFormErrors: () => Promise<void>;
|
|
206
|
-
isFieldValid: <P extends
|
|
195
|
+
isFieldValid: <P extends ExtendFormPath<T>>(path: P) => Promise<boolean>;
|
|
207
196
|
isFormValid: () => Promise<boolean>;
|
|
208
197
|
clearForm: () => void;
|
|
209
198
|
clearErrors: () => void;
|
|
210
199
|
register: <P extends FormPath<T>>(path: P, config?: FormSchemeType<T>[P]) => FieldPropsType<FormValue<T, P>>;
|
|
200
|
+
registerForm: () => Pick<React.ComponentProps<"form">, "onSubmit" | "onReset" | "noValidate">;
|
|
211
201
|
};
|
|
212
202
|
export {};
|