@yamada-ui/pin-input 1.0.28-dev-20240504023206 → 1.0.28-dev-20240504032813
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/{chunk-XNTJGWGG.mjs → chunk-LMKHNR4F.mjs} +95 -78
- package/dist/chunk-LMKHNR4F.mjs.map +1 -0
- package/dist/index.js +92 -75
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +1 -1
- package/dist/pin-input.js +92 -75
- package/dist/pin-input.js.map +1 -1
- package/dist/pin-input.mjs +1 -1
- package/package.json +3 -3
- package/dist/chunk-XNTJGWGG.mjs.map +0 -1
@@ -8,7 +8,7 @@ import {
|
|
8
8
|
omitThemeProps
|
9
9
|
} from "@yamada-ui/core";
|
10
10
|
import {
|
11
|
-
|
11
|
+
formControlProperties,
|
12
12
|
useFormControlProps
|
13
13
|
} from "@yamada-ui/form-control";
|
14
14
|
import { useControllableState } from "@yamada-ui/use-controllable-state";
|
@@ -22,7 +22,7 @@ import {
|
|
22
22
|
filterUndefined,
|
23
23
|
getValidChildren
|
24
24
|
} from "@yamada-ui/utils";
|
25
|
-
import { useCallback, useEffect, useId, useState } from "react";
|
25
|
+
import { useCallback, useEffect, useId, useMemo, useState } from "react";
|
26
26
|
import { jsx } from "react/jsx-runtime";
|
27
27
|
var toArray = (value) => value == null ? void 0 : value.split("");
|
28
28
|
var validate = (value, type) => {
|
@@ -54,12 +54,22 @@ var PinInput = forwardRef(
|
|
54
54
|
manageFocus = true,
|
55
55
|
otp = false,
|
56
56
|
mask,
|
57
|
-
|
57
|
+
readOnly,
|
58
|
+
"aria-readonly": ariaReadonly,
|
59
|
+
onChange: onChangeProp,
|
58
60
|
onComplete,
|
59
61
|
items = 4,
|
60
62
|
children,
|
61
63
|
...rest
|
62
64
|
} = useFormControlProps(omitThemeProps(mergedProps));
|
65
|
+
const formControlProps = useMemo(
|
66
|
+
() => ({
|
67
|
+
...pickObject(rest, formControlProperties),
|
68
|
+
readOnly,
|
69
|
+
ariaReadonly
|
70
|
+
}),
|
71
|
+
[ariaReadonly, readOnly, rest]
|
72
|
+
);
|
63
73
|
id != null ? id : id = useId();
|
64
74
|
const descendants = useDescendants();
|
65
75
|
const [moveFocus, setMoveFocus] = useState(true);
|
@@ -75,7 +85,7 @@ var PinInput = forwardRef(
|
|
75
85
|
const [values, setValues] = useControllableState({
|
76
86
|
value: toArray(value),
|
77
87
|
defaultValue: toArray(defaultValue) || [],
|
78
|
-
onChange: (values2) =>
|
88
|
+
onChange: (values2) => onChangeProp == null ? void 0 : onChangeProp(values2.join(""))
|
79
89
|
});
|
80
90
|
const focusNext = useCallback(
|
81
91
|
(index) => {
|
@@ -119,90 +129,98 @@ var PinInput = forwardRef(
|
|
119
129
|
},
|
120
130
|
[]
|
121
131
|
);
|
122
|
-
const
|
123
|
-
({
|
124
|
-
|
125
|
-
|
126
|
-
|
127
|
-
const
|
128
|
-
|
129
|
-
|
130
|
-
|
131
|
-
|
132
|
-
|
133
|
-
|
134
|
-
return;
|
135
|
-
}
|
136
|
-
if (eventValue.length > 2) {
|
137
|
-
if (!validate(eventValue, type))
|
138
|
-
return;
|
139
|
-
const nextValue2 = eventValue.split("").filter((_2, index2) => index2 < descendants.count());
|
140
|
-
setValues(nextValue2);
|
141
|
-
if (nextValue2.length === descendants.count()) {
|
142
|
-
onComplete == null ? void 0 : onComplete(nextValue2.join(""));
|
143
|
-
(_a = descendants.value(index)) == null ? void 0 : _a.node.blur();
|
144
|
-
}
|
145
|
-
} else {
|
146
|
-
if (validate(nextValue, type))
|
147
|
-
setValue(nextValue, index);
|
148
|
-
setMoveFocus(true);
|
149
|
-
}
|
150
|
-
};
|
151
|
-
const onKeyDown = ({
|
152
|
-
key,
|
153
|
-
target
|
154
|
-
}) => {
|
155
|
-
var _a;
|
156
|
-
if (key !== "Backspace" || !manageFocus)
|
132
|
+
const onChange = useCallback(
|
133
|
+
(index) => ({ 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))
|
157
144
|
return;
|
158
|
-
|
159
|
-
|
160
|
-
|
161
|
-
|
162
|
-
|
163
|
-
(_a = prevInput.node) == null ? void 0 : _a.focus();
|
164
|
-
setMoveFocus(true);
|
165
|
-
} else {
|
166
|
-
setMoveFocus(false);
|
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();
|
167
150
|
}
|
168
|
-
}
|
169
|
-
|
170
|
-
|
171
|
-
|
172
|
-
|
173
|
-
type: mask ? "password" : type === "number" ? "tel" : "text",
|
174
|
-
...pickObject(
|
175
|
-
rest,
|
176
|
-
getFormControlProperties({ omit: ["aria-readonly"] })
|
177
|
-
),
|
178
|
-
...filterUndefined(props2),
|
179
|
-
id: `${id}-${index}`,
|
180
|
-
value: values[index] || "",
|
181
|
-
onChange: handlerAll(props2.onChange, onChange2),
|
182
|
-
onKeyDown: handlerAll(props2.onKeyDown, onKeyDown),
|
183
|
-
onFocus: handlerAll(props2.onFocus, onFocus),
|
184
|
-
onBlur: handlerAll(props2.onBlur, onBlur),
|
185
|
-
autoComplete: otp ? "one-time-code" : "off",
|
186
|
-
placeholder: focusedIndex === index && !rest.readOnly && !props2.readOnly ? "" : placeholder
|
187
|
-
};
|
151
|
+
} else {
|
152
|
+
if (validate(nextValue, type))
|
153
|
+
setValue(nextValue, index);
|
154
|
+
setMoveFocus(true);
|
155
|
+
}
|
188
156
|
},
|
189
157
|
[
|
190
158
|
descendants,
|
191
|
-
focusedIndex,
|
192
159
|
getNextValue,
|
193
|
-
id,
|
194
|
-
manageFocus,
|
195
|
-
mask,
|
196
160
|
onComplete,
|
197
|
-
otp,
|
198
|
-
placeholder,
|
199
|
-
rest,
|
200
161
|
setValue,
|
201
162
|
setValues,
|
202
163
|
type,
|
203
164
|
values
|
204
165
|
]
|
205
166
|
);
|
167
|
+
const onKeyDown = useCallback(
|
168
|
+
(index) => ({ key, target }) => {
|
169
|
+
var _a;
|
170
|
+
if (key !== "Backspace" || !manageFocus)
|
171
|
+
return;
|
172
|
+
if (target.value === "") {
|
173
|
+
const prevInput = descendants.prevValue(index, void 0, false);
|
174
|
+
if (!prevInput)
|
175
|
+
return;
|
176
|
+
setValue("", index - 1, false);
|
177
|
+
(_a = prevInput.node) == null ? void 0 : _a.focus();
|
178
|
+
setMoveFocus(true);
|
179
|
+
} else {
|
180
|
+
setMoveFocus(false);
|
181
|
+
}
|
182
|
+
},
|
183
|
+
[descendants, manageFocus, setValue]
|
184
|
+
);
|
185
|
+
const onFocus = useCallback(
|
186
|
+
(index) => () => setFocusedIndex(index),
|
187
|
+
[]
|
188
|
+
);
|
189
|
+
const onBlur = useCallback(() => setFocusedIndex(-1), []);
|
190
|
+
const getInputProps = useCallback(
|
191
|
+
({
|
192
|
+
index,
|
193
|
+
...props2
|
194
|
+
}) => ({
|
195
|
+
inputMode: type === "number" ? "numeric" : "text",
|
196
|
+
type: mask ? "password" : type === "number" ? "tel" : "text",
|
197
|
+
...formControlProps,
|
198
|
+
...filterUndefined(props2),
|
199
|
+
id: `${id}-${index}`,
|
200
|
+
value: values[index] || "",
|
201
|
+
onChange: handlerAll(props2.onChange, onChange(index)),
|
202
|
+
onKeyDown: handlerAll(props2.onKeyDown, onKeyDown(index)),
|
203
|
+
onFocus: handlerAll(props2.onFocus, onFocus(index)),
|
204
|
+
onBlur: handlerAll(props2.onBlur, onBlur),
|
205
|
+
autoComplete: otp ? "one-time-code" : "off",
|
206
|
+
placeholder: focusedIndex === index && !readOnly && !props2.readOnly ? "" : placeholder
|
207
|
+
}),
|
208
|
+
[
|
209
|
+
type,
|
210
|
+
mask,
|
211
|
+
formControlProps,
|
212
|
+
id,
|
213
|
+
values,
|
214
|
+
onChange,
|
215
|
+
onKeyDown,
|
216
|
+
onFocus,
|
217
|
+
onBlur,
|
218
|
+
otp,
|
219
|
+
focusedIndex,
|
220
|
+
readOnly,
|
221
|
+
placeholder
|
222
|
+
]
|
223
|
+
);
|
206
224
|
const css = {
|
207
225
|
display: "flex",
|
208
226
|
alignItems: "center",
|
@@ -213,14 +231,13 @@ var PinInput = forwardRef(
|
|
213
231
|
for (let i = 0; i < items; i++) {
|
214
232
|
cloneChildren.push(/* @__PURE__ */ jsx(PinInputField, {}, i));
|
215
233
|
}
|
216
|
-
const { "aria-readonly": _, ...restWithoutAriaReadonly } = rest;
|
217
234
|
return /* @__PURE__ */ jsx(DescendantsContextProvider, { value: descendants, children: /* @__PURE__ */ jsx(PinInputProvider, { value: { getInputProps, styles }, children: /* @__PURE__ */ jsx(
|
218
235
|
ui.div,
|
219
236
|
{
|
220
237
|
ref,
|
221
238
|
className: cx("ui-pin-input", className),
|
222
239
|
__css: css,
|
223
|
-
...
|
240
|
+
...rest,
|
224
241
|
children: cloneChildren
|
225
242
|
}
|
226
243
|
) }) });
|
@@ -247,4 +264,4 @@ export {
|
|
247
264
|
PinInput,
|
248
265
|
PinInputField
|
249
266
|
};
|
250
|
-
//# sourceMappingURL=chunk-
|
267
|
+
//# sourceMappingURL=chunk-LMKHNR4F.mjs.map
|
@@ -0,0 +1 @@
|
|
1
|
+
{"version":3,"sources":["../src/pin-input.tsx"],"sourcesContent":["import type {\n CSSUIObject,\n HTMLUIProps,\n ThemeProps,\n ColorModeToken,\n CSS,\n} from \"@yamada-ui/core\"\nimport {\n ui,\n forwardRef,\n useMultiComponentStyle,\n omitThemeProps,\n} from \"@yamada-ui/core\"\nimport type { FormControlOptions } from \"@yamada-ui/form-control\"\nimport {\n formControlProperties,\n useFormControlProps,\n} from \"@yamada-ui/form-control\"\nimport { useControllableState } from \"@yamada-ui/use-controllable-state\"\nimport { createDescendant } from \"@yamada-ui/use-descendant\"\nimport {\n createContext,\n cx,\n handlerAll,\n mergeRefs,\n pickObject,\n filterUndefined,\n getValidChildren,\n} from \"@yamada-ui/utils\"\nimport type { ChangeEvent, KeyboardEvent, Ref } from \"react\"\nimport { useCallback, useEffect, useId, useMemo, useState } from \"react\"\n\nconst toArray = (value?: string) => value?.split(\"\")\n\nconst validate = (value: string, type: PinInputProps[\"type\"]) => {\n const NUMERIC_REGEX = /^[0-9]+$/\n const ALPHA_NUMERIC_REGEX = /^[a-zA-Z0-9]+$/i\n\n const regex = type === \"alphanumeric\" ? ALPHA_NUMERIC_REGEX : NUMERIC_REGEX\n\n return regex.test(value)\n}\n\ntype PinInputContext = {\n getInputProps: (\n props: PinInputFieldProps & {\n index: number\n ref?: Ref<HTMLInputElement>\n },\n ) => PinInputFieldProps\n styles: Record<string, CSSUIObject>\n}\n\nconst [PinInputProvider, usePinInputContext] = createContext<PinInputContext>({\n strict: false,\n name: \"PinInputContext\",\n})\n\nconst { DescendantsContextProvider, useDescendants, useDescendant } =\n createDescendant<HTMLInputElement>()\n\ntype PinInputOptions = {\n /**\n * The top-level id string that will be applied to the input fields.\n * The index of the input will be appended to this top-level id.\n */\n id?: string\n /**\n * The type of values the pin-input should allow.\n *\n * @default 'number'\n */\n type?: \"alphanumeric\" | \"number\"\n /**\n * The placeholder for the pin input.\n *\n * @default '○'\n */\n placeholder?: string\n /**\n * The value of the pin input.\n */\n value?: string\n /**\n * The initial value of the pin input.\n */\n defaultValue?: string\n /**\n * If `true`, the pin input receives focus on mount.\n *\n * @default false\n */\n autoFocus?: boolean\n /**\n * If `true`, focus will move automatically to the next input once filled.\n *\n * @default true\n */\n manageFocus?: boolean\n /**\n * If `true`, the pin input component signals to its fields that they should.\n */\n otp?: boolean\n /**\n * If `true`, the input's value will be masked just like `type=password`.\n */\n mask?: boolean\n /**\n * Function called on input change.\n */\n onChange?: (value: string) => void\n /**\n * Function called when all inputs have valid values.\n */\n onComplete?: (value: string) => void\n /**\n * The number of inputs to display.\n *\n * @default 4\n */\n items?: number\n /**\n * The border color when the input is focused.\n */\n focusBorderColor?: ColorModeToken<CSS.Property.BorderColor, \"colors\">\n /**\n * The border color when the input is invalid.\n */\n errorBorderColor?: ColorModeToken<CSS.Property.BorderColor, \"colors\">\n}\n\nexport type PinInputProps = Omit<HTMLUIProps<\"div\">, \"onChange\" | \"mask\"> &\n ThemeProps<\"PinInput\"> &\n FormControlOptions &\n PinInputOptions\n\n/**\n * `PinInput` is a component used to capture pin codes or OTP (One-Time Password) inputs.\n *\n * @see Docs https://yamada-ui.com/components/forms/pin-input\n */\nexport const PinInput = forwardRef<PinInputProps, \"div\">(\n ({ focusBorderColor, errorBorderColor, ...props }, ref) => {\n const [styles, mergedProps] = useMultiComponentStyle(\"PinInput\", {\n focusBorderColor,\n errorBorderColor,\n ...props,\n })\n let {\n id,\n className,\n type = \"number\",\n placeholder = \"○\",\n value,\n defaultValue,\n autoFocus,\n manageFocus = true,\n otp = false,\n mask,\n readOnly,\n \"aria-readonly\": ariaReadonly,\n onChange: onChangeProp,\n onComplete,\n items = 4,\n children,\n ...rest\n } = useFormControlProps(omitThemeProps(mergedProps))\n const formControlProps = useMemo(\n () => ({\n ...pickObject(rest, formControlProperties),\n readOnly,\n ariaReadonly,\n }),\n [ariaReadonly, readOnly, rest],\n )\n\n id ??= useId()\n\n const descendants = useDescendants()\n\n const [moveFocus, setMoveFocus] = useState<boolean>(true)\n const [focusedIndex, setFocusedIndex] = useState<number>(-1)\n\n useEffect(() => {\n if (!autoFocus) return\n\n const firstValue = descendants.firstValue()\n\n if (!firstValue) return\n\n requestAnimationFrame(() => firstValue.node.focus())\n }, [autoFocus, descendants])\n\n const [values, setValues] = useControllableState<string[]>({\n value: toArray(value),\n defaultValue: toArray(defaultValue) || [],\n onChange: (values) => onChangeProp?.(values.join(\"\")),\n })\n\n const focusNext = useCallback(\n (index: number) => {\n if (!moveFocus || !manageFocus) return\n\n const next = descendants.nextValue(index, undefined, false)\n\n if (!next) return\n\n requestAnimationFrame(() => next.node.focus())\n },\n [descendants, moveFocus, manageFocus],\n )\n\n const setValue = useCallback(\n (value: string, index: number, isFocus: boolean = true) => {\n let nextValues = [...values]\n\n nextValues[index] = value\n\n setValues(nextValues)\n\n nextValues = nextValues.filter(Boolean)\n\n const isComplete =\n value !== \"\" &&\n nextValues.length === descendants.count() &&\n nextValues.every((value) => value != null && value !== \"\")\n\n if (isComplete) {\n onComplete?.(nextValues.join(\"\"))\n descendants.value(index)?.node.blur()\n } else if (isFocus) {\n focusNext(index)\n }\n },\n [values, setValues, descendants, onComplete, focusNext],\n )\n\n const getNextValue = useCallback(\n (value: string | undefined, eventValue: string) => {\n let nextValue = eventValue\n\n if (!value?.length) return nextValue\n\n if (value[0] === eventValue.charAt(0)) {\n nextValue = eventValue.charAt(1)\n } else if (value[0] === eventValue.charAt(1)) {\n nextValue = eventValue.charAt(0)\n }\n\n return nextValue\n },\n [],\n )\n\n const onChange = useCallback(\n (index: number) =>\n ({ target }: ChangeEvent<HTMLInputElement>) => {\n const eventValue = target.value\n const currentValue = values[index]\n const nextValue = getNextValue(currentValue, eventValue)\n\n if (nextValue === \"\") {\n setValue(\"\", index)\n\n return\n }\n\n if (eventValue.length > 2) {\n if (!validate(eventValue, type)) return\n\n const nextValue = eventValue\n .split(\"\")\n .filter((_, index) => index < descendants.count())\n\n setValues(nextValue)\n\n if (nextValue.length === descendants.count()) {\n onComplete?.(nextValue.join(\"\"))\n descendants.value(index)?.node.blur()\n }\n } else {\n if (validate(nextValue, type)) setValue(nextValue, index)\n\n setMoveFocus(true)\n }\n },\n [\n descendants,\n getNextValue,\n onComplete,\n setValue,\n setValues,\n type,\n values,\n ],\n )\n\n const onKeyDown = useCallback(\n (index: number) =>\n ({ key, target }: KeyboardEvent<HTMLInputElement>) => {\n if (key !== \"Backspace\" || !manageFocus) return\n\n if ((target as HTMLInputElement).value === \"\") {\n const prevInput = descendants.prevValue(index, undefined, false)\n\n if (!prevInput) return\n\n setValue(\"\", index - 1, false)\n prevInput.node?.focus()\n setMoveFocus(true)\n } else {\n setMoveFocus(false)\n }\n },\n [descendants, manageFocus, setValue],\n )\n\n const onFocus = useCallback(\n (index: number) => () => setFocusedIndex(index),\n [],\n )\n\n const onBlur = useCallback(() => setFocusedIndex(-1), [])\n\n const getInputProps = useCallback(\n ({\n index,\n ...props\n }: PinInputFieldProps & {\n index: number\n ref?: Ref<HTMLInputElement>\n }): PinInputFieldProps => ({\n inputMode: type === \"number\" ? \"numeric\" : \"text\",\n type: mask ? \"password\" : type === \"number\" ? \"tel\" : \"text\",\n ...formControlProps,\n ...filterUndefined(props),\n id: `${id}-${index}`,\n value: values[index] || \"\",\n onChange: handlerAll(props.onChange, onChange(index)),\n onKeyDown: handlerAll(props.onKeyDown, onKeyDown(index)),\n onFocus: handlerAll(props.onFocus, onFocus(index)),\n onBlur: handlerAll(props.onBlur, onBlur),\n autoComplete: otp ? \"one-time-code\" : \"off\",\n placeholder:\n focusedIndex === index && !readOnly && !props.readOnly\n ? \"\"\n : placeholder,\n }),\n [\n type,\n mask,\n formControlProps,\n id,\n values,\n onChange,\n onKeyDown,\n onFocus,\n onBlur,\n otp,\n focusedIndex,\n readOnly,\n placeholder,\n ],\n )\n\n const css: CSSUIObject = {\n display: \"flex\",\n alignItems: \"center\",\n ...styles.container,\n }\n\n let cloneChildren = getValidChildren(children)\n\n if (!cloneChildren.length)\n for (let i = 0; i < items; i++) {\n cloneChildren.push(<PinInputField key={i} />)\n }\n\n return (\n <DescendantsContextProvider value={descendants}>\n <PinInputProvider value={{ getInputProps, styles }}>\n <ui.div\n ref={ref}\n className={cx(\"ui-pin-input\", className)}\n __css={css}\n {...rest}\n >\n {cloneChildren}\n </ui.div>\n </PinInputProvider>\n </DescendantsContextProvider>\n )\n },\n)\n\nexport type PinInputFieldProps = HTMLUIProps<\"input\"> & FormControlOptions\n\nexport const PinInputField = forwardRef<PinInputFieldProps, \"input\">(\n ({ className, ...rest }, ref) => {\n const { getInputProps, styles } = usePinInputContext()\n const { index, register } = useDescendant()\n\n rest = useFormControlProps(rest)\n\n const css: CSSUIObject = { ...styles.field }\n\n return (\n <ui.input\n className={cx(\"ui-pin-input__field\", className)}\n {...getInputProps({ ...rest, ref: mergeRefs(register, ref), index })}\n __css={css}\n />\n )\n },\n)\n"],"mappings":";;;AAOA;AAAA,EACE;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,OACK;AAEP;AAAA,EACE;AAAA,EACA;AAAA,OACK;AACP,SAAS,4BAA4B;AACrC,SAAS,wBAAwB;AACjC;AAAA,EACE;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,OACK;AAEP,SAAS,aAAa,WAAW,OAAO,SAAS,gBAAgB;AAyVtC;AAvV3B,IAAM,UAAU,CAAC,UAAmB,+BAAO,MAAM;AAEjD,IAAM,WAAW,CAAC,OAAe,SAAgC;AAC/D,QAAM,gBAAgB;AACtB,QAAM,sBAAsB;AAE5B,QAAM,QAAQ,SAAS,iBAAiB,sBAAsB;AAE9D,SAAO,MAAM,KAAK,KAAK;AACzB;AAYA,IAAM,CAAC,kBAAkB,kBAAkB,IAAI,cAA+B;AAAA,EAC5E,QAAQ;AAAA,EACR,MAAM;AACR,CAAC;AAED,IAAM,EAAE,4BAA4B,gBAAgB,cAAc,IAChE,iBAAmC;AAkF9B,IAAM,WAAW;AAAA,EACtB,CAAC,EAAE,kBAAkB,kBAAkB,GAAG,MAAM,GAAG,QAAQ;AACzD,UAAM,CAAC,QAAQ,WAAW,IAAI,uBAAuB,YAAY;AAAA,MAC/D;AAAA,MACA;AAAA,MACA,GAAG;AAAA,IACL,CAAC;AACD,QAAI;AAAA,MACF;AAAA,MACA;AAAA,MACA,OAAO;AAAA,MACP,cAAc;AAAA,MACd;AAAA,MACA;AAAA,MACA;AAAA,MACA,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AAA,MAAC,GAAG;AAAA,MAAH;AAAA,QACC,WAAW,GAAG,uBAAuB,SAAS;AAAA,QAC7C,GAAG,cAAc,EAAE,GAAG,MAAM,KAAK,UAAU,UAAU,GAAG,GAAG,MAAM,CAAC;AAAA,QACnE,OAAO;AAAA;AAAA,IACT;AAAA,EAEJ;AACF;","names":["values","value","nextValue","index","props"]}
|
package/dist/index.js
CHANGED
@@ -64,12 +64,22 @@ var PinInput = (0, import_core.forwardRef)(
|
|
64
64
|
manageFocus = true,
|
65
65
|
otp = false,
|
66
66
|
mask,
|
67
|
-
|
67
|
+
readOnly,
|
68
|
+
"aria-readonly": ariaReadonly,
|
69
|
+
onChange: onChangeProp,
|
68
70
|
onComplete,
|
69
71
|
items = 4,
|
70
72
|
children,
|
71
73
|
...rest
|
72
74
|
} = (0, import_form_control.useFormControlProps)((0, import_core.omitThemeProps)(mergedProps));
|
75
|
+
const formControlProps = (0, import_react.useMemo)(
|
76
|
+
() => ({
|
77
|
+
...(0, import_utils.pickObject)(rest, import_form_control.formControlProperties),
|
78
|
+
readOnly,
|
79
|
+
ariaReadonly
|
80
|
+
}),
|
81
|
+
[ariaReadonly, readOnly, rest]
|
82
|
+
);
|
73
83
|
id != null ? id : id = (0, import_react.useId)();
|
74
84
|
const descendants = useDescendants();
|
75
85
|
const [moveFocus, setMoveFocus] = (0, import_react.useState)(true);
|
@@ -85,7 +95,7 @@ var PinInput = (0, import_core.forwardRef)(
|
|
85
95
|
const [values, setValues] = (0, import_use_controllable_state.useControllableState)({
|
86
96
|
value: toArray(value),
|
87
97
|
defaultValue: toArray(defaultValue) || [],
|
88
|
-
onChange: (values2) =>
|
98
|
+
onChange: (values2) => onChangeProp == null ? void 0 : onChangeProp(values2.join(""))
|
89
99
|
});
|
90
100
|
const focusNext = (0, import_react.useCallback)(
|
91
101
|
(index) => {
|
@@ -129,90 +139,98 @@ var PinInput = (0, import_core.forwardRef)(
|
|
129
139
|
},
|
130
140
|
[]
|
131
141
|
);
|
132
|
-
const
|
133
|
-
({
|
134
|
-
|
135
|
-
|
136
|
-
|
137
|
-
const
|
138
|
-
|
139
|
-
|
140
|
-
|
141
|
-
|
142
|
-
|
143
|
-
|
144
|
-
return;
|
145
|
-
}
|
146
|
-
if (eventValue.length > 2) {
|
147
|
-
if (!validate(eventValue, type))
|
148
|
-
return;
|
149
|
-
const nextValue2 = eventValue.split("").filter((_2, index2) => index2 < descendants.count());
|
150
|
-
setValues(nextValue2);
|
151
|
-
if (nextValue2.length === descendants.count()) {
|
152
|
-
onComplete == null ? void 0 : onComplete(nextValue2.join(""));
|
153
|
-
(_a = descendants.value(index)) == null ? void 0 : _a.node.blur();
|
154
|
-
}
|
155
|
-
} else {
|
156
|
-
if (validate(nextValue, type))
|
157
|
-
setValue(nextValue, index);
|
158
|
-
setMoveFocus(true);
|
159
|
-
}
|
160
|
-
};
|
161
|
-
const onKeyDown = ({
|
162
|
-
key,
|
163
|
-
target
|
164
|
-
}) => {
|
165
|
-
var _a;
|
166
|
-
if (key !== "Backspace" || !manageFocus)
|
142
|
+
const onChange = (0, import_react.useCallback)(
|
143
|
+
(index) => ({ target }) => {
|
144
|
+
var _a;
|
145
|
+
const eventValue = target.value;
|
146
|
+
const currentValue = values[index];
|
147
|
+
const nextValue = getNextValue(currentValue, eventValue);
|
148
|
+
if (nextValue === "") {
|
149
|
+
setValue("", index);
|
150
|
+
return;
|
151
|
+
}
|
152
|
+
if (eventValue.length > 2) {
|
153
|
+
if (!validate(eventValue, type))
|
167
154
|
return;
|
168
|
-
|
169
|
-
|
170
|
-
|
171
|
-
|
172
|
-
|
173
|
-
(_a = prevInput.node) == null ? void 0 : _a.focus();
|
174
|
-
setMoveFocus(true);
|
175
|
-
} else {
|
176
|
-
setMoveFocus(false);
|
155
|
+
const nextValue2 = eventValue.split("").filter((_, index2) => index2 < descendants.count());
|
156
|
+
setValues(nextValue2);
|
157
|
+
if (nextValue2.length === descendants.count()) {
|
158
|
+
onComplete == null ? void 0 : onComplete(nextValue2.join(""));
|
159
|
+
(_a = descendants.value(index)) == null ? void 0 : _a.node.blur();
|
177
160
|
}
|
178
|
-
}
|
179
|
-
|
180
|
-
|
181
|
-
|
182
|
-
|
183
|
-
type: mask ? "password" : type === "number" ? "tel" : "text",
|
184
|
-
...(0, import_utils.pickObject)(
|
185
|
-
rest,
|
186
|
-
(0, import_form_control.getFormControlProperties)({ omit: ["aria-readonly"] })
|
187
|
-
),
|
188
|
-
...(0, import_utils.filterUndefined)(props2),
|
189
|
-
id: `${id}-${index}`,
|
190
|
-
value: values[index] || "",
|
191
|
-
onChange: (0, import_utils.handlerAll)(props2.onChange, onChange2),
|
192
|
-
onKeyDown: (0, import_utils.handlerAll)(props2.onKeyDown, onKeyDown),
|
193
|
-
onFocus: (0, import_utils.handlerAll)(props2.onFocus, onFocus),
|
194
|
-
onBlur: (0, import_utils.handlerAll)(props2.onBlur, onBlur),
|
195
|
-
autoComplete: otp ? "one-time-code" : "off",
|
196
|
-
placeholder: focusedIndex === index && !rest.readOnly && !props2.readOnly ? "" : placeholder
|
197
|
-
};
|
161
|
+
} else {
|
162
|
+
if (validate(nextValue, type))
|
163
|
+
setValue(nextValue, index);
|
164
|
+
setMoveFocus(true);
|
165
|
+
}
|
198
166
|
},
|
199
167
|
[
|
200
168
|
descendants,
|
201
|
-
focusedIndex,
|
202
169
|
getNextValue,
|
203
|
-
id,
|
204
|
-
manageFocus,
|
205
|
-
mask,
|
206
170
|
onComplete,
|
207
|
-
otp,
|
208
|
-
placeholder,
|
209
|
-
rest,
|
210
171
|
setValue,
|
211
172
|
setValues,
|
212
173
|
type,
|
213
174
|
values
|
214
175
|
]
|
215
176
|
);
|
177
|
+
const onKeyDown = (0, import_react.useCallback)(
|
178
|
+
(index) => ({ key, target }) => {
|
179
|
+
var _a;
|
180
|
+
if (key !== "Backspace" || !manageFocus)
|
181
|
+
return;
|
182
|
+
if (target.value === "") {
|
183
|
+
const prevInput = descendants.prevValue(index, void 0, false);
|
184
|
+
if (!prevInput)
|
185
|
+
return;
|
186
|
+
setValue("", index - 1, false);
|
187
|
+
(_a = prevInput.node) == null ? void 0 : _a.focus();
|
188
|
+
setMoveFocus(true);
|
189
|
+
} else {
|
190
|
+
setMoveFocus(false);
|
191
|
+
}
|
192
|
+
},
|
193
|
+
[descendants, manageFocus, setValue]
|
194
|
+
);
|
195
|
+
const onFocus = (0, import_react.useCallback)(
|
196
|
+
(index) => () => setFocusedIndex(index),
|
197
|
+
[]
|
198
|
+
);
|
199
|
+
const onBlur = (0, import_react.useCallback)(() => setFocusedIndex(-1), []);
|
200
|
+
const getInputProps = (0, import_react.useCallback)(
|
201
|
+
({
|
202
|
+
index,
|
203
|
+
...props2
|
204
|
+
}) => ({
|
205
|
+
inputMode: type === "number" ? "numeric" : "text",
|
206
|
+
type: mask ? "password" : type === "number" ? "tel" : "text",
|
207
|
+
...formControlProps,
|
208
|
+
...(0, import_utils.filterUndefined)(props2),
|
209
|
+
id: `${id}-${index}`,
|
210
|
+
value: values[index] || "",
|
211
|
+
onChange: (0, import_utils.handlerAll)(props2.onChange, onChange(index)),
|
212
|
+
onKeyDown: (0, import_utils.handlerAll)(props2.onKeyDown, onKeyDown(index)),
|
213
|
+
onFocus: (0, import_utils.handlerAll)(props2.onFocus, onFocus(index)),
|
214
|
+
onBlur: (0, import_utils.handlerAll)(props2.onBlur, onBlur),
|
215
|
+
autoComplete: otp ? "one-time-code" : "off",
|
216
|
+
placeholder: focusedIndex === index && !readOnly && !props2.readOnly ? "" : placeholder
|
217
|
+
}),
|
218
|
+
[
|
219
|
+
type,
|
220
|
+
mask,
|
221
|
+
formControlProps,
|
222
|
+
id,
|
223
|
+
values,
|
224
|
+
onChange,
|
225
|
+
onKeyDown,
|
226
|
+
onFocus,
|
227
|
+
onBlur,
|
228
|
+
otp,
|
229
|
+
focusedIndex,
|
230
|
+
readOnly,
|
231
|
+
placeholder
|
232
|
+
]
|
233
|
+
);
|
216
234
|
const css = {
|
217
235
|
display: "flex",
|
218
236
|
alignItems: "center",
|
@@ -223,14 +241,13 @@ var PinInput = (0, import_core.forwardRef)(
|
|
223
241
|
for (let i = 0; i < items; i++) {
|
224
242
|
cloneChildren.push(/* @__PURE__ */ (0, import_jsx_runtime.jsx)(PinInputField, {}, i));
|
225
243
|
}
|
226
|
-
const { "aria-readonly": _, ...restWithoutAriaReadonly } = rest;
|
227
244
|
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)(
|
228
245
|
import_core.ui.div,
|
229
246
|
{
|
230
247
|
ref,
|
231
248
|
className: (0, import_utils.cx)("ui-pin-input", className),
|
232
249
|
__css: css,
|
233
|
-
...
|
250
|
+
...rest,
|
234
251
|
children: cloneChildren
|
235
252
|
}
|
236
253
|
) }) });
|
package/dist/index.js.map
CHANGED
@@ -1 +1 @@
|
|
1
|
-
{"version":3,"sources":["../src/index.ts","../src/pin-input.tsx"],"sourcesContent":["export { PinInput, PinInputField } from \"./pin-input\"\nexport type { PinInputProps, PinInputFieldProps } from \"./pin-input\"\n","import type {\n CSSUIObject,\n HTMLUIProps,\n ThemeProps,\n ColorModeToken,\n CSS,\n} from \"@yamada-ui/core\"\nimport {\n ui,\n forwardRef,\n useMultiComponentStyle,\n omitThemeProps,\n} from \"@yamada-ui/core\"\nimport type { FormControlOptions } from \"@yamada-ui/form-control\"\nimport {\n getFormControlProperties,\n useFormControlProps,\n} from \"@yamada-ui/form-control\"\nimport { useControllableState } from \"@yamada-ui/use-controllable-state\"\nimport { createDescendant } from \"@yamada-ui/use-descendant\"\nimport {\n createContext,\n cx,\n handlerAll,\n mergeRefs,\n pickObject,\n filterUndefined,\n getValidChildren,\n} from \"@yamada-ui/utils\"\nimport type { ChangeEvent, KeyboardEvent, Ref } from \"react\"\nimport { useCallback, useEffect, useId, useState } from \"react\"\n\nconst toArray = (value?: string) => value?.split(\"\")\n\nconst validate = (value: string, type: PinInputProps[\"type\"]) => {\n const NUMERIC_REGEX = /^[0-9]+$/\n const ALPHA_NUMERIC_REGEX = /^[a-zA-Z0-9]+$/i\n\n const regex = type === \"alphanumeric\" ? ALPHA_NUMERIC_REGEX : NUMERIC_REGEX\n\n return regex.test(value)\n}\n\ntype PinInputContext = {\n getInputProps: (\n props: PinInputFieldProps & {\n index: number\n ref?: Ref<HTMLInputElement>\n },\n ) => PinInputFieldProps\n styles: Record<string, CSSUIObject>\n}\n\nconst [PinInputProvider, usePinInputContext] = createContext<PinInputContext>({\n strict: false,\n name: \"PinInputContext\",\n})\n\nconst { DescendantsContextProvider, useDescendants, useDescendant } =\n createDescendant<HTMLInputElement>()\n\ntype PinInputOptions = {\n /**\n * The top-level id string that will be applied to the input fields.\n * The index of the input will be appended to this top-level id.\n */\n id?: string\n /**\n * The type of values the pin-input should allow.\n *\n * @default 'number'\n */\n type?: \"alphanumeric\" | \"number\"\n /**\n * The placeholder for the pin input.\n *\n * @default '○'\n */\n placeholder?: string\n /**\n * The value of the pin input.\n */\n value?: string\n /**\n * The initial value of the pin input.\n */\n defaultValue?: string\n /**\n * If `true`, the pin input receives focus on mount.\n *\n * @default false\n */\n autoFocus?: boolean\n /**\n * If `true`, focus will move automatically to the next input once filled.\n *\n * @default true\n */\n manageFocus?: boolean\n /**\n * If `true`, the pin input component signals to its fields that they should.\n */\n otp?: boolean\n /**\n * If `true`, the input's value will be masked just like `type=password`.\n */\n mask?: boolean\n /**\n * Function called on input change.\n */\n onChange?: (value: string) => void\n /**\n * Function called when all inputs have valid values.\n */\n onComplete?: (value: string) => void\n /**\n * The number of inputs to display.\n *\n * @default 4\n */\n items?: number\n /**\n * The border color when the input is focused.\n */\n focusBorderColor?: ColorModeToken<CSS.Property.BorderColor, \"colors\">\n /**\n * The border color when the input is invalid.\n */\n errorBorderColor?: ColorModeToken<CSS.Property.BorderColor, \"colors\">\n}\n\nexport type PinInputProps = Omit<HTMLUIProps<\"div\">, \"onChange\" | \"mask\"> &\n ThemeProps<\"PinInput\"> &\n FormControlOptions &\n PinInputOptions\n\n/**\n * `PinInput` is a component used to capture pin codes or OTP (One-Time Password) inputs.\n *\n * @see Docs https://yamada-ui.com/components/forms/pin-input\n */\nexport const PinInput = forwardRef<PinInputProps, \"div\">(\n ({ focusBorderColor, errorBorderColor, ...props }, ref) => {\n const [styles, mergedProps] = useMultiComponentStyle(\"PinInput\", {\n focusBorderColor,\n errorBorderColor,\n ...props,\n })\n let {\n id,\n className,\n type = \"number\",\n placeholder = \"○\",\n value,\n defaultValue,\n autoFocus,\n manageFocus = true,\n otp = false,\n mask,\n onChange,\n onComplete,\n items = 4,\n children,\n ...rest\n } = useFormControlProps(omitThemeProps(mergedProps))\n\n id ??= useId()\n\n const descendants = useDescendants()\n\n const [moveFocus, setMoveFocus] = useState<boolean>(true)\n const [focusedIndex, setFocusedIndex] = useState<number>(-1)\n\n useEffect(() => {\n if (!autoFocus) return\n\n const firstValue = descendants.firstValue()\n\n if (!firstValue) return\n\n requestAnimationFrame(() => firstValue.node.focus())\n }, [autoFocus, descendants])\n\n const [values, setValues] = useControllableState<string[]>({\n value: toArray(value),\n defaultValue: toArray(defaultValue) || [],\n onChange: (values) => onChange?.(values.join(\"\")),\n })\n\n const focusNext = useCallback(\n (index: number) => {\n if (!moveFocus || !manageFocus) return\n\n const next = descendants.nextValue(index, undefined, false)\n\n if (!next) return\n\n requestAnimationFrame(() => next.node.focus())\n },\n [descendants, moveFocus, manageFocus],\n )\n\n const setValue = useCallback(\n (value: string, index: number, isFocus: boolean = true) => {\n let nextValues = [...values]\n\n nextValues[index] = value\n\n setValues(nextValues)\n\n nextValues = nextValues.filter(Boolean)\n\n const isComplete =\n value !== \"\" &&\n nextValues.length === descendants.count() &&\n nextValues.every((value) => value != null && value !== \"\")\n\n if (isComplete) {\n onComplete?.(nextValues.join(\"\"))\n descendants.value(index)?.node.blur()\n } else if (isFocus) {\n focusNext(index)\n }\n },\n [values, setValues, descendants, onComplete, focusNext],\n )\n\n const getNextValue = useCallback(\n (value: string | undefined, eventValue: string) => {\n let nextValue = eventValue\n\n if (!value?.length) return nextValue\n\n if (value[0] === eventValue.charAt(0)) {\n nextValue = eventValue.charAt(1)\n } else if (value[0] === eventValue.charAt(1)) {\n nextValue = eventValue.charAt(0)\n }\n\n return nextValue\n },\n [],\n )\n\n const getInputProps = useCallback(\n ({\n index,\n ...props\n }: PinInputFieldProps & {\n index: number\n ref?: Ref<HTMLInputElement>\n }): PinInputFieldProps => {\n const onChange = ({ target }: ChangeEvent<HTMLInputElement>) => {\n const eventValue = target.value\n const currentValue = values[index]\n const nextValue = getNextValue(currentValue, eventValue)\n\n if (nextValue === \"\") {\n setValue(\"\", index)\n\n return\n }\n\n if (eventValue.length > 2) {\n if (!validate(eventValue, type)) return\n\n const nextValue = eventValue\n .split(\"\")\n .filter((_, index) => index < descendants.count())\n\n setValues(nextValue)\n\n if (nextValue.length === descendants.count()) {\n onComplete?.(nextValue.join(\"\"))\n descendants.value(index)?.node.blur()\n }\n } else {\n if (validate(nextValue, type)) setValue(nextValue, index)\n\n setMoveFocus(true)\n }\n }\n\n const onKeyDown = ({\n key,\n target,\n }: KeyboardEvent<HTMLInputElement>) => {\n if (key !== \"Backspace\" || !manageFocus) return\n\n if ((target as HTMLInputElement).value === \"\") {\n const prevInput = descendants.prevValue(index, undefined, false)\n\n if (!prevInput) return\n\n setValue(\"\", index - 1, false)\n prevInput.node?.focus()\n setMoveFocus(true)\n } else {\n setMoveFocus(false)\n }\n }\n\n const onFocus = () => setFocusedIndex(index)\n\n const onBlur = () => setFocusedIndex(-1)\n\n return {\n inputMode: type === \"number\" ? \"numeric\" : \"text\",\n type: mask ? \"password\" : type === \"number\" ? \"tel\" : \"text\",\n ...pickObject(\n rest,\n getFormControlProperties({ omit: [\"aria-readonly\"] }),\n ),\n ...filterUndefined(props),\n id: `${id}-${index}`,\n value: values[index] || \"\",\n onChange: handlerAll(props.onChange, onChange),\n onKeyDown: handlerAll(props.onKeyDown, onKeyDown),\n onFocus: handlerAll(props.onFocus, onFocus),\n onBlur: handlerAll(props.onBlur, onBlur),\n autoComplete: otp ? \"one-time-code\" : \"off\",\n placeholder:\n focusedIndex === index && !rest.readOnly && !props.readOnly\n ? \"\"\n : placeholder,\n }\n },\n [\n descendants,\n focusedIndex,\n getNextValue,\n id,\n manageFocus,\n mask,\n onComplete,\n otp,\n placeholder,\n rest,\n setValue,\n setValues,\n type,\n values,\n ],\n )\n\n const css: CSSUIObject = {\n display: \"flex\",\n alignItems: \"center\",\n ...styles.container,\n }\n\n let cloneChildren = getValidChildren(children)\n\n if (!cloneChildren.length)\n for (let i = 0; i < items; i++) {\n cloneChildren.push(<PinInputField key={i} />)\n }\n\n const { \"aria-readonly\": _, ...restWithoutAriaReadonly } = rest\n\n return (\n <DescendantsContextProvider value={descendants}>\n <PinInputProvider value={{ getInputProps, styles }}>\n <ui.div\n ref={ref}\n className={cx(\"ui-pin-input\", className)}\n __css={css}\n {...restWithoutAriaReadonly}\n >\n {cloneChildren}\n </ui.div>\n </PinInputProvider>\n </DescendantsContextProvider>\n )\n },\n)\n\nexport type PinInputFieldProps = HTMLUIProps<\"input\"> & FormControlOptions\n\nexport const PinInputField = forwardRef<PinInputFieldProps, \"input\">(\n ({ className, ...rest }, ref) => {\n const { getInputProps, styles } = usePinInputContext()\n const { index, register } = useDescendant()\n\n rest = useFormControlProps(rest)\n\n const css: CSSUIObject = { ...styles.field }\n\n return (\n <ui.input\n className={cx(\"ui-pin-input__field\", className)}\n {...getInputProps({ ...rest, ref: mergeRefs(register, ref), index })}\n __css={css}\n />\n )\n },\n)\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;;;ACOA,kBAKO;AAEP,0BAGO;AACP,oCAAqC;AACrC,4BAAiC;AACjC,mBAQO;AAEP,mBAAwD;AAqU7B;AAnU3B,IAAM,UAAU,CAAC,UAAmB,+BAAO,MAAM;AAE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;AAAA,QACN,GAAG;AAAA,QAEH;AAAA;AAAA,IACH,GACF,GACF;AAAA,EAEJ;AACF;AAIO,IAAM,oBAAgB;AAAA,EAC3B,CAAC,EAAE,WAAW,GAAG,KAAK,GAAG,QAAQ;AAC/B,UAAM,EAAE,eAAe,OAAO,IAAI,mBAAmB;AACrD,UAAM,EAAE,OAAO,SAAS,IAAI,cAAc;AAE1C,eAAO,yCAAoB,IAAI;AAE/B,UAAM,MAAmB,EAAE,GAAG,OAAO,MAAM;AAE3C,WACE;AAAA,MAAC,eAAG;AAAA,MAAH;AAAA,QACC,eAAW,iBAAG,uBAAuB,SAAS;AAAA,QAC7C,GAAG,cAAc,EAAE,GAAG,MAAM,SAAK,wBAAU,UAAU,GAAG,GAAG,MAAM,CAAC;AAAA,QACnE,OAAO;AAAA;AAAA,IACT;AAAA,EAEJ;AACF;","names":["values","value","props","onChange","nextValue","_","index"]}
|
1
|
+
{"version":3,"sources":["../src/index.ts","../src/pin-input.tsx"],"sourcesContent":["export { PinInput, PinInputField } from \"./pin-input\"\nexport type { PinInputProps, PinInputFieldProps } from \"./pin-input\"\n","import type {\n CSSUIObject,\n HTMLUIProps,\n ThemeProps,\n ColorModeToken,\n CSS,\n} from \"@yamada-ui/core\"\nimport {\n ui,\n forwardRef,\n useMultiComponentStyle,\n omitThemeProps,\n} from \"@yamada-ui/core\"\nimport type { FormControlOptions } from \"@yamada-ui/form-control\"\nimport {\n formControlProperties,\n useFormControlProps,\n} from \"@yamada-ui/form-control\"\nimport { useControllableState } from \"@yamada-ui/use-controllable-state\"\nimport { createDescendant } from \"@yamada-ui/use-descendant\"\nimport {\n createContext,\n cx,\n handlerAll,\n mergeRefs,\n pickObject,\n filterUndefined,\n getValidChildren,\n} from \"@yamada-ui/utils\"\nimport type { ChangeEvent, KeyboardEvent, Ref } from \"react\"\nimport { useCallback, useEffect, useId, useMemo, useState } from \"react\"\n\nconst toArray = (value?: string) => value?.split(\"\")\n\nconst validate = (value: string, type: PinInputProps[\"type\"]) => {\n const NUMERIC_REGEX = /^[0-9]+$/\n const ALPHA_NUMERIC_REGEX = /^[a-zA-Z0-9]+$/i\n\n const regex = type === \"alphanumeric\" ? ALPHA_NUMERIC_REGEX : NUMERIC_REGEX\n\n return regex.test(value)\n}\n\ntype PinInputContext = {\n getInputProps: (\n props: PinInputFieldProps & {\n index: number\n ref?: Ref<HTMLInputElement>\n },\n ) => PinInputFieldProps\n styles: Record<string, CSSUIObject>\n}\n\nconst [PinInputProvider, usePinInputContext] = createContext<PinInputContext>({\n strict: false,\n name: \"PinInputContext\",\n})\n\nconst { DescendantsContextProvider, useDescendants, useDescendant } =\n createDescendant<HTMLInputElement>()\n\ntype PinInputOptions = {\n /**\n * The top-level id string that will be applied to the input fields.\n * The index of the input will be appended to this top-level id.\n */\n id?: string\n /**\n * The type of values the pin-input should allow.\n *\n * @default 'number'\n */\n type?: \"alphanumeric\" | \"number\"\n /**\n * The placeholder for the pin input.\n *\n * @default '○'\n */\n placeholder?: string\n /**\n * The value of the pin input.\n */\n value?: string\n /**\n * The initial value of the pin input.\n */\n defaultValue?: string\n /**\n * If `true`, the pin input receives focus on mount.\n *\n * @default false\n */\n autoFocus?: boolean\n /**\n * If `true`, focus will move automatically to the next input once filled.\n *\n * @default true\n */\n manageFocus?: boolean\n /**\n * If `true`, the pin input component signals to its fields that they should.\n */\n otp?: boolean\n /**\n * If `true`, the input's value will be masked just like `type=password`.\n */\n mask?: boolean\n /**\n * Function called on input change.\n */\n onChange?: (value: string) => void\n /**\n * Function called when all inputs have valid values.\n */\n onComplete?: (value: string) => void\n /**\n * The number of inputs to display.\n *\n * @default 4\n */\n items?: number\n /**\n * The border color when the input is focused.\n */\n focusBorderColor?: ColorModeToken<CSS.Property.BorderColor, \"colors\">\n /**\n * The border color when the input is invalid.\n */\n errorBorderColor?: ColorModeToken<CSS.Property.BorderColor, \"colors\">\n}\n\nexport type PinInputProps = Omit<HTMLUIProps<\"div\">, \"onChange\" | \"mask\"> &\n ThemeProps<\"PinInput\"> &\n FormControlOptions &\n PinInputOptions\n\n/**\n * `PinInput` is a component used to capture pin codes or OTP (One-Time Password) inputs.\n *\n * @see Docs https://yamada-ui.com/components/forms/pin-input\n */\nexport const PinInput = forwardRef<PinInputProps, \"div\">(\n ({ focusBorderColor, errorBorderColor, ...props }, ref) => {\n const [styles, mergedProps] = useMultiComponentStyle(\"PinInput\", {\n focusBorderColor,\n errorBorderColor,\n ...props,\n })\n let {\n id,\n className,\n type = \"number\",\n placeholder = \"○\",\n value,\n defaultValue,\n autoFocus,\n manageFocus = true,\n otp = false,\n mask,\n readOnly,\n \"aria-readonly\": ariaReadonly,\n onChange: onChangeProp,\n onComplete,\n items = 4,\n children,\n ...rest\n } = useFormControlProps(omitThemeProps(mergedProps))\n const formControlProps = useMemo(\n () => ({\n ...pickObject(rest, formControlProperties),\n readOnly,\n ariaReadonly,\n }),\n [ariaReadonly, readOnly, rest],\n )\n\n id ??= useId()\n\n const descendants = useDescendants()\n\n const [moveFocus, setMoveFocus] = useState<boolean>(true)\n const [focusedIndex, setFocusedIndex] = useState<number>(-1)\n\n useEffect(() => {\n if (!autoFocus) return\n\n const firstValue = descendants.firstValue()\n\n if (!firstValue) return\n\n requestAnimationFrame(() => firstValue.node.focus())\n }, [autoFocus, descendants])\n\n const [values, setValues] = useControllableState<string[]>({\n value: toArray(value),\n defaultValue: toArray(defaultValue) || [],\n onChange: (values) => onChangeProp?.(values.join(\"\")),\n })\n\n const focusNext = useCallback(\n (index: number) => {\n if (!moveFocus || !manageFocus) return\n\n const next = descendants.nextValue(index, undefined, false)\n\n if (!next) return\n\n requestAnimationFrame(() => next.node.focus())\n },\n [descendants, moveFocus, manageFocus],\n )\n\n const setValue = useCallback(\n (value: string, index: number, isFocus: boolean = true) => {\n let nextValues = [...values]\n\n nextValues[index] = value\n\n setValues(nextValues)\n\n nextValues = nextValues.filter(Boolean)\n\n const isComplete =\n value !== \"\" &&\n nextValues.length === descendants.count() &&\n nextValues.every((value) => value != null && value !== \"\")\n\n if (isComplete) {\n onComplete?.(nextValues.join(\"\"))\n descendants.value(index)?.node.blur()\n } else if (isFocus) {\n focusNext(index)\n }\n },\n [values, setValues, descendants, onComplete, focusNext],\n )\n\n const getNextValue = useCallback(\n (value: string | undefined, eventValue: string) => {\n let nextValue = eventValue\n\n if (!value?.length) return nextValue\n\n if (value[0] === eventValue.charAt(0)) {\n nextValue = eventValue.charAt(1)\n } else if (value[0] === eventValue.charAt(1)) {\n nextValue = eventValue.charAt(0)\n }\n\n return nextValue\n },\n [],\n )\n\n const onChange = useCallback(\n (index: number) =>\n ({ target }: ChangeEvent<HTMLInputElement>) => {\n const eventValue = target.value\n const currentValue = values[index]\n const nextValue = getNextValue(currentValue, eventValue)\n\n if (nextValue === \"\") {\n setValue(\"\", index)\n\n return\n }\n\n if (eventValue.length > 2) {\n if (!validate(eventValue, type)) return\n\n const nextValue = eventValue\n .split(\"\")\n .filter((_, index) => index < descendants.count())\n\n setValues(nextValue)\n\n if (nextValue.length === descendants.count()) {\n onComplete?.(nextValue.join(\"\"))\n descendants.value(index)?.node.blur()\n }\n } else {\n if (validate(nextValue, type)) setValue(nextValue, index)\n\n setMoveFocus(true)\n }\n },\n [\n descendants,\n getNextValue,\n onComplete,\n setValue,\n setValues,\n type,\n values,\n ],\n )\n\n const onKeyDown = useCallback(\n (index: number) =>\n ({ key, target }: KeyboardEvent<HTMLInputElement>) => {\n if (key !== \"Backspace\" || !manageFocus) return\n\n if ((target as HTMLInputElement).value === \"\") {\n const prevInput = descendants.prevValue(index, undefined, false)\n\n if (!prevInput) return\n\n setValue(\"\", index - 1, false)\n prevInput.node?.focus()\n setMoveFocus(true)\n } else {\n setMoveFocus(false)\n }\n },\n [descendants, manageFocus, setValue],\n )\n\n const onFocus = useCallback(\n (index: number) => () => setFocusedIndex(index),\n [],\n )\n\n const onBlur = useCallback(() => setFocusedIndex(-1), [])\n\n const getInputProps = useCallback(\n ({\n index,\n ...props\n }: PinInputFieldProps & {\n index: number\n ref?: Ref<HTMLInputElement>\n }): PinInputFieldProps => ({\n inputMode: type === \"number\" ? \"numeric\" : \"text\",\n type: mask ? \"password\" : type === \"number\" ? \"tel\" : \"text\",\n ...formControlProps,\n ...filterUndefined(props),\n id: `${id}-${index}`,\n value: values[index] || \"\",\n onChange: handlerAll(props.onChange, onChange(index)),\n onKeyDown: handlerAll(props.onKeyDown, onKeyDown(index)),\n onFocus: handlerAll(props.onFocus, onFocus(index)),\n onBlur: handlerAll(props.onBlur, onBlur),\n autoComplete: otp ? \"one-time-code\" : \"off\",\n placeholder:\n focusedIndex === index && !readOnly && !props.readOnly\n ? \"\"\n : placeholder,\n }),\n [\n type,\n mask,\n formControlProps,\n id,\n values,\n onChange,\n onKeyDown,\n onFocus,\n onBlur,\n otp,\n focusedIndex,\n readOnly,\n placeholder,\n ],\n )\n\n const css: CSSUIObject = {\n display: \"flex\",\n alignItems: \"center\",\n ...styles.container,\n }\n\n let cloneChildren = getValidChildren(children)\n\n if (!cloneChildren.length)\n for (let i = 0; i < items; i++) {\n cloneChildren.push(<PinInputField key={i} />)\n }\n\n return (\n <DescendantsContextProvider value={descendants}>\n <PinInputProvider value={{ getInputProps, styles }}>\n <ui.div\n ref={ref}\n className={cx(\"ui-pin-input\", className)}\n __css={css}\n {...rest}\n >\n {cloneChildren}\n </ui.div>\n </PinInputProvider>\n </DescendantsContextProvider>\n )\n },\n)\n\nexport type PinInputFieldProps = HTMLUIProps<\"input\"> & FormControlOptions\n\nexport const PinInputField = forwardRef<PinInputFieldProps, \"input\">(\n ({ className, ...rest }, ref) => {\n const { getInputProps, styles } = usePinInputContext()\n const { index, register } = useDescendant()\n\n rest = useFormControlProps(rest)\n\n const css: CSSUIObject = { ...styles.field }\n\n return (\n <ui.input\n className={cx(\"ui-pin-input__field\", className)}\n {...getInputProps({ ...rest, ref: mergeRefs(register, ref), index })}\n __css={css}\n />\n )\n },\n)\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA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,QACA;AAAA,MACF;AAAA,IACF;AAEA,UAAM,MAAmB;AAAA,MACvB,SAAS;AAAA,MACT,YAAY;AAAA,MACZ,GAAG,OAAO;AAAA,IACZ;AAEA,QAAI,oBAAgB,+BAAiB,QAAQ;AAE7C,QAAI,CAAC,cAAc;AACjB,eAAS,IAAI,GAAG,IAAI,OAAO,KAAK;AAC9B,sBAAc,KAAK,4CAAC,mBAAmB,CAAG,CAAE;AAAA,MAC9C;AAEF,WACE,4CAAC,8BAA2B,OAAO,aACjC,sDAAC,oBAAiB,OAAO,EAAE,eAAe,OAAO,GAC/C;AAAA,MAAC,eAAG;AAAA,MAAH;AAAA,QACC;AAAA,QACA,eAAW,iBAAG,gBAAgB,SAAS;AAAA,QACvC,OAAO;AAAA,QACN,GAAG;AAAA,QAEH;AAAA;AAAA,IACH,GACF,GACF;AAAA,EAEJ;AACF;AAIO,IAAM,oBAAgB;AAAA,EAC3B,CAAC,EAAE,WAAW,GAAG,KAAK,GAAG,QAAQ;AAC/B,UAAM,EAAE,eAAe,OAAO,IAAI,mBAAmB;AACrD,UAAM,EAAE,OAAO,SAAS,IAAI,cAAc;AAE1C,eAAO,yCAAoB,IAAI;AAE/B,UAAM,MAAmB,EAAE,GAAG,OAAO,MAAM;AAE3C,WACE;AAAA,MAAC,eAAG;AAAA,MAAH;AAAA,QACC,eAAW,iBAAG,uBAAuB,SAAS;AAAA,QAC7C,GAAG,cAAc,EAAE,GAAG,MAAM,SAAK,wBAAU,UAAU,GAAG,GAAG,MAAM,CAAC;AAAA,QACnE,OAAO;AAAA;AAAA,IACT;AAAA,EAEJ;AACF;","names":["values","value","nextValue","index","props"]}
|
package/dist/index.mjs
CHANGED
package/dist/pin-input.js
CHANGED
@@ -62,12 +62,22 @@ var PinInput = (0, import_core.forwardRef)(
|
|
62
62
|
manageFocus = true,
|
63
63
|
otp = false,
|
64
64
|
mask,
|
65
|
-
|
65
|
+
readOnly,
|
66
|
+
"aria-readonly": ariaReadonly,
|
67
|
+
onChange: onChangeProp,
|
66
68
|
onComplete,
|
67
69
|
items = 4,
|
68
70
|
children,
|
69
71
|
...rest
|
70
72
|
} = (0, import_form_control.useFormControlProps)((0, import_core.omitThemeProps)(mergedProps));
|
73
|
+
const formControlProps = (0, import_react.useMemo)(
|
74
|
+
() => ({
|
75
|
+
...(0, import_utils.pickObject)(rest, import_form_control.formControlProperties),
|
76
|
+
readOnly,
|
77
|
+
ariaReadonly
|
78
|
+
}),
|
79
|
+
[ariaReadonly, readOnly, rest]
|
80
|
+
);
|
71
81
|
id != null ? id : id = (0, import_react.useId)();
|
72
82
|
const descendants = useDescendants();
|
73
83
|
const [moveFocus, setMoveFocus] = (0, import_react.useState)(true);
|
@@ -83,7 +93,7 @@ var PinInput = (0, import_core.forwardRef)(
|
|
83
93
|
const [values, setValues] = (0, import_use_controllable_state.useControllableState)({
|
84
94
|
value: toArray(value),
|
85
95
|
defaultValue: toArray(defaultValue) || [],
|
86
|
-
onChange: (values2) =>
|
96
|
+
onChange: (values2) => onChangeProp == null ? void 0 : onChangeProp(values2.join(""))
|
87
97
|
});
|
88
98
|
const focusNext = (0, import_react.useCallback)(
|
89
99
|
(index) => {
|
@@ -127,90 +137,98 @@ var PinInput = (0, import_core.forwardRef)(
|
|
127
137
|
},
|
128
138
|
[]
|
129
139
|
);
|
130
|
-
const
|
131
|
-
({
|
132
|
-
|
133
|
-
|
134
|
-
|
135
|
-
const
|
136
|
-
|
137
|
-
|
138
|
-
|
139
|
-
|
140
|
-
|
141
|
-
|
142
|
-
return;
|
143
|
-
}
|
144
|
-
if (eventValue.length > 2) {
|
145
|
-
if (!validate(eventValue, type))
|
146
|
-
return;
|
147
|
-
const nextValue2 = eventValue.split("").filter((_2, index2) => index2 < descendants.count());
|
148
|
-
setValues(nextValue2);
|
149
|
-
if (nextValue2.length === descendants.count()) {
|
150
|
-
onComplete == null ? void 0 : onComplete(nextValue2.join(""));
|
151
|
-
(_a = descendants.value(index)) == null ? void 0 : _a.node.blur();
|
152
|
-
}
|
153
|
-
} else {
|
154
|
-
if (validate(nextValue, type))
|
155
|
-
setValue(nextValue, index);
|
156
|
-
setMoveFocus(true);
|
157
|
-
}
|
158
|
-
};
|
159
|
-
const onKeyDown = ({
|
160
|
-
key,
|
161
|
-
target
|
162
|
-
}) => {
|
163
|
-
var _a;
|
164
|
-
if (key !== "Backspace" || !manageFocus)
|
140
|
+
const onChange = (0, import_react.useCallback)(
|
141
|
+
(index) => ({ target }) => {
|
142
|
+
var _a;
|
143
|
+
const eventValue = target.value;
|
144
|
+
const currentValue = values[index];
|
145
|
+
const nextValue = getNextValue(currentValue, eventValue);
|
146
|
+
if (nextValue === "") {
|
147
|
+
setValue("", index);
|
148
|
+
return;
|
149
|
+
}
|
150
|
+
if (eventValue.length > 2) {
|
151
|
+
if (!validate(eventValue, type))
|
165
152
|
return;
|
166
|
-
|
167
|
-
|
168
|
-
|
169
|
-
|
170
|
-
|
171
|
-
(_a = prevInput.node) == null ? void 0 : _a.focus();
|
172
|
-
setMoveFocus(true);
|
173
|
-
} else {
|
174
|
-
setMoveFocus(false);
|
153
|
+
const nextValue2 = eventValue.split("").filter((_, index2) => index2 < descendants.count());
|
154
|
+
setValues(nextValue2);
|
155
|
+
if (nextValue2.length === descendants.count()) {
|
156
|
+
onComplete == null ? void 0 : onComplete(nextValue2.join(""));
|
157
|
+
(_a = descendants.value(index)) == null ? void 0 : _a.node.blur();
|
175
158
|
}
|
176
|
-
}
|
177
|
-
|
178
|
-
|
179
|
-
|
180
|
-
|
181
|
-
type: mask ? "password" : type === "number" ? "tel" : "text",
|
182
|
-
...(0, import_utils.pickObject)(
|
183
|
-
rest,
|
184
|
-
(0, import_form_control.getFormControlProperties)({ omit: ["aria-readonly"] })
|
185
|
-
),
|
186
|
-
...(0, import_utils.filterUndefined)(props2),
|
187
|
-
id: `${id}-${index}`,
|
188
|
-
value: values[index] || "",
|
189
|
-
onChange: (0, import_utils.handlerAll)(props2.onChange, onChange2),
|
190
|
-
onKeyDown: (0, import_utils.handlerAll)(props2.onKeyDown, onKeyDown),
|
191
|
-
onFocus: (0, import_utils.handlerAll)(props2.onFocus, onFocus),
|
192
|
-
onBlur: (0, import_utils.handlerAll)(props2.onBlur, onBlur),
|
193
|
-
autoComplete: otp ? "one-time-code" : "off",
|
194
|
-
placeholder: focusedIndex === index && !rest.readOnly && !props2.readOnly ? "" : placeholder
|
195
|
-
};
|
159
|
+
} else {
|
160
|
+
if (validate(nextValue, type))
|
161
|
+
setValue(nextValue, index);
|
162
|
+
setMoveFocus(true);
|
163
|
+
}
|
196
164
|
},
|
197
165
|
[
|
198
166
|
descendants,
|
199
|
-
focusedIndex,
|
200
167
|
getNextValue,
|
201
|
-
id,
|
202
|
-
manageFocus,
|
203
|
-
mask,
|
204
168
|
onComplete,
|
205
|
-
otp,
|
206
|
-
placeholder,
|
207
|
-
rest,
|
208
169
|
setValue,
|
209
170
|
setValues,
|
210
171
|
type,
|
211
172
|
values
|
212
173
|
]
|
213
174
|
);
|
175
|
+
const onKeyDown = (0, import_react.useCallback)(
|
176
|
+
(index) => ({ key, target }) => {
|
177
|
+
var _a;
|
178
|
+
if (key !== "Backspace" || !manageFocus)
|
179
|
+
return;
|
180
|
+
if (target.value === "") {
|
181
|
+
const prevInput = descendants.prevValue(index, void 0, false);
|
182
|
+
if (!prevInput)
|
183
|
+
return;
|
184
|
+
setValue("", index - 1, false);
|
185
|
+
(_a = prevInput.node) == null ? void 0 : _a.focus();
|
186
|
+
setMoveFocus(true);
|
187
|
+
} else {
|
188
|
+
setMoveFocus(false);
|
189
|
+
}
|
190
|
+
},
|
191
|
+
[descendants, manageFocus, setValue]
|
192
|
+
);
|
193
|
+
const onFocus = (0, import_react.useCallback)(
|
194
|
+
(index) => () => setFocusedIndex(index),
|
195
|
+
[]
|
196
|
+
);
|
197
|
+
const onBlur = (0, import_react.useCallback)(() => setFocusedIndex(-1), []);
|
198
|
+
const getInputProps = (0, import_react.useCallback)(
|
199
|
+
({
|
200
|
+
index,
|
201
|
+
...props2
|
202
|
+
}) => ({
|
203
|
+
inputMode: type === "number" ? "numeric" : "text",
|
204
|
+
type: mask ? "password" : type === "number" ? "tel" : "text",
|
205
|
+
...formControlProps,
|
206
|
+
...(0, import_utils.filterUndefined)(props2),
|
207
|
+
id: `${id}-${index}`,
|
208
|
+
value: values[index] || "",
|
209
|
+
onChange: (0, import_utils.handlerAll)(props2.onChange, onChange(index)),
|
210
|
+
onKeyDown: (0, import_utils.handlerAll)(props2.onKeyDown, onKeyDown(index)),
|
211
|
+
onFocus: (0, import_utils.handlerAll)(props2.onFocus, onFocus(index)),
|
212
|
+
onBlur: (0, import_utils.handlerAll)(props2.onBlur, onBlur),
|
213
|
+
autoComplete: otp ? "one-time-code" : "off",
|
214
|
+
placeholder: focusedIndex === index && !readOnly && !props2.readOnly ? "" : placeholder
|
215
|
+
}),
|
216
|
+
[
|
217
|
+
type,
|
218
|
+
mask,
|
219
|
+
formControlProps,
|
220
|
+
id,
|
221
|
+
values,
|
222
|
+
onChange,
|
223
|
+
onKeyDown,
|
224
|
+
onFocus,
|
225
|
+
onBlur,
|
226
|
+
otp,
|
227
|
+
focusedIndex,
|
228
|
+
readOnly,
|
229
|
+
placeholder
|
230
|
+
]
|
231
|
+
);
|
214
232
|
const css = {
|
215
233
|
display: "flex",
|
216
234
|
alignItems: "center",
|
@@ -221,14 +239,13 @@ var PinInput = (0, import_core.forwardRef)(
|
|
221
239
|
for (let i = 0; i < items; i++) {
|
222
240
|
cloneChildren.push(/* @__PURE__ */ (0, import_jsx_runtime.jsx)(PinInputField, {}, i));
|
223
241
|
}
|
224
|
-
const { "aria-readonly": _, ...restWithoutAriaReadonly } = rest;
|
225
242
|
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)(
|
226
243
|
import_core.ui.div,
|
227
244
|
{
|
228
245
|
ref,
|
229
246
|
className: (0, import_utils.cx)("ui-pin-input", className),
|
230
247
|
__css: css,
|
231
|
-
...
|
248
|
+
...rest,
|
232
249
|
children: cloneChildren
|
233
250
|
}
|
234
251
|
) }) });
|
package/dist/pin-input.js.map
CHANGED
@@ -1 +1 @@
|
|
1
|
-
{"version":3,"sources":["../src/pin-input.tsx"],"sourcesContent":["import type {\n CSSUIObject,\n HTMLUIProps,\n ThemeProps,\n ColorModeToken,\n CSS,\n} from \"@yamada-ui/core\"\nimport {\n ui,\n forwardRef,\n useMultiComponentStyle,\n omitThemeProps,\n} from \"@yamada-ui/core\"\nimport type { FormControlOptions } from \"@yamada-ui/form-control\"\nimport {\n getFormControlProperties,\n useFormControlProps,\n} from \"@yamada-ui/form-control\"\nimport { useControllableState } from \"@yamada-ui/use-controllable-state\"\nimport { createDescendant } from \"@yamada-ui/use-descendant\"\nimport {\n createContext,\n cx,\n handlerAll,\n mergeRefs,\n pickObject,\n filterUndefined,\n getValidChildren,\n} from \"@yamada-ui/utils\"\nimport type { ChangeEvent, KeyboardEvent, Ref } from \"react\"\nimport { useCallback, useEffect, useId, useState } from \"react\"\n\nconst toArray = (value?: string) => value?.split(\"\")\n\nconst validate = (value: string, type: PinInputProps[\"type\"]) => {\n const NUMERIC_REGEX = /^[0-9]+$/\n const ALPHA_NUMERIC_REGEX = /^[a-zA-Z0-9]+$/i\n\n const regex = type === \"alphanumeric\" ? ALPHA_NUMERIC_REGEX : NUMERIC_REGEX\n\n return regex.test(value)\n}\n\ntype PinInputContext = {\n getInputProps: (\n props: PinInputFieldProps & {\n index: number\n ref?: Ref<HTMLInputElement>\n },\n ) => PinInputFieldProps\n styles: Record<string, CSSUIObject>\n}\n\nconst [PinInputProvider, usePinInputContext] = createContext<PinInputContext>({\n strict: false,\n name: \"PinInputContext\",\n})\n\nconst { DescendantsContextProvider, useDescendants, useDescendant } =\n createDescendant<HTMLInputElement>()\n\ntype PinInputOptions = {\n /**\n * The top-level id string that will be applied to the input fields.\n * The index of the input will be appended to this top-level id.\n */\n id?: string\n /**\n * The type of values the pin-input should allow.\n *\n * @default 'number'\n */\n type?: \"alphanumeric\" | \"number\"\n /**\n * The placeholder for the pin input.\n *\n * @default '○'\n */\n placeholder?: string\n /**\n * The value of the pin input.\n */\n value?: string\n /**\n * The initial value of the pin input.\n */\n defaultValue?: string\n /**\n * If `true`, the pin input receives focus on mount.\n *\n * @default false\n */\n autoFocus?: boolean\n /**\n * If `true`, focus will move automatically to the next input once filled.\n *\n * @default true\n */\n manageFocus?: boolean\n /**\n * If `true`, the pin input component signals to its fields that they should.\n */\n otp?: boolean\n /**\n * If `true`, the input's value will be masked just like `type=password`.\n */\n mask?: boolean\n /**\n * Function called on input change.\n */\n onChange?: (value: string) => void\n /**\n * Function called when all inputs have valid values.\n */\n onComplete?: (value: string) => void\n /**\n * The number of inputs to display.\n *\n * @default 4\n */\n items?: number\n /**\n * The border color when the input is focused.\n */\n focusBorderColor?: ColorModeToken<CSS.Property.BorderColor, \"colors\">\n /**\n * The border color when the input is invalid.\n */\n errorBorderColor?: ColorModeToken<CSS.Property.BorderColor, \"colors\">\n}\n\nexport type PinInputProps = Omit<HTMLUIProps<\"div\">, \"onChange\" | \"mask\"> &\n ThemeProps<\"PinInput\"> &\n FormControlOptions &\n PinInputOptions\n\n/**\n * `PinInput` is a component used to capture pin codes or OTP (One-Time Password) inputs.\n *\n * @see Docs https://yamada-ui.com/components/forms/pin-input\n */\nexport const PinInput = forwardRef<PinInputProps, \"div\">(\n ({ focusBorderColor, errorBorderColor, ...props }, ref) => {\n const [styles, mergedProps] = useMultiComponentStyle(\"PinInput\", {\n focusBorderColor,\n errorBorderColor,\n ...props,\n })\n let {\n id,\n className,\n type = \"number\",\n placeholder = \"○\",\n value,\n defaultValue,\n autoFocus,\n manageFocus = true,\n otp = false,\n mask,\n onChange,\n onComplete,\n items = 4,\n children,\n ...rest\n } = useFormControlProps(omitThemeProps(mergedProps))\n\n id ??= useId()\n\n const descendants = useDescendants()\n\n const [moveFocus, setMoveFocus] = useState<boolean>(true)\n const [focusedIndex, setFocusedIndex] = useState<number>(-1)\n\n useEffect(() => {\n if (!autoFocus) return\n\n const firstValue = descendants.firstValue()\n\n if (!firstValue) return\n\n requestAnimationFrame(() => firstValue.node.focus())\n }, [autoFocus, descendants])\n\n const [values, setValues] = useControllableState<string[]>({\n value: toArray(value),\n defaultValue: toArray(defaultValue) || [],\n onChange: (values) => onChange?.(values.join(\"\")),\n })\n\n const focusNext = useCallback(\n (index: number) => {\n if (!moveFocus || !manageFocus) return\n\n const next = descendants.nextValue(index, undefined, false)\n\n if (!next) return\n\n requestAnimationFrame(() => next.node.focus())\n },\n [descendants, moveFocus, manageFocus],\n )\n\n const setValue = useCallback(\n (value: string, index: number, isFocus: boolean = true) => {\n let nextValues = [...values]\n\n nextValues[index] = value\n\n setValues(nextValues)\n\n nextValues = nextValues.filter(Boolean)\n\n const isComplete =\n value !== \"\" &&\n nextValues.length === descendants.count() &&\n nextValues.every((value) => value != null && value !== \"\")\n\n if (isComplete) {\n onComplete?.(nextValues.join(\"\"))\n descendants.value(index)?.node.blur()\n } else if (isFocus) {\n focusNext(index)\n }\n },\n [values, setValues, descendants, onComplete, focusNext],\n )\n\n const getNextValue = useCallback(\n (value: string | undefined, eventValue: string) => {\n let nextValue = eventValue\n\n if (!value?.length) return nextValue\n\n if (value[0] === eventValue.charAt(0)) {\n nextValue = eventValue.charAt(1)\n } else if (value[0] === eventValue.charAt(1)) {\n nextValue = eventValue.charAt(0)\n }\n\n return nextValue\n },\n [],\n )\n\n const getInputProps = useCallback(\n ({\n index,\n ...props\n }: PinInputFieldProps & {\n index: number\n ref?: Ref<HTMLInputElement>\n }): PinInputFieldProps => {\n const onChange = ({ target }: ChangeEvent<HTMLInputElement>) => {\n const eventValue = target.value\n const currentValue = values[index]\n const nextValue = getNextValue(currentValue, eventValue)\n\n if (nextValue === \"\") {\n setValue(\"\", index)\n\n return\n }\n\n if (eventValue.length > 2) {\n if (!validate(eventValue, type)) return\n\n const nextValue = eventValue\n .split(\"\")\n .filter((_, index) => index < descendants.count())\n\n setValues(nextValue)\n\n if (nextValue.length === descendants.count()) {\n onComplete?.(nextValue.join(\"\"))\n descendants.value(index)?.node.blur()\n }\n } else {\n if (validate(nextValue, type)) setValue(nextValue, index)\n\n setMoveFocus(true)\n }\n }\n\n const onKeyDown = ({\n key,\n target,\n }: KeyboardEvent<HTMLInputElement>) => {\n if (key !== \"Backspace\" || !manageFocus) return\n\n if ((target as HTMLInputElement).value === \"\") {\n const prevInput = descendants.prevValue(index, undefined, false)\n\n if (!prevInput) return\n\n setValue(\"\", index - 1, false)\n prevInput.node?.focus()\n setMoveFocus(true)\n } else {\n setMoveFocus(false)\n }\n }\n\n const onFocus = () => setFocusedIndex(index)\n\n const onBlur = () => setFocusedIndex(-1)\n\n return {\n inputMode: type === \"number\" ? \"numeric\" : \"text\",\n type: mask ? \"password\" : type === \"number\" ? \"tel\" : \"text\",\n ...pickObject(\n rest,\n getFormControlProperties({ omit: [\"aria-readonly\"] }),\n ),\n ...filterUndefined(props),\n id: `${id}-${index}`,\n value: values[index] || \"\",\n onChange: handlerAll(props.onChange, onChange),\n onKeyDown: handlerAll(props.onKeyDown, onKeyDown),\n onFocus: handlerAll(props.onFocus, onFocus),\n onBlur: handlerAll(props.onBlur, onBlur),\n autoComplete: otp ? \"one-time-code\" : \"off\",\n placeholder:\n focusedIndex === index && !rest.readOnly && !props.readOnly\n ? \"\"\n : placeholder,\n }\n },\n [\n descendants,\n focusedIndex,\n getNextValue,\n id,\n manageFocus,\n mask,\n onComplete,\n otp,\n placeholder,\n rest,\n setValue,\n setValues,\n type,\n values,\n ],\n )\n\n const css: CSSUIObject = {\n display: \"flex\",\n alignItems: \"center\",\n ...styles.container,\n }\n\n let cloneChildren = getValidChildren(children)\n\n if (!cloneChildren.length)\n for (let i = 0; i < items; i++) {\n cloneChildren.push(<PinInputField key={i} />)\n }\n\n const { \"aria-readonly\": _, ...restWithoutAriaReadonly } = rest\n\n return (\n <DescendantsContextProvider value={descendants}>\n <PinInputProvider value={{ getInputProps, styles }}>\n <ui.div\n ref={ref}\n className={cx(\"ui-pin-input\", className)}\n __css={css}\n {...restWithoutAriaReadonly}\n >\n {cloneChildren}\n </ui.div>\n </PinInputProvider>\n </DescendantsContextProvider>\n )\n },\n)\n\nexport type PinInputFieldProps = HTMLUIProps<\"input\"> & FormControlOptions\n\nexport const PinInputField = forwardRef<PinInputFieldProps, \"input\">(\n ({ className, ...rest }, ref) => {\n const { getInputProps, styles } = usePinInputContext()\n const { index, register } = useDescendant()\n\n rest = useFormControlProps(rest)\n\n const css: CSSUIObject = { ...styles.field }\n\n return (\n <ui.input\n className={cx(\"ui-pin-input__field\", className)}\n {...getInputProps({ ...rest, ref: mergeRefs(register, ref), index })}\n __css={css}\n />\n )\n },\n)\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAOA,kBAKO;AAEP,0BAGO;AACP,oCAAqC;AACrC,4BAAiC;AACjC,mBAQO;AAEP,mBAAwD;AAqU7B;AAnU3B,IAAM,UAAU,CAAC,UAAmB,+BAAO,MAAM;AAEjD,IAAM,WAAW,CAAC,OAAe,SAAgC;AAC/D,QAAM,gBAAgB;AACtB,QAAM,sBAAsB;AAE5B,QAAM,QAAQ,SAAS,iBAAiB,sBAAsB;AAE9D,SAAO,MAAM,KAAK,KAAK;AACzB;AAYA,IAAM,CAAC,kBAAkB,kBAAkB,QAAI,4BAA+B;AAAA,EAC5E,QAAQ;AAAA,EACR,MAAM;AACR,CAAC;AAED,IAAM,EAAE,4BAA4B,gBAAgB,cAAc,QAChE,wCAAmC;AAkF9B,IAAM,eAAW;AAAA,EACtB,CAAC,EAAE,kBAAkB,kBAAkB,GAAG,MAAM,GAAG,QAAQ;AACzD,UAAM,CAAC,QAAQ,WAAW,QAAI,oCAAuB,YAAY;AAAA,MAC/D;AAAA,MACA;AAAA,MACA,GAAG;AAAA,I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;AAAA;AAAA,IACT;AAAA,EAEJ;AACF;","names":["values","value","props","onChange","nextValue","_","index"]}
|
1
|
+
{"version":3,"sources":["../src/pin-input.tsx"],"sourcesContent":["import type {\n CSSUIObject,\n HTMLUIProps,\n ThemeProps,\n ColorModeToken,\n CSS,\n} from \"@yamada-ui/core\"\nimport {\n ui,\n forwardRef,\n useMultiComponentStyle,\n omitThemeProps,\n} from \"@yamada-ui/core\"\nimport type { FormControlOptions } from \"@yamada-ui/form-control\"\nimport {\n formControlProperties,\n useFormControlProps,\n} from \"@yamada-ui/form-control\"\nimport { useControllableState } from \"@yamada-ui/use-controllable-state\"\nimport { createDescendant } from \"@yamada-ui/use-descendant\"\nimport {\n createContext,\n cx,\n handlerAll,\n mergeRefs,\n pickObject,\n filterUndefined,\n getValidChildren,\n} from \"@yamada-ui/utils\"\nimport type { ChangeEvent, KeyboardEvent, Ref } from \"react\"\nimport { useCallback, useEffect, useId, useMemo, useState } from \"react\"\n\nconst toArray = (value?: string) => value?.split(\"\")\n\nconst validate = (value: string, type: PinInputProps[\"type\"]) => {\n const NUMERIC_REGEX = /^[0-9]+$/\n const ALPHA_NUMERIC_REGEX = /^[a-zA-Z0-9]+$/i\n\n const regex = type === \"alphanumeric\" ? ALPHA_NUMERIC_REGEX : NUMERIC_REGEX\n\n return regex.test(value)\n}\n\ntype PinInputContext = {\n getInputProps: (\n props: PinInputFieldProps & {\n index: number\n ref?: Ref<HTMLInputElement>\n },\n ) => PinInputFieldProps\n styles: Record<string, CSSUIObject>\n}\n\nconst [PinInputProvider, usePinInputContext] = createContext<PinInputContext>({\n strict: false,\n name: \"PinInputContext\",\n})\n\nconst { DescendantsContextProvider, useDescendants, useDescendant } =\n createDescendant<HTMLInputElement>()\n\ntype PinInputOptions = {\n /**\n * The top-level id string that will be applied to the input fields.\n * The index of the input will be appended to this top-level id.\n */\n id?: string\n /**\n * The type of values the pin-input should allow.\n *\n * @default 'number'\n */\n type?: \"alphanumeric\" | \"number\"\n /**\n * The placeholder for the pin input.\n *\n * @default '○'\n */\n placeholder?: string\n /**\n * The value of the pin input.\n */\n value?: string\n /**\n * The initial value of the pin input.\n */\n defaultValue?: string\n /**\n * If `true`, the pin input receives focus on mount.\n *\n * @default false\n */\n autoFocus?: boolean\n /**\n * If `true`, focus will move automatically to the next input once filled.\n *\n * @default true\n */\n manageFocus?: boolean\n /**\n * If `true`, the pin input component signals to its fields that they should.\n */\n otp?: boolean\n /**\n * If `true`, the input's value will be masked just like `type=password`.\n */\n mask?: boolean\n /**\n * Function called on input change.\n */\n onChange?: (value: string) => void\n /**\n * Function called when all inputs have valid values.\n */\n onComplete?: (value: string) => void\n /**\n * The number of inputs to display.\n *\n * @default 4\n */\n items?: number\n /**\n * The border color when the input is focused.\n */\n focusBorderColor?: ColorModeToken<CSS.Property.BorderColor, \"colors\">\n /**\n * The border color when the input is invalid.\n */\n errorBorderColor?: ColorModeToken<CSS.Property.BorderColor, \"colors\">\n}\n\nexport type PinInputProps = Omit<HTMLUIProps<\"div\">, \"onChange\" | \"mask\"> &\n ThemeProps<\"PinInput\"> &\n FormControlOptions &\n PinInputOptions\n\n/**\n * `PinInput` is a component used to capture pin codes or OTP (One-Time Password) inputs.\n *\n * @see Docs https://yamada-ui.com/components/forms/pin-input\n */\nexport const PinInput = forwardRef<PinInputProps, \"div\">(\n ({ focusBorderColor, errorBorderColor, ...props }, ref) => {\n const [styles, mergedProps] = useMultiComponentStyle(\"PinInput\", {\n focusBorderColor,\n errorBorderColor,\n ...props,\n })\n let {\n id,\n className,\n type = \"number\",\n placeholder = \"○\",\n value,\n defaultValue,\n autoFocus,\n manageFocus = true,\n otp = false,\n mask,\n readOnly,\n \"aria-readonly\": ariaReadonly,\n onChange: onChangeProp,\n onComplete,\n items = 4,\n children,\n ...rest\n } = useFormControlProps(omitThemeProps(mergedProps))\n const formControlProps = useMemo(\n () => ({\n ...pickObject(rest, formControlProperties),\n readOnly,\n ariaReadonly,\n }),\n [ariaReadonly, readOnly, rest],\n )\n\n id ??= useId()\n\n const descendants = useDescendants()\n\n const [moveFocus, setMoveFocus] = useState<boolean>(true)\n const [focusedIndex, setFocusedIndex] = useState<number>(-1)\n\n useEffect(() => {\n if (!autoFocus) return\n\n const firstValue = descendants.firstValue()\n\n if (!firstValue) return\n\n requestAnimationFrame(() => firstValue.node.focus())\n }, [autoFocus, descendants])\n\n const [values, setValues] = useControllableState<string[]>({\n value: toArray(value),\n defaultValue: toArray(defaultValue) || [],\n onChange: (values) => onChangeProp?.(values.join(\"\")),\n })\n\n const focusNext = useCallback(\n (index: number) => {\n if (!moveFocus || !manageFocus) return\n\n const next = descendants.nextValue(index, undefined, false)\n\n if (!next) return\n\n requestAnimationFrame(() => next.node.focus())\n },\n [descendants, moveFocus, manageFocus],\n )\n\n const setValue = useCallback(\n (value: string, index: number, isFocus: boolean = true) => {\n let nextValues = [...values]\n\n nextValues[index] = value\n\n setValues(nextValues)\n\n nextValues = nextValues.filter(Boolean)\n\n const isComplete =\n value !== \"\" &&\n nextValues.length === descendants.count() &&\n nextValues.every((value) => value != null && value !== \"\")\n\n if (isComplete) {\n onComplete?.(nextValues.join(\"\"))\n descendants.value(index)?.node.blur()\n } else if (isFocus) {\n focusNext(index)\n }\n },\n [values, setValues, descendants, onComplete, focusNext],\n )\n\n const getNextValue = useCallback(\n (value: string | undefined, eventValue: string) => {\n let nextValue = eventValue\n\n if (!value?.length) return nextValue\n\n if (value[0] === eventValue.charAt(0)) {\n nextValue = eventValue.charAt(1)\n } else if (value[0] === eventValue.charAt(1)) {\n nextValue = eventValue.charAt(0)\n }\n\n return nextValue\n },\n [],\n )\n\n const onChange = useCallback(\n (index: number) =>\n ({ target }: ChangeEvent<HTMLInputElement>) => {\n const eventValue = target.value\n const currentValue = values[index]\n const nextValue = getNextValue(currentValue, eventValue)\n\n if (nextValue === \"\") {\n setValue(\"\", index)\n\n return\n }\n\n if (eventValue.length > 2) {\n if (!validate(eventValue, type)) return\n\n const nextValue = eventValue\n .split(\"\")\n .filter((_, index) => index < descendants.count())\n\n setValues(nextValue)\n\n if (nextValue.length === descendants.count()) {\n onComplete?.(nextValue.join(\"\"))\n descendants.value(index)?.node.blur()\n }\n } else {\n if (validate(nextValue, type)) setValue(nextValue, index)\n\n setMoveFocus(true)\n }\n },\n [\n descendants,\n getNextValue,\n onComplete,\n setValue,\n setValues,\n type,\n values,\n ],\n )\n\n const onKeyDown = useCallback(\n (index: number) =>\n ({ key, target }: KeyboardEvent<HTMLInputElement>) => {\n if (key !== \"Backspace\" || !manageFocus) return\n\n if ((target as HTMLInputElement).value === \"\") {\n const prevInput = descendants.prevValue(index, undefined, false)\n\n if (!prevInput) return\n\n setValue(\"\", index - 1, false)\n prevInput.node?.focus()\n setMoveFocus(true)\n } else {\n setMoveFocus(false)\n }\n },\n [descendants, manageFocus, setValue],\n )\n\n const onFocus = useCallback(\n (index: number) => () => setFocusedIndex(index),\n [],\n )\n\n const onBlur = useCallback(() => setFocusedIndex(-1), [])\n\n const getInputProps = useCallback(\n ({\n index,\n ...props\n }: PinInputFieldProps & {\n index: number\n ref?: Ref<HTMLInputElement>\n }): PinInputFieldProps => ({\n inputMode: type === \"number\" ? \"numeric\" : \"text\",\n type: mask ? \"password\" : type === \"number\" ? \"tel\" : \"text\",\n ...formControlProps,\n ...filterUndefined(props),\n id: `${id}-${index}`,\n value: values[index] || \"\",\n onChange: handlerAll(props.onChange, onChange(index)),\n onKeyDown: handlerAll(props.onKeyDown, onKeyDown(index)),\n onFocus: handlerAll(props.onFocus, onFocus(index)),\n onBlur: handlerAll(props.onBlur, onBlur),\n autoComplete: otp ? \"one-time-code\" : \"off\",\n placeholder:\n focusedIndex === index && !readOnly && !props.readOnly\n ? \"\"\n : placeholder,\n }),\n [\n type,\n mask,\n formControlProps,\n id,\n values,\n onChange,\n onKeyDown,\n onFocus,\n onBlur,\n otp,\n focusedIndex,\n readOnly,\n placeholder,\n ],\n )\n\n const css: CSSUIObject = {\n display: \"flex\",\n alignItems: \"center\",\n ...styles.container,\n }\n\n let cloneChildren = getValidChildren(children)\n\n if (!cloneChildren.length)\n for (let i = 0; i < items; i++) {\n cloneChildren.push(<PinInputField key={i} />)\n }\n\n return (\n <DescendantsContextProvider value={descendants}>\n <PinInputProvider value={{ getInputProps, styles }}>\n <ui.div\n ref={ref}\n className={cx(\"ui-pin-input\", className)}\n __css={css}\n {...rest}\n >\n {cloneChildren}\n </ui.div>\n </PinInputProvider>\n </DescendantsContextProvider>\n )\n },\n)\n\nexport type PinInputFieldProps = HTMLUIProps<\"input\"> & FormControlOptions\n\nexport const PinInputField = forwardRef<PinInputFieldProps, \"input\">(\n ({ className, ...rest }, ref) => {\n const { getInputProps, styles } = usePinInputContext()\n const { index, register } = useDescendant()\n\n rest = useFormControlProps(rest)\n\n const css: CSSUIObject = { ...styles.field }\n\n return (\n <ui.input\n className={cx(\"ui-pin-input__field\", className)}\n {...getInputProps({ ...rest, ref: mergeRefs(register, ref), index })}\n __css={css}\n />\n )\n },\n)\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAOA,kBAKO;AAEP,0BAGO;AACP,oCAAqC;AACrC,4BAAiC;AACjC,mBAQO;AAEP,mBAAiE;AAyVtC;AAvV3B,IAAM,UAAU,CAAC,UAAmB,+BAAO,MAAM;AAEjD,IAAM,WAAW,CAAC,OAAe,SAAgC;AAC/D,QAAM,gBAAgB;AACtB,QAAM,sBAAsB;AAE5B,QAAM,QAAQ,SAAS,iBAAiB,sBAAsB;AAE9D,SAAO,MAAM,KAAK,KAAK;AACzB;AAYA,IAAM,CAAC,kBAAkB,kBAAkB,QAAI,4BAA+B;AAAA,EAC5E,QAAQ;AAAA,EACR,MAAM;AACR,CAAC;AAED,IAAM,EAAE,4BAA4B,gBAAgB,cAAc,QAChE,wCAAmC;AAkF9B,IAAM,eAAW;AAAA,EACtB,CAAC,EAAE,kBAAkB,kBAAkB,GAAG,MAAM,GAAG,QAAQ;AACzD,UAAM,CAAC,QAAQ,W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EAAE,WAAW,GAAG,KAAK,GAAG,QAAQ;AAC/B,UAAM,EAAE,eAAe,OAAO,IAAI,mBAAmB;AACrD,UAAM,EAAE,OAAO,SAAS,IAAI,cAAc;AAE1C,eAAO,yCAAoB,IAAI;AAE/B,UAAM,MAAmB,EAAE,GAAG,OAAO,MAAM;AAE3C,WACE;AAAA,MAAC,eAAG;AAAA,MAAH;AAAA,QACC,eAAW,iBAAG,uBAAuB,SAAS;AAAA,QAC7C,GAAG,cAAc,EAAE,GAAG,MAAM,SAAK,wBAAU,UAAU,GAAG,GAAG,MAAM,CAAC;AAAA,QACnE,OAAO;AAAA;AAAA,IACT;AAAA,EAEJ;AACF;","names":["values","value","nextValue","index","props"]}
|
package/dist/pin-input.mjs
CHANGED
package/package.json
CHANGED
@@ -1,6 +1,6 @@
|
|
1
1
|
{
|
2
2
|
"name": "@yamada-ui/pin-input",
|
3
|
-
"version": "1.0.28-dev-
|
3
|
+
"version": "1.0.28-dev-20240504032813",
|
4
4
|
"description": "Yamada UI pin input component",
|
5
5
|
"keywords": [
|
6
6
|
"yamada",
|
@@ -36,9 +36,9 @@
|
|
36
36
|
"url": "https://github.com/yamada-ui/yamada-ui/issues"
|
37
37
|
},
|
38
38
|
"dependencies": {
|
39
|
-
"@yamada-ui/core": "1.6.7-dev-
|
39
|
+
"@yamada-ui/core": "1.6.7-dev-20240504032813",
|
40
40
|
"@yamada-ui/utils": "1.2.0",
|
41
|
-
"@yamada-ui/form-control": "1.0.28-dev-
|
41
|
+
"@yamada-ui/form-control": "1.0.28-dev-20240504032813",
|
42
42
|
"@yamada-ui/use-descendant": "1.0.13",
|
43
43
|
"@yamada-ui/use-controllable-state": "1.0.13"
|
44
44
|
},
|
@@ -1 +0,0 @@
|
|
1
|
-
{"version":3,"sources":["../src/pin-input.tsx"],"sourcesContent":["import type {\n CSSUIObject,\n HTMLUIProps,\n ThemeProps,\n ColorModeToken,\n CSS,\n} from \"@yamada-ui/core\"\nimport {\n ui,\n forwardRef,\n useMultiComponentStyle,\n omitThemeProps,\n} from \"@yamada-ui/core\"\nimport type { FormControlOptions } from \"@yamada-ui/form-control\"\nimport {\n getFormControlProperties,\n useFormControlProps,\n} from \"@yamada-ui/form-control\"\nimport { useControllableState } from \"@yamada-ui/use-controllable-state\"\nimport { createDescendant } from \"@yamada-ui/use-descendant\"\nimport {\n createContext,\n cx,\n handlerAll,\n mergeRefs,\n pickObject,\n filterUndefined,\n getValidChildren,\n} from \"@yamada-ui/utils\"\nimport type { ChangeEvent, KeyboardEvent, Ref } from \"react\"\nimport { useCallback, useEffect, useId, useState } from \"react\"\n\nconst toArray = (value?: string) => value?.split(\"\")\n\nconst validate = (value: string, type: PinInputProps[\"type\"]) => {\n const NUMERIC_REGEX = /^[0-9]+$/\n const ALPHA_NUMERIC_REGEX = /^[a-zA-Z0-9]+$/i\n\n const regex = type === \"alphanumeric\" ? ALPHA_NUMERIC_REGEX : NUMERIC_REGEX\n\n return regex.test(value)\n}\n\ntype PinInputContext = {\n getInputProps: (\n props: PinInputFieldProps & {\n index: number\n ref?: Ref<HTMLInputElement>\n },\n ) => PinInputFieldProps\n styles: Record<string, CSSUIObject>\n}\n\nconst [PinInputProvider, usePinInputContext] = createContext<PinInputContext>({\n strict: false,\n name: \"PinInputContext\",\n})\n\nconst { DescendantsContextProvider, useDescendants, useDescendant } =\n createDescendant<HTMLInputElement>()\n\ntype PinInputOptions = {\n /**\n * The top-level id string that will be applied to the input fields.\n * The index of the input will be appended to this top-level id.\n */\n id?: string\n /**\n * The type of values the pin-input should allow.\n *\n * @default 'number'\n */\n type?: \"alphanumeric\" | \"number\"\n /**\n * The placeholder for the pin input.\n *\n * @default '○'\n */\n placeholder?: string\n /**\n * The value of the pin input.\n */\n value?: string\n /**\n * The initial value of the pin input.\n */\n defaultValue?: string\n /**\n * If `true`, the pin input receives focus on mount.\n *\n * @default false\n */\n autoFocus?: boolean\n /**\n * If `true`, focus will move automatically to the next input once filled.\n *\n * @default true\n */\n manageFocus?: boolean\n /**\n * If `true`, the pin input component signals to its fields that they should.\n */\n otp?: boolean\n /**\n * If `true`, the input's value will be masked just like `type=password`.\n */\n mask?: boolean\n /**\n * Function called on input change.\n */\n onChange?: (value: string) => void\n /**\n * Function called when all inputs have valid values.\n */\n onComplete?: (value: string) => void\n /**\n * The number of inputs to display.\n *\n * @default 4\n */\n items?: number\n /**\n * The border color when the input is focused.\n */\n focusBorderColor?: ColorModeToken<CSS.Property.BorderColor, \"colors\">\n /**\n * The border color when the input is invalid.\n */\n errorBorderColor?: ColorModeToken<CSS.Property.BorderColor, \"colors\">\n}\n\nexport type PinInputProps = Omit<HTMLUIProps<\"div\">, \"onChange\" | \"mask\"> &\n ThemeProps<\"PinInput\"> &\n FormControlOptions &\n PinInputOptions\n\n/**\n * `PinInput` is a component used to capture pin codes or OTP (One-Time Password) inputs.\n *\n * @see Docs https://yamada-ui.com/components/forms/pin-input\n */\nexport const PinInput = forwardRef<PinInputProps, \"div\">(\n ({ focusBorderColor, errorBorderColor, ...props }, ref) => {\n const [styles, mergedProps] = useMultiComponentStyle(\"PinInput\", {\n focusBorderColor,\n errorBorderColor,\n ...props,\n })\n let {\n id,\n className,\n type = \"number\",\n placeholder = \"○\",\n value,\n defaultValue,\n autoFocus,\n manageFocus = true,\n otp = false,\n mask,\n onChange,\n onComplete,\n items = 4,\n children,\n ...rest\n } = useFormControlProps(omitThemeProps(mergedProps))\n\n id ??= useId()\n\n const descendants = useDescendants()\n\n const [moveFocus, setMoveFocus] = useState<boolean>(true)\n const [focusedIndex, setFocusedIndex] = useState<number>(-1)\n\n useEffect(() => {\n if (!autoFocus) return\n\n const firstValue = descendants.firstValue()\n\n if (!firstValue) return\n\n requestAnimationFrame(() => firstValue.node.focus())\n }, [autoFocus, descendants])\n\n const [values, setValues] = useControllableState<string[]>({\n value: toArray(value),\n defaultValue: toArray(defaultValue) || [],\n onChange: (values) => onChange?.(values.join(\"\")),\n })\n\n const focusNext = useCallback(\n (index: number) => {\n if (!moveFocus || !manageFocus) return\n\n const next = descendants.nextValue(index, undefined, false)\n\n if (!next) return\n\n requestAnimationFrame(() => next.node.focus())\n },\n [descendants, moveFocus, manageFocus],\n )\n\n const setValue = useCallback(\n (value: string, index: number, isFocus: boolean = true) => {\n let nextValues = [...values]\n\n nextValues[index] = value\n\n setValues(nextValues)\n\n nextValues = nextValues.filter(Boolean)\n\n const isComplete =\n value !== \"\" &&\n nextValues.length === descendants.count() &&\n nextValues.every((value) => value != null && value !== \"\")\n\n if (isComplete) {\n onComplete?.(nextValues.join(\"\"))\n descendants.value(index)?.node.blur()\n } else if (isFocus) {\n focusNext(index)\n }\n },\n [values, setValues, descendants, onComplete, focusNext],\n )\n\n const getNextValue = useCallback(\n (value: string | undefined, eventValue: string) => {\n let nextValue = eventValue\n\n if (!value?.length) return nextValue\n\n if (value[0] === eventValue.charAt(0)) {\n nextValue = eventValue.charAt(1)\n } else if (value[0] === eventValue.charAt(1)) {\n nextValue = eventValue.charAt(0)\n }\n\n return nextValue\n },\n [],\n )\n\n const getInputProps = useCallback(\n ({\n index,\n ...props\n }: PinInputFieldProps & {\n index: number\n ref?: Ref<HTMLInputElement>\n }): PinInputFieldProps => {\n const onChange = ({ target }: ChangeEvent<HTMLInputElement>) => {\n const eventValue = target.value\n const currentValue = values[index]\n const nextValue = getNextValue(currentValue, eventValue)\n\n if (nextValue === \"\") {\n setValue(\"\", index)\n\n return\n }\n\n if (eventValue.length > 2) {\n if (!validate(eventValue, type)) return\n\n const nextValue = eventValue\n .split(\"\")\n .filter((_, index) => index < descendants.count())\n\n setValues(nextValue)\n\n if (nextValue.length === descendants.count()) {\n onComplete?.(nextValue.join(\"\"))\n descendants.value(index)?.node.blur()\n }\n } else {\n if (validate(nextValue, type)) setValue(nextValue, index)\n\n setMoveFocus(true)\n }\n }\n\n const onKeyDown = ({\n key,\n target,\n }: KeyboardEvent<HTMLInputElement>) => {\n if (key !== \"Backspace\" || !manageFocus) return\n\n if ((target as HTMLInputElement).value === \"\") {\n const prevInput = descendants.prevValue(index, undefined, false)\n\n if (!prevInput) return\n\n setValue(\"\", index - 1, false)\n prevInput.node?.focus()\n setMoveFocus(true)\n } else {\n setMoveFocus(false)\n }\n }\n\n const onFocus = () => setFocusedIndex(index)\n\n const onBlur = () => setFocusedIndex(-1)\n\n return {\n inputMode: type === \"number\" ? \"numeric\" : \"text\",\n type: mask ? \"password\" : type === \"number\" ? \"tel\" : \"text\",\n ...pickObject(\n rest,\n getFormControlProperties({ omit: [\"aria-readonly\"] }),\n ),\n ...filterUndefined(props),\n id: `${id}-${index}`,\n value: values[index] || \"\",\n onChange: handlerAll(props.onChange, onChange),\n onKeyDown: handlerAll(props.onKeyDown, onKeyDown),\n onFocus: handlerAll(props.onFocus, onFocus),\n onBlur: handlerAll(props.onBlur, onBlur),\n autoComplete: otp ? \"one-time-code\" : \"off\",\n placeholder:\n focusedIndex === index && !rest.readOnly && !props.readOnly\n ? \"\"\n : placeholder,\n }\n },\n [\n descendants,\n focusedIndex,\n getNextValue,\n id,\n manageFocus,\n mask,\n onComplete,\n otp,\n placeholder,\n rest,\n setValue,\n setValues,\n type,\n values,\n ],\n )\n\n const css: CSSUIObject = {\n display: \"flex\",\n alignItems: \"center\",\n ...styles.container,\n }\n\n let cloneChildren = getValidChildren(children)\n\n if (!cloneChildren.length)\n for (let i = 0; i < items; i++) {\n cloneChildren.push(<PinInputField key={i} />)\n }\n\n const { \"aria-readonly\": _, ...restWithoutAriaReadonly } = rest\n\n return (\n <DescendantsContextProvider value={descendants}>\n <PinInputProvider value={{ getInputProps, styles }}>\n <ui.div\n ref={ref}\n className={cx(\"ui-pin-input\", className)}\n __css={css}\n {...restWithoutAriaReadonly}\n >\n {cloneChildren}\n </ui.div>\n </PinInputProvider>\n </DescendantsContextProvider>\n )\n },\n)\n\nexport type PinInputFieldProps = HTMLUIProps<\"input\"> & FormControlOptions\n\nexport const PinInputField = forwardRef<PinInputFieldProps, \"input\">(\n ({ className, ...rest }, ref) => {\n const { getInputProps, styles } = usePinInputContext()\n const { index, register } = useDescendant()\n\n rest = useFormControlProps(rest)\n\n const css: CSSUIObject = { ...styles.field }\n\n return (\n <ui.input\n className={cx(\"ui-pin-input__field\", className)}\n {...getInputProps({ ...rest, ref: mergeRefs(register, ref), index })}\n __css={css}\n />\n )\n },\n)\n"],"mappings":";;;AAOA;AAAA,EACE;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,OACK;AAEP;AAAA,EACE;AAAA,EACA;AAAA,OACK;AACP,SAAS,4BAA4B;AACrC,SAAS,wBAAwB;AACjC;AAAA,EACE;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,OACK;AAEP,SAAS,aAAa,WAAW,OAAO,gBAAgB;AAqU7B;AAnU3B,IAAM,UAAU,CAAC,UAAmB,+BAAO,MAAM;AAEjD,IAAM,WAAW,CAAC,OAAe,SAAgC;AAC/D,QAAM,gBAAgB;AACtB,QAAM,sBAAsB;AAE5B,QAAM,QAAQ,SAAS,iBAAiB,sBAAsB;AAE9D,SAAO,MAAM,KAAK,KAAK;AACzB;AAYA,IAAM,CAAC,kBAAkB,kBAAkB,IAAI,cAA+B;AAAA,EAC5E,QAAQ;AAAA,EACR,MAAM;AACR,CAAC;AAED,IAAM,EAAE,4BAA4B,gBAAgB,cAAc,IAChE,iBAAmC;AAkF9B,IAAM,WAAW;AAAA,EACtB,CAAC,EAAE,kBAAkB,kBAAkB,GAAG,MAAM,GAAG,QAAQ;AACzD,UAAM,CAAC,QAAQ,WAAW,IAAI,uBAAuB,YAAY;AAAA,MAC/D;AAAA,MACA;AAAA,MACA,GAAG;AAAA,IACL,CAAC;AACD,QAAI;AAAA,MACF;AAAA,MACA;AAAA,MACA,OAAO;AAAA,MACP,cAAc;AAAA,MACd;AAAA,MACA;AAAA,MACA;AAAA,MACA,cAAc;AAAA,MACd,MAAM;AAAA,MACN;AAAA,MACA;AAAA,MACA;AAAA,MACA,QAAQ;AAAA,MACR;AAAA,MACA,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