@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/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2020 Next UI Inc.
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md ADDED
@@ -0,0 +1,26 @@
1
+ # @sytechui/input-otp
2
+
3
+ InputOTP is a component that allows users to enter otp input. It can be used to get user otp in forms.
4
+
5
+ This package contains the InputOTPcomponent.
6
+
7
+ Please refer to the [documentation](https://heroui.com/docs/components/input-otp) for more information.
8
+
9
+ ## Installation
10
+
11
+ ```sh
12
+ yarn add @sytechui/input-otp
13
+ # or
14
+ npm i @sytechui/input-otp
15
+ ```
16
+
17
+ ## Contribution
18
+
19
+ Yes please! See the
20
+ [contributing guidelines](https://github.com/heroui-inc/heroui/blob/master/CONTRIBUTING.md)
21
+ for details.
22
+
23
+ ## License
24
+
25
+ This project is licensed under the terms of the
26
+ [MIT license](https://github.com/heroui-inc/heroui/blob/master/LICENSE).
@@ -0,0 +1,46 @@
1
+ "use client";
2
+ import {
3
+ useInputOtpContext
4
+ } from "./chunk-R2XKH6IK.mjs";
5
+
6
+ // src/input-otp-segment.tsx
7
+ import { useMemo } from "react";
8
+ import { dataAttr } from "@sytechui/shared-utils";
9
+ import { cn } from "@sytechui/theme";
10
+ import { jsx } from "react/jsx-runtime";
11
+ var InputOtpSegment = ({
12
+ ...props
13
+ }) => {
14
+ var _a;
15
+ const { classNames, slots, type } = useInputOtpContext();
16
+ const passwordCharStyles = cn(classNames == null ? void 0 : classNames.passwordChar);
17
+ const caretStyles = cn(classNames == null ? void 0 : classNames.caret);
18
+ const segmentStyles = cn(classNames == null ? void 0 : classNames.segment);
19
+ const displayValue = useMemo(() => {
20
+ var _a2, _b;
21
+ if (props.isActive && !props.char) {
22
+ return /* @__PURE__ */ jsx("div", { className: cn((_a2 = slots.caret) == null ? void 0 : _a2.call(slots, { class: caretStyles })) });
23
+ }
24
+ if (props.char) {
25
+ return type === "password" ? /* @__PURE__ */ jsx("div", { className: cn((_b = slots.passwordChar) == null ? void 0 : _b.call(slots, { class: passwordCharStyles })) }) : /* @__PURE__ */ jsx("div", { children: props.char });
26
+ }
27
+ return /* @__PURE__ */ jsx("div", { children: props.placeholderChar });
28
+ }, [props.char, props.isActive, props.placeholderChar, type]);
29
+ return /* @__PURE__ */ jsx(
30
+ "div",
31
+ {
32
+ className: cn((_a = slots.segment) == null ? void 0 : _a.call(slots, { class: segmentStyles })),
33
+ "data-active": dataAttr(props.isActive),
34
+ "data-focus": dataAttr(props.isFocused && props.isActive),
35
+ "data-focus-visible": dataAttr(props.isFocusVisible && props.isActive),
36
+ "data-has-value": dataAttr(!!props.char),
37
+ "data-slot": "segment2",
38
+ role: "presentation",
39
+ children: displayValue
40
+ }
41
+ );
42
+ };
43
+
44
+ export {
45
+ InputOtpSegment
46
+ };
@@ -0,0 +1,13 @@
1
+ "use client";
2
+
3
+ // src/input-otp-context.ts
4
+ import { createContext } from "@sytechui/react-utils";
5
+ var [InputOtpProvider, useInputOtpContext] = createContext({
6
+ name: "InputOtpContext",
7
+ errorMessage: "useInputOtpContext: `context` is undefined. Seems like you forgot to wrap all input-otp components within `<InputOtp />`"
8
+ });
9
+
10
+ export {
11
+ InputOtpProvider,
12
+ useInputOtpContext
13
+ };
@@ -0,0 +1,250 @@
1
+ "use client";
2
+
3
+ // src/use-input-otp.ts
4
+ import { mapPropsVariants, useProviderContext } from "@sytechui/system";
5
+ import { inputOtp, cn } from "@sytechui/theme";
6
+ import { filterDOMProps, useDOMRef } from "@sytechui/react-utils";
7
+ import { dataAttr, objectToDeps, isPatternNumeric, chain, mergeProps } from "@sytechui/shared-utils";
8
+ import { useFormReset } from "@sytechui/use-form-reset";
9
+ import { useCallback, useMemo } from "react";
10
+ import { useControlledState } from "@react-stately/utils";
11
+ import { useFormValidationState } from "@react-stately/form";
12
+ import { useFormValidation } from "@react-aria/form";
13
+ import { useFocusRing } from "@react-aria/focus";
14
+ import { FormContext, useSlottedContext } from "@sytechui/form";
15
+ function useInputOtp(originalProps) {
16
+ var _a, _b, _c, _d;
17
+ const globalContext = useProviderContext();
18
+ const { validationBehavior: formValidationBehavior } = useSlottedContext(FormContext) || {};
19
+ const [props, variantProps] = mapPropsVariants(originalProps, inputOtp.variantKeys);
20
+ const {
21
+ ref,
22
+ baseRef,
23
+ as,
24
+ className,
25
+ classNames,
26
+ length = 4,
27
+ autoFocus,
28
+ "aria-label": ariaLabel = "One-time password input",
29
+ onValueChange = () => {
30
+ },
31
+ allowedKeys = "^[0-9]*$",
32
+ validationBehavior = (_a = formValidationBehavior != null ? formValidationBehavior : globalContext == null ? void 0 : globalContext.validationBehavior) != null ? _a : "native",
33
+ type,
34
+ name,
35
+ maxLength,
36
+ minLength,
37
+ textAlign = "center",
38
+ onComplete = () => {
39
+ },
40
+ pushPasswordManagerStrategy,
41
+ pasteTransformer,
42
+ containerClassName,
43
+ noScriptCSSFallback,
44
+ onChange,
45
+ inputMode,
46
+ ...otherProps
47
+ } = props;
48
+ const Component = as || "div";
49
+ const inputRef = useDOMRef(ref);
50
+ const baseDomRef = useDOMRef(baseRef);
51
+ const { isFocusVisible, isFocused, focusProps } = useFocusRing({
52
+ autoFocus,
53
+ isTextInput: true
54
+ });
55
+ const handleValueChange = useCallback(
56
+ (value2) => {
57
+ onValueChange(value2 != null ? value2 : "");
58
+ },
59
+ [onValueChange]
60
+ );
61
+ const [value, setValue] = useControlledState(
62
+ props.value,
63
+ (_b = props.defaultValue) != null ? _b : "",
64
+ handleValueChange
65
+ );
66
+ const disableAnimation = (_d = (_c = originalProps.disableAnimation) != null ? _c : globalContext == null ? void 0 : globalContext.disableAnimation) != null ? _d : false;
67
+ const isDisabled = originalProps.isDisabled;
68
+ const baseStyles = cn(classNames == null ? void 0 : classNames.base, className);
69
+ const validationState = useFormValidationState({
70
+ ...props,
71
+ validationBehavior,
72
+ value
73
+ });
74
+ useFormReset(inputRef, value, setValue);
75
+ useFormValidation({ ...props, validationBehavior }, validationState, inputRef);
76
+ const {
77
+ isInvalid: isAriaInvalid,
78
+ validationErrors,
79
+ validationDetails
80
+ } = validationState.displayValidation;
81
+ const isReadOnly = originalProps.isReadOnly;
82
+ const isRequired = originalProps.isRequired;
83
+ const isInvalid = originalProps.isInvalid || isAriaInvalid;
84
+ const errorMessage = typeof props.errorMessage === "function" ? props.errorMessage({ isInvalid, validationErrors, validationDetails }) : props.errorMessage || (validationErrors == null ? void 0 : validationErrors.join(" "));
85
+ const description = props.description;
86
+ const hasHelper = !!description || !!errorMessage;
87
+ const slots = useMemo(
88
+ () => inputOtp({
89
+ ...variantProps,
90
+ disableAnimation,
91
+ isInvalid,
92
+ isReadOnly
93
+ }),
94
+ [objectToDeps(variantProps), disableAnimation, isInvalid, isReadOnly]
95
+ );
96
+ const getBaseProps = useCallback(
97
+ (props2 = {}) => {
98
+ return {
99
+ ref: baseDomRef,
100
+ className: slots.base({
101
+ class: baseStyles
102
+ }),
103
+ "data-slot": "base",
104
+ "data-disabled": dataAttr(isDisabled),
105
+ "data-invalid": dataAttr(isInvalid),
106
+ "data-required": dataAttr(originalProps == null ? void 0 : originalProps.isRequired),
107
+ "data-readonly": dataAttr(originalProps == null ? void 0 : originalProps.isReadOnly),
108
+ "data-filled": dataAttr(value.length === length),
109
+ "aria-label": ariaLabel,
110
+ "aria-required": dataAttr(originalProps.isRequired),
111
+ "aria-readonly": dataAttr(originalProps == null ? void 0 : originalProps.isReadOnly),
112
+ role: "base",
113
+ ...mergeProps(
114
+ filterDOMProps(otherProps, {
115
+ enabled: true
116
+ }),
117
+ filterDOMProps(props2)
118
+ ),
119
+ onChange: (e) => {
120
+ var _a2;
121
+ const val = (_a2 = e.target) == null ? void 0 : _a2.value;
122
+ const regex = new RegExp(allowedKeys);
123
+ if (regex.test(val)) {
124
+ onChange == null ? void 0 : onChange(e);
125
+ }
126
+ }
127
+ };
128
+ },
129
+ [baseDomRef, slots, baseStyles, isDisabled, isInvalid, isRequired, isReadOnly, value, length]
130
+ );
131
+ const getInputOtpProps = useCallback(
132
+ (props2 = {}) => {
133
+ var _a2;
134
+ const otpProps = {
135
+ ...focusProps,
136
+ required: isRequired,
137
+ disabled: isDisabled,
138
+ readOnly: isReadOnly,
139
+ pattern: allowedKeys,
140
+ maxLength: maxLength != null ? maxLength : length,
141
+ minLength: minLength != null ? minLength : length,
142
+ textAlign,
143
+ ref: inputRef,
144
+ name,
145
+ value,
146
+ autoFocus,
147
+ onChange: setValue,
148
+ onBlur: chain(focusProps.onBlur, props2 == null ? void 0 : props2.onBlur),
149
+ onComplete,
150
+ pushPasswordManagerStrategy,
151
+ pasteTransformer,
152
+ noScriptCSSFallback,
153
+ inputMode: inputMode != null ? inputMode : isPatternNumeric(allowedKeys) ? "numeric" : "text",
154
+ containerClassName: (_a2 = slots.wrapper) == null ? void 0 : _a2.call(slots, { class: cn(classNames == null ? void 0 : classNames.wrapper, containerClassName) }),
155
+ ...props2
156
+ };
157
+ return otpProps;
158
+ },
159
+ [
160
+ inputMode,
161
+ isRequired,
162
+ isDisabled,
163
+ isReadOnly,
164
+ allowedKeys,
165
+ inputRef,
166
+ name,
167
+ value,
168
+ length,
169
+ setValue,
170
+ props.onBlur,
171
+ onComplete,
172
+ autoFocus
173
+ ]
174
+ );
175
+ const getSegmentWrapperProps = useCallback(
176
+ (props2 = {}) => {
177
+ return {
178
+ className: slots.segmentWrapper({
179
+ class: cn(classNames == null ? void 0 : classNames.segmentWrapper, props2 == null ? void 0 : props2.className)
180
+ }),
181
+ "data-slot": "segment-wrapper",
182
+ "data-disabled": dataAttr(isDisabled),
183
+ "aria-label": ariaLabel,
184
+ ...props2
185
+ };
186
+ },
187
+ [classNames == null ? void 0 : classNames.segmentWrapper, isDisabled]
188
+ );
189
+ const getHelperWrapperProps = useCallback(
190
+ (props2 = {}) => {
191
+ return {
192
+ className: slots.helperWrapper({
193
+ class: cn(classNames == null ? void 0 : classNames.helperWrapper, props2 == null ? void 0 : props2.className)
194
+ }),
195
+ "data-slot": "helper-wrapper",
196
+ ...props2
197
+ };
198
+ },
199
+ [classNames == null ? void 0 : classNames.helperWrapper]
200
+ );
201
+ const getErrorMessageProps = useCallback(
202
+ (props2 = {}) => {
203
+ return {
204
+ className: slots.errorMessage({
205
+ class: cn(classNames == null ? void 0 : classNames.errorMessage, props2 == null ? void 0 : props2.className)
206
+ }),
207
+ "data-slot": "error-message",
208
+ ...props2
209
+ };
210
+ },
211
+ [classNames == null ? void 0 : classNames.errorMessage]
212
+ );
213
+ const getDescriptionProps = useCallback(
214
+ (props2 = {}) => {
215
+ return {
216
+ className: slots.description({
217
+ class: cn(classNames == null ? void 0 : classNames.description, props2 == null ? void 0 : props2.className)
218
+ }),
219
+ "data-slot": "description",
220
+ ...props2
221
+ };
222
+ },
223
+ [classNames == null ? void 0 : classNames.description]
224
+ );
225
+ return {
226
+ Component,
227
+ inputRef,
228
+ length,
229
+ value,
230
+ type,
231
+ slots,
232
+ hasHelper,
233
+ classNames,
234
+ isInvalid,
235
+ description,
236
+ errorMessage,
237
+ isFocusVisible,
238
+ isFocused,
239
+ getBaseProps,
240
+ getInputOtpProps,
241
+ getSegmentWrapperProps,
242
+ getHelperWrapperProps,
243
+ getErrorMessageProps,
244
+ getDescriptionProps
245
+ };
246
+ }
247
+
248
+ export {
249
+ useInputOtp
250
+ };
@@ -0,0 +1,72 @@
1
+ "use client";
2
+ import {
3
+ InputOtpSegment
4
+ } from "./chunk-IJHHP6DJ.mjs";
5
+ import {
6
+ InputOtpProvider
7
+ } from "./chunk-R2XKH6IK.mjs";
8
+ import {
9
+ useInputOtp
10
+ } from "./chunk-VZ7MKXTH.mjs";
11
+
12
+ // src/input-otp.tsx
13
+ import { forwardRef } from "@sytechui/system";
14
+ import { useMemo } from "react";
15
+ import { OTPInput } from "input-otp";
16
+ import { jsx, jsxs } from "react/jsx-runtime";
17
+ var InputOtp = forwardRef((props, ref) => {
18
+ const context = useInputOtp({ ...props, ref });
19
+ const {
20
+ Component,
21
+ hasHelper,
22
+ isInvalid,
23
+ errorMessage,
24
+ description,
25
+ isFocusVisible,
26
+ isFocused,
27
+ getBaseProps,
28
+ getInputOtpProps,
29
+ getSegmentWrapperProps,
30
+ getHelperWrapperProps,
31
+ getErrorMessageProps,
32
+ getDescriptionProps
33
+ } = context;
34
+ const helperSection = useMemo(() => {
35
+ if (!hasHelper) {
36
+ return null;
37
+ }
38
+ return /* @__PURE__ */ jsx("div", { ...getHelperWrapperProps(), children: isInvalid && errorMessage ? /* @__PURE__ */ jsx("div", { ...getErrorMessageProps(), children: errorMessage }) : /* @__PURE__ */ jsx("div", { ...getDescriptionProps(), children: description }) });
39
+ }, [
40
+ hasHelper,
41
+ isInvalid,
42
+ errorMessage,
43
+ description,
44
+ getHelperWrapperProps,
45
+ getErrorMessageProps,
46
+ getDescriptionProps
47
+ ]);
48
+ return /* @__PURE__ */ jsx(InputOtpProvider, { value: context, children: /* @__PURE__ */ jsxs(Component, { ...getBaseProps(), children: [
49
+ /* @__PURE__ */ jsx(
50
+ OTPInput,
51
+ {
52
+ ...getInputOtpProps(),
53
+ render: ({ slots }) => /* @__PURE__ */ jsx("div", { ...getSegmentWrapperProps(), children: slots.map((slot, idx) => /* @__PURE__ */ jsx(
54
+ InputOtpSegment,
55
+ {
56
+ ...slot,
57
+ isFocusVisible,
58
+ isFocused
59
+ },
60
+ idx
61
+ )) })
62
+ }
63
+ ),
64
+ helperSection
65
+ ] }) });
66
+ });
67
+ InputOtp.displayName = "HeroUI.InputOtp";
68
+ var input_otp_default = InputOtp;
69
+
70
+ export {
71
+ input_otp_default
72
+ };
@@ -0,0 +1,9 @@
1
+ export { default as InputOtp, InputOtpProps } from './input-otp.mjs';
2
+ export { useInputOtp } from './use-input-otp.mjs';
3
+ export { REGEXP_ONLY_CHARS, REGEXP_ONLY_DIGITS, REGEXP_ONLY_DIGITS_AND_CHARS } from 'input-otp';
4
+ import '@sytechui/system';
5
+ import 'tailwind-variants';
6
+ import 'react';
7
+ import '@sytechui/theme';
8
+ import '@sytechui/react-utils';
9
+ import '@react-types/textfield';
@@ -0,0 +1,9 @@
1
+ export { default as InputOtp, InputOtpProps } from './input-otp.js';
2
+ export { useInputOtp } from './use-input-otp.js';
3
+ export { REGEXP_ONLY_CHARS, REGEXP_ONLY_DIGITS, REGEXP_ONLY_DIGITS_AND_CHARS } from 'input-otp';
4
+ import '@sytechui/system';
5
+ import 'tailwind-variants';
6
+ import 'react';
7
+ import '@sytechui/theme';
8
+ import '@sytechui/react-utils';
9
+ import '@react-types/textfield';