fn

useMaskedField

v0.0.14testeddemo

A masked form field: fuses useField with useMaskedInput so a formatted value is shown while the form stores the raw value (validation, dirty/touched, schema, and submit all read raw). Returns a single bind object to spread onto the input — it merges the field's name/onBlur/aria-invalid with the mask bindings (ref + handlers). Purely additive — it composes the existing form composables without modifying them.

Example

ts
const { bind, errorMessage } = useMaskedField('phone', { mask: '+1 (###) ###-####' });
// <input v-bind="bind">  — the form stores the raw digits

Demo

Loading demo…

Signature

ts
export function useMaskedField<T = string>(
  path: MaybeRefOrGetter<string>,
  options: UseMaskedFieldOptions<T>,
): UseMaskedFieldReturn<T>{ ... }

Type Parameters

T= string

Parameters

ParameterTypeDescription
pathMaybeRefOrGetter<string>The dotted field path
optionsUseMaskedFieldOptions<T>The mask plus any useField options

Returns

UseMaskedFieldReturn<T>The field API plus display, unmasked, complete, bind
PropertyTypeDescription
displayShallowRef<string>The masked display text (read-only; the input shows it via bind).
unmaskedReadonly<ShallowRef<string>>The raw unmasked value (mirrors what is written to the form by default).
completeComputedRef<boolean>Whether the mask is fully satisfied.
bindComputedRef<Record<string, unknown>>Spread onto the input (<input v-bind="bind">): merges the field's name/onBlur/aria-invalid with the mask bindings (ref + input handlers).
valueRef<T>The field's writable value (bound to the form path, or local in standalone).
errorsComputedRef<string[]>The field's error messages.
errorMessageComputedRef<string | undefined>The field's first error message, if any.
metaFieldMetaGrouped reactive meta for the field.
handleBlur(event?: Event) => voidBlur handler — marks touched and (re)validates per trigger.
handleChange(value: T, shouldValidate?: boolean) => voidSet the value and optionally validate.
handleInput(event: Event) => voidinput/change DOM handler for native elements.
setValue(value: T) => voidSet the field value programmatically.
setTouched(touched?: boolean) => voidMark the field touched/untouched.
setErrors(message: string | string[] | null) => voidSet or clear (with null) the field's errors.
validate() => Promise<FieldValidationResultDetail>Validate just this field.
reset() => voidReset the field to its initial value.
attrsComputedRef<FieldBindingProps>Props to spread on the field element (v-bind="attrs").