@yamada-ui/pin-input 0.1.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/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2023 Hirotomo Yamada
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,28 @@
1
+ # @yamada-ui/pin-input
2
+
3
+ ## Installation
4
+
5
+ ```sh
6
+ $ pnpm add @yamada-ui/pin-input
7
+ ```
8
+
9
+ or
10
+
11
+ ```sh
12
+ $ yarn add @yamada-ui/pin-input
13
+ ```
14
+
15
+ or
16
+
17
+ ```sh
18
+ $ npm install @yamada-ui/pin-input
19
+ ```
20
+
21
+ ## Contribution
22
+
23
+ Wouldn't you like to contribute? That's amazing! We have prepared a [contribution guide](https://github.com/hirotomoyamada/yamada-ui/blob/main/CONTRIBUTING.md) to assist you.
24
+
25
+ ## Licence
26
+
27
+ This package is licensed under the terms of the
28
+ [MIT license](https://github.com/hirotomoyamada/yamada-ui/blob/main/LICENSE).
@@ -0,0 +1,228 @@
1
+ // src/pin-input.tsx
2
+ import {
3
+ ui,
4
+ forwardRef,
5
+ useMultiComponentStyle,
6
+ omitThemeProps
7
+ } from "@yamada-ui/core";
8
+ import {
9
+ useFormControlProps,
10
+ formControlProperties
11
+ } from "@yamada-ui/form-control";
12
+ import { useControllableState } from "@yamada-ui/use-controllable-state";
13
+ import { createDescendant } from "@yamada-ui/use-descendant";
14
+ import {
15
+ createContext,
16
+ cx,
17
+ handlerAll,
18
+ mergeRefs,
19
+ pickObject,
20
+ filterUndefined,
21
+ getValidChildren
22
+ } from "@yamada-ui/utils";
23
+ import { useCallback, useEffect, useId, useState } from "react";
24
+ import { jsx } from "react/jsx-runtime";
25
+ var toArray = (value) => value == null ? void 0 : value.split("");
26
+ var validate = (value, type) => {
27
+ const NUMERIC_REGEX = /^[0-9]+$/;
28
+ const ALPHA_NUMERIC_REGEX = /^[a-zA-Z0-9]+$/i;
29
+ const regex = type === "alphanumeric" ? ALPHA_NUMERIC_REGEX : NUMERIC_REGEX;
30
+ return regex.test(value);
31
+ };
32
+ var [PinInputProvider, usePinInputContext] = createContext({
33
+ strict: false,
34
+ name: "PinInputContext"
35
+ });
36
+ var { DescendantsContextProvider, useDescendants, useDescendant } = createDescendant();
37
+ var PinInput = forwardRef(
38
+ ({ focusBorderColor, errorBorderColor, ...props }, ref) => {
39
+ const [styles, mergedProps] = useMultiComponentStyle("PinInput", {
40
+ focusBorderColor,
41
+ errorBorderColor,
42
+ ...props
43
+ });
44
+ let {
45
+ id,
46
+ className,
47
+ type = "number",
48
+ placeholder = "\u25CB",
49
+ value,
50
+ defaultValue,
51
+ autoFocus,
52
+ manageFocus = true,
53
+ otp = false,
54
+ mask,
55
+ onChange,
56
+ onComplete,
57
+ fileds = 4,
58
+ children,
59
+ ...rest
60
+ } = useFormControlProps(omitThemeProps(mergedProps));
61
+ id = id != null ? id : useId();
62
+ const descendants = useDescendants();
63
+ const [moveFocus, setMoveFocus] = useState(true);
64
+ const [focusedIndex, setFocusedIndex] = useState(-1);
65
+ useEffect(() => {
66
+ if (!autoFocus)
67
+ return;
68
+ const firstValue = descendants.firstValue();
69
+ if (!firstValue)
70
+ return;
71
+ requestAnimationFrame(() => firstValue.node.focus());
72
+ }, [autoFocus, descendants]);
73
+ const [values, setValues] = useControllableState({
74
+ value: toArray(value),
75
+ defaultValue: toArray(defaultValue) || [],
76
+ onChange: (values2) => onChange == null ? void 0 : onChange(values2.join(""))
77
+ });
78
+ const focusNext = useCallback(
79
+ (index) => {
80
+ if (!moveFocus || !manageFocus)
81
+ return;
82
+ const next = descendants.nextValue(index, void 0, false);
83
+ if (!next)
84
+ return;
85
+ requestAnimationFrame(() => next.node.focus());
86
+ },
87
+ [descendants, moveFocus, manageFocus]
88
+ );
89
+ const setValue = useCallback(
90
+ (value2, index, isFocus = true) => {
91
+ var _a;
92
+ let nextValues = [...values];
93
+ nextValues[index] = value2;
94
+ setValues(nextValues);
95
+ nextValues = nextValues.filter(Boolean);
96
+ const isComplete = value2 !== "" && nextValues.length === descendants.count() && nextValues.every((value3) => value3 != null && value3 !== "");
97
+ if (isComplete) {
98
+ onComplete == null ? void 0 : onComplete(nextValues.join(""));
99
+ (_a = descendants.value(index)) == null ? void 0 : _a.node.blur();
100
+ } else if (isFocus) {
101
+ focusNext(index);
102
+ }
103
+ },
104
+ [values, setValues, descendants, onComplete, focusNext]
105
+ );
106
+ const getNextValue = useCallback((value2, eventValue) => {
107
+ let nextValue = eventValue;
108
+ if (!(value2 == null ? void 0 : value2.length))
109
+ return nextValue;
110
+ if (value2[0] === eventValue.charAt(0)) {
111
+ nextValue = eventValue.charAt(1);
112
+ } else if (value2[0] === eventValue.charAt(1)) {
113
+ nextValue = eventValue.charAt(0);
114
+ }
115
+ return nextValue;
116
+ }, []);
117
+ const getInputProps = useCallback(
118
+ ({
119
+ index,
120
+ ...props2
121
+ }) => {
122
+ const onChange2 = ({ target }) => {
123
+ var _a;
124
+ const eventValue = target.value;
125
+ const currentValue = values[index];
126
+ const nextValue = getNextValue(currentValue, eventValue);
127
+ if (nextValue === "") {
128
+ setValue("", index);
129
+ return;
130
+ }
131
+ if (eventValue.length > 2) {
132
+ if (!validate(eventValue, type))
133
+ return;
134
+ const nextValue2 = eventValue.split("").filter((_, index2) => index2 < descendants.count());
135
+ setValues(nextValue2);
136
+ if (nextValue2.length === descendants.count()) {
137
+ onComplete == null ? void 0 : onComplete(nextValue2.join(""));
138
+ (_a = descendants.value(index)) == null ? void 0 : _a.node.blur();
139
+ }
140
+ } else {
141
+ if (validate(nextValue, type))
142
+ setValue(nextValue, index);
143
+ setMoveFocus(true);
144
+ }
145
+ };
146
+ const onKeyDown = ({ key, target }) => {
147
+ var _a;
148
+ if (key !== "Backspace" || !manageFocus)
149
+ return;
150
+ if (target.value === "") {
151
+ const prevInput = descendants.prevValue(index, void 0, false);
152
+ if (!prevInput)
153
+ return;
154
+ setValue("", index - 1, false);
155
+ (_a = prevInput.node) == null ? void 0 : _a.focus();
156
+ setMoveFocus(true);
157
+ } else {
158
+ setMoveFocus(false);
159
+ }
160
+ };
161
+ const onFocus = () => setFocusedIndex(index);
162
+ const onBlur = () => setFocusedIndex(-1);
163
+ return {
164
+ inputMode: type === "number" ? "numeric" : "text",
165
+ type: mask ? "password" : type === "number" ? "tel" : "text",
166
+ ...pickObject(rest, formControlProperties),
167
+ ...filterUndefined(props2),
168
+ id: `${id}-${index}`,
169
+ value: values[index] || "",
170
+ onChange: handlerAll(props2.onChange, onChange2),
171
+ onKeyDown: handlerAll(props2.onKeyDown, onKeyDown),
172
+ onFocus: handlerAll(props2.onFocus, onFocus),
173
+ onBlur: handlerAll(props2.onBlur, onBlur),
174
+ autoComplete: otp ? "one-time-code" : "off",
175
+ placeholder: focusedIndex === index && !rest.readOnly && !props2.readOnly ? "" : placeholder
176
+ };
177
+ },
178
+ [
179
+ descendants,
180
+ focusedIndex,
181
+ getNextValue,
182
+ id,
183
+ manageFocus,
184
+ mask,
185
+ onComplete,
186
+ otp,
187
+ placeholder,
188
+ rest,
189
+ setValue,
190
+ setValues,
191
+ type,
192
+ values
193
+ ]
194
+ );
195
+ const css = {
196
+ display: "flex",
197
+ alignItems: "center",
198
+ ...styles.container
199
+ };
200
+ let cloneChildren = getValidChildren(children);
201
+ if (!cloneChildren.length)
202
+ for (let i = 0; i < fileds; i++) {
203
+ cloneChildren.push(/* @__PURE__ */ jsx(PinInputField, {}, i));
204
+ }
205
+ return /* @__PURE__ */ jsx(DescendantsContextProvider, { value: descendants, children: /* @__PURE__ */ jsx(PinInputProvider, { value: { getInputProps, styles }, children: /* @__PURE__ */ jsx(ui.div, { ref, className: cx("ui-pin-input", className), ...rest, __css: css, children: cloneChildren }) }) });
206
+ }
207
+ );
208
+ var PinInputField = forwardRef(
209
+ ({ className, ...rest }, ref) => {
210
+ const { getInputProps, styles } = usePinInputContext();
211
+ const { index, register } = useDescendant();
212
+ rest = useFormControlProps(rest);
213
+ const css = { ...styles.field };
214
+ return /* @__PURE__ */ jsx(
215
+ ui.input,
216
+ {
217
+ className: cx("ui-pin-input-field", className),
218
+ ...getInputProps({ ...rest, ref: mergeRefs(register, ref), index }),
219
+ __css: css
220
+ }
221
+ );
222
+ }
223
+ );
224
+
225
+ export {
226
+ PinInput,
227
+ PinInputField
228
+ };
@@ -0,0 +1,3 @@
1
+ export { PinInput, PinInputField, PinInputFieldProps, PinInputProps } from './pin-input.js';
2
+ import '@yamada-ui/core';
3
+ import '@yamada-ui/form-control';
package/dist/index.js ADDED
@@ -0,0 +1,239 @@
1
+ "use strict";
2
+ var __defProp = Object.defineProperty;
3
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
4
+ var __getOwnPropNames = Object.getOwnPropertyNames;
5
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
6
+ var __export = (target, all) => {
7
+ for (var name in all)
8
+ __defProp(target, name, { get: all[name], enumerable: true });
9
+ };
10
+ var __copyProps = (to, from, except, desc) => {
11
+ if (from && typeof from === "object" || typeof from === "function") {
12
+ for (let key of __getOwnPropNames(from))
13
+ if (!__hasOwnProp.call(to, key) && key !== except)
14
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
15
+ }
16
+ return to;
17
+ };
18
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
19
+
20
+ // src/index.ts
21
+ var src_exports = {};
22
+ __export(src_exports, {
23
+ PinInput: () => PinInput,
24
+ PinInputField: () => PinInputField
25
+ });
26
+ module.exports = __toCommonJS(src_exports);
27
+
28
+ // src/pin-input.tsx
29
+ var import_core = require("@yamada-ui/core");
30
+ var import_form_control = require("@yamada-ui/form-control");
31
+ var import_use_controllable_state = require("@yamada-ui/use-controllable-state");
32
+ var import_use_descendant = require("@yamada-ui/use-descendant");
33
+ var import_utils = require("@yamada-ui/utils");
34
+ var import_react = require("react");
35
+ var import_jsx_runtime = require("react/jsx-runtime");
36
+ var toArray = (value) => value == null ? void 0 : value.split("");
37
+ var validate = (value, type) => {
38
+ const NUMERIC_REGEX = /^[0-9]+$/;
39
+ const ALPHA_NUMERIC_REGEX = /^[a-zA-Z0-9]+$/i;
40
+ const regex = type === "alphanumeric" ? ALPHA_NUMERIC_REGEX : NUMERIC_REGEX;
41
+ return regex.test(value);
42
+ };
43
+ var [PinInputProvider, usePinInputContext] = (0, import_utils.createContext)({
44
+ strict: false,
45
+ name: "PinInputContext"
46
+ });
47
+ var { DescendantsContextProvider, useDescendants, useDescendant } = (0, import_use_descendant.createDescendant)();
48
+ var PinInput = (0, import_core.forwardRef)(
49
+ ({ focusBorderColor, errorBorderColor, ...props }, ref) => {
50
+ const [styles, mergedProps] = (0, import_core.useMultiComponentStyle)("PinInput", {
51
+ focusBorderColor,
52
+ errorBorderColor,
53
+ ...props
54
+ });
55
+ let {
56
+ id,
57
+ className,
58
+ type = "number",
59
+ placeholder = "\u25CB",
60
+ value,
61
+ defaultValue,
62
+ autoFocus,
63
+ manageFocus = true,
64
+ otp = false,
65
+ mask,
66
+ onChange,
67
+ onComplete,
68
+ fileds = 4,
69
+ children,
70
+ ...rest
71
+ } = (0, import_form_control.useFormControlProps)((0, import_core.omitThemeProps)(mergedProps));
72
+ id = id != null ? id : (0, import_react.useId)();
73
+ const descendants = useDescendants();
74
+ const [moveFocus, setMoveFocus] = (0, import_react.useState)(true);
75
+ const [focusedIndex, setFocusedIndex] = (0, import_react.useState)(-1);
76
+ (0, import_react.useEffect)(() => {
77
+ if (!autoFocus)
78
+ return;
79
+ const firstValue = descendants.firstValue();
80
+ if (!firstValue)
81
+ return;
82
+ requestAnimationFrame(() => firstValue.node.focus());
83
+ }, [autoFocus, descendants]);
84
+ const [values, setValues] = (0, import_use_controllable_state.useControllableState)({
85
+ value: toArray(value),
86
+ defaultValue: toArray(defaultValue) || [],
87
+ onChange: (values2) => onChange == null ? void 0 : onChange(values2.join(""))
88
+ });
89
+ const focusNext = (0, import_react.useCallback)(
90
+ (index) => {
91
+ if (!moveFocus || !manageFocus)
92
+ return;
93
+ const next = descendants.nextValue(index, void 0, false);
94
+ if (!next)
95
+ return;
96
+ requestAnimationFrame(() => next.node.focus());
97
+ },
98
+ [descendants, moveFocus, manageFocus]
99
+ );
100
+ const setValue = (0, import_react.useCallback)(
101
+ (value2, index, isFocus = true) => {
102
+ var _a;
103
+ let nextValues = [...values];
104
+ nextValues[index] = value2;
105
+ setValues(nextValues);
106
+ nextValues = nextValues.filter(Boolean);
107
+ const isComplete = value2 !== "" && nextValues.length === descendants.count() && nextValues.every((value3) => value3 != null && value3 !== "");
108
+ if (isComplete) {
109
+ onComplete == null ? void 0 : onComplete(nextValues.join(""));
110
+ (_a = descendants.value(index)) == null ? void 0 : _a.node.blur();
111
+ } else if (isFocus) {
112
+ focusNext(index);
113
+ }
114
+ },
115
+ [values, setValues, descendants, onComplete, focusNext]
116
+ );
117
+ const getNextValue = (0, import_react.useCallback)((value2, eventValue) => {
118
+ let nextValue = eventValue;
119
+ if (!(value2 == null ? void 0 : value2.length))
120
+ return nextValue;
121
+ if (value2[0] === eventValue.charAt(0)) {
122
+ nextValue = eventValue.charAt(1);
123
+ } else if (value2[0] === eventValue.charAt(1)) {
124
+ nextValue = eventValue.charAt(0);
125
+ }
126
+ return nextValue;
127
+ }, []);
128
+ const getInputProps = (0, import_react.useCallback)(
129
+ ({
130
+ index,
131
+ ...props2
132
+ }) => {
133
+ const onChange2 = ({ target }) => {
134
+ var _a;
135
+ const eventValue = target.value;
136
+ const currentValue = values[index];
137
+ const nextValue = getNextValue(currentValue, eventValue);
138
+ if (nextValue === "") {
139
+ setValue("", index);
140
+ return;
141
+ }
142
+ if (eventValue.length > 2) {
143
+ if (!validate(eventValue, type))
144
+ return;
145
+ const nextValue2 = eventValue.split("").filter((_, index2) => index2 < descendants.count());
146
+ setValues(nextValue2);
147
+ if (nextValue2.length === descendants.count()) {
148
+ onComplete == null ? void 0 : onComplete(nextValue2.join(""));
149
+ (_a = descendants.value(index)) == null ? void 0 : _a.node.blur();
150
+ }
151
+ } else {
152
+ if (validate(nextValue, type))
153
+ setValue(nextValue, index);
154
+ setMoveFocus(true);
155
+ }
156
+ };
157
+ const onKeyDown = ({ key, target }) => {
158
+ var _a;
159
+ if (key !== "Backspace" || !manageFocus)
160
+ return;
161
+ if (target.value === "") {
162
+ const prevInput = descendants.prevValue(index, void 0, false);
163
+ if (!prevInput)
164
+ return;
165
+ setValue("", index - 1, false);
166
+ (_a = prevInput.node) == null ? void 0 : _a.focus();
167
+ setMoveFocus(true);
168
+ } else {
169
+ setMoveFocus(false);
170
+ }
171
+ };
172
+ const onFocus = () => setFocusedIndex(index);
173
+ const onBlur = () => setFocusedIndex(-1);
174
+ return {
175
+ inputMode: type === "number" ? "numeric" : "text",
176
+ type: mask ? "password" : type === "number" ? "tel" : "text",
177
+ ...(0, import_utils.pickObject)(rest, import_form_control.formControlProperties),
178
+ ...(0, import_utils.filterUndefined)(props2),
179
+ id: `${id}-${index}`,
180
+ value: values[index] || "",
181
+ onChange: (0, import_utils.handlerAll)(props2.onChange, onChange2),
182
+ onKeyDown: (0, import_utils.handlerAll)(props2.onKeyDown, onKeyDown),
183
+ onFocus: (0, import_utils.handlerAll)(props2.onFocus, onFocus),
184
+ onBlur: (0, import_utils.handlerAll)(props2.onBlur, onBlur),
185
+ autoComplete: otp ? "one-time-code" : "off",
186
+ placeholder: focusedIndex === index && !rest.readOnly && !props2.readOnly ? "" : placeholder
187
+ };
188
+ },
189
+ [
190
+ descendants,
191
+ focusedIndex,
192
+ getNextValue,
193
+ id,
194
+ manageFocus,
195
+ mask,
196
+ onComplete,
197
+ otp,
198
+ placeholder,
199
+ rest,
200
+ setValue,
201
+ setValues,
202
+ type,
203
+ values
204
+ ]
205
+ );
206
+ const css = {
207
+ display: "flex",
208
+ alignItems: "center",
209
+ ...styles.container
210
+ };
211
+ let cloneChildren = (0, import_utils.getValidChildren)(children);
212
+ if (!cloneChildren.length)
213
+ for (let i = 0; i < fileds; i++) {
214
+ cloneChildren.push(/* @__PURE__ */ (0, import_jsx_runtime.jsx)(PinInputField, {}, i));
215
+ }
216
+ return /* @__PURE__ */ (0, import_jsx_runtime.jsx)(DescendantsContextProvider, { value: descendants, children: /* @__PURE__ */ (0, import_jsx_runtime.jsx)(PinInputProvider, { value: { getInputProps, styles }, children: /* @__PURE__ */ (0, import_jsx_runtime.jsx)(import_core.ui.div, { ref, className: (0, import_utils.cx)("ui-pin-input", className), ...rest, __css: css, children: cloneChildren }) }) });
217
+ }
218
+ );
219
+ var PinInputField = (0, import_core.forwardRef)(
220
+ ({ className, ...rest }, ref) => {
221
+ const { getInputProps, styles } = usePinInputContext();
222
+ const { index, register } = useDescendant();
223
+ rest = (0, import_form_control.useFormControlProps)(rest);
224
+ const css = { ...styles.field };
225
+ return /* @__PURE__ */ (0, import_jsx_runtime.jsx)(
226
+ import_core.ui.input,
227
+ {
228
+ className: (0, import_utils.cx)("ui-pin-input-field", className),
229
+ ...getInputProps({ ...rest, ref: (0, import_utils.mergeRefs)(register, ref), index }),
230
+ __css: css
231
+ }
232
+ );
233
+ }
234
+ );
235
+ // Annotate the CommonJS export names for ESM import in node:
236
+ 0 && (module.exports = {
237
+ PinInput,
238
+ PinInputField
239
+ });
package/dist/index.mjs ADDED
@@ -0,0 +1,8 @@
1
+ import {
2
+ PinInput,
3
+ PinInputField
4
+ } from "./chunk-WNHBQS2L.mjs";
5
+ export {
6
+ PinInput,
7
+ PinInputField
8
+ };
@@ -0,0 +1,26 @@
1
+ import * as _yamada_ui_core from '@yamada-ui/core';
2
+ import { HTMLUIProps, ThemeProps, CSSUIProps } from '@yamada-ui/core';
3
+ import { FormControlOptions } from '@yamada-ui/form-control';
4
+
5
+ type PinInputOptions = {
6
+ id?: string;
7
+ type?: 'alphanumeric' | 'number';
8
+ placeholder?: string;
9
+ value?: string;
10
+ defaultValue?: string;
11
+ autoFocus?: boolean;
12
+ manageFocus?: boolean;
13
+ otp?: boolean;
14
+ mask?: boolean;
15
+ onChange?: (value: string) => void;
16
+ onComplete?: (value: string) => void;
17
+ fileds?: number;
18
+ focusBorderColor?: CSSUIProps<'unresponsive'>['borderColor'];
19
+ errorBorderColor?: CSSUIProps<'unresponsive'>['borderColor'];
20
+ };
21
+ type PinInputProps = Omit<HTMLUIProps<'div'>, 'onChange'> & ThemeProps<'PinInput'> & FormControlOptions & PinInputOptions;
22
+ declare const PinInput: _yamada_ui_core.Component<"div", PinInputProps>;
23
+ type PinInputFieldProps = HTMLUIProps<'input'> & FormControlOptions;
24
+ declare const PinInputField: _yamada_ui_core.Component<"input", PinInputFieldProps>;
25
+
26
+ export { PinInput, PinInputField, PinInputFieldProps, PinInputProps };
@@ -0,0 +1,237 @@
1
+ "use strict";
2
+ var __defProp = Object.defineProperty;
3
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
4
+ var __getOwnPropNames = Object.getOwnPropertyNames;
5
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
6
+ var __export = (target, all) => {
7
+ for (var name in all)
8
+ __defProp(target, name, { get: all[name], enumerable: true });
9
+ };
10
+ var __copyProps = (to, from, except, desc) => {
11
+ if (from && typeof from === "object" || typeof from === "function") {
12
+ for (let key of __getOwnPropNames(from))
13
+ if (!__hasOwnProp.call(to, key) && key !== except)
14
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
15
+ }
16
+ return to;
17
+ };
18
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
19
+
20
+ // src/pin-input.tsx
21
+ var pin_input_exports = {};
22
+ __export(pin_input_exports, {
23
+ PinInput: () => PinInput,
24
+ PinInputField: () => PinInputField
25
+ });
26
+ module.exports = __toCommonJS(pin_input_exports);
27
+ var import_core = require("@yamada-ui/core");
28
+ var import_form_control = require("@yamada-ui/form-control");
29
+ var import_use_controllable_state = require("@yamada-ui/use-controllable-state");
30
+ var import_use_descendant = require("@yamada-ui/use-descendant");
31
+ var import_utils = require("@yamada-ui/utils");
32
+ var import_react = require("react");
33
+ var import_jsx_runtime = require("react/jsx-runtime");
34
+ var toArray = (value) => value == null ? void 0 : value.split("");
35
+ var validate = (value, type) => {
36
+ const NUMERIC_REGEX = /^[0-9]+$/;
37
+ const ALPHA_NUMERIC_REGEX = /^[a-zA-Z0-9]+$/i;
38
+ const regex = type === "alphanumeric" ? ALPHA_NUMERIC_REGEX : NUMERIC_REGEX;
39
+ return regex.test(value);
40
+ };
41
+ var [PinInputProvider, usePinInputContext] = (0, import_utils.createContext)({
42
+ strict: false,
43
+ name: "PinInputContext"
44
+ });
45
+ var { DescendantsContextProvider, useDescendants, useDescendant } = (0, import_use_descendant.createDescendant)();
46
+ var PinInput = (0, import_core.forwardRef)(
47
+ ({ focusBorderColor, errorBorderColor, ...props }, ref) => {
48
+ const [styles, mergedProps] = (0, import_core.useMultiComponentStyle)("PinInput", {
49
+ focusBorderColor,
50
+ errorBorderColor,
51
+ ...props
52
+ });
53
+ let {
54
+ id,
55
+ className,
56
+ type = "number",
57
+ placeholder = "\u25CB",
58
+ value,
59
+ defaultValue,
60
+ autoFocus,
61
+ manageFocus = true,
62
+ otp = false,
63
+ mask,
64
+ onChange,
65
+ onComplete,
66
+ fileds = 4,
67
+ children,
68
+ ...rest
69
+ } = (0, import_form_control.useFormControlProps)((0, import_core.omitThemeProps)(mergedProps));
70
+ id = id != null ? id : (0, import_react.useId)();
71
+ const descendants = useDescendants();
72
+ const [moveFocus, setMoveFocus] = (0, import_react.useState)(true);
73
+ const [focusedIndex, setFocusedIndex] = (0, import_react.useState)(-1);
74
+ (0, import_react.useEffect)(() => {
75
+ if (!autoFocus)
76
+ return;
77
+ const firstValue = descendants.firstValue();
78
+ if (!firstValue)
79
+ return;
80
+ requestAnimationFrame(() => firstValue.node.focus());
81
+ }, [autoFocus, descendants]);
82
+ const [values, setValues] = (0, import_use_controllable_state.useControllableState)({
83
+ value: toArray(value),
84
+ defaultValue: toArray(defaultValue) || [],
85
+ onChange: (values2) => onChange == null ? void 0 : onChange(values2.join(""))
86
+ });
87
+ const focusNext = (0, import_react.useCallback)(
88
+ (index) => {
89
+ if (!moveFocus || !manageFocus)
90
+ return;
91
+ const next = descendants.nextValue(index, void 0, false);
92
+ if (!next)
93
+ return;
94
+ requestAnimationFrame(() => next.node.focus());
95
+ },
96
+ [descendants, moveFocus, manageFocus]
97
+ );
98
+ const setValue = (0, import_react.useCallback)(
99
+ (value2, index, isFocus = true) => {
100
+ var _a;
101
+ let nextValues = [...values];
102
+ nextValues[index] = value2;
103
+ setValues(nextValues);
104
+ nextValues = nextValues.filter(Boolean);
105
+ const isComplete = value2 !== "" && nextValues.length === descendants.count() && nextValues.every((value3) => value3 != null && value3 !== "");
106
+ if (isComplete) {
107
+ onComplete == null ? void 0 : onComplete(nextValues.join(""));
108
+ (_a = descendants.value(index)) == null ? void 0 : _a.node.blur();
109
+ } else if (isFocus) {
110
+ focusNext(index);
111
+ }
112
+ },
113
+ [values, setValues, descendants, onComplete, focusNext]
114
+ );
115
+ const getNextValue = (0, import_react.useCallback)((value2, eventValue) => {
116
+ let nextValue = eventValue;
117
+ if (!(value2 == null ? void 0 : value2.length))
118
+ return nextValue;
119
+ if (value2[0] === eventValue.charAt(0)) {
120
+ nextValue = eventValue.charAt(1);
121
+ } else if (value2[0] === eventValue.charAt(1)) {
122
+ nextValue = eventValue.charAt(0);
123
+ }
124
+ return nextValue;
125
+ }, []);
126
+ const getInputProps = (0, import_react.useCallback)(
127
+ ({
128
+ index,
129
+ ...props2
130
+ }) => {
131
+ const onChange2 = ({ target }) => {
132
+ var _a;
133
+ const eventValue = target.value;
134
+ const currentValue = values[index];
135
+ const nextValue = getNextValue(currentValue, eventValue);
136
+ if (nextValue === "") {
137
+ setValue("", index);
138
+ return;
139
+ }
140
+ if (eventValue.length > 2) {
141
+ if (!validate(eventValue, type))
142
+ return;
143
+ const nextValue2 = eventValue.split("").filter((_, index2) => index2 < descendants.count());
144
+ setValues(nextValue2);
145
+ if (nextValue2.length === descendants.count()) {
146
+ onComplete == null ? void 0 : onComplete(nextValue2.join(""));
147
+ (_a = descendants.value(index)) == null ? void 0 : _a.node.blur();
148
+ }
149
+ } else {
150
+ if (validate(nextValue, type))
151
+ setValue(nextValue, index);
152
+ setMoveFocus(true);
153
+ }
154
+ };
155
+ const onKeyDown = ({ key, target }) => {
156
+ var _a;
157
+ if (key !== "Backspace" || !manageFocus)
158
+ return;
159
+ if (target.value === "") {
160
+ const prevInput = descendants.prevValue(index, void 0, false);
161
+ if (!prevInput)
162
+ return;
163
+ setValue("", index - 1, false);
164
+ (_a = prevInput.node) == null ? void 0 : _a.focus();
165
+ setMoveFocus(true);
166
+ } else {
167
+ setMoveFocus(false);
168
+ }
169
+ };
170
+ const onFocus = () => setFocusedIndex(index);
171
+ const onBlur = () => setFocusedIndex(-1);
172
+ return {
173
+ inputMode: type === "number" ? "numeric" : "text",
174
+ type: mask ? "password" : type === "number" ? "tel" : "text",
175
+ ...(0, import_utils.pickObject)(rest, import_form_control.formControlProperties),
176
+ ...(0, import_utils.filterUndefined)(props2),
177
+ id: `${id}-${index}`,
178
+ value: values[index] || "",
179
+ onChange: (0, import_utils.handlerAll)(props2.onChange, onChange2),
180
+ onKeyDown: (0, import_utils.handlerAll)(props2.onKeyDown, onKeyDown),
181
+ onFocus: (0, import_utils.handlerAll)(props2.onFocus, onFocus),
182
+ onBlur: (0, import_utils.handlerAll)(props2.onBlur, onBlur),
183
+ autoComplete: otp ? "one-time-code" : "off",
184
+ placeholder: focusedIndex === index && !rest.readOnly && !props2.readOnly ? "" : placeholder
185
+ };
186
+ },
187
+ [
188
+ descendants,
189
+ focusedIndex,
190
+ getNextValue,
191
+ id,
192
+ manageFocus,
193
+ mask,
194
+ onComplete,
195
+ otp,
196
+ placeholder,
197
+ rest,
198
+ setValue,
199
+ setValues,
200
+ type,
201
+ values
202
+ ]
203
+ );
204
+ const css = {
205
+ display: "flex",
206
+ alignItems: "center",
207
+ ...styles.container
208
+ };
209
+ let cloneChildren = (0, import_utils.getValidChildren)(children);
210
+ if (!cloneChildren.length)
211
+ for (let i = 0; i < fileds; i++) {
212
+ cloneChildren.push(/* @__PURE__ */ (0, import_jsx_runtime.jsx)(PinInputField, {}, i));
213
+ }
214
+ return /* @__PURE__ */ (0, import_jsx_runtime.jsx)(DescendantsContextProvider, { value: descendants, children: /* @__PURE__ */ (0, import_jsx_runtime.jsx)(PinInputProvider, { value: { getInputProps, styles }, children: /* @__PURE__ */ (0, import_jsx_runtime.jsx)(import_core.ui.div, { ref, className: (0, import_utils.cx)("ui-pin-input", className), ...rest, __css: css, children: cloneChildren }) }) });
215
+ }
216
+ );
217
+ var PinInputField = (0, import_core.forwardRef)(
218
+ ({ className, ...rest }, ref) => {
219
+ const { getInputProps, styles } = usePinInputContext();
220
+ const { index, register } = useDescendant();
221
+ rest = (0, import_form_control.useFormControlProps)(rest);
222
+ const css = { ...styles.field };
223
+ return /* @__PURE__ */ (0, import_jsx_runtime.jsx)(
224
+ import_core.ui.input,
225
+ {
226
+ className: (0, import_utils.cx)("ui-pin-input-field", className),
227
+ ...getInputProps({ ...rest, ref: (0, import_utils.mergeRefs)(register, ref), index }),
228
+ __css: css
229
+ }
230
+ );
231
+ }
232
+ );
233
+ // Annotate the CommonJS export names for ESM import in node:
234
+ 0 && (module.exports = {
235
+ PinInput,
236
+ PinInputField
237
+ });
@@ -0,0 +1,8 @@
1
+ import {
2
+ PinInput,
3
+ PinInputField
4
+ } from "./chunk-WNHBQS2L.mjs";
5
+ export {
6
+ PinInput,
7
+ PinInputField
8
+ };
package/package.json ADDED
@@ -0,0 +1,78 @@
1
+ {
2
+ "name": "@yamada-ui/pin-input",
3
+ "version": "0.1.0",
4
+ "description": "Yamada UI pin input component",
5
+ "keywords": [
6
+ "yamada",
7
+ "yamada ui",
8
+ "react",
9
+ "emotion",
10
+ "component",
11
+ "pin-input",
12
+ "ui",
13
+ "uikit",
14
+ "styled",
15
+ "style-props",
16
+ "styled-component",
17
+ "css-in-js"
18
+ ],
19
+ "author": "Hirotomo Yamada <hirotomo.yamada@avap.co.jp>",
20
+ "license": "MIT",
21
+ "main": "dist/index.js",
22
+ "files": [
23
+ "dist"
24
+ ],
25
+ "sideEffects": false,
26
+ "publishConfig": {
27
+ "access": "public"
28
+ },
29
+ "repository": {
30
+ "type": "git",
31
+ "url": "git+https://github.com/hirotomoyamada/yamada-ui",
32
+ "directory": "packages/components/pin-input"
33
+ },
34
+ "bugs": {
35
+ "url": "https://github.com/hirotomoyamada/yamada-ui/issues"
36
+ },
37
+ "dependencies": {
38
+ "@yamada-ui/core": "0.1.0",
39
+ "@yamada-ui/form-control": "0.1.0",
40
+ "@yamada-ui/utils": "0.1.0",
41
+ "@yamada-ui/use-controllable-state": "0.1.0",
42
+ "@yamada-ui/use-descendant": "0.1.0"
43
+ },
44
+ "devDependencies": {
45
+ "react": "^18.0.0",
46
+ "clean-package": "2.2.0"
47
+ },
48
+ "peerDependencies": {
49
+ "react": ">=18"
50
+ },
51
+ "clean-package": "../../../clean-package.config.json",
52
+ "tsup": {
53
+ "clean": true,
54
+ "target": "es2019",
55
+ "format": [
56
+ "cjs",
57
+ "esm"
58
+ ]
59
+ },
60
+ "module": "dist/index.mjs",
61
+ "types": "dist/index.d.ts",
62
+ "exports": {
63
+ ".": {
64
+ "types": "./dist/index.d.ts",
65
+ "import": "./dist/index.mjs",
66
+ "require": "./dist/index.js"
67
+ },
68
+ "./package.json": "./package.json"
69
+ },
70
+ "scripts": {
71
+ "dev": "pnpm build:fast -- --watch",
72
+ "build": "tsup src --dts",
73
+ "build:fast": "tsup src",
74
+ "clean": "rimraf dist .turbo",
75
+ "typecheck": "tsc --noEmit",
76
+ "gen:docs": "tsx ../../../scripts/generate-docs"
77
+ }
78
+ }