@cloud-ru/uikit-product-fields-predefined 3.0.2-preview-a4523a3d.0 → 3.0.3-preview-7cd1981e.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/CHANGELOG.md +11 -0
- package/README.md +28 -0
- package/dist/cjs/components/FieldCode/FieldCode.d.ts +32 -0
- package/dist/cjs/components/FieldCode/FieldCode.js +45 -0
- package/dist/cjs/components/FieldCode/components/Cell/Cell.d.ts +7 -0
- package/dist/cjs/components/FieldCode/components/Cell/Cell.js +27 -0
- package/dist/cjs/components/FieldCode/components/Cell/index.d.ts +1 -0
- package/dist/cjs/components/FieldCode/components/Cell/index.js +17 -0
- package/dist/cjs/components/FieldCode/components/Cell/styles.module.css +19 -0
- package/dist/cjs/components/FieldCode/components/ResendCode/ResendCode.d.ts +8 -0
- package/dist/cjs/components/FieldCode/components/ResendCode/ResendCode.js +29 -0
- package/dist/cjs/components/FieldCode/components/ResendCode/index.d.ts +1 -0
- package/dist/cjs/components/FieldCode/components/ResendCode/index.js +17 -0
- package/dist/cjs/components/FieldCode/components/ResendCode/utils.d.ts +1 -0
- package/dist/cjs/components/FieldCode/components/ResendCode/utils.js +8 -0
- package/dist/cjs/components/FieldCode/components/index.d.ts +2 -0
- package/dist/cjs/components/FieldCode/components/index.js +18 -0
- package/dist/cjs/components/FieldCode/constants.d.ts +1 -0
- package/dist/cjs/components/FieldCode/constants.js +4 -0
- package/dist/cjs/components/FieldCode/hooks/index.d.ts +4 -0
- package/dist/cjs/components/FieldCode/hooks/index.js +20 -0
- package/dist/cjs/components/FieldCode/hooks/useCodeInput.d.ts +22 -0
- package/dist/cjs/components/FieldCode/hooks/useCodeInput.js +98 -0
- 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/hooks/useFieldHelpers.d.ts +12 -0
- package/dist/cjs/components/FieldCode/hooks/useFieldHelpers.js +30 -0
- package/dist/cjs/components/FieldCode/hooks/useFocusCell.d.ts +5 -0
- package/dist/cjs/components/FieldCode/hooks/useFocusCell.js +22 -0
- package/dist/cjs/components/FieldCode/index.d.ts +2 -0
- package/dist/cjs/components/FieldCode/index.js +20 -0
- package/dist/cjs/components/FieldCode/styles.module.css +37 -0
- package/dist/cjs/components/FieldCode/utils.d.ts +6 -0
- package/dist/cjs/components/FieldCode/utils.js +21 -0
- package/dist/cjs/components/index.d.ts +1 -0
- package/dist/cjs/components/index.js +1 -0
- package/dist/esm/components/FieldCode/FieldCode.d.ts +32 -0
- package/dist/esm/components/FieldCode/FieldCode.js +39 -0
- package/dist/esm/components/FieldCode/components/Cell/Cell.d.ts +7 -0
- package/dist/esm/components/FieldCode/components/Cell/Cell.js +21 -0
- package/dist/esm/components/FieldCode/components/Cell/index.d.ts +1 -0
- package/dist/esm/components/FieldCode/components/Cell/index.js +1 -0
- package/dist/esm/components/FieldCode/components/Cell/styles.module.css +19 -0
- package/dist/esm/components/FieldCode/components/ResendCode/ResendCode.d.ts +8 -0
- package/dist/esm/components/FieldCode/components/ResendCode/ResendCode.js +26 -0
- package/dist/esm/components/FieldCode/components/ResendCode/index.d.ts +1 -0
- package/dist/esm/components/FieldCode/components/ResendCode/index.js +1 -0
- package/dist/esm/components/FieldCode/components/ResendCode/utils.d.ts +1 -0
- package/dist/esm/components/FieldCode/components/ResendCode/utils.js +5 -0
- package/dist/esm/components/FieldCode/components/index.d.ts +2 -0
- package/dist/esm/components/FieldCode/components/index.js +2 -0
- package/dist/esm/components/FieldCode/constants.d.ts +1 -0
- package/dist/esm/components/FieldCode/constants.js +1 -0
- package/dist/esm/components/FieldCode/hooks/index.d.ts +4 -0
- package/dist/esm/components/FieldCode/hooks/index.js +4 -0
- package/dist/esm/components/FieldCode/hooks/useCodeInput.d.ts +22 -0
- package/dist/esm/components/FieldCode/hooks/useCodeInput.js +95 -0
- 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/hooks/useFieldHelpers.d.ts +12 -0
- package/dist/esm/components/FieldCode/hooks/useFieldHelpers.js +27 -0
- package/dist/esm/components/FieldCode/hooks/useFocusCell.d.ts +5 -0
- package/dist/esm/components/FieldCode/hooks/useFocusCell.js +19 -0
- package/dist/esm/components/FieldCode/index.d.ts +2 -0
- package/dist/esm/components/FieldCode/index.js +2 -0
- package/dist/esm/components/FieldCode/styles.module.css +37 -0
- package/dist/esm/components/FieldCode/utils.d.ts +6 -0
- package/dist/esm/components/FieldCode/utils.js +13 -0
- package/dist/esm/components/index.d.ts +1 -0
- package/dist/esm/components/index.js +1 -0
- package/dist/tsconfig.cjs.tsbuildinfo +1 -1
- package/dist/tsconfig.esm.tsbuildinfo +1 -1
- package/package.json +9 -6
- package/src/components/FieldCode/FieldCode.tsx +129 -0
- package/src/components/FieldCode/components/Cell/Cell.tsx +35 -0
- package/src/components/FieldCode/components/Cell/index.ts +1 -0
- package/src/components/FieldCode/components/Cell/styles.module.scss +24 -0
- package/src/components/FieldCode/components/ResendCode/ResendCode.tsx +36 -0
- package/src/components/FieldCode/components/ResendCode/index.ts +1 -0
- package/src/components/FieldCode/components/ResendCode/utils.ts +5 -0
- package/src/components/FieldCode/components/index.ts +2 -0
- package/src/components/FieldCode/constants.ts +1 -0
- package/src/components/FieldCode/hooks/index.ts +4 -0
- package/src/components/FieldCode/hooks/useCodeInput.ts +147 -0
- package/src/components/FieldCode/hooks/useFieldCodeValidate.ts +35 -0
- package/src/components/FieldCode/hooks/useFieldHelpers.ts +44 -0
- package/src/components/FieldCode/hooks/useFocusCell.ts +29 -0
- package/src/components/FieldCode/index.ts +2 -0
- package/src/components/FieldCode/styles.module.scss +42 -0
- package/src/components/FieldCode/utils.ts +23 -0
- package/src/components/index.ts +1 -0
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
import { FieldTextProps } from '@snack-uikit/fields';
|
|
2
|
+
export declare const isNumberChar: (char: string) => boolean;
|
|
3
|
+
export declare const isStringCodeLength: (input: string, codeLength: number) => boolean;
|
|
4
|
+
export declare const isZeroWidthSpace: (value: string) => value is "";
|
|
5
|
+
export declare function getFirstEmptyCellIndex(code: readonly string[]): number;
|
|
6
|
+
export declare const getCellValidationState: (value: string, showEmptyChars?: boolean, error?: boolean) => FieldTextProps["validationState"];
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.getCellValidationState = exports.isZeroWidthSpace = exports.isStringCodeLength = exports.isNumberChar = void 0;
|
|
4
|
+
exports.getFirstEmptyCellIndex = getFirstEmptyCellIndex;
|
|
5
|
+
const constants_1 = require("./constants");
|
|
6
|
+
const isNumberChar = (char) => /^\d$/.test(char);
|
|
7
|
+
exports.isNumberChar = isNumberChar;
|
|
8
|
+
const isStringCodeLength = (input, codeLength) => new RegExp(`^\\d{${codeLength}}$`).test(input);
|
|
9
|
+
exports.isStringCodeLength = isStringCodeLength;
|
|
10
|
+
const isZeroWidthSpace = (value) => value === constants_1.ZERO_WIDTH_SPACE;
|
|
11
|
+
exports.isZeroWidthSpace = isZeroWidthSpace;
|
|
12
|
+
function getFirstEmptyCellIndex(code) {
|
|
13
|
+
return code.findIndex(exports.isZeroWidthSpace);
|
|
14
|
+
}
|
|
15
|
+
const getCellValidationState = (value, showEmptyChars, error) => {
|
|
16
|
+
if (showEmptyChars) {
|
|
17
|
+
return (0, exports.isZeroWidthSpace)(value) ? 'error' : 'default';
|
|
18
|
+
}
|
|
19
|
+
return error ? 'error' : 'default';
|
|
20
|
+
};
|
|
21
|
+
exports.getCellValidationState = getCellValidationState;
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
import { FieldDecoratorProps } from '@snack-uikit/fields';
|
|
2
|
+
import { type ResendCodeProps } from './components';
|
|
3
|
+
import { UseCodeInputParams } from './hooks';
|
|
4
|
+
export type FieldCodeRef = {
|
|
5
|
+
/** Перенести фокус на ячейку с индексом `index` */
|
|
6
|
+
moveFocus: (index: number) => void;
|
|
7
|
+
/** Убрать фокус со всех ячеек кода */
|
|
8
|
+
blurFields: () => void;
|
|
9
|
+
/** Сбросить значение кода */
|
|
10
|
+
resetCode: () => void;
|
|
11
|
+
};
|
|
12
|
+
/** Собственные пропсы `FieldCode` */
|
|
13
|
+
export type FieldCodeOwnProps = {
|
|
14
|
+
/** CSS-класс компонента */
|
|
15
|
+
className?: string;
|
|
16
|
+
/** CSS-класс ячейки кода */
|
|
17
|
+
cellClassName?: string;
|
|
18
|
+
/** Позиции, после которых нужно вставить пробел (индексы символов, после которых будет пробел) */
|
|
19
|
+
spacing?: number[];
|
|
20
|
+
/** Подсветить пустые символы кода */
|
|
21
|
+
showEmptyChars?: boolean;
|
|
22
|
+
/** Компонент отправки нового кода */
|
|
23
|
+
resendCode?: ResendCodeProps;
|
|
24
|
+
/** Отключить автофокус (при монтировании, сбросе и при ошибке валидации) */
|
|
25
|
+
isMobile: boolean;
|
|
26
|
+
/** Сообщение при неверном коде, если не передан свой `error` */
|
|
27
|
+
invalidCode?: string;
|
|
28
|
+
/** Растягивать ячейки на всю доступную ширину; иначе фиксированная ширина по `size` */
|
|
29
|
+
stretchCells?: boolean;
|
|
30
|
+
};
|
|
31
|
+
export type FieldCodeProps = FieldCodeOwnProps & Omit<UseCodeInputParams, 'moveFocus'> & Pick<FieldDecoratorProps, 'size' | 'disabled' | 'label' | 'error' | 'data-test-id'>;
|
|
32
|
+
export declare const FieldCode: import("react").ForwardRefExoticComponent<FieldCodeOwnProps & Omit<UseCodeInputParams, "moveFocus"> & Pick<FieldDecoratorProps, "size" | "label" | "error" | "disabled" | "data-test-id"> & import("react").RefAttributes<FieldCodeRef>>;
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
|
|
2
|
+
import cn from 'classnames';
|
|
3
|
+
import { forwardRef, useImperativeHandle } from 'react';
|
|
4
|
+
import { FieldDecorator } from '@snack-uikit/fields';
|
|
5
|
+
import { Cell, ResendCode } from './components';
|
|
6
|
+
import { useCodeInput, useFieldHelpers, useFocusCell } from './hooks';
|
|
7
|
+
import styles from './styles.module.css';
|
|
8
|
+
import { getCellValidationState } from './utils';
|
|
9
|
+
export const FieldCode = forwardRef(function FieldCode(props, ref) {
|
|
10
|
+
var _a;
|
|
11
|
+
const { codeLength, className, cellClassName, value, onChange, spacing, onComplete, size, disabled, label, error, invalidCode, showEmptyChars, resendCode, isMobile, stretchCells = false, 'data-test-id': dataTestId, } = props;
|
|
12
|
+
const { inputsRef, moveFocus, blurFields } = useFocusCell(codeLength);
|
|
13
|
+
const { code, cellHandlers, onChangeCode } = useCodeInput({ value, onChange, codeLength, moveFocus, onComplete });
|
|
14
|
+
const { resetCode } = useFieldHelpers({
|
|
15
|
+
onChangeCode,
|
|
16
|
+
isMobile,
|
|
17
|
+
moveFocus,
|
|
18
|
+
showEmptyChars,
|
|
19
|
+
code,
|
|
20
|
+
codeLength,
|
|
21
|
+
});
|
|
22
|
+
useImperativeHandle(ref, () => ({
|
|
23
|
+
moveFocus,
|
|
24
|
+
blurFields,
|
|
25
|
+
resetCode,
|
|
26
|
+
}), [moveFocus, blurFields, resetCode]);
|
|
27
|
+
const resolvedError = error !== null && error !== void 0 ? error : invalidCode;
|
|
28
|
+
const resolvedDecoratorProps = {
|
|
29
|
+
label,
|
|
30
|
+
disabled,
|
|
31
|
+
size,
|
|
32
|
+
error: resolvedError,
|
|
33
|
+
};
|
|
34
|
+
return (_jsxs("div", Object.assign({ className: cn(styles.fieldCode, className), "data-stretch-cells": stretchCells || undefined }, (dataTestId ? { 'data-test-id': dataTestId } : undefined), { children: [_jsx(FieldDecorator, Object.assign({ className: styles.fieldDecorator }, resolvedDecoratorProps, { children: _jsx("div", { className: styles.codeContainer, "data-size": size, "data-stretch-cells": stretchCells || undefined, children: code.map((char, index) => (_jsx(Cell, { ref: inputRef => {
|
|
35
|
+
if (inputRef) {
|
|
36
|
+
inputsRef.current[index] = inputRef;
|
|
37
|
+
}
|
|
38
|
+
}, className: cn((spacing === null || spacing === void 0 ? void 0 : spacing.includes(index)) && styles.cellSpacing, cellClassName), stretchCells: stretchCells, size: size, value: char, disabled: disabled, autoComplete: index === 0 ? 'one-time-code' : undefined, onKeyDown: e => cellHandlers.onKeyDown(e, index), onPaste: cellHandlers.onPaste, onChange: e => cellHandlers.onChange(e, index), validationState: getCellValidationState(char, showEmptyChars, Boolean(resolvedError)) }, index))) }) })), resendCode ? _jsx(ResendCode, Object.assign({}, resendCode, { size: (_a = resendCode.size) !== null && _a !== void 0 ? _a : size })) : null] })));
|
|
39
|
+
});
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
import { FieldTextProps } from '@snack-uikit/fields';
|
|
2
|
+
export declare const Cell: import("react").ForwardRefExoticComponent<{
|
|
3
|
+
/** CSS-класс ячейки кода */
|
|
4
|
+
className?: string;
|
|
5
|
+
/** Растягивать ячейку на всю доступную ширину */
|
|
6
|
+
stretchCells?: boolean;
|
|
7
|
+
} & Pick<FieldTextProps, "size" | "onPaste" | "onChange" | "onKeyDown" | "value" | "disabled" | "autoComplete" | "validationState"> & import("react").RefAttributes<HTMLInputElement>>;
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
var __rest = (this && this.__rest) || function (s, e) {
|
|
2
|
+
var t = {};
|
|
3
|
+
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0)
|
|
4
|
+
t[p] = s[p];
|
|
5
|
+
if (s != null && typeof Object.getOwnPropertySymbols === "function")
|
|
6
|
+
for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) {
|
|
7
|
+
if (e.indexOf(p[i]) < 0 && Object.prototype.propertyIsEnumerable.call(s, p[i]))
|
|
8
|
+
t[p[i]] = s[p[i]];
|
|
9
|
+
}
|
|
10
|
+
return t;
|
|
11
|
+
};
|
|
12
|
+
import { jsx as _jsx } from "react/jsx-runtime";
|
|
13
|
+
import cn from 'classnames';
|
|
14
|
+
import { forwardRef } from 'react';
|
|
15
|
+
import { FieldText } from '@snack-uikit/fields';
|
|
16
|
+
import { ZERO_WIDTH_SPACE } from '../../constants';
|
|
17
|
+
import styles from './styles.module.css';
|
|
18
|
+
export const Cell = forwardRef((props, ref) => {
|
|
19
|
+
const { className, size, stretchCells, value } = props, fieldCellProps = __rest(props, ["className", "size", "stretchCells", "value"]);
|
|
20
|
+
return (_jsx(FieldText, Object.assign({ inputMode: 'numeric', ref: ref, className: cn(styles.cell, className), "data-size": size, "data-stretch-cells": stretchCells || undefined, showClearButton: false, value: value === ZERO_WIDTH_SPACE ? '' : value, size: size }, fieldCellProps)));
|
|
21
|
+
});
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from './Cell';
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from './Cell';
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
.cell{
|
|
2
|
+
flex-shrink:0;
|
|
3
|
+
}
|
|
4
|
+
.cell input{
|
|
5
|
+
text-align:center;
|
|
6
|
+
}
|
|
7
|
+
.cell[data-size=s]:not([data-stretch-cells]){
|
|
8
|
+
width:32px;
|
|
9
|
+
}
|
|
10
|
+
.cell[data-size=m]:not([data-stretch-cells]){
|
|
11
|
+
width:40px;
|
|
12
|
+
}
|
|
13
|
+
.cell[data-size=l]:not([data-stretch-cells]){
|
|
14
|
+
width:52px;
|
|
15
|
+
}
|
|
16
|
+
.cell[data-stretch-cells]{
|
|
17
|
+
flex:1 1 0;
|
|
18
|
+
min-width:0;
|
|
19
|
+
}
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
import { ButtonFunctionProps } from '@snack-uikit/button';
|
|
2
|
+
export type ResendCodeProps = {
|
|
3
|
+
/** Колбек отправки нового кода */
|
|
4
|
+
onResend: () => void;
|
|
5
|
+
/** Количество секунд до следующего отправления кода */
|
|
6
|
+
secondsToNextResend: number;
|
|
7
|
+
} & Pick<ButtonFunctionProps, 'size' | 'disabled'>;
|
|
8
|
+
export declare function ResendCode(props: ResendCodeProps): import("react/jsx-runtime").JSX.Element;
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
var __rest = (this && this.__rest) || function (s, e) {
|
|
2
|
+
var t = {};
|
|
3
|
+
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0)
|
|
4
|
+
t[p] = s[p];
|
|
5
|
+
if (s != null && typeof Object.getOwnPropertySymbols === "function")
|
|
6
|
+
for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) {
|
|
7
|
+
if (e.indexOf(p[i]) < 0 && Object.prototype.propertyIsEnumerable.call(s, p[i]))
|
|
8
|
+
t[p[i]] = s[p[i]];
|
|
9
|
+
}
|
|
10
|
+
return t;
|
|
11
|
+
};
|
|
12
|
+
import { jsx as _jsx } from "react/jsx-runtime";
|
|
13
|
+
import { UpdateSVG } from '@cloud-ru/uikit-product-icons';
|
|
14
|
+
import { useLocale } from '@cloud-ru/uikit-product-locale';
|
|
15
|
+
import { ButtonFunction } from '@snack-uikit/button';
|
|
16
|
+
import { formatSecondsAsMmSs } from './utils';
|
|
17
|
+
export function ResendCode(props) {
|
|
18
|
+
const { onResend, secondsToNextResend } = props, buttonProps = __rest(props, ["onResend", "secondsToNextResend"]);
|
|
19
|
+
const { t } = useLocale('FieldsPredefined');
|
|
20
|
+
const isResendCodeWithVia = secondsToNextResend > 0;
|
|
21
|
+
return (_jsx(ButtonFunction, Object.assign({ label: isResendCodeWithVia
|
|
22
|
+
? t('FieldCode.resendCodeButtons.resendCodeWithVia', {
|
|
23
|
+
timer: formatSecondsAsMmSs(secondsToNextResend),
|
|
24
|
+
})
|
|
25
|
+
: t('FieldCode.resendCodeButtons.resendCode'), onClick: onResend, icon: _jsx(UpdateSVG, {}), iconPosition: 'before', disabled: isResendCodeWithVia !== null && isResendCodeWithVia !== void 0 ? isResendCodeWithVia : buttonProps.disabled }, buttonProps)));
|
|
26
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from './ResendCode';
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from './ResendCode';
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export declare function formatSecondsAsMmSs(totalSeconds: number): string;
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export declare const ZERO_WIDTH_SPACE = "\u200B";
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export const ZERO_WIDTH_SPACE = '\u200B';
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
import { ClipboardEvent, KeyboardEvent } from 'react';
|
|
2
|
+
export type UseCodeInputParams = {
|
|
3
|
+
/** Количество цифр в коде (ожидается целое ≥ 1) */
|
|
4
|
+
codeLength: number;
|
|
5
|
+
/** Значение кода */
|
|
6
|
+
value?: string;
|
|
7
|
+
/** Колбек изменения значения */
|
|
8
|
+
onChange?: (code: string) => void;
|
|
9
|
+
/** Функция фокуса */
|
|
10
|
+
moveFocus: (index: number) => void;
|
|
11
|
+
/** Колбек достижения ввода всех символов кода */
|
|
12
|
+
onComplete?: (code: string) => void;
|
|
13
|
+
};
|
|
14
|
+
export declare function useCodeInput(params: UseCodeInputParams): {
|
|
15
|
+
code: string[];
|
|
16
|
+
cellHandlers: {
|
|
17
|
+
onKeyDown: (e: KeyboardEvent<HTMLInputElement>, index: number) => void;
|
|
18
|
+
onPaste: (e: ClipboardEvent<HTMLInputElement>) => void;
|
|
19
|
+
onChange: (code: string, index: number) => void;
|
|
20
|
+
};
|
|
21
|
+
onChangeCode: (value: any, ...args: any[]) => any | void;
|
|
22
|
+
};
|
|
@@ -0,0 +1,95 @@
|
|
|
1
|
+
import { useRefState } from '@siberiacancode/reactuse';
|
|
2
|
+
import { useCallback, useEffect } from 'react';
|
|
3
|
+
import { useValueControl } from '@snack-uikit/utils';
|
|
4
|
+
import { ZERO_WIDTH_SPACE } from '../constants';
|
|
5
|
+
import { isNumberChar, isStringCodeLength, isZeroWidthSpace } from '../utils';
|
|
6
|
+
const buildCodeArray = (str, codeLength) => Array.from({ length: codeLength }, (_, idx) => str[idx] || ZERO_WIDTH_SPACE);
|
|
7
|
+
export function useCodeInput(params) {
|
|
8
|
+
const { value: valueProp, onChange: onChangeProp, codeLength, moveFocus, onComplete } = params;
|
|
9
|
+
const [value = '', onChange] = useValueControl({
|
|
10
|
+
value: valueProp,
|
|
11
|
+
onChange: onChangeProp,
|
|
12
|
+
defaultValue: '',
|
|
13
|
+
});
|
|
14
|
+
const codeRef = useRefState(buildCodeArray(value, codeLength));
|
|
15
|
+
const updateCodeByIndex = useCallback((index, newChar) => {
|
|
16
|
+
codeRef.current[index] = newChar;
|
|
17
|
+
onChange === null || onChange === void 0 ? void 0 : onChange(codeRef.current.join(''));
|
|
18
|
+
}, [codeRef, onChange]);
|
|
19
|
+
const updateFullCode = useCallback((newCode) => {
|
|
20
|
+
codeRef.current = newCode.split('');
|
|
21
|
+
onChange === null || onChange === void 0 ? void 0 : onChange(newCode);
|
|
22
|
+
moveFocus(codeLength - 1);
|
|
23
|
+
onComplete === null || onComplete === void 0 ? void 0 : onComplete(newCode);
|
|
24
|
+
}, [codeLength, codeRef, moveFocus, onChange, onComplete]);
|
|
25
|
+
const handleAfterCellUpdate = useCallback((index) => {
|
|
26
|
+
const normalizedCode = codeRef.current.join('');
|
|
27
|
+
const isLastInput = index === codeLength - 1;
|
|
28
|
+
const isAllInputsFilled = isStringCodeLength(normalizedCode, codeLength);
|
|
29
|
+
if (!isLastInput) {
|
|
30
|
+
moveFocus(index + 1);
|
|
31
|
+
}
|
|
32
|
+
else if (isAllInputsFilled) {
|
|
33
|
+
onComplete === null || onComplete === void 0 ? void 0 : onComplete(normalizedCode);
|
|
34
|
+
}
|
|
35
|
+
}, [codeLength, codeRef, moveFocus, onComplete]);
|
|
36
|
+
const deleteChar = useCallback((index) => {
|
|
37
|
+
if (codeRef.current[index] && !isZeroWidthSpace(codeRef.current[index])) {
|
|
38
|
+
updateCodeByIndex(index, ZERO_WIDTH_SPACE);
|
|
39
|
+
}
|
|
40
|
+
else if (index > 0) {
|
|
41
|
+
moveFocus(index - 1);
|
|
42
|
+
}
|
|
43
|
+
}, [codeRef, moveFocus, updateCodeByIndex]);
|
|
44
|
+
const onAutoCompleteInput = useCallback((code, index) => {
|
|
45
|
+
if (isStringCodeLength(code, codeLength)) {
|
|
46
|
+
updateFullCode(code);
|
|
47
|
+
return;
|
|
48
|
+
}
|
|
49
|
+
if (!isNumberChar(code)) {
|
|
50
|
+
return;
|
|
51
|
+
}
|
|
52
|
+
updateCodeByIndex(index, code);
|
|
53
|
+
handleAfterCellUpdate(index);
|
|
54
|
+
}, [codeLength, handleAfterCellUpdate, updateCodeByIndex, updateFullCode]);
|
|
55
|
+
const onKeyDown = useCallback((e, index) => {
|
|
56
|
+
switch (e.key) {
|
|
57
|
+
case 'ArrowLeft':
|
|
58
|
+
moveFocus(index - 1);
|
|
59
|
+
break;
|
|
60
|
+
case 'ArrowRight':
|
|
61
|
+
moveFocus(index + 1);
|
|
62
|
+
break;
|
|
63
|
+
case 'Backspace':
|
|
64
|
+
deleteChar(index);
|
|
65
|
+
break;
|
|
66
|
+
default:
|
|
67
|
+
if (isNumberChar(e.key)) {
|
|
68
|
+
e.preventDefault();
|
|
69
|
+
updateCodeByIndex(index, e.key);
|
|
70
|
+
handleAfterCellUpdate(index);
|
|
71
|
+
}
|
|
72
|
+
break;
|
|
73
|
+
}
|
|
74
|
+
}, [deleteChar, handleAfterCellUpdate, moveFocus, updateCodeByIndex]);
|
|
75
|
+
const onPaste = useCallback((e) => {
|
|
76
|
+
var _a;
|
|
77
|
+
const codeInput = (_a = e === null || e === void 0 ? void 0 : e.clipboardData.getData('text')) !== null && _a !== void 0 ? _a : '';
|
|
78
|
+
if (!isStringCodeLength(codeInput, codeLength)) {
|
|
79
|
+
return;
|
|
80
|
+
}
|
|
81
|
+
updateFullCode(codeInput);
|
|
82
|
+
}, [codeLength, updateFullCode]);
|
|
83
|
+
useEffect(() => {
|
|
84
|
+
codeRef.current = buildCodeArray(value, codeLength);
|
|
85
|
+
}, [codeLength, codeRef, value]);
|
|
86
|
+
return {
|
|
87
|
+
code: codeRef.current,
|
|
88
|
+
cellHandlers: {
|
|
89
|
+
onKeyDown,
|
|
90
|
+
onPaste,
|
|
91
|
+
onChange: onAutoCompleteInput,
|
|
92
|
+
},
|
|
93
|
+
onChangeCode: onChange,
|
|
94
|
+
};
|
|
95
|
+
}
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
export type UseFieldCodeValidateParams = {
|
|
2
|
+
/** Ожидаемая длина кода (цифр) */
|
|
3
|
+
codeLength: number;
|
|
4
|
+
};
|
|
5
|
+
/**
|
|
6
|
+
* Возвращает функцию валидации значения кода (пусто / неполный код).
|
|
7
|
+
*/
|
|
8
|
+
export declare function useFieldCodeValidate(params: UseFieldCodeValidateParams): (value?: string | number) => string | undefined;
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
import { useCallback } from 'react';
|
|
2
|
+
import { useLocale } from '@cloud-ru/uikit-product-locale';
|
|
3
|
+
import { isNumberChar } from '../utils';
|
|
4
|
+
/**
|
|
5
|
+
* Возвращает функцию валидации значения кода (пусто / неполный код).
|
|
6
|
+
*/
|
|
7
|
+
export function useFieldCodeValidate(params) {
|
|
8
|
+
const { codeLength } = params;
|
|
9
|
+
const { t } = useLocale('FieldsPredefined');
|
|
10
|
+
return useCallback((value) => {
|
|
11
|
+
const str = value != null ? String(value) : '';
|
|
12
|
+
const digits = str.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]);
|
|
21
|
+
}
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
type UseFieldHelpersParams = {
|
|
2
|
+
onChangeCode: (code: string) => void;
|
|
3
|
+
moveFocus: (index: number) => void;
|
|
4
|
+
isMobile?: boolean;
|
|
5
|
+
showEmptyChars?: boolean;
|
|
6
|
+
code: readonly string[];
|
|
7
|
+
codeLength: number;
|
|
8
|
+
};
|
|
9
|
+
export declare function useFieldHelpers(params: UseFieldHelpersParams): {
|
|
10
|
+
resetCode: () => void;
|
|
11
|
+
};
|
|
12
|
+
export {};
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
import { useCallback, useEffect } from 'react';
|
|
2
|
+
import { getFirstEmptyCellIndex } from '../utils';
|
|
3
|
+
export function useFieldHelpers(params) {
|
|
4
|
+
const { onChangeCode, moveFocus, isMobile = false, showEmptyChars, code, codeLength } = params;
|
|
5
|
+
const resetCode = useCallback(() => {
|
|
6
|
+
onChangeCode('');
|
|
7
|
+
if (!isMobile) {
|
|
8
|
+
moveFocus(0);
|
|
9
|
+
}
|
|
10
|
+
}, [isMobile, moveFocus, onChangeCode]);
|
|
11
|
+
useEffect(() => {
|
|
12
|
+
if (!isMobile) {
|
|
13
|
+
moveFocus(0);
|
|
14
|
+
}
|
|
15
|
+
}, [isMobile, moveFocus]);
|
|
16
|
+
useEffect(() => {
|
|
17
|
+
if (isMobile || !showEmptyChars) {
|
|
18
|
+
return;
|
|
19
|
+
}
|
|
20
|
+
const emptyIndex = getFirstEmptyCellIndex(code);
|
|
21
|
+
if (emptyIndex >= 0) {
|
|
22
|
+
moveFocus(emptyIndex);
|
|
23
|
+
}
|
|
24
|
+
// eslint-disable-next-line react-hooks/exhaustive-deps
|
|
25
|
+
}, [showEmptyChars, isMobile, moveFocus, codeLength]);
|
|
26
|
+
return { resetCode };
|
|
27
|
+
}
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
import { useCallback, useRef } from 'react';
|
|
2
|
+
export function useFocusCell(codeLength) {
|
|
3
|
+
const inputsRef = useRef([]);
|
|
4
|
+
const focusInput = useCallback((index) => {
|
|
5
|
+
var _a;
|
|
6
|
+
(_a = inputsRef.current[index]) === null || _a === void 0 ? void 0 : _a.focus();
|
|
7
|
+
}, [inputsRef]);
|
|
8
|
+
const moveFocus = useCallback((newIndex) => {
|
|
9
|
+
if (newIndex >= 0 && newIndex < codeLength) {
|
|
10
|
+
focusInput(newIndex);
|
|
11
|
+
}
|
|
12
|
+
}, [codeLength, focusInput]);
|
|
13
|
+
const blurFields = useCallback(() => {
|
|
14
|
+
inputsRef.current.forEach(input => {
|
|
15
|
+
input === null || input === void 0 ? void 0 : input.blur();
|
|
16
|
+
});
|
|
17
|
+
}, [inputsRef]);
|
|
18
|
+
return { inputsRef, moveFocus, blurFields };
|
|
19
|
+
}
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
.fieldCode{
|
|
2
|
+
display:flex;
|
|
3
|
+
flex-direction:column;
|
|
4
|
+
align-items:center;
|
|
5
|
+
gap:8px;
|
|
6
|
+
}
|
|
7
|
+
.fieldCode[data-stretch-cells]{
|
|
8
|
+
width:100%;
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
.fieldDecorator{
|
|
12
|
+
width:unset;
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
.codeContainer{
|
|
16
|
+
display:flex;
|
|
17
|
+
justify-content:center;
|
|
18
|
+
}
|
|
19
|
+
.codeContainer[data-size=s]{
|
|
20
|
+
gap:8px;
|
|
21
|
+
}
|
|
22
|
+
.codeContainer[data-size=m]{
|
|
23
|
+
gap:8px;
|
|
24
|
+
}
|
|
25
|
+
.codeContainer[data-size=l]{
|
|
26
|
+
gap:12px;
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
.cellSpacing[data-size=s]{
|
|
30
|
+
margin-right:8px;
|
|
31
|
+
}
|
|
32
|
+
.cellSpacing[data-size=m]{
|
|
33
|
+
margin-right:8px;
|
|
34
|
+
}
|
|
35
|
+
.cellSpacing[data-size=l]{
|
|
36
|
+
margin-right:12px;
|
|
37
|
+
}
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
import { FieldTextProps } from '@snack-uikit/fields';
|
|
2
|
+
export declare const isNumberChar: (char: string) => boolean;
|
|
3
|
+
export declare const isStringCodeLength: (input: string, codeLength: number) => boolean;
|
|
4
|
+
export declare const isZeroWidthSpace: (value: string) => value is "";
|
|
5
|
+
export declare function getFirstEmptyCellIndex(code: readonly string[]): number;
|
|
6
|
+
export declare const getCellValidationState: (value: string, showEmptyChars?: boolean, error?: boolean) => FieldTextProps["validationState"];
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import { ZERO_WIDTH_SPACE } from './constants';
|
|
2
|
+
export const isNumberChar = (char) => /^\d$/.test(char);
|
|
3
|
+
export const isStringCodeLength = (input, codeLength) => new RegExp(`^\\d{${codeLength}}$`).test(input);
|
|
4
|
+
export const isZeroWidthSpace = (value) => value === ZERO_WIDTH_SPACE;
|
|
5
|
+
export function getFirstEmptyCellIndex(code) {
|
|
6
|
+
return code.findIndex(isZeroWidthSpace);
|
|
7
|
+
}
|
|
8
|
+
export const getCellValidationState = (value, showEmptyChars, error) => {
|
|
9
|
+
if (showEmptyChars) {
|
|
10
|
+
return isZeroWidthSpace(value) ? 'error' : 'default';
|
|
11
|
+
}
|
|
12
|
+
return error ? 'error' : 'default';
|
|
13
|
+
};
|