createOtp API
API reference for the createOtp composable.
Related
Explore these related pages for additional context and usage patterns.
| Page | Category |
|---|---|
| createOtp | Forms |
| createInput | Forms |
| createValidation | Forms |
Functions
createOtp
(options?: OtpOptions) => OtpContextOptions
pattern
MaybeRefOrGetter<OtpPattern> | undefinedPer-character pattern. String presets compile to internal RegExps; custom RegExp must match a single character.
Default: 'numeric'
onComplete
((value: string) => boolean | void | PromiseLike<boolean | void>) | undefinedFire when the joined value first reaches `length`. Decisional — return / resolve `false` to reject (clears value, sets error). A rejected Promise is treated as `false` and logs a warning.
Properties
value
Readonly<Ref<string, string>>The joined OTP string. Use the mutation helpers (`write`, `distribute`, `fill`, `clear`) to update. Writing through this ref directly is intentionally prevented to preserve pattern, length, and lock invariants.
input
Omit<InputContext<string>, "value"> & { value: Readonly<Ref<string>>; }The underlying `createInput` context — exposes ARIA IDs, validation state, focus/touched, and the `validate` / `reset` methods. Note: `input.value` aliases `OtpContext.value` and is also readonly on the public surface. Use the mutation helpers (`write`, `distribute`, `fill`, `clear`) to update.
isComplete
Readonly<Ref<boolean, boolean>>True when `value` reaches `length` and every character matches `pattern`.
isValidating
Readonly<Ref<boolean, boolean>>True while an async `onComplete` is in flight. Mutation helpers no-op during this window; consumers can render a spinner against this ref.
Methods
write
(index: number, char: string) => voidWrite a single character at `index`. Empty string clears trailing characters from `index` onward. Out-of-range indices and characters rejected by `pattern` are ignored. No-ops while locked.
distribute
(text: string, index?: number) => numberSplice pattern-accepted characters from `text` into `value` starting at `index`. Returns the number of characters written. No-ops while locked or when no characters pass the pattern.
fill
(text: string) => voidReplace `value` with pattern-accepted characters from `text`, truncated to `length`. Clears rejection state on deliberate empty input or any accepted characters. No-ops while locked.
accepts
(char: string) => booleanWhether `char` is a single character matching the current `pattern`.