@ozen-ui/kit 0.63.1 → 0.64.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.
Files changed (79) hide show
  1. package/InputOTP/package.json +5 -0
  2. package/__inner__/cjs/components/InputOTP/InputOTP.css +60 -0
  3. package/__inner__/cjs/components/InputOTP/InputOTP.d.ts +4 -0
  4. package/__inner__/cjs/components/InputOTP/InputOTP.js +179 -0
  5. package/__inner__/cjs/components/InputOTP/constants.d.ts +8 -0
  6. package/__inner__/cjs/components/InputOTP/constants.js +13 -0
  7. package/__inner__/cjs/components/InputOTP/helper.d.ts +13 -0
  8. package/__inner__/cjs/components/InputOTP/helper.js +87 -0
  9. package/__inner__/cjs/components/InputOTP/hooks/index.d.ts +1 -0
  10. package/__inner__/cjs/components/InputOTP/hooks/index.js +4 -0
  11. package/__inner__/cjs/components/InputOTP/hooks/useInputOTPEvents.d.ts +20 -0
  12. package/__inner__/cjs/components/InputOTP/hooks/useInputOTPEvents.js +183 -0
  13. package/__inner__/cjs/components/InputOTP/index.d.ts +3 -0
  14. package/__inner__/cjs/components/InputOTP/index.js +6 -0
  15. package/__inner__/cjs/components/InputOTP/presets.d.ts +12 -0
  16. package/__inner__/cjs/components/InputOTP/presets.js +27 -0
  17. package/__inner__/cjs/components/InputOTP/types.d.ts +101 -0
  18. package/__inner__/cjs/components/InputOTP/types.js +7 -0
  19. package/__inner__/cjs/components/ThemeProvider/types.d.ts +1 -0
  20. package/__inner__/cjs/hooks/useStoredValue/index.d.ts +3 -0
  21. package/__inner__/cjs/hooks/useStoredValue/index.js +6 -0
  22. package/__inner__/cjs/hooks/useStoredValue/types.d.ts +2 -0
  23. package/__inner__/cjs/hooks/useStoredValue/types.js +2 -0
  24. package/__inner__/cjs/hooks/useStoredValue/useStoredValue.d.ts +4 -0
  25. package/__inner__/cjs/hooks/useStoredValue/useStoredValue.js +16 -0
  26. package/__inner__/cjs/hooks/useStoredValue/utils.d.ts +2 -0
  27. package/__inner__/cjs/hooks/useStoredValue/utils.js +8 -0
  28. package/__inner__/cjs/utils/array/create-empty/createEmptyArray.d.ts +1 -0
  29. package/__inner__/cjs/utils/array/create-empty/createEmptyArray.js +8 -0
  30. package/__inner__/cjs/utils/array/create-empty/index.d.ts +1 -0
  31. package/__inner__/cjs/utils/array/create-empty/index.js +4 -0
  32. package/__inner__/cjs/utils/array/create-filled/createFilledArray.d.ts +1 -0
  33. package/__inner__/cjs/utils/array/create-filled/createFilledArray.js +7 -0
  34. package/__inner__/cjs/utils/array/create-filled/index.d.ts +1 -0
  35. package/__inner__/cjs/utils/array/create-filled/index.js +4 -0
  36. package/__inner__/cjs/utils/array/index.d.ts +2 -0
  37. package/__inner__/cjs/utils/array/index.js +5 -0
  38. package/__inner__/cjs/utils/isFunction.d.ts +1 -0
  39. package/__inner__/cjs/utils/isFunction.js +8 -0
  40. package/__inner__/esm/components/InputOTP/InputOTP.css +60 -0
  41. package/__inner__/esm/components/InputOTP/InputOTP.d.ts +4 -0
  42. package/__inner__/esm/components/InputOTP/InputOTP.js +176 -0
  43. package/__inner__/esm/components/InputOTP/constants.d.ts +8 -0
  44. package/__inner__/esm/components/InputOTP/constants.js +10 -0
  45. package/__inner__/esm/components/InputOTP/helper.d.ts +13 -0
  46. package/__inner__/esm/components/InputOTP/helper.js +79 -0
  47. package/__inner__/esm/components/InputOTP/hooks/index.d.ts +1 -0
  48. package/__inner__/esm/components/InputOTP/hooks/index.js +1 -0
  49. package/__inner__/esm/components/InputOTP/hooks/useInputOTPEvents.d.ts +20 -0
  50. package/__inner__/esm/components/InputOTP/hooks/useInputOTPEvents.js +179 -0
  51. package/__inner__/esm/components/InputOTP/index.d.ts +3 -0
  52. package/__inner__/esm/components/InputOTP/index.js +3 -0
  53. package/__inner__/esm/components/InputOTP/presets.d.ts +12 -0
  54. package/__inner__/esm/components/InputOTP/presets.js +24 -0
  55. package/__inner__/esm/components/InputOTP/types.d.ts +101 -0
  56. package/__inner__/esm/components/InputOTP/types.js +4 -0
  57. package/__inner__/esm/components/ThemeProvider/types.d.ts +1 -0
  58. package/__inner__/esm/hooks/useStoredValue/index.d.ts +3 -0
  59. package/__inner__/esm/hooks/useStoredValue/index.js +3 -0
  60. package/__inner__/esm/hooks/useStoredValue/types.d.ts +2 -0
  61. package/__inner__/esm/hooks/useStoredValue/types.js +1 -0
  62. package/__inner__/esm/hooks/useStoredValue/useStoredValue.d.ts +4 -0
  63. package/__inner__/esm/hooks/useStoredValue/useStoredValue.js +13 -0
  64. package/__inner__/esm/hooks/useStoredValue/utils.d.ts +2 -0
  65. package/__inner__/esm/hooks/useStoredValue/utils.js +5 -0
  66. package/__inner__/esm/utils/array/create-empty/createEmptyArray.d.ts +1 -0
  67. package/__inner__/esm/utils/array/create-empty/createEmptyArray.js +4 -0
  68. package/__inner__/esm/utils/array/create-empty/index.d.ts +1 -0
  69. package/__inner__/esm/utils/array/create-empty/index.js +1 -0
  70. package/__inner__/esm/utils/array/create-filled/createFilledArray.d.ts +1 -0
  71. package/__inner__/esm/utils/array/create-filled/createFilledArray.js +3 -0
  72. package/__inner__/esm/utils/array/create-filled/index.d.ts +1 -0
  73. package/__inner__/esm/utils/array/create-filled/index.js +1 -0
  74. package/__inner__/esm/utils/array/index.d.ts +2 -0
  75. package/__inner__/esm/utils/array/index.js +2 -0
  76. package/__inner__/esm/utils/isFunction.d.ts +1 -0
  77. package/__inner__/esm/utils/isFunction.js +4 -0
  78. package/package.json +4 -4
  79. package/useStoredValue/package.json +5 -0
