@sytechui/input-otp 2.1.32

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 ADDED
@@ -0,0 +1,391 @@
1
+ "use client";
2
+ "use strict";
3
+ var __defProp = Object.defineProperty;
4
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
5
+ var __getOwnPropNames = Object.getOwnPropertyNames;
6
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
7
+ var __export = (target, all) => {
8
+ for (var name in all)
9
+ __defProp(target, name, { get: all[name], enumerable: true });
10
+ };
11
+ var __copyProps = (to, from, except, desc) => {
12
+ if (from && typeof from === "object" || typeof from === "function") {
13
+ for (let key of __getOwnPropNames(from))
14
+ if (!__hasOwnProp.call(to, key) && key !== except)
15
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
16
+ }
17
+ return to;
18
+ };
19
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
20
+
21
+ // src/index.ts
22
+ var index_exports = {};
23
+ __export(index_exports, {
24
+ InputOtp: () => input_otp_default,
25
+ REGEXP_ONLY_CHARS: () => import_input_otp3.REGEXP_ONLY_CHARS,
26
+ REGEXP_ONLY_DIGITS: () => import_input_otp3.REGEXP_ONLY_DIGITS,
27
+ REGEXP_ONLY_DIGITS_AND_CHARS: () => import_input_otp3.REGEXP_ONLY_DIGITS_AND_CHARS,
28
+ useInputOtp: () => useInputOtp
29
+ });
30
+ module.exports = __toCommonJS(index_exports);
31
+
32
+ // src/input-otp.tsx
33
+ var import_system2 = require("@sytechui/system");
34
+ var import_react3 = require("react");
35
+ var import_input_otp = require("input-otp");
36
+
37
+ // src/use-input-otp.ts
38
+ var import_system = require("@sytechui/system");
39
+ var import_theme = require("@sytechui/theme");
40
+ var import_react_utils = require("@sytechui/react-utils");
41
+ var import_shared_utils = require("@sytechui/shared-utils");
42
+ var import_use_form_reset = require("@sytechui/use-form-reset");
43
+ var import_react = require("react");
44
+ var import_utils = require("@react-stately/utils");
45
+ var import_form = require("@react-stately/form");
46
+ var import_form2 = require("@react-aria/form");
47
+ var import_focus = require("@react-aria/focus");
48
+ var import_form3 = require("@sytechui/form");
49
+ function useInputOtp(originalProps) {
50
+ var _a, _b, _c, _d;
51
+ const globalContext = (0, import_system.useProviderContext)();
52
+ const { validationBehavior: formValidationBehavior } = (0, import_form3.useSlottedContext)(import_form3.FormContext) || {};
53
+ const [props, variantProps] = (0, import_system.mapPropsVariants)(originalProps, import_theme.inputOtp.variantKeys);
54
+ const {
55
+ ref,
56
+ baseRef,
57
+ as,
58
+ className,
59
+ classNames,
60
+ length = 4,
61
+ autoFocus,
62
+ "aria-label": ariaLabel = "One-time password input",
63
+ onValueChange = () => {
64
+ },
65
+ allowedKeys = "^[0-9]*$",
66
+ validationBehavior = (_a = formValidationBehavior != null ? formValidationBehavior : globalContext == null ? void 0 : globalContext.validationBehavior) != null ? _a : "native",
67
+ type,
68
+ name,
69
+ maxLength,
70
+ minLength,
71
+ textAlign = "center",
72
+ onComplete = () => {
73
+ },
74
+ pushPasswordManagerStrategy,
75
+ pasteTransformer,
76
+ containerClassName,
77
+ noScriptCSSFallback,
78
+ onChange,
79
+ inputMode,
80
+ ...otherProps
81
+ } = props;
82
+ const Component = as || "div";
83
+ const inputRef = (0, import_react_utils.useDOMRef)(ref);
84
+ const baseDomRef = (0, import_react_utils.useDOMRef)(baseRef);
85
+ const { isFocusVisible, isFocused, focusProps } = (0, import_focus.useFocusRing)({
86
+ autoFocus,
87
+ isTextInput: true
88
+ });
89
+ const handleValueChange = (0, import_react.useCallback)(
90
+ (value2) => {
91
+ onValueChange(value2 != null ? value2 : "");
92
+ },
93
+ [onValueChange]
94
+ );
95
+ const [value, setValue] = (0, import_utils.useControlledState)(
96
+ props.value,
97
+ (_b = props.defaultValue) != null ? _b : "",
98
+ handleValueChange
99
+ );
100
+ const disableAnimation = (_d = (_c = originalProps.disableAnimation) != null ? _c : globalContext == null ? void 0 : globalContext.disableAnimation) != null ? _d : false;
101
+ const isDisabled = originalProps.isDisabled;
102
+ const baseStyles = (0, import_theme.cn)(classNames == null ? void 0 : classNames.base, className);
103
+ const validationState = (0, import_form.useFormValidationState)({
104
+ ...props,
105
+ validationBehavior,
106
+ value
107
+ });
108
+ (0, import_use_form_reset.useFormReset)(inputRef, value, setValue);
109
+ (0, import_form2.useFormValidation)({ ...props, validationBehavior }, validationState, inputRef);
110
+ const {
111
+ isInvalid: isAriaInvalid,
112
+ validationErrors,
113
+ validationDetails
114
+ } = validationState.displayValidation;
115
+ const isReadOnly = originalProps.isReadOnly;
116
+ const isRequired = originalProps.isRequired;
117
+ const isInvalid = originalProps.isInvalid || isAriaInvalid;
118
+ const errorMessage = typeof props.errorMessage === "function" ? props.errorMessage({ isInvalid, validationErrors, validationDetails }) : props.errorMessage || (validationErrors == null ? void 0 : validationErrors.join(" "));
119
+ const description = props.description;
120
+ const hasHelper = !!description || !!errorMessage;
121
+ const slots = (0, import_react.useMemo)(
122
+ () => (0, import_theme.inputOtp)({
123
+ ...variantProps,
124
+ disableAnimation,
125
+ isInvalid,
126
+ isReadOnly
127
+ }),
128
+ [(0, import_shared_utils.objectToDeps)(variantProps), disableAnimation, isInvalid, isReadOnly]
129
+ );
130
+ const getBaseProps = (0, import_react.useCallback)(
131
+ (props2 = {}) => {
132
+ return {
133
+ ref: baseDomRef,
134
+ className: slots.base({
135
+ class: baseStyles
136
+ }),
137
+ "data-slot": "base",
138
+ "data-disabled": (0, import_shared_utils.dataAttr)(isDisabled),
139
+ "data-invalid": (0, import_shared_utils.dataAttr)(isInvalid),
140
+ "data-required": (0, import_shared_utils.dataAttr)(originalProps == null ? void 0 : originalProps.isRequired),
141
+ "data-readonly": (0, import_shared_utils.dataAttr)(originalProps == null ? void 0 : originalProps.isReadOnly),
142
+ "data-filled": (0, import_shared_utils.dataAttr)(value.length === length),
143
+ "aria-label": ariaLabel,
144
+ "aria-required": (0, import_shared_utils.dataAttr)(originalProps.isRequired),
145
+ "aria-readonly": (0, import_shared_utils.dataAttr)(originalProps == null ? void 0 : originalProps.isReadOnly),
146
+ role: "base",
147
+ ...(0, import_shared_utils.mergeProps)(
148
+ (0, import_react_utils.filterDOMProps)(otherProps, {
149
+ enabled: true
150
+ }),
151
+ (0, import_react_utils.filterDOMProps)(props2)
152
+ ),
153
+ onChange: (e) => {
154
+ var _a2;
155
+ const val = (_a2 = e.target) == null ? void 0 : _a2.value;
156
+ const regex = new RegExp(allowedKeys);
157
+ if (regex.test(val)) {
158
+ onChange == null ? void 0 : onChange(e);
159
+ }
160
+ }
161
+ };
162
+ },
163
+ [baseDomRef, slots, baseStyles, isDisabled, isInvalid, isRequired, isReadOnly, value, length]
164
+ );
165
+ const getInputOtpProps = (0, import_react.useCallback)(
166
+ (props2 = {}) => {
167
+ var _a2;
168
+ const otpProps = {
169
+ ...focusProps,
170
+ required: isRequired,
171
+ disabled: isDisabled,
172
+ readOnly: isReadOnly,
173
+ pattern: allowedKeys,
174
+ maxLength: maxLength != null ? maxLength : length,
175
+ minLength: minLength != null ? minLength : length,
176
+ textAlign,
177
+ ref: inputRef,
178
+ name,
179
+ value,
180
+ autoFocus,
181
+ onChange: setValue,
182
+ onBlur: (0, import_shared_utils.chain)(focusProps.onBlur, props2 == null ? void 0 : props2.onBlur),
183
+ onComplete,
184
+ pushPasswordManagerStrategy,
185
+ pasteTransformer,
186
+ noScriptCSSFallback,
187
+ inputMode: inputMode != null ? inputMode : (0, import_shared_utils.isPatternNumeric)(allowedKeys) ? "numeric" : "text",
188
+ containerClassName: (_a2 = slots.wrapper) == null ? void 0 : _a2.call(slots, { class: (0, import_theme.cn)(classNames == null ? void 0 : classNames.wrapper, containerClassName) }),
189
+ ...props2
190
+ };
191
+ return otpProps;
192
+ },
193
+ [
194
+ inputMode,
195
+ isRequired,
196
+ isDisabled,
197
+ isReadOnly,
198
+ allowedKeys,
199
+ inputRef,
200
+ name,
201
+ value,
202
+ length,
203
+ setValue,
204
+ props.onBlur,
205
+ onComplete,
206
+ autoFocus
207
+ ]
208
+ );
209
+ const getSegmentWrapperProps = (0, import_react.useCallback)(
210
+ (props2 = {}) => {
211
+ return {
212
+ className: slots.segmentWrapper({
213
+ class: (0, import_theme.cn)(classNames == null ? void 0 : classNames.segmentWrapper, props2 == null ? void 0 : props2.className)
214
+ }),
215
+ "data-slot": "segment-wrapper",
216
+ "data-disabled": (0, import_shared_utils.dataAttr)(isDisabled),
217
+ "aria-label": ariaLabel,
218
+ ...props2
219
+ };
220
+ },
221
+ [classNames == null ? void 0 : classNames.segmentWrapper, isDisabled]
222
+ );
223
+ const getHelperWrapperProps = (0, import_react.useCallback)(
224
+ (props2 = {}) => {
225
+ return {
226
+ className: slots.helperWrapper({
227
+ class: (0, import_theme.cn)(classNames == null ? void 0 : classNames.helperWrapper, props2 == null ? void 0 : props2.className)
228
+ }),
229
+ "data-slot": "helper-wrapper",
230
+ ...props2
231
+ };
232
+ },
233
+ [classNames == null ? void 0 : classNames.helperWrapper]
234
+ );
235
+ const getErrorMessageProps = (0, import_react.useCallback)(
236
+ (props2 = {}) => {
237
+ return {
238
+ className: slots.errorMessage({
239
+ class: (0, import_theme.cn)(classNames == null ? void 0 : classNames.errorMessage, props2 == null ? void 0 : props2.className)
240
+ }),
241
+ "data-slot": "error-message",
242
+ ...props2
243
+ };
244
+ },
245
+ [classNames == null ? void 0 : classNames.errorMessage]
246
+ );
247
+ const getDescriptionProps = (0, import_react.useCallback)(
248
+ (props2 = {}) => {
249
+ return {
250
+ className: slots.description({
251
+ class: (0, import_theme.cn)(classNames == null ? void 0 : classNames.description, props2 == null ? void 0 : props2.className)
252
+ }),
253
+ "data-slot": "description",
254
+ ...props2
255
+ };
256
+ },
257
+ [classNames == null ? void 0 : classNames.description]
258
+ );
259
+ return {
260
+ Component,
261
+ inputRef,
262
+ length,
263
+ value,
264
+ type,
265
+ slots,
266
+ hasHelper,
267
+ classNames,
268
+ isInvalid,
269
+ description,
270
+ errorMessage,
271
+ isFocusVisible,
272
+ isFocused,
273
+ getBaseProps,
274
+ getInputOtpProps,
275
+ getSegmentWrapperProps,
276
+ getHelperWrapperProps,
277
+ getErrorMessageProps,
278
+ getDescriptionProps
279
+ };
280
+ }
281
+
282
+ // src/input-otp-context.ts
283
+ var import_react_utils2 = require("@sytechui/react-utils");
284
+ var [InputOtpProvider, useInputOtpContext] = (0, import_react_utils2.createContext)({
285
+ name: "InputOtpContext",
286
+ errorMessage: "useInputOtpContext: `context` is undefined. Seems like you forgot to wrap all input-otp components within `<InputOtp />`"
287
+ });
288
+
289
+ // src/input-otp-segment.tsx
290
+ var import_react2 = require("react");
291
+ var import_shared_utils2 = require("@sytechui/shared-utils");
292
+ var import_theme2 = require("@sytechui/theme");
293
+ var import_jsx_runtime = require("react/jsx-runtime");
294
+ var InputOtpSegment = ({
295
+ ...props
296
+ }) => {
297
+ var _a;
298
+ const { classNames, slots, type } = useInputOtpContext();
299
+ const passwordCharStyles = (0, import_theme2.cn)(classNames == null ? void 0 : classNames.passwordChar);
300
+ const caretStyles = (0, import_theme2.cn)(classNames == null ? void 0 : classNames.caret);
301
+ const segmentStyles = (0, import_theme2.cn)(classNames == null ? void 0 : classNames.segment);
302
+ const displayValue = (0, import_react2.useMemo)(() => {
303
+ var _a2, _b;
304
+ if (props.isActive && !props.char) {
305
+ return /* @__PURE__ */ (0, import_jsx_runtime.jsx)("div", { className: (0, import_theme2.cn)((_a2 = slots.caret) == null ? void 0 : _a2.call(slots, { class: caretStyles })) });
306
+ }
307
+ if (props.char) {
308
+ return type === "password" ? /* @__PURE__ */ (0, import_jsx_runtime.jsx)("div", { className: (0, import_theme2.cn)((_b = slots.passwordChar) == null ? void 0 : _b.call(slots, { class: passwordCharStyles })) }) : /* @__PURE__ */ (0, import_jsx_runtime.jsx)("div", { children: props.char });
309
+ }
310
+ return /* @__PURE__ */ (0, import_jsx_runtime.jsx)("div", { children: props.placeholderChar });
311
+ }, [props.char, props.isActive, props.placeholderChar, type]);
312
+ return /* @__PURE__ */ (0, import_jsx_runtime.jsx)(
313
+ "div",
314
+ {
315
+ className: (0, import_theme2.cn)((_a = slots.segment) == null ? void 0 : _a.call(slots, { class: segmentStyles })),
316
+ "data-active": (0, import_shared_utils2.dataAttr)(props.isActive),
317
+ "data-focus": (0, import_shared_utils2.dataAttr)(props.isFocused && props.isActive),
318
+ "data-focus-visible": (0, import_shared_utils2.dataAttr)(props.isFocusVisible && props.isActive),
319
+ "data-has-value": (0, import_shared_utils2.dataAttr)(!!props.char),
320
+ "data-slot": "segment2",
321
+ role: "presentation",
322
+ children: displayValue
323
+ }
324
+ );
325
+ };
326
+
327
+ // src/input-otp.tsx
328
+ var import_jsx_runtime2 = require("react/jsx-runtime");
329
+ var InputOtp = (0, import_system2.forwardRef)((props, ref) => {
330
+ const context = useInputOtp({ ...props, ref });
331
+ const {
332
+ Component,
333
+ hasHelper,
334
+ isInvalid,
335
+ errorMessage,
336
+ description,
337
+ isFocusVisible,
338
+ isFocused,
339
+ getBaseProps,
340
+ getInputOtpProps,
341
+ getSegmentWrapperProps,
342
+ getHelperWrapperProps,
343
+ getErrorMessageProps,
344
+ getDescriptionProps
345
+ } = context;
346
+ const helperSection = (0, import_react3.useMemo)(() => {
347
+ if (!hasHelper) {
348
+ return null;
349
+ }
350
+ return /* @__PURE__ */ (0, import_jsx_runtime2.jsx)("div", { ...getHelperWrapperProps(), children: isInvalid && errorMessage ? /* @__PURE__ */ (0, import_jsx_runtime2.jsx)("div", { ...getErrorMessageProps(), children: errorMessage }) : /* @__PURE__ */ (0, import_jsx_runtime2.jsx)("div", { ...getDescriptionProps(), children: description }) });
351
+ }, [
352
+ hasHelper,
353
+ isInvalid,
354
+ errorMessage,
355
+ description,
356
+ getHelperWrapperProps,
357
+ getErrorMessageProps,
358
+ getDescriptionProps
359
+ ]);
360
+ return /* @__PURE__ */ (0, import_jsx_runtime2.jsx)(InputOtpProvider, { value: context, children: /* @__PURE__ */ (0, import_jsx_runtime2.jsxs)(Component, { ...getBaseProps(), children: [
361
+ /* @__PURE__ */ (0, import_jsx_runtime2.jsx)(
362
+ import_input_otp.OTPInput,
363
+ {
364
+ ...getInputOtpProps(),
365
+ render: ({ slots }) => /* @__PURE__ */ (0, import_jsx_runtime2.jsx)("div", { ...getSegmentWrapperProps(), children: slots.map((slot, idx) => /* @__PURE__ */ (0, import_jsx_runtime2.jsx)(
366
+ InputOtpSegment,
367
+ {
368
+ ...slot,
369
+ isFocusVisible,
370
+ isFocused
371
+ },
372
+ idx
373
+ )) })
374
+ }
375
+ ),
376
+ helperSection
377
+ ] }) });
378
+ });
379
+ InputOtp.displayName = "HeroUI.InputOtp";
380
+ var input_otp_default = InputOtp;
381
+
382
+ // src/index.ts
383
+ var import_input_otp3 = require("input-otp");
384
+ // Annotate the CommonJS export names for ESM import in node:
385
+ 0 && (module.exports = {
386
+ InputOtp,
387
+ REGEXP_ONLY_CHARS,
388
+ REGEXP_ONLY_DIGITS,
389
+ REGEXP_ONLY_DIGITS_AND_CHARS,
390
+ useInputOtp
391
+ });
package/dist/index.mjs ADDED
@@ -0,0 +1,19 @@
1
+ "use client";
2
+ import {
3
+ input_otp_default
4
+ } from "./chunk-ZVXVMEIW.mjs";
5
+ import "./chunk-IJHHP6DJ.mjs";
6
+ import "./chunk-R2XKH6IK.mjs";
7
+ import {
8
+ useInputOtp
9
+ } from "./chunk-VZ7MKXTH.mjs";
10
+
11
+ // src/index.ts
12
+ import { REGEXP_ONLY_DIGITS, REGEXP_ONLY_CHARS, REGEXP_ONLY_DIGITS_AND_CHARS } from "input-otp";
13
+ export {
14
+ input_otp_default as InputOtp,
15
+ REGEXP_ONLY_CHARS,
16
+ REGEXP_ONLY_DIGITS,
17
+ REGEXP_ONLY_DIGITS_AND_CHARS,
18
+ useInputOtp
19
+ };