@radix-ui/react-one-time-password-field 0.1.0-rc.1744661316162 → 0.1.0-rc.1744831331200

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.d.mts CHANGED
@@ -1,45 +1,130 @@
1
- import { Primitive } from '@radix-ui/react-primitive';
1
+ import * as Primitive from '@radix-ui/react-primitive';
2
2
  import * as RovingFocusGroup from '@radix-ui/react-roving-focus';
3
3
  import * as React from 'react';
4
4
 
5
- type InputType = 'password' | 'text';
6
- type AutoComplete = 'off' | 'one-time-code';
7
5
  type InputValidationType = 'alpha' | 'numeric' | 'alphanumeric' | 'none';
8
- type RovingFocusGroupProps = React.ComponentPropsWithoutRef<typeof RovingFocusGroup.Root>;
6
+ type RovingFocusGroupProps = RovingFocusGroup.RovingFocusGroupProps;
9
7
  interface OneTimePasswordFieldOwnProps {
10
- onValueChange?: (value: string) => void;
11
- id?: string;
12
- value?: string;
13
- defaultValue?: string;
14
- autoSubmit?: boolean;
15
- onAutoSubmit?: (value: string) => void;
16
- validationType?: InputValidationType;
17
- disabled?: boolean;
18
- readOnly?: boolean;
8
+ /**
9
+ * Specifies what—if any—permission the user agent has to provide automated
10
+ * assistance in filling out form field values, as well as guidance to the
11
+ * browser as to the type of information expected in the field. Allows
12
+ * `"one-time-code"` or `"off"`.
13
+ *
14
+ * @defaultValue `"one-time-code"`
15
+ * @see https://developer.mozilla.org/en-US/docs/Web/HTML/Reference/Attributes/autocomplete
16
+ */
19
17
  autoComplete?: AutoComplete;
18
+ /**
19
+ * Whether or not the first fillable input should be focused on page-load.
20
+ *
21
+ * @defaultValue `false`
22
+ * @see https://developer.mozilla.org/en-US/docs/Web/HTML/Reference/Global_attributes/autofocus
23
+ */
20
24
  autoFocus?: boolean;
25
+ /**
26
+ * Whether or not the component should attempt to automatically submit when
27
+ * all fields are filled. If the field is associated with an HTML `form`
28
+ * element, the form's `requestSubmit` method will be called.
29
+ *
30
+ * @defaultValue `false`
31
+ */
32
+ autoSubmit?: boolean;
33
+ /**
34
+ * The initial value of the uncontrolled field.
35
+ */
36
+ defaultValue?: string;
37
+ /**
38
+ * Indicates the horizontal directionality of the parent element's text.
39
+ *
40
+ * @see https://developer.mozilla.org/en-US/docs/Web/HTML/Reference/Global_attributes/dir
41
+ */
42
+ dir?: RovingFocusGroupProps['dir'];
43
+ /**
44
+ * Whether or not the the field's input elements are disabled.
45
+ */
46
+ disabled?: boolean;
47
+ /**
48
+ * A string specifying the `form` element with which the input is associated.
49
+ * This string's value, if present, must match the id of a `form` element in
50
+ * the same document.
51
+ *
52
+ * @see https://developer.mozilla.org/en-US/docs/Web/HTML/Reference/Elements/input#form
53
+ */
21
54
  form?: string | undefined;
55
+ /**
56
+ * A string specifying a name for the input control. This name is submitted
57
+ * along with the control's value when the form data is submitted.
58
+ *
59
+ * @see https://developer.mozilla.org/en-US/docs/Web/HTML/Reference/Elements/input#name
60
+ */
22
61
  name?: string | undefined;
62
+ /**
63
+ * When the `autoSubmit` prop is set to `true`, this callback will be fired
64
+ * before attempting to submit the associated form. It will be called whether
65
+ * or not a form is located, or if submission is not allowed.
66
+ */
67
+ onAutoSubmit?: (value: string) => void;
68
+ /**
69
+ * A callback fired when the field's value changes. When the component is
70
+ * controlled, this should update the state passed to the `value` prop.
71
+ */
72
+ onValueChange?: (value: string) => void;
73
+ /**
74
+ * Indicates the vertical directionality of the input elements.
75
+ *
76
+ * @defaultValue `"horizontal"`
77
+ */
78
+ orientation?: RovingFocusGroupProps['orientation'];
79
+ /**
80
+ * Defines the text displayed in a form control when the control has no value.
81
+ *
82
+ * @see https://developer.mozilla.org/en-US/docs/Web/HTML/Reference/Attributes/placeholder
83
+ */
23
84
  placeholder?: string | undefined;
24
- required?: boolean;
85
+ /**
86
+ * Whether or not the input elements can be updated by the user.
87
+ *
88
+ * @see https://developer.mozilla.org/en-US/docs/Web/HTML/Reference/Attributes/readonly
89
+ */
90
+ readOnly?: boolean;
91
+ /**
92
+ * Function for custom sanitization when `validationType` is set to `"none"`.
93
+ * This function will be called before updating values in response to user
94
+ * interactions.
95
+ */
96
+ sanitizeValue?: (value: string) => string;
97
+ /**
98
+ * The input type of the field's input elements. Can be `"password"` or `"text"`.
99
+ */
25
100
  type?: InputType;
26
- dir?: RovingFocusGroupProps['dir'];
27
- orientation?: RovingFocusGroupProps['orientation'];
101
+ /**
102
+ * Specifies the type of input validation to be used. Can be `"alpha"`,
103
+ * `"numeric"`, `"alphanumeric"` or `"none"`.
104
+ *
105
+ * @defaultValue `"numeric"`
106
+ */
107
+ validationType?: InputValidationType;
108
+ /**
109
+ * The controlled value of the field.
110
+ */
111
+ value?: string;
28
112
  }
