react-smart-copy 1.21.0 → 1.23.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (44) hide show
  1. package/README.md +67 -0
  2. package/dist/chunks/digits-BUEvSVrs.cjs +51 -0
  3. package/dist/chunks/digits-BUEvSVrs.cjs.map +1 -0
  4. package/dist/chunks/digits-DkU-O9_A.js +46 -0
  5. package/dist/chunks/digits-DkU-O9_A.js.map +1 -0
  6. package/dist/chunks/field-BHo5y-t_.js +535 -0
  7. package/dist/chunks/field-BHo5y-t_.js.map +1 -0
  8. package/dist/chunks/field-BTsKTs2M.d.cts +87 -0
  9. package/dist/chunks/field-BTsKTs2M.d.ts +87 -0
  10. package/dist/chunks/field-Bytz2WfN.cjs +558 -0
  11. package/dist/chunks/field-Bytz2WfN.cjs.map +1 -0
  12. package/dist/chunks/{text-field-Ba8W3v4R.cjs → text-field-BEzUVOag.cjs} +26 -5
  13. package/dist/chunks/text-field-BEzUVOag.cjs.map +1 -0
  14. package/dist/chunks/{text-field-DUvrMMaK.js → text-field-D7PF2WlX.js} +21 -6
  15. package/dist/chunks/text-field-D7PF2WlX.js.map +1 -0
  16. package/dist/code.cjs +1 -1
  17. package/dist/code.js +1 -1
  18. package/dist/color.cjs +1 -1
  19. package/dist/color.js +1 -1
  20. package/dist/index.cjs +89 -0
  21. package/dist/index.cjs.map +1 -1
  22. package/dist/index.d.cts +42 -1
  23. package/dist/index.d.ts +42 -1
  24. package/dist/index.js +89 -1
  25. package/dist/index.js.map +1 -1
  26. package/dist/link.cjs +1 -1
  27. package/dist/link.js +1 -1
  28. package/dist/markdown.cjs +1 -1
  29. package/dist/markdown.js +1 -1
  30. package/dist/number.cjs +442 -0
  31. package/dist/number.cjs.map +1 -0
  32. package/dist/number.d.cts +172 -0
  33. package/dist/number.d.ts +172 -0
  34. package/dist/number.js +436 -0
  35. package/dist/number.js.map +1 -0
  36. package/dist/otp.cjs +7 -0
  37. package/dist/otp.d.cts +54 -0
  38. package/dist/otp.d.ts +54 -0
  39. package/dist/otp.js +2 -0
  40. package/dist/secrets.cjs +1 -1
  41. package/dist/secrets.js +1 -1
  42. package/package.json +71 -3
  43. package/dist/chunks/text-field-Ba8W3v4R.cjs.map +0 -1
  44. package/dist/chunks/text-field-DUvrMMaK.js.map +0 -1
@@ -41,14 +41,29 @@ function insertText(field, text) {
41
41
  }
42
42
  if (start === null) setNativeValue(field, field.value + insert);
43
43
  else field.setRangeText(insert, from, to, "end");
