indicator-ui 1.1.21 → 1.1.22

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.
@@ -2,7 +2,7 @@ import { ExtendFormPath, ExtendFormValue, FormErrorsType, FormErrorType, FormPat
2
2
  export declare class Form<T> {
3
3
  private data;
4
4
  private defaultValues;
5
- constructor(data?: T | undefined);
5
+ constructor(data?: T | Form<T>);
6
6
  getFormData: () => T | undefined;
7
7
  setFormData: (data: T | undefined) => void;
8
8
  get formData(): T | undefined;
@@ -1,27 +1,27 @@
1
1
  import { ExtendFormPath, FormErrorsType, FormErrorType, FormPath, FormValue, UseFormServices } from '../types';
2
2
  import { Form } from '../classes';
3
3
  import { Nullable, Undefinable } from '../../../types';
4
- export declare function createFormServices<F, T = Nullable<Undefinable<F>>>(deps: {
4
+ export declare function createFormServices<T, F = Nullable<Undefinable<T>>>(deps: {
5
5
  /** Form */
6
- getForm: () => Form<T>;
7
- setFormData: (data: T) => void;
8
- getValidForm: () => Promise<F | null>;
6
+ getForm: () => Form<F>;
7
+ setFormData: (data: F) => void;
8
+ getValidForm: () => Promise<T | null>;
9
9
  /** Fields */
10
- getField: <P extends FormPath<T>>(path: P) => FormValue<T, P> | null | undefined;
11
- getFieldSync: <P extends FormPath<T>>(path: P) => FormValue<T, P> | undefined;
12
- setField: <P extends FormPath<T>>(path: P, value: FormValue<T, P> | undefined) => void;
13
- clearField: (path: FormPath<T> | FormPath<T>[]) => void;
10
+ getField: <P extends FormPath<F>>(path: P) => FormValue<F, P> | null | undefined;
11
+ getFieldSync: <P extends FormPath<F>>(path: P) => FormValue<F, P> | undefined;
12
+ setField: <P extends FormPath<F>>(path: P, value: FormValue<F, P> | undefined) => void;
13
+ clearField: (path: FormPath<F> | FormPath<F>[]) => void;
14
14
  /** Errors */
15
- getError: <P extends FormPath<T>>(path: P) => FormErrorType | undefined;
16
- setError: <P extends FormPath<T>>(path: P, error: FormErrorType) => void;
17
- setErrors: (errors: FormErrorsType<T>) => void;
15
+ getError: <P extends FormPath<F>>(path: P) => FormErrorType | undefined;
16
+ setError: <P extends FormPath<F>>(path: P, error: FormErrorType) => void;
17
+ setErrors: (errors: FormErrorsType<F>) => void;
18
18
  clearErrors: () => void;
19
19
  /** Validation */
20
- isFieldValid: <P extends ExtendFormPath<T>>(path: P) => Promise<boolean>;
21
- areFieldsValid: (paths: ExtendFormPath<T>[]) => Promise<boolean>;
20
+ isFieldValid: <P extends ExtendFormPath<F>>(path: P) => Promise<boolean>;
21
+ areFieldsValid: (paths: ExtendFormPath<F>[]) => Promise<boolean>;
22
22
  isFormValid: () => Promise<boolean>;
23
23
  /** Highlight (side-effect validation) */
24
- highlightField: <P extends ExtendFormPath<T>>(path: P) => Promise<boolean>;
25
- highlightFields: (paths: ExtendFormPath<T>[]) => Promise<boolean>;
24
+ highlightField: <P extends ExtendFormPath<F>>(path: P) => Promise<boolean>;
25
+ highlightFields: (paths: ExtendFormPath<F>[]) => Promise<boolean>;
26
26
  highlightFormErrors: () => Promise<boolean>;
27
- }): UseFormServices<F, T>;
27
+ }): UseFormServices<T, F>;
@@ -1,6 +1,7 @@
1
1
  import { FocusEvent, SetStateAction } from 'react';
2
2
  import { Nullable, Undefinable } from '../../../types';
3
3
  import { ExtendFormPath, ExtendFormValue, FormErrorType, FormPath, FormValue } from './formTypes';
4
+ import { UseFormServices } from '../..';
4
5
  export type StringFieldValidators = Partial<{
5
6
  /** Минимальная длина строки */
6
7
  minStr: number;
@@ -152,6 +153,10 @@ export type FieldConfigType<T, F> = DefaultValueConfig<T, F> & BaseFieldValidato
152
153
  export type FormSchemeType<T> = Nullable<Undefinable<T>> extends infer Form ? {
153
154
  [K in ExtendFormPath<Form>]?: FieldConfigType<Form, ExtendFormValue<Form, K>>;
154
155
  } : never;
156
+ type WatcherCallback<T, P extends FormPath<T>, F = Nullable<Undefinable<T>>> = (value: FormValue<F, P>, services: UseFormServices<T>) => any;
157
+ export type FormWatchType<T> = {
158
+ [K in FormPath<T>]?: WatcherCallback<T, K>;
159
+ };
155
160
  export type FieldChangeMode = 'change' | 'update';
156
161
  export type FieldOnChange<V, M extends FieldChangeMode> = M extends 'change' ? (action?: V | null) => void : (action: SetStateAction<V | undefined | null>) => void;
157
162
  export type FieldPropsType<V, M extends FieldChangeMode = 'change'> = {
@@ -1,52 +1,52 @@
1
1
  import { ExtendFormPath, FormErrorsType, FormErrorType, FormPath, FormValue } from './formTypes';
2
2
  import { Form } from '../classes';
3
3
  import { Nullable, Undefinable } from '../../../types';
4
- export type UseFormServices<F, T = Nullable<Undefinable<F>>> = {
4
+ export type UseFormServices<T, F = Nullable<Undefinable<T>>> = {
5
5
  /** Работа с данными формы */
6
6
  form: {
7
7
  /** Получить актуальные данные формы */
8
- getData: () => Form<T>;
8
+ getData: () => Form<F>;
9
9
  /** Полностью заменить данные формы */
10
- setData: (data: T) => void;
11
- getValidData: () => Promise<F | null>;
10
+ setData: (data: F) => void;
11
+ getValidData: () => Promise<T | null>;
12
12
  };
13
13
  /** Работа с полями */
14
14
  fields: {
15
15
  /** Получить значение поля (реактивное) */
16
- get: <P extends FormPath<T>>(path: P) => FormValue<T, P> | null | undefined;
16
+ get: <P extends FormPath<F>>(path: P) => FormValue<F, P> | null | undefined;
17
17
  /** Получить значение поля (гарантированно актуальное) */
18
- getSync: <P extends FormPath<T>>(path: P) => FormValue<T, P> | undefined;
18
+ getSync: <P extends FormPath<F>>(path: P) => FormValue<F, P> | undefined;
19
19
  /** Установить значение поля */
20
- set: <P extends FormPath<T>>(path: P, value: FormValue<T, P> | undefined) => void;
20
+ set: <P extends FormPath<F>>(path: P, value: FormValue<F, P> | undefined) => void;
21
21
  /** Очистить одно или несколько полей */
22
- clear: (path: FormPath<T> | FormPath<T>[]) => void;
22
+ clear: (path: FormPath<F> | FormPath<F>[]) => void;
23
23
  };
24
24
  /** Работа с ошибками */
25
25
  errors: {
26
26
  /** Получить ошибку поля */
27
- get: <P extends FormPath<T>>(path: P) => FormErrorType | undefined;
27
+ get: <P extends FormPath<F>>(path: P) => FormErrorType | undefined;
28
28
  /** Установить ошибку поля */
29
- set: <P extends FormPath<T>>(path: P, error: FormErrorType) => void;
29
+ set: <P extends FormPath<F>>(path: P, error: FormErrorType) => void;
30
30
  /** Массово установить ошибки (например, серверные) */
31
- setAll: (errors: FormErrorsType<T>) => void;
31
+ setAll: (errors: FormErrorsType<F>) => void;
32
32
  /** Очистить все ошибки */
33
33
  clear: () => void;
34
34
  };
35
35
  /** Валидация (без мутаций ошибок) */
36
36
  validation: {
37
37
  /** Проверить валидность поля */
38
- isFieldValid: <P extends ExtendFormPath<T>>(path: P) => Promise<boolean>;
38
+ isFieldValid: <P extends ExtendFormPath<F>>(path: P) => Promise<boolean>;
39
39
  /** Проверить несколько полей */
40
- areFieldsValid: (paths: ExtendFormPath<T>[]) => Promise<boolean>;
40
+ areFieldsValid: (paths: ExtendFormPath<F>[]) => Promise<boolean>;
41
41
  /** Проверить валидность всей формы */
42
42
  isFormValid: () => Promise<boolean>;
43
43
  };
44
44
  /** Подсветка ошибок (side-effect валидации) */
45
45
  highlight: {
46
46
  /** Подсветить ошибку конкретного поля */
47
- field: <P extends ExtendFormPath<T>>(path: P) => Promise<boolean>;
47
+ field: <P extends ExtendFormPath<F>>(path: P) => Promise<boolean>;
48
48
  /** Подсветить ошибки нескольких полей */
49
- fields: (paths: ExtendFormPath<T>[]) => Promise<boolean>;
49
+ fields: (paths: ExtendFormPath<F>[]) => Promise<boolean>;
50
50
  /** Подсветить все ошибки формы */
51
51
  form: () => Promise<boolean>;
52
52
  };
@@ -1,23 +1,22 @@
1
- import { default as React, FormEvent, Ref } from 'react';
2
- import { ExtendFormPath, FieldPropsType, FormErrorsType, FormErrorType } from '..';
1
+ import { default as React, Ref } from 'react';
2
+ import { ExtendFormPath, FieldPropsType, FormErrorsType, FormErrorType, FormWatchType } from '..';
3
3
  import { InstanceRefAttributes, Nullable, Undefinable } from '../../types';
4
4
  import { FormPath, FormSchemeType, FormValue, UseFormServices } from './types';
5
5
  type FormRef = React.ComponentRef<'form'>;
6
6
  type FormInstance = Ref<FormRef> | Array<Ref<FormRef>>;
7
- type PropsType<T, Form> = [
8
- props?: {
9
- ref?: FormInstance;
10
- useInstanceRefApi?: boolean;
11
- initFormData?: T;
12
- scheme?: FormSchemeType<T>;
13
- onSubmit?: (data: Form, event: FormEvent<HTMLFormElement>, services: UseFormServices<Form, T>) => void | Promise<void>;
14
- /** Callback при ошибки валидации полей */
15
- onSubmitError?: (event: {
16
- errors: FormErrorsType<T>;
17
- }, services: UseFormServices<Form, T>) => void;
18
- onSubmitAttempt?: (data: T | undefined | null, event: FormEvent<HTMLFormElement>, services: UseFormServices<Form, T>) => void;
19
- }
20
- ];
7
+ type PropsType<Form, T> = {
8
+ ref?: FormInstance;
9
+ useInstanceRefApi?: boolean;
10
+ initFormData?: T;
11
+ scheme?: FormSchemeType<T>;
12
+ onSubmit?: (data: Form, event: React.SubmitEvent<HTMLFormElement>, services: UseFormServices<Form, T>) => void | Promise<void>;
13
+ /** Callback при ошибки валидации полей */
14
+ onSubmitError?: (event: {
15
+ errors: FormErrorsType<T>;
16
+ }, services: UseFormServices<Form, T>) => void;
17
+ onSubmitAttempt?: (data: T | undefined | null, event: React.SubmitEvent<HTMLFormElement>, services: UseFormServices<Form, T>) => void;
18
+ watch?: FormWatchType<Form>;
19
+ };
21
20
  /**
22
21
  * `useForm` — типобезопасный хук для управления состоянием и валидации форм.
23
22
  *
@@ -212,7 +211,7 @@ type PropsType<T, Form> = [
212
211
  * @template Form Полный тип формы
213
212
  * @template T Тип начальных данных
214
213
  */
215
- export declare function useForm<Form, T extends Nullable<Undefinable<Form>> = Nullable<Undefinable<Form>>>(...args: PropsType<T, Form>): {
214
+ export declare function useForm<F, T extends Nullable<Undefinable<F>> = Nullable<Undefinable<F>>>(props?: PropsType<F, T>): {
216
215
  formData: T | undefined;
217
216
  setFormData: (formData: T) => void;
218
217
  getFormData: () => T | undefined;
@@ -237,7 +236,7 @@ export declare function useForm<Form, T extends Nullable<Undefinable<Form>> = Nu
237
236
  <P extends FormPath<T>>(path: P, config?: FormSchemeType<T>[P]): FieldPropsType<FormValue<T, P>, "update">;
238
237
  };
239
238
  registerForm: (refsInstance?: FormInstance) => Pick<React.DetailedHTMLProps<React.FormHTMLAttributes<HTMLFormElement>, HTMLFormElement>, "ref" | "onReset" | "onSubmit" | "noValidate"> & Pick<InstanceRefAttributes<HTMLFormElement>, "instanceRef">;
240
- getValidForm: () => Promise<Form | null>;
239
+ getValidForm: () => Promise<F | null>;
241
240
  submitForm: () => void;
242
241
  resetForm: () => void;
243
242
  };
package/package.json CHANGED
@@ -11,7 +11,7 @@
11
11
  "react-components",
12
12
  "ui-kit"
13
13
  ],
14
- "version": "1.1.21",
14
+ "version": "1.1.22",
15
15
  "exports": {
16
16
  ".": {
17
17
  "types": "./dist/types/index.d.ts",