fn
useMaskedInput
v0.0.14testeddemoHeadless input masking. Returns a bind object to spread onto an
<input>/<textarea> (<input v-bind="bind">) — it carries the template ref
and the event handlers, so there is no separate ref wiring. Conforms the value
on every keystroke (insert/delete/paste/IME) with a correct caret, and exposes
the masked/unmasked views plus a complete signal.
Examples
ts
const phone = useMaskedInput({ mask: '+1 (###) ###-####' });
// <input v-bind="phone.bind">
// phone.unmasked.value → '1234567890'ts
const amount = useMaskedInput({
mask: maskNumberOptions({ thousandSeparator: ',', precision: 2, prefix: '$' }),
onAccept: ({ unmasked }) => save(unmasked),
});Demo
Loading demo…
Signature
ts
export function useMaskedInput(options: UseMaskedInputOptions): UseMaskedInputReturn{ ... }Parameters
| Parameter | Type | Description |
|---|---|---|
options | UseMaskedInputOptions | The mask and behavior |
Returns
UseMaskedInputReturnbind, masked, unmasked, complete, ensureFitsMask, setValue| Property | Type | Description |
|---|---|---|
masked | ShallowRef<string> | The conformed, displayed value (kept in sync with the element). |
unmasked | Readonly<ShallowRef<string>> | The raw value with fixed mask characters (and preset separators) stripped. |
complete | ComputedRef<boolean> | Whether masked fully satisfies the mask. |
bind | MaskInputBindings | Spread onto the input element: <input v-bind="bind">. |
ensureFitsMask | () => void | Re-conform the element's current value (e.g. after a programmatic set). |
setValue | (value: string) => void | Set the value programmatically — treated as raw input and conformed. |