44
+ field.dispatchEvent(pasteInputEvent(doc, insert));
45
+ return true;
46
+ }
47
+ /**
48
+ * Replaces a field's whole value, as a paste would, for fields with no selection to paste
49
+ * into (`<input type="number">`): an `input` event fires, so React and other frameworks see
50
+ * the change. Returns `false` for disabled or read-only fields.
51
+ */
52
+ function replaceFieldValue(field, value) {
53
+ if (field.disabled || field.readOnly) return false;
54
+ field.focus();
55
+ setNativeValue(field, value);
56
+ field.dispatchEvent(pasteInputEvent(field.ownerDocument, value));
57
+ return true;
58
+ }
59
+ /** The `input` event a paste fires: an `InputEvent` where the window has one. */
60
+ function pasteInputEvent(doc, data) {
44
61
  const view = doc.defaultView;
45
- const event = view !== null && typeof view.InputEvent === "function" ? new view.InputEvent("input", {
62
+ return view !== null && typeof view.InputEvent === "function" ? new view.InputEvent("input", {
46
63
  bubbles: true,
47
64
  inputType: "insertFromPaste",
48
- data: insert
65
+ data
49
66
  }) : new Event("input", { bubbles: true });
50
- field.dispatchEvent(event);
51
- return true;
52
67
  }
53
68
  /**
54
69
  * Sets a field's value through the prototype's setter. React tracks the value
@@ -73,5 +88,11 @@ Object.defineProperty(exports, "insertText", {
73
88
  return insertText;
74
89
  }
75
90
  });
91
+ Object.defineProperty(exports, "replaceFieldValue", {
92
+ enumerable: true,
93
+ get: function() {
94
+ return replaceFieldValue;
95
+ }
96
+ });
76
97
 
77
- //# sourceMappingURL=text-field-Ba8W3v4R.cjs.map
98
+ //# sourceMappingURL=text-field-BEzUVOag.cjs.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"text-field-BEzUVOag.cjs","names":[],"sources":["../../src/core/text-field.ts"],"sourcesContent":["/**\n * Text-field helpers shared by the paste handlers of the opt-in entry points.\n * Internal: not part of `react-smart-copy/core`'s public API.\n */\n\n/** Text fields `setRangeText` and `execCommand('insertText')` both support. */\nexport type TextField = HTMLTextAreaElement | HTMLInputElement;\n\nconst TEXT_INPUTS = new Set(['text', 'search', 'url', 'tel', 'password']);\n\nexport function asTextField(target: EventTarget | null): TextField | null {\n if (typeof target !== 'object' || target === null) return null;\n const element = target as Partial<Element> & { type?: unknown };\n if (element.nodeName === 'TEXTAREA') return target as HTMLTextAreaElement;\n if (element.nodeName === 'INPUT' && typeof element.type === 'string' && TEXT_INPUTS.has(element.type)) return target as HTMLInputElement;\n return null;\n}\n\n/**\n * Inserts text at the cursor of a `<textarea>` or text `<input>`, replacing the\n * selection, as typing or pasting would: the browser's undo (Ctrl/⌘+Z) still\n * works, `input` events fire (so React and other frameworks see the change),\n * and `maxlength` is respected. Returns `false` for disabled or read-only fields.\n */\nexport function insertText(field: TextField, text: string): boolean {\n if (field.disabled || field.readOnly) return false;\n // An <input> holds one line: each run of line breaks (a paragraph break too) becomes one space.\n const value = field.nodeName === 'INPUT' ? text.replace(/[ \\t]*(?:\\r\\n|\\r|\\n)+[ \\t]*/g, ' ') : text;\n const doc = field.ownerDocument;\n field.focus();\n // `null` for inputs without a selection API (email, number): the text is appended.\n const start = field.selectionStart;\n const end = field.selectionEnd;\n try {\n // Re-applying the selection ends the current typing run, so the paste gets its own undo step\n // (as a native paste does) instead of merging with what was typed just before.\n if (start !== null && end !== null) field.setSelectionRange(start, end);\n // The only way to keep the native undo stack; supported for text fields in every current browser.\n // eslint-disable-next-line @typescript-eslint/no-deprecated -- no replacement keeps undo history\n if (doc.activeElement === field && doc.execCommand('insertText', false, value)) return true;\n } catch {\n // Fall through to the manual insertion.\n }\n const from = start ?? field.value.length;\n const to = end ?? from;\n let insert = value;\n if (field.maxLength >= 0) {\n const room = Math.max(0, field.maxLength - (field.value.length - (to - from)));\n insert = insert.slice(0, room);\n // Never leave half of a surrogate pair (an emoji) behind.\n if (/[\\ud800-\\udbff]$/.test(insert)) insert = insert.slice(0, -1);\n }\n if (start === null) setNativeValue(field, field.value + insert);\n else field.setRangeText(insert, from, to, 'end');\n field.dispatchEvent(pasteInputEvent(doc, insert));\n return true;\n}\n\n/**\n * Replaces a field's whole value, as a paste would, for fields with no selection to paste\n * into (`<input type=\"number\">`): an `input` event fires, so React and other frameworks see\n * the change. Returns `false` for disabled or read-only fields.\n */\nexport function replaceFieldValue(field: HTMLInputElement, value: string): boolean {\n if (field.disabled || field.readOnly) return false;\n field.focus();\n setNativeValue(field, value);\n field.dispatchEvent(pasteInputEvent(field.ownerDocument, value));\n return true;\n}\n\n/** The `input` event a paste fires: an `InputEvent` where the window has one. */\nfunction pasteInputEvent(doc: Document, data: string): Event {\n const view = doc.defaultView;\n return view !== null && typeof view.InputEvent === 'function'\n ? new view.InputEvent('input', { bubbles: true, inputType: 'insertFromPaste', data })\n : new Event('input', { bubbles: true });\n}\n\n/**\n * Sets a field's value through the prototype's setter. React tracks the value\n * set on the element itself; going around it lets React see the change and\n * call `onChange` for the `input` event that follows.\n */\nfunction setNativeValue(field: TextField, next: string): void {\n const descriptor = Object.getOwnPropertyDescriptor(Object.getPrototypeOf(field) as object, 'value');\n // eslint-disable-next-line @typescript-eslint/unbound-method -- it is called right below with the field as `this`\n const setter = descriptor?.set;\n if (setter) setter.call(field, next);\n else field.value = next;\n}\n"],"mappings":";;AAQA,MAAM,8BAAc,IAAI,IAAI;CAAC;CAAQ;CAAU;CAAO;CAAO;AAAU,CAAC;AAExE,SAAgB,YAAY,QAA8C;CACxE,IAAI,OAAO,WAAW,YAAY,WAAW,MAAM,OAAO;CAC1D,MAAM,UAAU;CAChB,IAAI,QAAQ,aAAa,YAAY,OAAO;CAC5C,IAAI,QAAQ,aAAa,WAAW,OAAO,QAAQ,SAAS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K,eAAe,aAC/C,IAAI,KAAK,WAAW,SAAS;EAAE,SAAS;EAAM,WAAW;EAAmB;CAAK,CAAC,IAClF,IAAI,MAAM,SAAS,EAAE,SAAS,KAAK,CAAC;AAC1C;;;;;;AAOA,SAAS,eAAe,OAAkB,MAAoB;CAG5D,MAAM,SAFa,OAAO,yBAAyB,OAAO,eAAe,KAAK,GAAa,OAEnE,CAAC,EAAE;CAC3B,IAAI,QAAQ,OAAO,KAAK,OAAO,IAAI;MAC9B,MAAM,QAAQ;AACrB"}
@@ -41,14 +41,29 @@ function insertText(field, text) {
41
41
  }
42
42
  if (start === null) setNativeValue(field, field.value + insert);
43
43
  else field.setRangeText(insert, from, to, "end");
44
+ field.dispatchEvent(pasteInputEvent(doc, insert));
45
+ return true;
46
+ }
47
+ /**
48
+ * Replaces a field's whole value, as a paste would, for fields with no selection to paste
49
+ * into (`<input type="number">`): an `input` event fires, so React and other frameworks see
50
+ * the change. Returns `false` for disabled or read-only fields.
51
+ */
52
+ function replaceFieldValue(field, value) {
53
+ if (field.disabled || field.readOnly) return false;
54
+ field.focus();
55
+ setNativeValue(field, value);
56
+ field.dispatchEvent(pasteInputEvent(field.ownerDocument, value));
57
+ return true;
58
+ }
59
+ /** The `input` event a paste fires: an `InputEvent` where the window has one. */
60
+ function pasteInputEvent(doc, data) {
44
61
  const view = doc.defaultView;
45
- const event = view !== null && typeof view.InputEvent === "function" ? new view.InputEvent("input", {
62
+ return view !== null && typeof view.InputEvent === "function" ? new view.InputEvent("input", {
46
63
  bubbles: true,
47
64
  inputType: "insertFromPaste",
48
- data: insert
65
+ data
49
66
  }) : new Event("input", { bubbles: true });
50
- field.dispatchEvent(event);
51
- return true;
52
67
  }
53
68
  /**
54
69
  * Sets a field's value through the prototype's setter. React tracks the value
@@ -61,6 +76,6 @@ function setNativeValue(field, next) {
61
76
  else field.value = next;
62
77
  }
63
78
  //#endregion
64
- export { insertText as n, asTextField as t };
79
+ export { insertText as n, replaceFieldValue as r, asTextField as t };
65
80
 
66
- //# sourceMappingURL=text-field-DUvrMMaK.js.map
81
+ //# sourceMappingURL=text-field-D7PF2WlX.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"text-field-D7PF2WlX.js","names":[],"sources":["../../src/core/text-field.ts"],"sourcesContent":["/**\n * Text-field helpers shared by the paste handlers of the opt-in entry points.\n * Internal: not part of `react-smart-copy/core`'s public API.\n */\n\n/** Text fields `setRangeText` and `execCommand('insertText')` both support. */\nexport type TextField = HTMLTextAreaElement | HTMLInputElement;\n\nconst TEXT_INPUTS = new Set(['text', 'search', 'url', 'tel', 'password']);\n\nexport function asTextField(target: EventTarget | null): TextField | null {\n if (typeof target !== 'object' || target === null) return null;\n const element = target as Partial<Element> & { type?: unknown };\n if (element.nodeName === 'TEXTAREA') return target as HTMLTextAreaElement;\n if (element.nodeName === 'INPUT' && typeof element.type === 'string' && TEXT_INPUTS.has(element.type)) return target as HTMLInputElement;\n return null;\n}\n\n/**\n * Inserts text at the cursor of a `<textarea>` or text `<input>`, replacing the\n * selection, as typing or pasting would: the browser's undo (Ctrl/⌘+Z) still\n * works, `input` events fire (so React and other frameworks see the change),\n * and `maxlength` is respected. Returns `false` for disabled or read-only fields.\n */\nexport function insertText(field: TextField, text: string): boolean {\n if (field.disabled || field.readOnly) return false;\n // An <input> holds one line: each run of line breaks (a paragraph break too) becomes one space.\n const value = field.nodeName === 'INPUT' ? text.replace(/[ \\t]*(?:\\r\\n|\\r|\\n)+[ \\t]*/g, ' ') : text;\n const doc = field.ownerDocument;\n field.focus();\n // `null` for inputs without a selection API (email, number): the text is appended.\n const start = field.selectionStart;\n const end = field.selectionEnd;\n try {\n // Re-applying the selection ends the current typing run, so the paste gets its own undo step\n // (as a native paste does) instead of merging with what was typed just before.\n if (start !== null && end !== null) field.setSelectionRange(start, end);\n // The only way to keep the native undo stack; supported for text fields in every current browser.\n // eslint-disable-next-line @typescript-eslint/no-deprecated -- no replacement keeps undo history\n if (doc.activeElement === field && doc.execCommand('insertText', false, value)) return true;\n } catch {\n // Fall through to the manual insertion.\n }\n const from = start ?? field.value.length;\n const to = end ?? from;\n let insert = value;\n if (field.maxLength >= 0) {\n const room = Math.max(0, field.maxLength - (field.value.length - (to - from)));\n insert = insert.slice(0, room);\n // Never leave half of a surrogate pair (an emoji) behind.\n if (/[\\ud800-\\udbff]$/.test(insert)) insert = insert.slice(0, -1);\n }\n if (start === null) setNativeValue(field, field.value + insert);\n else field.setRangeText(insert, from, to, 'end');\n field.dispatchEvent(pasteInputEvent(doc, insert));\n return true;\n}\n\n/**\n * Replaces a field's whole value, as a paste would, for fields with no selection to paste\n * into (`<input type=\"number\">`): an `input` event fires, so React and other frameworks see\n * the change. Returns `false` for disabled or read-only fields.\n */\nexport function replaceFieldValue(field: HTMLInputElement, value: string): boolean {\n if (field.disabled || field.readOnly) return false;\n field.focus();\n setNativeValue(field, value);\n field.dispatchEvent(pasteInputEvent(field.ownerDocument, value));\n return true;\n}\n\n/** The `input` event a paste fires: an `InputEvent` where the window has one. */\nfunction pasteInputEvent(doc: Document, data: string): Event {\n const view = doc.defaultView;\n return view !== null && typeof view.InputEvent === 'function'\n ? new view.InputEvent('input', { bubbles: true, inputType: 'insertFromPaste', data })\n : new Event('input', { bubbles: true });\n}\n\n/**\n * Sets a field's value through the prototype's setter. React tracks the value\n * set on the element itself; going around it lets React see the change and\n * call `onChange` for the `input` event that follows.\n */\nfunction setNativeValue(field: TextField, next: string): void {\n const descriptor = Object.getOwnPropertyDescriptor(Object.getPrototypeOf(field) as object, 'value');\n // eslint-disable-next-line @typescript-eslint/unbound-method -- it is called right below with the field as `this`\n const setter = descriptor?.set;\n if (setter) setter.call(field, next);\n else field.value = next;\n}\n"],"mappings":";;AAQA,MAAM,8BAAc,IAAI,IAAI;CAAC;CAAQ;CAAU;CAAO;CAAO;AAAU,CAAC;AAExE,SAAgB,YAAY,QAA8C;CACxE,IAAI,OAAO,WAAW,YAAY,WAAW,MAAM,OAAO;CAC1D,MAAM,UAAU;CAChB,I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BAAgB,KAAe,MAAqB;CAC3D,MAAM,OAAO,IAAI;CACjB,OAAO,SAAS,QAAQ,OAAO,KAAK,eAAe,aAC/C,IAAI,KAAK,WAAW,SAAS;EAAE,SAAS;EAAM,WAAW;EAAmB;CAAK,CAAC,IAClF,IAAI,MAAM,SAAS,EAAE,SAAS,KAAK,CAAC;AAC1C;;;;;;AAOA,SAAS,eAAe,OAAkB,MAAoB;CAG5D,MAAM,SAFa,OAAO,yBAAyB,OAAO,eAAe,KAAK,GAAa,OAEnE,CAAC,EAAE;CAC3B,IAAI,QAAQ,OAAO,KAAK,OAAO,IAAI;MAC9B,MAAM,QAAQ;AACrB"}
package/dist/code.cjs CHANGED
@@ -2,7 +2,7 @@
2
2
  Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
3
3
  const require_errors = require("./chunks/errors-BwOF6T_M.cjs");
4
4
  const require_inert_html = require("./chunks/inert-html-DyxjWDok.cjs");
5
- const require_text_field = require("./chunks/text-field-Ba8W3v4R.cjs");
5
+ const require_text_field = require("./chunks/text-field-BEzUVOag.cjs");
6
6
  //#region src/code/inspect.ts
7
7
  /** Every finding kind, in a stable order. */
8
8
  const CODE_FINDING_KINDS = Object.freeze([
package/dist/code.js CHANGED
@@ -1,7 +1,7 @@
1
1
 
2
2
  import { n as copyFailure, o as isCopyFailure } from "./chunks/errors-9h8sdFFX.js";
3
3
  import { t as parseInertHtml } from "./chunks/inert-html-D75NyVvT.js";
4
- import { n as insertText, t as asTextField } from "./chunks/text-field-DUvrMMaK.js";
4
+ import { n as insertText, t as asTextField } from "./chunks/text-field-D7PF2WlX.js";
5
5
  //#region src/code/inspect.ts
6
6
  /** Every finding kind, in a stable order. */
7
7
  const CODE_FINDING_KINDS = Object.freeze([
package/dist/color.cjs CHANGED
@@ -1,7 +1,7 @@
1
1
 
2
2
  Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
3
3
  const require_errors = require("./chunks/errors-BwOF6T_M.cjs");
4
- const require_text_field = require("./chunks/text-field-Ba8W3v4R.cjs");
4
+ const require_text_field = require("./chunks/text-field-BEzUVOag.cjs");
5
5
  const require_contrast = require("./chunks/contrast-C6CVklEI.cjs");
6
6
  //#region src/color/payload.ts
7
7
  /** The sRGB hex of a colour for the swatch's inline style: always generated here, never user text. */
package/dist/color.js CHANGED
@@ -1,6 +1,6 @@
1
1
 
2
2
  import { n as copyFailure } from "./chunks/errors-9h8sdFFX.js";
3
- import { n as insertText, t as asTextField } from "./chunks/text-field-DUvrMMaK.js";
3
+ import { n as insertText, t as asTextField } from "./chunks/text-field-D7PF2WlX.js";
4
4
  import { a as toColor, c as inGamut, i as formatColor, l as toGamut, n as contrastRatio, o as parseColor, r as COLOR_FORMATS, t as contrastLevel } from "./chunks/contrast-DpK2JIVu.js";
5
5
  //#region src/color/payload.ts
6
6
  /** The sRGB hex of a colour for the swatch's inline style: always generated here, never user text. */
package/dist/index.cjs CHANGED
@@ -7,6 +7,7 @@ const require_clipboard_adapter = require("./chunks/clipboard-adapter-Cfz-rzw3.c
7
7
  const require_core = require("./chunks/core-DYf_Lhbe.cjs");
8
8
  const require_paste_reader = require("./chunks/paste-reader-Bo01IMC_.cjs");
9
9
  const require_share = require("./chunks/share-ayDz974N.cjs");
10
+ const require_field = require("./chunks/field-Bytz2WfN.cjs");
10
11
  let react = require("react");
11
12
  let react_jsx_runtime = require("react/jsx-runtime");
12
13
  //#region src/react/CopyGroup.tsx
@@ -1113,6 +1114,93 @@ function useShare(options = {}) {
1113
1114
  ]);
1114
1115
  }
1115
1116
  //#endregion
1117
+ //#region src/react/useOtpField.ts
1118
+ const defaultLabel = (numeric) => (index, length) => `${numeric ? "Digit" : "Character"} ${String(index + 1)} of ${String(length)}`;
1119
+ /**
1120
+ * A one-time code field of single-character boxes, headless: typing moves to the next box
1121
+ * and Backspace to the one before; pasting a code (or an SMS holding one) into any box
1122
+ * fills them all; an SMS autofill (iOS, Android) and, with `webOtp`, the WebOTP API do too.
1123
+ * Every box has an accessible name; name the group around them.
1124
+ *
1125
+ * `length`, `alphabet` and `webOtp` are read when the field is made: a change makes a new,
1126
+ * empty field. Callbacks and `label` may change on every render.
1127
+ *
1128
+ * @example
1129
+ * const otp = useOtpField({ length: 6, onComplete: (code) => void verify(code) });
1130
+ * <div role="group" aria-label="Verification code">
1131
+ * {Array.from({ length: 6 }, (_, index) => <input key={index} {...otp.getInputProps(index)} />)}
1132
+ * </div>
1133
+ */
1134
+ function useOtpField(options = {}) {
1135
+ const latest = (0, react.useRef)(options);
1136
+ useIsomorphicLayoutEffect(() => {
1137
+ latest.current = options;
1138
+ });
1139
+ const length = options.length ?? 6;
1140
+ const alphabet = options.alphabet ?? "numeric";
1141
+ const webOtp = options.webOtp === true;
1142
+ const field = (0, react.useMemo)(() => require_field.createOtpField({
1143
+ length,
1144
+ alphabet,
1145
+ webOtp,
1146
+ label: (index, total) => (latest.current.label ?? defaultLabel(alphabet === "numeric"))(index, total),
1147
+ onChange: (snapshot) => latest.current.onChange?.(snapshot),
1148
+ onComplete: (code) => latest.current.onComplete?.(code)
1149
+ }), [
1150
+ length,
1151
+ alphabet,
1152
+ webOtp
1153
+ ]);
1154
+ const snapshot = (0, react.useSyncExternalStore)(field.subscribe, field.getSnapshot, field.getSnapshot);
1155
+ const elements = (0, react.useRef)([]);
1156
+ const refs = (0, react.useMemo)(() => Array.from({ length }, (_, index) => (element) => {
1157
+ elements.current[index] = element;
1158
+ }), [length]);
1159
+ const attached = (0, react.useRef)(null);
1160
+ useIsomorphicLayoutEffect(() => {
1161
+ const inputs = elements.current.slice(0, length).filter((element) => element !== null);
1162
+ const current = attached.current;
1163
+ if (current !== null && current.field === field && current.inputs.length === inputs.length && current.inputs.every((input, index) => input === inputs[index])) return;
1164
+ current?.detach();
1165
+ attached.current = inputs.length === length ? {
1166
+ field,
1167
+ inputs,
1168
+ detach: field.attach(inputs)
1169
+ } : null;
1170
+ });
1171
+ (0, react.useEffect)(() => () => {
1172
+ attached.current?.detach();
1173
+ attached.current = null;
1174
+ }, []);
1175
+ const labelOf = options.label;
1176
+ const getInputProps = (0, react.useCallback)((index) => {
1177
+ const numeric = alphabet === "numeric";
1178
+ const label = (labelOf ?? defaultLabel(numeric))(index, length);
1179
+ return {
1180
+ ref: refs[index] ?? (() => void 0),
1181
+ type: "text",
1182
+ autoComplete: index === 0 ? "one-time-code" : "off",
1183
+ inputMode: numeric ? "numeric" : "text",
1184
+ "aria-label": label,
1185
+ autoCapitalize: numeric ? "off" : "characters",
1186
+ spellCheck: false,
1187
+ ...numeric ? { pattern: "[0-9]*" } : {}
1188
+ };
1189
+ }, [
1190
+ alphabet,
1191
+ length,
1192
+ refs,
1193
+ labelOf
1194
+ ]);
1195
+ return {
1196
+ ...snapshot,
1197
+ getInputProps,
1198
+ setValue: field.setValue,
1199
+ clear: field.clear,
1200
+ focus: field.focus
1201
+ };
1202
+ }
1203
+ //#endregion
1116
1204
  exports.CopyFailure = require_errors.CopyFailure;
1117
1205
  exports.CopyField = CopyField;
1118
1206
  exports.CopyGroup = CopyGroup;
@@ -1173,6 +1261,7 @@ exports.useCopyGroup = useCopyGroup;
1173
1261
  exports.useCopyInterceptor = useCopyInterceptor;
1174
1262
  exports.useDisplayStatus = useDisplayStatus;
1175
1263
  exports.useDragCopy = useDragCopy;
1264
+ exports.useOtpField = useOtpField;
1176
1265
  exports.usePaste = usePaste;
1177
1266
  exports.usePasteDisplayStatus = usePasteDisplayStatus;
1178
1267
  exports.usePasteField = usePasteField;