@cloud-ru/uikit-product-fields-predefined 2.4.8-preview-611dfd0c.0 → 2.4.8-preview-5e39e65e.0
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 +8 -3
- package/dist/cjs/components/FieldCode/FieldCode.d.ts +3 -8
- package/dist/cjs/components/FieldCode/FieldCode.js +2 -9
- package/dist/cjs/components/FieldCode/hooks/index.d.ts +1 -1
- package/dist/cjs/components/FieldCode/hooks/index.js +1 -1
- package/dist/cjs/components/FieldCode/hooks/useFieldCodeValidate.d.ts +8 -0
- package/dist/cjs/components/FieldCode/hooks/useFieldCodeValidate.js +24 -0
- package/dist/cjs/components/FieldCode/index.d.ts +1 -0
- package/dist/cjs/components/FieldCode/index.js +3 -0
- package/dist/cjs/components/FieldCode/styles.module.css +1 -1
- package/dist/esm/components/FieldCode/FieldCode.d.ts +3 -8
- package/dist/esm/components/FieldCode/FieldCode.js +3 -10
- package/dist/esm/components/FieldCode/hooks/index.d.ts +1 -1
- package/dist/esm/components/FieldCode/hooks/index.js +1 -1
- package/dist/esm/components/FieldCode/hooks/useFieldCodeValidate.d.ts +8 -0
- package/dist/esm/components/FieldCode/hooks/useFieldCodeValidate.js +21 -0
- package/dist/esm/components/FieldCode/index.d.ts +1 -0
- package/dist/esm/components/FieldCode/index.js +1 -0
- package/dist/esm/components/FieldCode/styles.module.css +1 -1
- package/dist/tsconfig.cjs.tsbuildinfo +1 -1
- package/dist/tsconfig.esm.tsbuildinfo +1 -1
- package/package.json +2 -2
- package/src/components/FieldCode/FieldCode.tsx +7 -21
- package/src/components/FieldCode/hooks/index.ts +1 -1
- package/src/components/FieldCode/hooks/useFieldCodeValidate.ts +35 -0
- package/src/components/FieldCode/index.ts +1 -0
- package/src/components/FieldCode/styles.module.scss +1 -1
- package/dist/cjs/components/FieldCode/hooks/useFieldCodeError.d.ts +0 -14
- package/dist/cjs/components/FieldCode/hooks/useFieldCodeError.js +0 -26
- package/dist/esm/components/FieldCode/hooks/useFieldCodeError.d.ts +0 -14
- package/dist/esm/components/FieldCode/hooks/useFieldCodeError.js +0 -23
- package/src/components/FieldCode/hooks/useFieldCodeError.ts +0 -44
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@cloud-ru/uikit-product-fields-predefined",
|
|
3
3
|
"title": "Fields Predefined",
|
|
4
|
-
"version": "2.4.8-preview-
|
|
4
|
+
"version": "2.4.8-preview-5e39e65e.0",
|
|
5
5
|
"sideEffects": [
|
|
6
6
|
"*.css",
|
|
7
7
|
"*.woff",
|
|
@@ -65,5 +65,5 @@
|
|
|
65
65
|
"react-hook-form": ">=7.51.0",
|
|
66
66
|
"yup": ">=0.32.0"
|
|
67
67
|
},
|
|
68
|
-
"gitHead": "
|
|
68
|
+
"gitHead": "0861ace3fef6d7e3bfecb3720061a8734cd30e47"
|
|
69
69
|
}
|
|
@@ -5,7 +5,7 @@ import { FieldDecorator, FieldDecoratorProps } from '@snack-uikit/fields';
|
|
|
5
5
|
|
|
6
6
|
import { Cell, ResendCode, type ResendCodeProps } from './components';
|
|
7
7
|
import { FIELD_CODE_DEFAULT_FOCUS_EFFECTS, type FieldCodeFocusEffect } from './constants';
|
|
8
|
-
import { useCodeInput, UseCodeInputParams,
|
|
8
|
+
import { useCodeInput, UseCodeInputParams, useFieldHelpers, useFocusCell } from './hooks';
|
|
9
9
|
import styles from './styles.module.scss';
|
|
10
10
|
import { getCellValidationState } from './utils';
|
|
11
11
|
|
|
@@ -26,19 +26,14 @@ export type FieldCodeOwnProps = {
|
|
|
26
26
|
cellClassName?: string;
|
|
27
27
|
/** Позиции, после которых нужно вставить пробел (индексы символов, после которых будет пробел) */
|
|
28
28
|
spacing?: number[];
|
|
29
|
-
/**
|
|
30
|
-
* Подсветить пустые символы кода.
|
|
31
|
-
* Если не задано, совпадает с `validate`.
|
|
32
|
-
*/
|
|
29
|
+
/** Подсветить пустые символы кода */
|
|
33
30
|
showEmptyChars?: boolean;
|
|
34
31
|
/** Компонент отправки нового кода */
|
|
35
32
|
resendCode?: ResendCodeProps;
|
|
36
33
|
/** Сценарии автофокуса; по умолчанию — первая ячейка при монтировании и после сброса (см. `FieldCodeFocusEffect`) */
|
|
37
34
|
focusEffects?: readonly FieldCodeFocusEffect[];
|
|
38
|
-
/**
|
|
39
|
-
|
|
40
|
-
/** Показать сообщение «неверный код» (учитывается, если нет своего `error` и нет ошибки валидации) */
|
|
41
|
-
invalidCode?: boolean;
|
|
35
|
+
/** Сообщение при неверном коде, если не передан свой `error` */
|
|
36
|
+
invalidCode?: string;
|
|
42
37
|
};
|
|
43
38
|
|
|
44
39
|
export type FieldCodeProps = FieldCodeOwnProps &
|
|
@@ -58,16 +53,13 @@ export const FieldCode = forwardRef<FieldCodeRef, FieldCodeProps>(function Field
|
|
|
58
53
|
disabled,
|
|
59
54
|
label,
|
|
60
55
|
error,
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
showEmptyChars: showEmptyCharsProp,
|
|
56
|
+
invalidCode,
|
|
57
|
+
showEmptyChars,
|
|
64
58
|
resendCode,
|
|
65
59
|
focusEffects = FIELD_CODE_DEFAULT_FOCUS_EFFECTS,
|
|
66
60
|
'data-test-id': dataTestId,
|
|
67
61
|
} = props;
|
|
68
62
|
|
|
69
|
-
const showEmptyChars = showEmptyCharsProp ?? validate;
|
|
70
|
-
|
|
71
63
|
const { inputsRef, moveFocus, blurFields } = useFocusCell(codeLength);
|
|
72
64
|
const { code, cellHandlers, onChangeCode } = useCodeInput({ value, onChange, codeLength, moveFocus, onComplete });
|
|
73
65
|
const { resetCode } = useFieldHelpers({
|
|
@@ -89,13 +81,7 @@ export const FieldCode = forwardRef<FieldCodeRef, FieldCodeProps>(function Field
|
|
|
89
81
|
[moveFocus, blurFields, resetCode],
|
|
90
82
|
);
|
|
91
83
|
|
|
92
|
-
const resolvedError =
|
|
93
|
-
validate,
|
|
94
|
-
value,
|
|
95
|
-
codeLength,
|
|
96
|
-
invalidCode,
|
|
97
|
-
error,
|
|
98
|
-
});
|
|
84
|
+
const resolvedError = error ?? invalidCode;
|
|
99
85
|
|
|
100
86
|
const resolvedDecoratorProps = {
|
|
101
87
|
label,
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
import { useCallback } from 'react';
|
|
2
|
+
|
|
3
|
+
import { useLocale } from '@cloud-ru/uikit-product-locale';
|
|
4
|
+
|
|
5
|
+
import { isNumberChar } from '../utils';
|
|
6
|
+
|
|
7
|
+
export type UseFieldCodeValidateParams = {
|
|
8
|
+
/** Ожидаемая длина кода (цифр) */
|
|
9
|
+
codeLength: number;
|
|
10
|
+
};
|
|
11
|
+
|
|
12
|
+
/**
|
|
13
|
+
* Возвращает функцию валидации значения кода (пусто / неполный код).
|
|
14
|
+
*/
|
|
15
|
+
export function useFieldCodeValidate(params: UseFieldCodeValidateParams) {
|
|
16
|
+
const { codeLength } = params;
|
|
17
|
+
const { t } = useLocale('FieldsPredefined');
|
|
18
|
+
|
|
19
|
+
return useCallback(
|
|
20
|
+
(value?: string | number) => {
|
|
21
|
+
const str = value != null ? String(value) : '';
|
|
22
|
+
const digits = str.split('').filter(isNumberChar).join('');
|
|
23
|
+
if (digits.length === 0) {
|
|
24
|
+
return t('FieldCode.required');
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
if (digits.length < codeLength) {
|
|
28
|
+
return t('FieldCode.minLength', { count: codeLength });
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
return undefined;
|
|
32
|
+
},
|
|
33
|
+
[codeLength, t],
|
|
34
|
+
);
|
|
35
|
+
}
|
|
@@ -1,14 +0,0 @@
|
|
|
1
|
-
/** Параметры хука разрешения текста ошибки: `error` > валидация (`validate`) > `invalidCode`. */
|
|
2
|
-
export type UseFieldCodeErrorParams = {
|
|
3
|
-
/** Включить встроенную валидацию (пустое значение и неполный код) */
|
|
4
|
-
validate?: boolean;
|
|
5
|
-
/** Значение кода */
|
|
6
|
-
value?: string;
|
|
7
|
-
/** Ожидаемая длина кода */
|
|
8
|
-
codeLength: number;
|
|
9
|
-
/** Показать сообщение о неверном коде */
|
|
10
|
-
invalidCode?: boolean;
|
|
11
|
-
/** Ошибка снаружи */
|
|
12
|
-
error?: string;
|
|
13
|
-
};
|
|
14
|
-
export declare function useFieldCodeError(params: UseFieldCodeErrorParams): string | undefined;
|
|
@@ -1,26 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.useFieldCodeError = useFieldCodeError;
|
|
4
|
-
const react_1 = require("react");
|
|
5
|
-
const uikit_product_locale_1 = require("@cloud-ru/uikit-product-locale");
|
|
6
|
-
const utils_1 = require("../utils");
|
|
7
|
-
function useFieldCodeError(params) {
|
|
8
|
-
var _a;
|
|
9
|
-
const { validate = false, value, codeLength, invalidCode = false, error } = params;
|
|
10
|
-
const { t } = (0, uikit_product_locale_1.useLocale)('FieldsPredefined');
|
|
11
|
-
const validationMessage = (0, react_1.useMemo)(() => {
|
|
12
|
-
if (!validate) {
|
|
13
|
-
return undefined;
|
|
14
|
-
}
|
|
15
|
-
const digits = (value !== null && value !== void 0 ? value : '').split('').filter(utils_1.isNumberChar).join('');
|
|
16
|
-
if (digits.length === 0) {
|
|
17
|
-
return t('FieldCode.required');
|
|
18
|
-
}
|
|
19
|
-
if (digits.length < codeLength) {
|
|
20
|
-
return t('FieldCode.minLength', { count: codeLength });
|
|
21
|
-
}
|
|
22
|
-
return undefined;
|
|
23
|
-
}, [codeLength, t, validate, value]);
|
|
24
|
-
const invalidCodeMessage = invalidCode ? t('FieldCode.invalidCode') : undefined;
|
|
25
|
-
return (_a = error !== null && error !== void 0 ? error : validationMessage) !== null && _a !== void 0 ? _a : invalidCodeMessage;
|
|
26
|
-
}
|
|
@@ -1,14 +0,0 @@
|
|
|
1
|
-
/** Параметры хука разрешения текста ошибки: `error` > валидация (`validate`) > `invalidCode`. */
|
|
2
|
-
export type UseFieldCodeErrorParams = {
|
|
3
|
-
/** Включить встроенную валидацию (пустое значение и неполный код) */
|
|
4
|
-
validate?: boolean;
|
|
5
|
-
/** Значение кода */
|
|
6
|
-
value?: string;
|
|
7
|
-
/** Ожидаемая длина кода */
|
|
8
|
-
codeLength: number;
|
|
9
|
-
/** Показать сообщение о неверном коде */
|
|
10
|
-
invalidCode?: boolean;
|
|
11
|
-
/** Ошибка снаружи */
|
|
12
|
-
error?: string;
|
|
13
|
-
};
|
|
14
|
-
export declare function useFieldCodeError(params: UseFieldCodeErrorParams): string | undefined;
|
|
@@ -1,23 +0,0 @@
|
|
|
1
|
-
import { useMemo } from 'react';
|
|
2
|
-
import { useLocale } from '@cloud-ru/uikit-product-locale';
|
|
3
|
-
import { isNumberChar } from '../utils';
|
|
4
|
-
export function useFieldCodeError(params) {
|
|
5
|
-
var _a;
|
|
6
|
-
const { validate = false, value, codeLength, invalidCode = false, error } = params;
|
|
7
|
-
const { t } = useLocale('FieldsPredefined');
|
|
8
|
-
const validationMessage = useMemo(() => {
|
|
9
|
-
if (!validate) {
|
|
10
|
-
return undefined;
|
|
11
|
-
}
|
|
12
|
-
const digits = (value !== null && value !== void 0 ? value : '').split('').filter(isNumberChar).join('');
|
|
13
|
-
if (digits.length === 0) {
|
|
14
|
-
return t('FieldCode.required');
|
|
15
|
-
}
|
|
16
|
-
if (digits.length < codeLength) {
|
|
17
|
-
return t('FieldCode.minLength', { count: codeLength });
|
|
18
|
-
}
|
|
19
|
-
return undefined;
|
|
20
|
-
}, [codeLength, t, validate, value]);
|
|
21
|
-
const invalidCodeMessage = invalidCode ? t('FieldCode.invalidCode') : undefined;
|
|
22
|
-
return (_a = error !== null && error !== void 0 ? error : validationMessage) !== null && _a !== void 0 ? _a : invalidCodeMessage;
|
|
23
|
-
}
|
|
@@ -1,44 +0,0 @@
|
|
|
1
|
-
import { useMemo } from 'react';
|
|
2
|
-
|
|
3
|
-
import { useLocale } from '@cloud-ru/uikit-product-locale';
|
|
4
|
-
|
|
5
|
-
import { isNumberChar } from '../utils';
|
|
6
|
-
|
|
7
|
-
/** Параметры хука разрешения текста ошибки: `error` > валидация (`validate`) > `invalidCode`. */
|
|
8
|
-
export type UseFieldCodeErrorParams = {
|
|
9
|
-
/** Включить встроенную валидацию (пустое значение и неполный код) */
|
|
10
|
-
validate?: boolean;
|
|
11
|
-
/** Значение кода */
|
|
12
|
-
value?: string;
|
|
13
|
-
/** Ожидаемая длина кода */
|
|
14
|
-
codeLength: number;
|
|
15
|
-
/** Показать сообщение о неверном коде */
|
|
16
|
-
invalidCode?: boolean;
|
|
17
|
-
/** Ошибка снаружи */
|
|
18
|
-
error?: string;
|
|
19
|
-
};
|
|
20
|
-
|
|
21
|
-
export function useFieldCodeError(params: UseFieldCodeErrorParams): string | undefined {
|
|
22
|
-
const { validate = false, value, codeLength, invalidCode = false, error } = params;
|
|
23
|
-
const { t } = useLocale('FieldsPredefined');
|
|
24
|
-
|
|
25
|
-
const validationMessage = useMemo(() => {
|
|
26
|
-
if (!validate) {
|
|
27
|
-
return undefined;
|
|
28
|
-
}
|
|
29
|
-
|
|
30
|
-
const digits = (value ?? '').split('').filter(isNumberChar).join('');
|
|
31
|
-
if (digits.length === 0) {
|
|
32
|
-
return t('FieldCode.required');
|
|
33
|
-
}
|
|
34
|
-
|
|
35
|
-
if (digits.length < codeLength) {
|
|
36
|
-
return t('FieldCode.minLength', { count: codeLength });
|
|
37
|
-
}
|
|
38
|
-
|
|
39
|
-
return undefined;
|
|
40
|
-
}, [codeLength, t, validate, value]);
|
|
41
|
-
|
|
42
|
-
const invalidCodeMessage = invalidCode ? t('FieldCode.invalidCode') : undefined;
|
|
43
|
-
return error ?? validationMessage ?? invalidCodeMessage;
|
|
44
|
-
}
|