Usage#
import { useMask } from '@primereact/hooks/use-mask';const { ref, value, rawValue, isComplete } = useMask({ mask: '(999) 999-9999' });
<InputText ref={ref} />;Attach ref to any component that forwards a ref to a native input. The hook formats keystrokes against mask and keeps both the masked and raw values in sync.
Signature#
function useMask(options: UseMaskOptions): UseMaskReturn;Options#
mask— pattern string:9numeric,aalphabetic,*alphanumeric. Literals like(,),-and/are inserted automatically. A?marks everything after it as optional.slotChar?— placeholder for unfilled slots (default_). Pass a full template likedd/mm/yyyyto label each slot.autoClear?— clear an incomplete value on blur (defaulttrue).value?/defaultValue?/onValueChange?— controlled / uncontrolled masked value.onComplete?— fires when every required slot is filled.
Returns#
{ ref, value, rawValue, isComplete, setValue }. value is the masked string, rawValue strips the literals and placeholders, isComplete is true once all required slots are filled.
Basic#
basic-demo
'use client';
import { useMask } from '@primereact/hooks';
import { InputText } from '@primereact/ui/inputtext';
export default function BasicDemo() {
const { ref } = useMask({ mask: '99-999999' });
return (
<div className="flex justify-center">
<InputText ref={ref} placeholder="99-999999" />
</div>
);
}