29
- interface OneTimePasswordFieldProps extends OneTimePasswordFieldOwnProps, Omit<React.ComponentPropsWithoutRef<typeof Primitive.div>, keyof OneTimePasswordFieldOwnProps> {
113
+ interface OneTimePasswordFieldProps extends OneTimePasswordFieldOwnProps, Omit<Primitive.PrimitivePropsWithRef<'div'>, keyof OneTimePasswordFieldOwnProps> {
30
114
  }
31
- declare const OneTimePasswordField: React.ForwardRefExoticComponent<OneTimePasswordFieldProps & React.RefAttributes<HTMLDivElement>>;
115
+ declare const OneTimePasswordField: React.ForwardRefExoticComponent<Omit<OneTimePasswordFieldProps, "ref"> & React.RefAttributes<HTMLDivElement>>;
32
116
  interface OneTimePasswordFieldHiddenInputProps extends Omit<React.ComponentProps<'input'>, keyof 'value' | 'defaultValue' | 'type' | 'onChange' | 'readOnly' | 'disabled' | 'autoComplete' | 'autoFocus'> {
33
117
  }
34
118
  declare const OneTimePasswordFieldHiddenInput: React.ForwardRefExoticComponent<Omit<OneTimePasswordFieldHiddenInputProps, "ref"> & React.RefAttributes<HTMLInputElement>>;
35
- interface OneTimePasswordFieldInputOwnProps {
36
- autoComplete?: 'one-time-code' | 'off';
37
- }
38
- interface OneTimePasswordFieldInputProps extends OneTimePasswordFieldInputOwnProps, Omit<React.ComponentProps<typeof Primitive.input>, keyof OneTimePasswordFieldInputOwnProps> {
119
+ interface OneTimePasswordFieldInputProps extends Omit<Primitive.PrimitivePropsWithRef<'input'>, 'value' | 'defaultValue' | 'disabled' | 'readOnly' | 'autoComplete' | 'autoFocus' | 'form' | 'name' | 'placeholder' | 'type'> {
120
+ /**
121
+ * Callback fired when the user input fails native HTML input validation.
122
+ */
123
+ onInvalidChange?: (character: string) => void;
39
124
  }
40
125
  declare const OneTimePasswordFieldInput: React.ForwardRefExoticComponent<Omit<OneTimePasswordFieldInputProps, "ref"> & React.RefAttributes<HTMLInputElement>>;
41
- declare const Root: React.ForwardRefExoticComponent<OneTimePasswordFieldProps & React.RefAttributes<HTMLDivElement>>;
42
- declare const Input: React.ForwardRefExoticComponent<Omit<OneTimePasswordFieldInputProps, "ref"> & React.RefAttributes<HTMLInputElement>>;
43
- declare const HiddenInput: React.ForwardRefExoticComponent<Omit<OneTimePasswordFieldHiddenInputProps, "ref"> & React.RefAttributes<HTMLInputElement>>;
44
126
 
45
- export { HiddenInput, Input, type InputValidationType, OneTimePasswordField, OneTimePasswordFieldHiddenInput, type OneTimePasswordFieldHiddenInputProps, OneTimePasswordFieldInput, type OneTimePasswordFieldInputProps, type OneTimePasswordFieldProps, Root };
127
+ type InputType = 'password' | 'text';
128
+ type AutoComplete = 'off' | 'one-time-code';
129
+
130
+ export { OneTimePasswordFieldHiddenInput as HiddenInput, OneTimePasswordFieldInput as Input, type InputValidationType, OneTimePasswordField, OneTimePasswordFieldHiddenInput, type OneTimePasswordFieldHiddenInputProps, OneTimePasswordFieldInput, type OneTimePasswordFieldInputProps, type OneTimePasswordFieldProps, OneTimePasswordField as Root };
package/dist/index.d.ts CHANGED
@@ -1,45 +1,130 @@
1
- import { Primitive } from '@radix-ui/react-primitive';
1
+ import * as Primitive from '@radix-ui/react-primitive';
2
2
  import * as RovingFocusGroup from '@radix-ui/react-roving-focus';
3
3
  import * as React from 'react';
4
4
 
5
- type InputType = 'password' | 'text';
6
- type AutoComplete = 'off' | 'one-time-code';
7
5
  type InputValidationType = 'alpha' | 'numeric' | 'alphanumeric' | 'none';
8
- type RovingFocusGroupProps = React.ComponentPropsWithoutRef<typeof RovingFocusGroup.Root>;
6
+ type RovingFocusGroupProps = RovingFocusGroup.RovingFocusGroupProps;
9
7
  interface OneTimePasswordFieldOwnProps {
10
- onValueChange?: (value: string) => void;
11
- id?: string;
12
- value?: string;
13
- defaultValue?: string;
14
- autoSubmit?: boolean;
15
- onAutoSubmit?: (value: string) => void;
16
- validationType?: InputValidationType;
17
- disabled?: boolean;
18
- readOnly?: boolean;
8
+ /**
9
+ * Specifies what—if any—permission the user agent has to provide automated
10
+ * assistance in filling out form field values, as well as guidance to the
11
+ * browser as to the type of information expected in the field. Allows
12
+ * `"one-time-code"` or `"off"`.
13
+ *
14
+ * @defaultValue `"one-time-code"`
15
+ * @see https://developer.mozilla.org/en-US/docs/Web/HTML/Reference/Attributes/autocomplete
16
+ */
19
17
  autoComplete?: AutoComplete;
18
+ /**
19
+ * Whether or not the first fillable input should be focused on page-load.
20
+ *
21
+ * @defaultValue `false`
22
+ * @see https://developer.mozilla.org/en-US/docs/Web/HTML/Reference/Global_attributes/autofocus
23
+ */
20
24
  autoFocus?: boolean;
25
+ /**
26
+ * Whether or not the component should attempt to automatically submit when
27
+ * all fields are filled. If the field is associated with an HTML `form`
28
+ * element, the form's `requestSubmit` method will be called.
29
+ *
30
+ * @defaultValue `false`
31
+ */
32
+ autoSubmit?: boolean;
33
+ /**
34
+ * The initial value of the uncontrolled field.
35
+ */
36
+ defaultValue?: string;
37
+ /**
38
+ * Indicates the horizontal directionality of the parent element's text.
39
+ *
40
+ * @see https://developer.mozilla.org/en-US/docs/Web/HTML/Reference/Global_attributes/dir
41
+ */
42
+ dir?: RovingFocusGroupProps['dir'];
43
+ /**
44
+ * Whether or not the the field's input elements are disabled.
45
+ */
46
+ disabled?: boolean;
47
+ /**
48
+ * A string specifying the `form` element with which the input is associated.
49
+ * This string's value, if present, must match the id of a `form` element in
50
+ * the same document.
51
+ *
52
+ * @see https://developer.mozilla.org/en-US/docs/Web/HTML/Reference/Elements/input#form
53
+ */
21
54
  form?: string | undefined;
55
+ /**
56
+ * A string specifying a name for the input control. This name is submitted
57
+ * along with the control's value when the form data is submitted.
58
+ *
59
+ * @see https://developer.mozilla.org/en-US/docs/Web/HTML/Reference/Elements/input#name
60
+ */
22
61
  name?: string | undefined;
62
+ /**
63
+ * When the `autoSubmit` prop is set to `true`, this callback will be fired
64
+ * before attempting to submit the associated form. It will be called whether
65
+ * or not a form is located, or if submission is not allowed.
66
+ */
67
+ onAutoSubmit?: (value: string) => void;
68
+ /**
69
+ * A callback fired when the field's value changes. When the component is
70
+ * controlled, this should update the state passed to the `value` prop.
71
+ */
72
+ onValueChange?: (value: string) => void;
73
+ /**
74
+ * Indicates the vertical directionality of the input elements.
75
+ *
76
+ * @defaultValue `"horizontal"`
77
+ */
78
+ orientation?: RovingFocusGroupProps['orientation'];
79
+ /**
80
+ * Defines the text displayed in a form control when the control has no value.
81
+ *
82
+ * @see https://developer.mozilla.org/en-US/docs/Web/HTML/Reference/Attributes/placeholder
83
+ */
23
84
  placeholder?: string | undefined;
24
- required?: boolean;
85
+ /**
86
+ * Whether or not the input elements can be updated by the user.
87
+ *
88
+ * @see https://developer.mozilla.org/en-US/docs/Web/HTML/Reference/Attributes/readonly
89
+ */
90
+ readOnly?: boolean;
91
+ /**
92
+ * Function for custom sanitization when `validationType` is set to `"none"`.
93
+ * This function will be called before updating values in response to user
94
+ * interactions.
95
+ */
96
+ sanitizeValue?: (value: string) => string;
97
+ /**
98
+ * The input type of the field's input elements. Can be `"password"` or `"text"`.
99
+ */
25
100
  type?: InputType;
26
- dir?: RovingFocusGroupProps['dir'];
27
- orientation?: RovingFocusGroupProps['orientation'];
101
+ /**
102
+ * Specifies the type of input validation to be used. Can be `"alpha"`,
103
+ * `"numeric"`, `"alphanumeric"` or `"none"`.
104
+ *
105
+ * @defaultValue `"numeric"`
106
+ */
107
+ validationType?: InputValidationType;
108
+ /**
109
+ * The controlled value of the field.
110
+ */
111
+ value?: string;
28
112
  }
29
- interface OneTimePasswordFieldProps extends OneTimePasswordFieldOwnProps, Omit<React.ComponentPropsWithoutRef<typeof Primitive.div>, keyof OneTimePasswordFieldOwnProps> {
113
+ interface OneTimePasswordFieldProps extends OneTimePasswordFieldOwnProps, Omit<Primitive.PrimitivePropsWithRef<'div'>, keyof OneTimePasswordFieldOwnProps> {
30
114
  }
31
- declare const OneTimePasswordField: React.ForwardRefExoticComponent<OneTimePasswordFieldProps & React.RefAttributes<HTMLDivElement>>;
115
+ declare const OneTimePasswordField: React.ForwardRefExoticComponent<Omit<OneTimePasswordFieldProps, "ref"> & React.RefAttributes<HTMLDivElement>>;
32
116
  interface OneTimePasswordFieldHiddenInputProps extends Omit<React.ComponentProps<'input'>, keyof 'value' | 'defaultValue' | 'type' | 'onChange' | 'readOnly' | 'disabled' | 'autoComplete' | 'autoFocus'> {
33
117
  }
34
118
  declare const OneTimePasswordFieldHiddenInput: React.ForwardRefExoticComponent<Omit<OneTimePasswordFieldHiddenInputProps, "ref"> & React.RefAttributes<HTMLInputElement>>;
35
- interface OneTimePasswordFieldInputOwnProps {
36
- autoComplete?: 'one-time-code' | 'off';
37
- }
38
- interface OneTimePasswordFieldInputProps extends OneTimePasswordFieldInputOwnProps, Omit<React.ComponentProps<typeof Primitive.input>, keyof OneTimePasswordFieldInputOwnProps> {
119
+ interface OneTimePasswordFieldInputProps extends Omit<Primitive.PrimitivePropsWithRef<'input'>, 'value' | 'defaultValue' | 'disabled' | 'readOnly' | 'autoComplete' | 'autoFocus' | 'form' | 'name' | 'placeholder' | 'type'> {
120
+ /**
121
+ * Callback fired when the user input fails native HTML input validation.
122
+ */
123
+ onInvalidChange?: (character: string) => void;
39
124
  }
40
125
  declare const OneTimePasswordFieldInput: React.ForwardRefExoticComponent<Omit<OneTimePasswordFieldInputProps, "ref"> & React.RefAttributes<HTMLInputElement>>;
41
- declare const Root: React.ForwardRefExoticComponent<OneTimePasswordFieldProps & React.RefAttributes<HTMLDivElement>>;
42
- declare const Input: React.ForwardRefExoticComponent<Omit<OneTimePasswordFieldInputProps, "ref"> & React.RefAttributes<HTMLInputElement>>;
43
- declare const HiddenInput: React.ForwardRefExoticComponent<Omit<OneTimePasswordFieldHiddenInputProps, "ref"> & React.RefAttributes<HTMLInputElement>>;
44
126
 
45
- export { HiddenInput, Input, type InputValidationType, OneTimePasswordField, OneTimePasswordFieldHiddenInput, type OneTimePasswordFieldHiddenInputProps, OneTimePasswordFieldInput, type OneTimePasswordFieldInputProps, type OneTimePasswordFieldProps, Root };
127
+ type InputType = 'password' | 'text';
128
+ type AutoComplete = 'off' | 'one-time-code';
129
+
130
+ export { OneTimePasswordFieldHiddenInput as HiddenInput, OneTimePasswordFieldInput as Input, type InputValidationType, OneTimePasswordField, OneTimePasswordFieldHiddenInput, type OneTimePasswordFieldHiddenInputProps, OneTimePasswordFieldInput, type OneTimePasswordFieldInputProps, type OneTimePasswordFieldProps, OneTimePasswordField as Root };
package/dist/index.js CHANGED
@@ -31,17 +31,17 @@ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: tru
31
31
  // src/index.ts
32
32
  var index_exports = {};
33
33
  __export(index_exports, {
34
- HiddenInput: () => HiddenInput,
35
- Input: () => Input,
34
+ HiddenInput: () => OneTimePasswordFieldHiddenInput,
35
+ Input: () => OneTimePasswordFieldInput,
36
36
  OneTimePasswordField: () => OneTimePasswordField,
37
37
  OneTimePasswordFieldHiddenInput: () => OneTimePasswordFieldHiddenInput,
38
38
  OneTimePasswordFieldInput: () => OneTimePasswordFieldInput,
39
- Root: () => Root2
39
+ Root: () => OneTimePasswordField
40
40
  });
41
41
  module.exports = __toCommonJS(index_exports);
42
42
 
43
43
  // src/one-time-password-field.tsx
44
- var import_react_primitive = require("@radix-ui/react-primitive");
44
+ var Primitive = __toESM(require("@radix-ui/react-primitive"));
45
45
  var import_react_compose_refs = require("@radix-ui/react-compose-refs");
46
46
  var import_react_use_controllable_state = require("@radix-ui/react-use-controllable-state");
47
47
  var import_primitive = require("@radix-ui/primitive");
@@ -74,32 +74,24 @@ var INPUT_VALIDATION_MAP = {
74
74
  regexp: /[^a-zA-Z0-9]/g,
75
75
  pattern: "[a-zA-Z0-9]{1}",
76
76
  inputMode: "text"
77
- }
77
+ },
78
+ none: null
78
79
  };
79
80
  var ONE_TIME_PASSWORD_FIELD_NAME = "OneTimePasswordField";
80
- var [Collection, useCollection, createCollectionScope] = (0, import_react_collection.unstable_createCollection)(
81
- ONE_TIME_PASSWORD_FIELD_NAME
82
- );
81
+ var [Collection, { useCollection, createCollectionScope, useInitCollection }] = (0, import_react_collection.unstable_createCollection)(ONE_TIME_PASSWORD_FIELD_NAME);
83
82
  var [createOneTimePasswordFieldContext] = (0, import_react_context.createContextScope)(ONE_TIME_PASSWORD_FIELD_NAME, [
84
83
  createCollectionScope,
85
84
  import_react_roving_focus.createRovingFocusGroupScope
86
85
  ]);
87
86
  var useRovingFocusGroupScope = (0, import_react_roving_focus.createRovingFocusGroupScope)();
88
87
  var [OneTimePasswordFieldContext, useOneTimePasswordFieldContext] = createOneTimePasswordFieldContext(ONE_TIME_PASSWORD_FIELD_NAME);
89
- var OneTimePasswordFieldCollectionProvider = ({
90
- __scopeOneTimePasswordField,
91
- children
92
- }) => {
93
- return /* @__PURE__ */ (0, import_jsx_runtime.jsx)(Collection.Provider, { scope: __scopeOneTimePasswordField, children: /* @__PURE__ */ (0, import_jsx_runtime.jsx)(Collection.Slot, { scope: __scopeOneTimePasswordField, children }) });
94
- };
95
- var OneTimePasswordFieldImpl = React.forwardRef(
96
- function OneTimePasswordFieldImpl2({
88
+ var OneTimePasswordField = React.forwardRef(
89
+ function OneTimePasswordFieldImpl({
97
90
  __scopeOneTimePasswordField,
98
- id,
99
91
  defaultValue,
100
92
  value: valueProp,
101
93
  onValueChange,
102
- autoSubmit,
94
+ autoSubmit = false,
103
95
  children,
104
96
  onPaste,
105
97
  onAutoSubmit,
@@ -110,26 +102,47 @@ var OneTimePasswordFieldImpl = React.forwardRef(
110
102
  form,
111
103
  name,
112
104
  placeholder,
113
- required = false,
114
105
  type = "password",
115
106
  // TODO: Change default to vertical when inputs use vertical writing mode
116
107
  orientation = "horizontal",
117
108
  dir,
118
109
  validationType = "numeric",
110
+ sanitizeValue: sanitizeValueProp,
119
111
  ...domProps
120
112
  }, forwardedRef) {
121
113
  const rovingFocusGroupScope = useRovingFocusGroupScope(__scopeOneTimePasswordField);
122
114
  const direction = (0, import_react_direction.useDirection)(dir);
123
- const collection = useCollection(__scopeOneTimePasswordField);
124
- const validation = validationType in INPUT_VALIDATION_MAP ? INPUT_VALIDATION_MAP[validationType] : void 0;
115
+ const collectionState = useInitCollection();
116
+ const [collection] = collectionState;
117
+ const validation = INPUT_VALIDATION_MAP[validationType] ? INPUT_VALIDATION_MAP[validationType] : null;
118
+ const sanitizeValue = React.useCallback(
119
+ (value2) => {
120
+ if (Array.isArray(value2)) {
121
+ value2 = value2.map(removeWhitespace).join("");
122
+ } else {
123
+ value2 = removeWhitespace(value2);
124
+ }
125
+ if (validation) {
126
+ const regexp = new RegExp(validation.regexp);
127
+ value2 = value2.replace(regexp, "");
128
+ } else if (sanitizeValueProp) {
129
+ value2 = sanitizeValueProp(value2);
130
+ }
131
+ return value2.split("");
132
+ },
133
+ [validation, sanitizeValueProp]
134
+ );
125
135
  const controlledValue = React.useMemo(() => {
126
- return valueProp != null ? sanitizeValue(valueProp, validation?.regexp) : void 0;
127
- }, [valueProp, validation?.regexp]);
136
+ return valueProp != null ? sanitizeValue(valueProp) : void 0;
137
+ }, [valueProp, sanitizeValue]);
128
138
  const [value, setValue] = (0, import_react_use_controllable_state.useControllableState)({
129
139
  caller: "OneTimePasswordField",
130
140
  prop: controlledValue,
131
- defaultProp: defaultValue != null ? sanitizeValue(defaultValue, validation?.regexp) : [],
132
- onChange: (value2) => onValueChange?.(value2.filter(Boolean).join(""))
141
+ defaultProp: defaultValue != null ? sanitizeValue(defaultValue) : [],
142
+ onChange: React.useCallback(
143
+ (value2) => onValueChange?.(value2.join("")),
144
+ [onValueChange]
145
+ )
133
146
  });
134
147
  const dispatch = (0, import_react_use_effect_event.useEffectEvent)((action) => {
135
148
  switch (action.type) {
@@ -159,12 +172,8 @@ var OneTimePasswordFieldImpl = React.forwardRef(
159
172
  focusInput(next);
160
173
  return;
161
174
  }
162
- const newValue = [
163
- //
164
- ...value.slice(0, index),
165
- char,
166
- ...value.slice(index)
167
- ];
175
+ const newValue = [...value];
176
+ newValue[index] = char;
168
177
  const lastElement = collection.at(-1)?.element;
169
178
  (0, import_react_dom.flushSync)(() => setValue(newValue));
170
179
  if (currentTarget !== lastElement) {
@@ -205,7 +214,7 @@ var OneTimePasswordFieldImpl = React.forwardRef(
205
214
  }
206
215
  case "PASTE": {
207
216
  const { value: pastedValue } = action;
208
- const value2 = sanitizeValue(pastedValue, validation?.regexp);
217
+ const value2 = sanitizeValue(pastedValue);
209
218
  if (!value2) {
210
219
  return;
211
220
  }
@@ -222,9 +231,9 @@ var OneTimePasswordFieldImpl = React.forwardRef(
222
231
  }
223
232
  if (validationTypeRef.current?.type !== validation.type) {
224
233
  validationTypeRef.current = validation;
225
- setValue(sanitizeValue(value, validation.regexp));
234
+ setValue(sanitizeValue(value.join("")));
226
235
  }
227
- }, [setValue, validation, value]);
236
+ }, [sanitizeValue, setValue, validation, value]);
228
237
  const hiddenInputRef = React.useRef(null);
229
238
  const userActionRef = React.useRef(null);
230
239
  const rootRef = React.useRef(null);
@@ -285,7 +294,6 @@ var OneTimePasswordFieldImpl = React.forwardRef(
285
294
  form,
286
295
  name,
287
296
  placeholder,
288
- required,
289
297
  type,
290
298
  hiddenInputRef,
291
299
  userActionRef,
@@ -294,7 +302,7 @@ var OneTimePasswordFieldImpl = React.forwardRef(
294
302
  orientation,
295
303
  preHydrationIndexTracker,
296
304
  isHydrated,
297
- children: /* @__PURE__ */ (0, import_jsx_runtime.jsx)(
305
+ children: /* @__PURE__ */ (0, import_jsx_runtime.jsx)(Collection.Provider, { scope: __scopeOneTimePasswordField, state: collectionState, children: /* @__PURE__ */ (0, import_jsx_runtime.jsx)(Collection.Slot, { scope: __scopeOneTimePasswordField, children: /* @__PURE__ */ (0, import_jsx_runtime.jsx)(
298
306
  RovingFocusGroup.Root,
299
307
  {
300
308
  asChild: true,
@@ -302,7 +310,7 @@ var OneTimePasswordFieldImpl = React.forwardRef(
302
310
  orientation,
303
311
  dir: direction,
304
312
  children: /* @__PURE__ */ (0, import_jsx_runtime.jsx)(
305
- import_react_primitive.Primitive.div,
313
+ Primitive.Root.div,
306
314
  {
307
315
  ...domProps,
308
316
  role: "group",
@@ -312,7 +320,7 @@ var OneTimePasswordFieldImpl = React.forwardRef(
312
320
  (event) => {
313
321
  event.preventDefault();
314
322
  const pastedValue = event.clipboardData.getData("Text");
315
- const value2 = sanitizeValue(pastedValue, validation?.regexp);
323
+ const value2 = sanitizeValue(pastedValue);
316
324
  if (!value2) {
317
325
  return;
318
326
  }
@@ -323,18 +331,13 @@ var OneTimePasswordFieldImpl = React.forwardRef(
323
331
  }
324
332
  )
325
333
  }
326
- )
334
+ ) }) })
327
335
  }
328
336
  );
329
337
  }
330
338
  );
331
- var OneTimePasswordField = React.forwardRef(
332
- function OneTimePasswordField2(props, ref) {
333
- return /* @__PURE__ */ (0, import_jsx_runtime.jsx)(OneTimePasswordFieldCollectionProvider, { children: /* @__PURE__ */ (0, import_jsx_runtime.jsx)(OneTimePasswordFieldImpl, { ...props, ref }) });
334
- }
335
- );
336
339
  var OneTimePasswordFieldHiddenInput = React.forwardRef(function OneTimePasswordFieldHiddenInput2({ __scopeOneTimePasswordField, ...props }, forwardedRef) {
337
- const { value, hiddenInputRef } = useOneTimePasswordFieldContext(
340
+ const { value, hiddenInputRef, name } = useOneTimePasswordFieldContext(
338
341
  "OneTimePasswordFieldHiddenInput",
339
342
  __scopeOneTimePasswordField
340
343
  );
@@ -343,20 +346,25 @@ var OneTimePasswordFieldHiddenInput = React.forwardRef(function OneTimePasswordF
343
346
  "input",
344
347
  {
345
348
  ref,
346
- ...props,
347
- type: "hidden",
348
- readOnly: true,
349
+ name,
349
350
  value: value.join("").trim(),
350
351
  autoComplete: "off",
351
352
  autoFocus: false,
352
353
  autoCapitalize: "off",
353
354
  autoCorrect: "off",
354
355
  autoSave: "off",
355
- spellCheck: false
356
+ spellCheck: false,
357
+ ...props,
358
+ type: "hidden",
359
+ readOnly: true
356
360
  }
357
361
  );
358
362
  });
359
- var OneTimePasswordFieldInput = React.forwardRef(function OneTimePasswordFieldInput2({ __scopeOneTimePasswordField, ...props }, forwardedRef) {
363
+ var OneTimePasswordFieldInput = React.forwardRef(function OneTimePasswordFieldInput2({
364
+ __scopeOneTimePasswordField,
365
+ onInvalidChange,
366
+ ...props
367
+ }, forwardedRef) {
360
368
  const {
361
369
  value: _value,
362
370
  defaultValue: _defaultValue,
@@ -367,7 +375,6 @@ var OneTimePasswordFieldInput = React.forwardRef(function OneTimePasswordFieldIn
367
375
  form: _form,
368
376
  name: _name,
369
377
  placeholder: _placeholder,
370
- required: _required,
371
378
  type: _type,
372
379
  ...domProps
373
380
  } = props;
@@ -380,12 +387,16 @@ var OneTimePasswordFieldInput = React.forwardRef(function OneTimePasswordFieldIn
380
387
  const rovingFocusGroupScope = useRovingFocusGroupScope(__scopeOneTimePasswordField);
381
388
  const inputRef = React.useRef(null);
382
389
  const [element, setElement] = React.useState(null);
390
+ let placeholder;
383
391
  let index;
384
392
  if (!isHydrated) {
385
393
  index = preHydrationIndexTracker.current;
386
394
  preHydrationIndexTracker.current++;
387
395
  } else {
388
396
  index = element ? collection.indexOf(element) : -1;
397
+ if (context.placeholder && context.value.length === 0) {
398
+ placeholder = context.placeholder[index];
399
+ }
389
400
  }
390
401
  const composedInputRef = (0, import_react_compose_refs.useComposedRefs)(forwardedRef, inputRef, setElement);
391
402
  const char = context.value[index] ?? "";
@@ -407,7 +418,7 @@ var OneTimePasswordFieldInput = React.forwardRef(function OneTimePasswordFieldIn
407
418
  focusable: !context.disabled && isFocusable,
408
419
  active: index === lastSelectableIndex,
409
420
  children: /* @__PURE__ */ (0, import_jsx_runtime.jsx)(
410
- import_react_primitive.Primitive.input,
421
+ Primitive.Root.input,
411
422
  {
412
423
  ref: composedInputRef,
413
424
  type: "text",
@@ -418,6 +429,7 @@ var OneTimePasswordFieldInput = React.forwardRef(function OneTimePasswordFieldIn
418
429
  pattern: validation?.pattern,
419
430
  readOnly: context.readOnly,
420
431
  value: char,
432
+ placeholder,
421
433
  "data-radix-otp-input": "",
422
434
  "data-radix-index": index,
423
435
  ...domProps,
@@ -448,7 +460,7 @@ var OneTimePasswordFieldInput = React.forwardRef(function OneTimePasswordFieldIn
448
460
  return;
449
461
  }
450
462
  const isClearing = action.key === "Backspace" && (action.metaKey || action.ctrlKey);
451
- if (isClearing) {
463
+ if (action.key === "Clear" || isClearing) {
452
464
  dispatch({ type: "CLEAR", reason: "Backspace" });
453
465
  } else {
454
466
  dispatch({ type: "CLEAR_CHAR", index, reason: action.key });
@@ -476,6 +488,7 @@ var OneTimePasswordFieldInput = React.forwardRef(function OneTimePasswordFieldIn
476
488
  }
477
489
  } else {
478
490
  const element2 = event.target;
491
+ onInvalidChange?.(element2.value);
479
492
  requestAnimationFrame(() => {
480
493
  if (element2.ownerDocument.activeElement === element2) {
481
494
  element2.select();
@@ -485,12 +498,13 @@ var OneTimePasswordFieldInput = React.forwardRef(function OneTimePasswordFieldIn
485
498
  }),
486
499
  onKeyDown: (0, import_primitive.composeEventHandlers)(props.onKeyDown, (event) => {
487
500
  switch (event.key) {
501
+ case "Clear":
488
502
  case "Delete":
489
503
  case "Backspace": {
490
504
  const currentValue = event.currentTarget.value;
491
505
  if (currentValue === "") {
492
506
  if (event.key === "Delete") return;
493
- const isClearing = event.metaKey || event.ctrlKey;
507
+ const isClearing = event.key === "Clear" || event.metaKey || event.ctrlKey;
494
508
  if (isClearing) {
495
509
  dispatch({ type: "CLEAR", reason: "Backspace" });
496
510
  } else {
@@ -580,21 +594,11 @@ var OneTimePasswordFieldInput = React.forwardRef(function OneTimePasswordFieldIn
580
594
  }
581
595
  ) });
582
596
  });
583
- var Root2 = OneTimePasswordField;
584
- var Input = OneTimePasswordFieldInput;
585
- var HiddenInput = OneTimePasswordFieldHiddenInput;
586
597
  function isFormElement(element) {
587
598
  return element?.tagName === "FORM";
588
599
  }
589
- function sanitizeValue(value, regexp) {
590
- if (Array.isArray(value)) {
591
- value = value.join("");
592
- }
593
- if (regexp) {
594
- regexp = new RegExp(regexp);
595
- return value.replace(regexp, "").split("").filter(Boolean);
596
- }
597
- return value.split("").filter(Boolean);
600
+ function removeWhitespace(value) {
601
+ return value.replace(/\s/g, "");
598
602
  }
599
603
  function focusInput(element) {
600
604
  if (!element) return;