import { InputContext, InputOptions } from "../createInput/index.mjs"; import { ComputedRef, MaybeRefOrGetter, Ref } from "vue"; //#region src/composables/createOtp/index.d.ts /** * Pattern presets — string literals compile to internal RegExps; arbitrary * RegExp values are applied per character. * * @example * ```ts * const otp = createOtp({ pattern: 'numeric' }) * const hex = createOtp({ pattern: /^[0-9a-fA-F]$/ }) * ``` */ type OtpPattern = 'numeric' | 'alphanumeric' | 'alphabetic' | RegExp; type OtpItemState = 'filled' | 'empty'; /** * Per-character descriptor for iterating boxes from `length`. * * @example * ```ts * const otp = createOtp({ length: 4 }) * otp.items.value[0] // { index: 0, value: '', state: 'empty' } * otp.write(0, '4') * otp.items.value[0] // { index: 0, value: '4', state: 'filled' } * ``` */ interface OtpItemDescriptor { /** 0-based position */ index: number; /** Character at this index, or '' when empty */ value: string; /** Fill state */ state: OtpItemState; } /** * Options accepted by `createOtp`. * * @example * ```ts * const otp = createOtp({ * length: 6, * pattern: 'numeric', * disabled: false, * onComplete: async value => { * const ok = await verify(value) * return ok * }, * }) * ``` */ interface OtpOptions extends Omit, 'value' | 'error' | 'errorMessages'> { /** Number of characters. @default 6 */ length?: MaybeRefOrGetter; /** Joined value source. @default shallowRef('') */ value?: Ref; /** * Per-character pattern. String presets compile to internal RegExps; * custom RegExp must match a single character. * @default 'numeric' */ pattern?: MaybeRefOrGetter; /** * Fire when the joined value becomes a new complete string. Decisional — * return / resolve `false` to reject (clears value, sets error). * A rejected Promise is treated as `false` and logs a warning. * The same complete string is deduped until the value goes incomplete. */ onComplete?: (value: string) => boolean | void | PromiseLike; } /** * Reactive context returned by `createOtp`. Consumers index into `value` * for per-character rendering and call the mutation helpers to drive state. * * @example * ```ts * const otp = createOtp({ length: 6 }) * * otp.write(0, '4') * otp.distribute('12345', 1) // returns 5 * otp.isComplete.value // true * otp.clear() * ``` */ interface OtpContext { /** * 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. */ value: Readonly>; /** Resolved character count from the `length` option. */ length: Readonly>; /** * One descriptor per box, derived from `length` and `value`. * Iterate this instead of `v-for="i in length"`. */ items: ComputedRef; /** * 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. */ input: Omit, 'value'> & { value: Readonly>; }; /** True when `value` reaches `length` and every character matches `pattern`. */ isComplete: Readonly>; /** * True while an async `onComplete` is in flight. Mutation helpers no-op * during this window; consumers can render a spinner against this ref. */ isValidating: Readonly>; /** * Write 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. */ write: (index: number, char: string) => void; /** * Splice 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. */ distribute: (text: string, index?: number) => number; /** Clear `value` and rejection state. No-ops while locked. */ clear: () => void; /** * Replace `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. */ fill: (text: string) => void; /** Whether `char` is a single character matching the current `pattern`. */ accepts: (char: string) => boolean; } declare function createOtp(_options?: OtpOptions): OtpContext; //#endregion export { OtpContext, OtpItemDescriptor, OtpItemState, OtpOptions, OtpPattern, createOtp };