@@ -0,0 +1,5 @@
1
+ {
2
+ "main": "../__inner__/cjs/components/InputOTP/index.js",
3
+ "module": "../__inner__/esm/components/InputOTP/index.js",
4
+ "types": "../__inner__/esm/components/InputOTP/index.d.ts"
5
+ }
@@ -0,0 +1,60 @@
1
+ /* stylelint-disable */
2
+ .InputOTP-Field {
3
+ position: relative;
4
+ }
5
+ .InputOTP-Field_notAvailable {
6
+ pointer-events: none;
7
+ }
8
+ .InputOTP .FieldControl_error:not(.FieldControl_disabled) {
9
+ --textfield-color: var(--color-content-error);
10
+ --textfield-background-color: var(--color-background-primary);
11
+ }
12
+ .InputOTP:not(.InputOTP_disabled) .InputOTP-InputContainer {
13
+ cursor: pointer;
14
+ }
15
+ .InputOTP_size_s {
16
+ --otp-input-inline-size: 40px;
17
+ }
18
+ .InputOTP_size_s .InputOTP-Input {
19
+ font: var(--typography-text-s-font);
20
+ letter-spacing: var(--typography-text-s-letter_spacing, 0);
21
+ text-transform: var(--typography-text-s-text_transform, none);
22
+ }
23
+ .InputOTP_size_m {
24
+ --otp-input-inline-size: 44px;
25
+ }
26
+ .InputOTP_size_m .InputOTP-Input {
27
+ font: var(--typography-text-l-font);
28
+ letter-spacing: var(--typography-text-l-letter_spacing, 0);
29
+ text-transform: var(--typography-text-l-text_transform, none);
30
+ }
31
+ .InputOTP-Input {
32
+ box-sizing: border-box;
33
+ padding: var(--textfield-input-padding);
34
+ background-color: var(--textfield-background-color);
35
+ transition: background-color box-shadow var(--transition-slow);
36
+ color: var(--textfield-color);
37
+ border-radius: var(--textfield-border-radius);
38
+ block-size: var(--textfield-input-height);
39
+ text-align: center;
40
+ border: none;
41
+ outline: none;
42
+ display: block;
43
+ inline-size: var(--otp-input-inline-size);
44
+ }
45
+ .InputOTP-Input:-webkit-autofill,
46
+ .InputOTP-Input:-webkit-autofill:hover,
47
+ .InputOTP-Input:-webkit-autofill:focus {
48
+ box-shadow: 0 0 0 1000px var(--textfield-background-color) inset !important;
49
+ background-color: transparent !important;
50
+ -webkit-text-fill-color: var(--textfield-color);
51
+ }
52
+ .InputOTP-Input::-webkit-outer-spin-button,
53
+ .InputOTP-Input::-webkit-inner-spin-button {
54
+ -webkit-appearance: none;
55
+ appearance: none;
56
+ margin: 0;
57
+ }
58
+ .InputOTP-Input:disabled {
59
+ opacity: 1;
60
+ }
@@ -0,0 +1,4 @@
1
+ import './InputOTP.css';
2
+ import type { InputOTPBaseProps } from './types';
3
+ export declare const cnInputOTP: import("@bem-react/classname").ClassNameFormatter;
4
+ export declare const InputOTP: import("../../utils/polymorphicComponentWithRef").PolymorphicComponentWithRef<InputOTPBaseProps, "div">;
@@ -0,0 +1,179 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.InputOTP = exports.cnInputOTP = void 0;
4
+ var tslib_1 = require("tslib");
5
+ require("./InputOTP.css");
6
+ var react_1 = tslib_1.__importStar(require("react"));
7
+ var useControlled_1 = require("../../hooks/useControlled");
8
+ var useStoredValue_1 = require("../../hooks/useStoredValue");
9
+ var useThemeProps_1 = require("../../hooks/useThemeProps");
10
+ var array_1 = require("../../utils/array");
11
+ var classname_1 = require("../../utils/classname");
12
+ var isFunction_1 = require("../../utils/isFunction");
13
+ var isString_1 = require("../../utils/isString");
14
+ var polymorphicComponentWithRef_1 = require("../../utils/polymorphicComponentWithRef");
15
+ var setRef_1 = require("../../utils/setRef");
16
+ var FieldControl_1 = require("../FieldControl");
17
+ var FieldInput_1 = require("../FieldInput");
18
+ var Fieldset_1 = require("../Fieldset");
19
+ var Stack_1 = require("../Stack");
20
+ var constants_1 = require("./constants");
21
+ var helper_1 = require("./helper");
22
+ var hooks_1 = require("./hooks");
23
+ exports.cnInputOTP = (0, classname_1.cn)('InputOTP');
24
+ exports.InputOTP = (0, polymorphicComponentWithRef_1.polymorphicComponentWithRef)(function (inProps, ref) {
25
+ var props = (0, useThemeProps_1.useThemeProps)({
26
+ props: inProps,
27
+ name: 'InputOTP',
28
+ });
29
+ var _a = props.as, as = _a === void 0 ? constants_1.INPUT_OTP_DEFAULT_TAG : _a, className = props.className, length = props.length, autoComplete = props.autoComplete, _b = props.autoCompleteMode, autoCompleteMode = _b === void 0 ? constants_1.INPUT_OTP_DEFAULT_AUTO_COMPLETE_MODE : _b, placeholder = props.placeholder, disabled = props.disabled, error = props.error, onChangeFromProps = props.onChange, ariaLabel = props.ariaLabel, value = props.value, defaultValue = props.defaultValue, mask = props.mask, autoFocus = props.autoFocus, onFinish = props.onFinish, onPasteFromProps = props.onPaste, onKeyDownFromProps = props.onKeyDown, _c = props.disableStroke, disableStroke = _c === void 0 ? constants_1.INPUT_OTP_DEFAULT_DISABLE_STROKE : _c, validate = props.validate, _d = props.size, size = _d === void 0 ? constants_1.INPUT_OTP_DEFAULT_SIZE : _d, inputProps = props.inputProps, fieldProps = props.fieldProps, _e = props.finishBehavior, finishBehavior = _e === void 0 ? constants_1.INPUT_OTP_DEFAULT_FINISH_BEHAVIOR : _e, inputContainerProps = props.inputContainerProps, other = tslib_1.__rest(props, ["as", "className", "length", "autoComplete", "autoCompleteMode", "placeholder", "disabled", "error", "onChange", "ariaLabel", "value", "defaultValue", "mask", "autoFocus", "onFinish", "onPaste", "onKeyDown", "disableStroke", "validate", "size", "inputProps", "fieldProps", "finishBehavior", "inputContainerProps"]);
30
+ var _f = tslib_1.__read((0, react_1.useState)(null), 2), focusedInputIndex = _f[0], setFocusedInputIndex = _f[1];
31
+ var _g = tslib_1.__read((0, useControlled_1.useControlled)({
32
+ value: (0, isString_1.isString)(value) ? (0, helper_1.valueToArray)(value) : value,
33
+ defaultValue: (0, react_1.useMemo)(function () {
34
+ if ((0, isString_1.isString)(defaultValue)) {
35
+ return (0, helper_1.valueToArray)(defaultValue);
36
+ }
37
+ return defaultValue !== null && defaultValue !== void 0 ? defaultValue : [];
38
+ }, []),
39
+ name: 'InputOtp',
40
+ }), 2), valueByIndex = _g[0], setValueByIndexState = _g[1];
41
+ var inputs = (0, useStoredValue_1.useStoredValue)([]);
42
+ var setValueByIndex = function (valueByIndex) {
43
+ setValueByIndexState(valueByIndex);
44
+ if (onChangeFromProps) {
45
+ var maskedValueByIndex = (0, helper_1.maskValue)(mask, valueByIndex);
46
+ onChangeFromProps((0, helper_1.valueToString)(valueByIndex), valueByIndex, (0, helper_1.valueToString)(maskedValueByIndex), maskedValueByIndex);
47
+ }
48
+ };
49
+ var calculateInput = function (valueByIndex, index) {
50
+ return (0, helper_1.calculateInput)({
51
+ valueByIndex: valueByIndex,
52
+ mask: mask,
53
+ disabled: disabled,
54
+ index: index,
55
+ length: length,
56
+ focusedInputIndex: focusedInputIndex,
57
+ });
58
+ };
59
+ var _h = (0, hooks_1.useInputOTPEvents)({
60
+ inputs: inputs,
61
+ valueByIndex: valueByIndex,
62
+ setValueByIndex: setValueByIndex,
63
+ validate: validate,
64
+ onFinish: onFinish,
65
+ onPaste: onPasteFromProps,
66
+ onKeyDown: onKeyDownFromProps,
67
+ length: length,
68
+ mask: mask,
69
+ finishBehavior: finishBehavior,
70
+ }), onInputSelect = _h.onInputSelect, onInputChange = _h.onInputChange, onKeyDown = _h.onKeyDown, onPaste = _h.onPaste;
71
+ var getFirstNonFilledInput = function () {
72
+ var firstNonFilledInputIndex = inputs.current.findIndex(function (_, index) {
73
+ var _a;
74
+ var inputValue = (_a = valueByIndex === null || valueByIndex === void 0 ? void 0 : valueByIndex[index]) !== null && _a !== void 0 ? _a : '';
75
+ return inputValue === '';
76
+ });
77
+ return inputs.current[firstNonFilledInputIndex];
78
+ };
79
+ var focusFirstNonFilledInput = function () {
80
+ var _a;
81
+ (_a = getFirstNonFilledInput()) === null || _a === void 0 ? void 0 : _a.focus();
82
+ };
83
+ (0, react_1.useEffect)(function () {
84
+ if (!autoFocus) {
85
+ return;
86
+ }
87
+ focusFirstNonFilledInput();
88
+ }, []);
89
+ (0, react_1.useImperativeHandle)(ref, function () { return ({
90
+ focus: function () {
91
+ var _a;
92
+ if (getFirstNonFilledInput()) {
93
+ focusFirstNonFilledInput();
94
+ }
95
+ else {
96
+ (_a = inputs.current[length - 1]) === null || _a === void 0 ? void 0 : _a.focus();
97
+ }
98
+ },
99
+ }); });
100
+ return (react_1.default.createElement(Stack_1.Stack, tslib_1.__assign({ as: as, className: (0, exports.cnInputOTP)({
101
+ size: size,
102
+ disabled: disabled,
103
+ }, [className]), gap: constants_1.INPUT_OTP_SPACING_BY_SIZE[size] }, other, { ref: ref }), (0, array_1.createEmptyArray)(length).map(function (_, index) {
104
+ var _a, _b;
105
+ var calculatedInput = calculateInput(valueByIndex, index);
106
+ var isInputDisabled = calculatedInput.isDisabled, value = calculatedInput.value;
107
+ var processedAutocomplete = (function () {
108
+ if (autoCompleteMode === 'firstOnly') {
109
+ return !!autoComplete && index === 0 ? autoComplete : 'off';
110
+ }
111
+ return autoComplete;
112
+ })();
113
+ var processedContainerProps = (0, isFunction_1.isFunction)(inputContainerProps)
114
+ ? inputContainerProps(calculatedInput, index)
115
+ : inputContainerProps;
116
+ var processedFieldProps = (0, isFunction_1.isFunction)(fieldProps)
117
+ ? fieldProps(calculatedInput, index)
118
+ : fieldProps;
119
+ var processedInputProps = (0, isFunction_1.isFunction)(inputProps)
120
+ ? inputProps(calculatedInput, index)
121
+ : inputProps;
122
+ var processedPlaceholder = (0, isFunction_1.isFunction)(placeholder)
123
+ ? placeholder(calculatedInput, index)
124
+ : placeholder;
125
+ return (
126
+ // eslint-disable-next-line jsx-a11y/click-events-have-key-events,jsx-a11y/no-static-element-interactions
127
+ react_1.default.createElement("div", tslib_1.__assign({ key: index }, processedContainerProps, { className: (0, exports.cnInputOTP)('InputContainer', [
128
+ processedContainerProps === null || processedContainerProps === void 0 ? void 0 : processedContainerProps.className,
129
+ ]), onClick: function () {
130
+ if (isInputDisabled) {
131
+ focusFirstNonFilledInput();
132
+ }
133
+ } }),
134
+ react_1.default.createElement(FieldControl_1.FieldControl, tslib_1.__assign({ size: size, disabled: disabled, error: error, disableStroke: disableStroke }, processedFieldProps, { className: (0, exports.cnInputOTP)('Field', {
135
+ notAvailable: isInputDisabled,
136
+ }, [processedFieldProps === null || processedFieldProps === void 0 ? void 0 : processedFieldProps.className]) }),
137
+ react_1.default.createElement(FieldInput_1.FieldInput, tslib_1.__assign({ inputMode: (_a = processedInputProps === null || processedInputProps === void 0 ? void 0 : processedInputProps.inputMode) !== null && _a !== void 0 ? _a : 'numeric', type: (_b = processedInputProps === null || processedInputProps === void 0 ? void 0 : processedInputProps.type) !== null && _b !== void 0 ? _b : 'text', value: value, autoComplete: processedAutocomplete, "aria-label": ariaLabel && "".concat(ariaLabel, ": ").concat(index + 1, "."), placeholder: processedPlaceholder, disabled: isInputDisabled }, processedInputProps, { onMouseDown: function (event) {
138
+ var _a;
139
+ (_a = processedInputProps === null || processedInputProps === void 0 ? void 0 : processedInputProps.onMouseDown) === null || _a === void 0 ? void 0 : _a.call(processedInputProps, event);
140
+ onInputSelect(index);
141
+ }, onTouchStart: function (event) {
142
+ var _a;
143
+ (_a = processedInputProps === null || processedInputProps === void 0 ? void 0 : processedInputProps.onTouchStart) === null || _a === void 0 ? void 0 : _a.call(processedInputProps, event);
144
+ onInputSelect(index);
145
+ }, onPointerDown: function (event) {
146
+ var _a;
147
+ (_a = processedInputProps === null || processedInputProps === void 0 ? void 0 : processedInputProps.onPointerDown) === null || _a === void 0 ? void 0 : _a.call(processedInputProps, event);
148
+ onInputSelect(index);
149
+ }, onChange: function (event) {
150
+ var _a;
151
+ (_a = processedInputProps === null || processedInputProps === void 0 ? void 0 : processedInputProps.onChange) === null || _a === void 0 ? void 0 : _a.call(processedInputProps, event);
152
+ onInputChange(event, index);
153
+ }, onKeyDown: function (event) {
154
+ var _a;
155
+ (_a = processedInputProps === null || processedInputProps === void 0 ? void 0 : processedInputProps.onKeyDown) === null || _a === void 0 ? void 0 : _a.call(processedInputProps, event);
156
+ onKeyDown(event, index);
157
+ }, onPaste: function (event) {
158
+ var _a;
159
+ (_a = processedInputProps === null || processedInputProps === void 0 ? void 0 : processedInputProps.onPaste) === null || _a === void 0 ? void 0 : _a.call(processedInputProps, event);
160
+ onPaste(event, index);
161
+ }, onFocus: function (event) {
162
+ var _a;
163
+ (_a = processedInputProps === null || processedInputProps === void 0 ? void 0 : processedInputProps.onFocus) === null || _a === void 0 ? void 0 : _a.call(processedInputProps, event);
164
+ setFocusedInputIndex(index);
165
+ }, onBlur: function (event) {
166
+ var _a;
167
+ (_a = processedInputProps === null || processedInputProps === void 0 ? void 0 : processedInputProps.onBlur) === null || _a === void 0 ? void 0 : _a.call(processedInputProps, event);
168
+ setFocusedInputIndex(null);
169
+ }, className: (0, exports.cnInputOTP)('Input', [
170
+ processedInputProps === null || processedInputProps === void 0 ? void 0 : processedInputProps.className,
171
+ ]), ref: function (instance) {
172
+ if (instance) {
173
+ inputs.current[index] = instance;
174
+ }
175
+ (0, setRef_1.setRef)(processedInputProps === null || processedInputProps === void 0 ? void 0 : processedInputProps.ref, instance);
176
+ } })),
177
+ react_1.default.createElement(Fieldset_1.Fieldset, null))));
178
+ })));
179
+ });
@@ -0,0 +1,8 @@
1
+ import type { StackGapVariant } from '../Stack';
2
+ import type { InputOTPAutoCompleteMode, InputOTPSizeVariant } from './types';
3
+ export declare const INPUT_OTP_DEFAULT_TAG = "div";
4
+ export declare const INPUT_OTP_DEFAULT_AUTO_COMPLETE_MODE: InputOTPAutoCompleteMode;
5
+ export declare const INPUT_OTP_DEFAULT_DISABLE_STROKE = false;
6
+ export declare const INPUT_OTP_DEFAULT_SIZE: InputOTPSizeVariant;
7
+ export declare const INPUT_OTP_SPACING_BY_SIZE: Readonly<Record<InputOTPSizeVariant, StackGapVariant>>;
8
+ export declare const INPUT_OTP_DEFAULT_FINISH_BEHAVIOR: ({ isFullFilled }: import("./types").InputOTPFinishBehaviorContext) => boolean;
@@ -0,0 +1,13 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.INPUT_OTP_DEFAULT_FINISH_BEHAVIOR = exports.INPUT_OTP_SPACING_BY_SIZE = exports.INPUT_OTP_DEFAULT_SIZE = exports.INPUT_OTP_DEFAULT_DISABLE_STROKE = exports.INPUT_OTP_DEFAULT_AUTO_COMPLETE_MODE = exports.INPUT_OTP_DEFAULT_TAG = void 0;
4
+ var presets_1 = require("./presets");
5
+ exports.INPUT_OTP_DEFAULT_TAG = 'div';
6
+ exports.INPUT_OTP_DEFAULT_AUTO_COMPLETE_MODE = 'firstOnly';
7
+ exports.INPUT_OTP_DEFAULT_DISABLE_STROKE = false;
8
+ exports.INPUT_OTP_DEFAULT_SIZE = 's';
9
+ exports.INPUT_OTP_SPACING_BY_SIZE = {
10
+ s: 's',
11
+ m: 's',
12
+ };
13
+ exports.INPUT_OTP_DEFAULT_FINISH_BEHAVIOR = presets_1.INPUT_OTP_FINISH_BEHAVIOR_PRESET.FULL_FILLED_NON_LAST_INPUT;
@@ -0,0 +1,13 @@
1
+ import type { InputOTPMask, InputOTPRawValue, InputOTPCalculatedInput } from './types';
2
+ export declare const maskSymbol: (mask: InputOTPMask | undefined, value: string, index: number) => string;
3
+ export declare const maskValue: (mask: InputOTPMask | undefined, value: InputOTPRawValue | undefined) => string[];
4
+ export declare const calculateInput: ({ valueByIndex, index, mask, length, disabled: isDisabledGlobal, focusedInputIndex, }: {
5
+ valueByIndex: string[] | undefined;
6
+ index: number;
7
+ mask: InputOTPMask | undefined;
8
+ length: number;
9
+ disabled: boolean | undefined;
10
+ focusedInputIndex: number | null;
11
+ }) => InputOTPCalculatedInput;
12
+ export declare const valueToString: (value: InputOTPRawValue | undefined) => string;
13
+ export declare const valueToArray: (value: string) => string[];
@@ -0,0 +1,87 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.valueToArray = exports.valueToString = exports.calculateInput = exports.maskValue = exports.maskSymbol = void 0;
4
+ var isFunction_1 = require("../../utils/isFunction");
5
+ var isNumber_1 = require("../../utils/isNumber");
6
+ var isString_1 = require("../../utils/isString");
7
+ var maskSymbol = function (mask, value, index) {
8
+ if (value === '') {
9
+ return '';
10
+ }
11
+ if ((0, isString_1.isString)(mask)) {
12
+ return mask;
13
+ }
14
+ if ((0, isFunction_1.isFunction)(mask)) {
15
+ return mask(value, index);
16
+ }
17
+ return value;
18
+ };
19
+ exports.maskSymbol = maskSymbol;
20
+ var maskValue = function (mask, value) { var _a; return (_a = value === null || value === void 0 ? void 0 : value.map(function (value, index) { return (0, exports.maskSymbol)(mask, value, index); })) !== null && _a !== void 0 ? _a : []; };
21
+ exports.maskValue = maskValue;
22
+ var calculateInput = function (_a) {
23
+ var _b;
24
+ var valueByIndex = _a.valueByIndex, index = _a.index, mask = _a.mask, length = _a.length, isDisabledGlobal = _a.disabled, focusedInputIndex = _a.focusedInputIndex;
25
+ var requiredValueByIndex = valueByIndex !== null && valueByIndex !== void 0 ? valueByIndex : [];
26
+ var value = (_b = requiredValueByIndex[index]) !== null && _b !== void 0 ? _b : '';
27
+ var maskedValue = (0, exports.maskSymbol)(mask, value, index);
28
+ var lastFilledInputIndex = requiredValueByIndex.findLastIndex(function (value) { return value !== '' && value !== undefined; });
29
+ var isBackward = index <= lastFilledInputIndex;
30
+ var isPrevious = index === lastFilledInputIndex;
31
+ var isNext = index === lastFilledInputIndex + 2;
32
+ var isCurrentInput = index === lastFilledInputIndex + 1;
33
+ var isLast = index === length - 1;
34
+ var isPreviousByFocusedInput = (0, isNumber_1.isNumber)(focusedInputIndex) && index < focusedInputIndex;
35
+ var isFocusedInput = index === focusedInputIndex;
36
+ var isDisabled = (function () {
37
+ if (isDisabledGlobal) {
38
+ return true;
39
+ }
40
+ if (isFocusedInput) {
41
+ return false;
42
+ }
43
+ if (isPreviousByFocusedInput) {
44
+ return false;
45
+ }
46
+ if (isBackward) {
47
+ return false;
48
+ }
49
+ if (value !== '') {
50
+ return false;
51
+ }
52
+ return !isCurrentInput;
53
+ })();
54
+ return {
55
+ value: maskedValue,
56
+ rawValue: value,
57
+ isPrevious: isPrevious,
58
+ isNext: isNext,
59
+ isCurrentInput: isCurrentInput,
60
+ isLast: isLast,
61
+ isDisabled: isDisabled,
62
+ };
63
+ };
64
+ exports.calculateInput = calculateInput;
65
+ var valueToString = function (value) {
66
+ if (!value) {
67
+ return '';
68
+ }
69
+ return value
70
+ .map(function (value) {
71
+ if (value === '') {
72
+ return ' ';
73
+ }
74
+ return value;
75
+ })
76
+ .join('');
77
+ };
78
+ exports.valueToString = valueToString;
79
+ var valueToArray = function (value) {
80
+ return value.split('').map(function (value) {
81
+ if (value === ' ') {
82
+ return '';
83
+ }
84
+ return value;
85
+ });
86
+ };
87
+ exports.valueToArray = valueToArray;
@@ -0,0 +1 @@
1
+ export * from './useInputOTPEvents';
@@ -0,0 +1,4 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ var tslib_1 = require("tslib");
4
+ tslib_1.__exportStar(require("./useInputOTPEvents"), exports);
@@ -0,0 +1,20 @@
1
+ import type { ChangeEvent, ClipboardEvent, ClipboardEventHandler, KeyboardEvent, KeyboardEventHandler } from 'react';
2
+ import type { StoredValue } from '../../../hooks/useStoredValue';
3
+ import type { InputOTPRawValue, InputOTPInputs, InputOTPOnFinish, InputOTPValidate, InputOTPMask, InputOTPFinishBehavior } from '../types';
4
+ export declare const useInputOTPEvents: ({ inputs, valueByIndex, setValueByIndex, validate: validateFromProps, length, onPaste: onPasteFromProps, onKeyDown: onKeyDownFromProps, onFinish, mask, finishBehavior, }: {
5
+ inputs: StoredValue<InputOTPInputs>;
6
+ valueByIndex: InputOTPRawValue | undefined;
7
+ setValueByIndex: (valueByIndex: InputOTPRawValue) => void;
8
+ validate: InputOTPValidate | undefined;
9
+ length: number;
10
+ onKeyDown: KeyboardEventHandler<HTMLElement> | undefined;
11
+ onPaste: ClipboardEventHandler<HTMLElement> | undefined;
12
+ onFinish: InputOTPOnFinish | undefined;
13
+ mask: InputOTPMask | undefined;
14
+ finishBehavior: InputOTPFinishBehavior;
15
+ }) => {
16
+ onInputChange: (event: ChangeEvent<HTMLInputElement>, index: number) => void;
17
+ onKeyDown: (event: KeyboardEvent<HTMLInputElement>, index: number) => void;
18
+ onPaste: (event: ClipboardEvent<HTMLInputElement>, inputIndex: number) => void;
19
+ onInputSelect: (index: number) => void;
20
+ };
@@ -0,0 +1,183 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.useInputOTPEvents = void 0;
4
+ var tslib_1 = require("tslib");
5
+ var isFunction_1 = require("../../../utils/isFunction");
6
+ var isKey_1 = require("../../../utils/isKey");
7
+ var isKeys_1 = require("../../../utils/isKeys");
8
+ var isNumber_1 = require("../../../utils/isNumber");
9
+ var helper_1 = require("../helper");
10
+ var useInputOTPEvents = function (_a) {
11
+ var inputs = _a.inputs, valueByIndex = _a.valueByIndex, setValueByIndex = _a.setValueByIndex, validateFromProps = _a.validate, length = _a.length, onPasteFromProps = _a.onPaste, onKeyDownFromProps = _a.onKeyDown, onFinish = _a.onFinish, mask = _a.mask, finishBehavior = _a.finishBehavior;
12
+ var validate = function (value) {
13
+ if ((0, isFunction_1.isFunction)(validateFromProps)) {
14
+ return validateFromProps(value);
15
+ }
16
+ return true;
17
+ };
18
+ var tryToFinish = function (valueByIndex, index, options) {
19
+ var _a;
20
+ var _b = (options !== null && options !== void 0 ? options : {}).isEnter, isEnter = _b === void 0 ? false : _b;
21
+ var requiredValueByIndex = valueByIndex !== null && valueByIndex !== void 0 ? valueByIndex : [];
22
+ var stringValue = (0, helper_1.valueToString)(requiredValueByIndex);
23
+ var isFullFilledValue = requiredValueByIndex.filter(function (value) { return value !== ''; }).length === length;
24
+ var isChangeOnLastInput = index + 1 === length;
25
+ var maskedRawValue = (0, helper_1.maskValue)(mask, valueByIndex);
26
+ var finishContext = {
27
+ isChangeOnLastInput: isChangeOnLastInput,
28
+ inputIndex: index,
29
+ value: (0, helper_1.valueToString)(valueByIndex),
30
+ rawValue: valueByIndex !== null && valueByIndex !== void 0 ? valueByIndex : [],
31
+ maskedValue: (0, helper_1.valueToString)(maskedRawValue),
32
+ maskedRawValue: maskedRawValue,
33
+ isFullFilled: isFullFilledValue,
34
+ isEnter: isEnter,
35
+ };
36
+ var isFinish = finishBehavior(finishContext);
37
+ if (isFinish) {
38
+ var activeElement = document.activeElement;
39
+ if (activeElement instanceof HTMLElement) {
40
+ activeElement.blur();
41
+ }
42
+ if (onFinish) {
43
+ var maskedValueByIndex = (_a = valueByIndex === null || valueByIndex === void 0 ? void 0 : valueByIndex.map(function (value, index) { return (0, helper_1.maskSymbol)(mask, value, index); })) !== null && _a !== void 0 ? _a : [];
44
+ onFinish(stringValue, valueByIndex !== null && valueByIndex !== void 0 ? valueByIndex : [], (0, helper_1.valueToString)(maskedValueByIndex), maskedValueByIndex);
45
+ }
46
+ }
47
+ };
48
+ var onInputChange = function (event, index) {
49
+ event.preventDefault();
50
+ var target = event.target;
51
+ var value = (function () {
52
+ var _a;
53
+ var previousValue = (_a = valueByIndex === null || valueByIndex === void 0 ? void 0 : valueByIndex[index]) !== null && _a !== void 0 ? _a : '';
54
+ var previousMaskedValue = (0, helper_1.maskSymbol)(mask, previousValue, index);
55
+ var rawValue = target.value;
56
+ var changedValuePart = rawValue.replace(previousMaskedValue, '');
57
+ if (changedValuePart.length > 1) {
58
+ return changedValuePart.slice(0, 1);
59
+ }
60
+ return changedValuePart;
61
+ })();
62
+ var isValidValue = validate(value);
63
+ var newValueByIndex = valueByIndex ? tslib_1.__spreadArray([], tslib_1.__read(valueByIndex), false) : [];
64
+ if (isValidValue) {
65
+ newValueByIndex[index] = value;
66
+ var isLastInput = index === length - 1;
67
+ var firstEmptyInputIndex = newValueByIndex.findIndex(function (value) { return !value; });
68
+ if (isLastInput && firstEmptyInputIndex !== -1) {
69
+ var firstEmptyInput = inputs.current[firstEmptyInputIndex];
70
+ if (firstEmptyInput) {
71
+ firstEmptyInput.focus();
72
+ }
73
+ }
74
+ else {
75
+ var nextInput = inputs.current[index + 1];
76
+ if (nextInput) {
77
+ nextInput.disabled = false;
78
+ nextInput.focus();
79
+ }
80
+ }
81
+ setValueByIndex(newValueByIndex);
82
+ }
83
+ tryToFinish(newValueByIndex, index);
84
+ };
85
+ var onKeyDown = function (event, index) {
86
+ if (onKeyDownFromProps) {
87
+ onKeyDownFromProps(event);
88
+ }
89
+ var target = event.target;
90
+ if ((0, isKey_1.isKey)(event, 'Backspace')) {
91
+ var newValueByIndex = valueByIndex ? tslib_1.__spreadArray([], tslib_1.__read(valueByIndex), false) : [];
92
+ if (target.value === '') {
93
+ var previousInput = inputs.current[index - 1];
94
+ var previousInputIndex = index - 1;
95
+ if (previousInput) {
96
+ newValueByIndex[previousInputIndex] = '';
97
+ previousInput.focus();
98
+ event.preventDefault();
99
+ }
100
+ }
101
+ else {
102
+ newValueByIndex[index] = '';
103
+ }
104
+ setValueByIndex(newValueByIndex);
105
+ }
106
+ else if ((0, isKeys_1.isKeys)(event, ['ArrowLeft', 'ArrowDown'])) {
107
+ event.preventDefault();
108
+ var previousInput = inputs.current[index - 1];
109
+ if (previousInput) {
110
+ previousInput.focus();
111
+ }
112
+ }
113
+ else if ((0, isKeys_1.isKeys)(event, ['ArrowRight', 'ArrowUp'])) {
114
+ event.preventDefault();
115
+ var nextInput = inputs.current[index + 1];
116
+ if (nextInput) {
117
+ nextInput.focus();
118
+ }
119
+ }
120
+ else if ((0, isKey_1.isKey)(event, 'Enter')) {
121
+ tryToFinish(valueByIndex, index, {
122
+ isEnter: true,
123
+ });
124
+ }
125
+ };
126
+ var onPaste = function (event, inputIndex) {
127
+ event.preventDefault();
128
+ if (onPasteFromProps) {
129
+ onPasteFromProps(event);
130
+ }
131
+ var currentInputs = inputs.current;
132
+ var pastedValue = event.clipboardData.getData('Text');
133
+ var newValueByIndex = valueByIndex ? tslib_1.__spreadArray([], tslib_1.__read(valueByIndex), false) : [];
134
+ var lastChangedInputIndex = {
135
+ current: null,
136
+ };
137
+ var focusedInputIndex = {
138
+ current: null,
139
+ };
140
+ tslib_1.__spreadArray([], tslib_1.__read(pastedValue), false).forEach(function (symbol, symbolIndex) {
141
+ var currentInputIndex = symbolIndex + inputIndex;
142
+ var isIndexExistInArray = currentInputIndex >= 0 && currentInputIndex < currentInputs.length;
143
+ if (isIndexExistInArray && validate(symbol)) {
144
+ lastChangedInputIndex.current = currentInputIndex;
145
+ newValueByIndex[currentInputIndex] = symbol;
146
+ var nextInput = currentInputs[currentInputIndex + 1];
147
+ if (nextInput) {
148
+ focusedInputIndex.current = currentInputIndex + 1;
149
+ }
150
+ }
151
+ });
152
+ if ((0, isNumber_1.isNumber)(focusedInputIndex.current)) {
153
+ var input = currentInputs[focusedInputIndex.current];
154
+ if (input) {
155
+ input.disabled = false;
156
+ input.focus();
157
+ }
158
+ }
159
+ setValueByIndex(newValueByIndex);
160
+ if (lastChangedInputIndex.current !== null) {
161
+ tryToFinish(newValueByIndex, lastChangedInputIndex.current);
162
+ }
163
+ };
164
+ var onInputSelect = function (index) {
165
+ setTimeout(function () {
166
+ var currentInput = inputs.current[index];
167
+ if (!currentInput) {
168
+ return;
169
+ }
170
+ var originalType = currentInput.type;
171
+ currentInput.type = 'text';
172
+ currentInput.setSelectionRange(1, 1);
173
+ currentInput.type = originalType;
174
+ });
175
+ };
176
+ return {
177
+ onInputChange: onInputChange,
178
+ onKeyDown: onKeyDown,
179
+ onPaste: onPaste,
180
+ onInputSelect: onInputSelect,
181
+ };
182
+ };
183
+ exports.useInputOTPEvents = useInputOTPEvents;
@@ -0,0 +1,3 @@
1
+ export * from './presets';
2
+ export * from './InputOTP';
3
+ export * from './types';
@@ -0,0 +1,6 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ var tslib_1 = require("tslib");
4
+ tslib_1.__exportStar(require("./presets"), exports);
5
+ tslib_1.__exportStar(require("./InputOTP"), exports);
6
+ tslib_1.__exportStar(require("./types"), exports);
@@ -0,0 +1,12 @@
1
+ export declare const INPUT_OTP_VALIDATE_PRESET: {
2
+ TEXT: (value: string) => boolean;
3
+ NUMERIC: (value: string) => boolean;
4
+ TEXT_NUMERIC: (value: string) => boolean;
5
+ };
6
+ export declare const INPUT_OTP_MASK_PRESET: {
7
+ PASSWORD: string;
8
+ };
9
+ export declare const INPUT_OTP_FINISH_BEHAVIOR_PRESET: {
10
+ FULL_FILLED_NON_LAST_INPUT: ({ isFullFilled }: import("./types").InputOTPFinishBehaviorContext) => boolean;
11
+ FULL_FILLED_ON_LAST_INPUT: ({ isFullFilled, isChangeOnLastInput, isEnter, }: import("./types").InputOTPFinishBehaviorContext) => boolean;
12
+ };
@@ -0,0 +1,27 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.INPUT_OTP_FINISH_BEHAVIOR_PRESET = exports.INPUT_OTP_MASK_PRESET = exports.INPUT_OTP_VALIDATE_PRESET = void 0;
4
+ exports.INPUT_OTP_VALIDATE_PRESET = {
5
+ TEXT: function (value) { return /[a-zA-Z]{1}/.test(value); },
6
+ NUMERIC: function (value) { return /\d{1}/.test(value); },
7
+ TEXT_NUMERIC: function (value) { return /[a-zA-Z0-9]{1}/.test(value); },
8
+ };
9
+ exports.INPUT_OTP_MASK_PRESET = {
10
+ PASSWORD: '•',
11
+ };
12
+ exports.INPUT_OTP_FINISH_BEHAVIOR_PRESET = {
13
+ FULL_FILLED_NON_LAST_INPUT: function (_a) {
14
+ var isFullFilled = _a.isFullFilled;
15
+ return isFullFilled;
16
+ },
17
+ FULL_FILLED_ON_LAST_INPUT: function (_a) {
18
+ var isFullFilled = _a.isFullFilled, isChangeOnLastInput = _a.isChangeOnLastInput, isEnter = _a.isEnter;
19
+ if (isFullFilled && isChangeOnLastInput) {
20
+ return true;
21
+ }
22
+ if (isFullFilled && isEnter) {
23
+ return true;
24
+ }
25
+ return false;
26
+ },
27
+ };