Masks
Pattern masks such as (000) 000-0000 and locale date masks.
Reach for them for formatted text input. The helpers are pure: no DOM at import time or in the call path, so they run on the server and in tests. Each result below is computed when the page is built, by running the call shown.
Import
import {
applyInputMask,
parseMaskPattern,
formatWithMask,
extractMaskInput,
maskToPlaceholder,
maskInputSlotCount,
buildDateMaskPattern,
applyDateMask,
getDefaultDatePlaceholder,
maskedDigitsToParts,
partsToDate,
} from '@ecopages/radiant-ui/helpers';Pattern masks
In a pattern 0 is a digit slot, a a letter and * any character; {text} is fixed text that counts as input, [...] an optional section and \ escapes the next character. Other characters are literals. parseMaskPattern turns a pattern into tokens that formatWithMask and extractMaskInput use.
| Call | Result |
|---|---|
applyInputMask('5551234567', '(000) 000-0000') | "(555) 123-4567" |
maskToPlaceholder('(000) 000-0000') | "(___) ___-____" |
formatWithMask('12345', parseMaskPattern('+00 000')) | "+12 345" |
extractMaskInput('+12 345', parseMaskPattern('+00 000')) | "12345" |
Date masks
The mask and placeholder come from the locale, and typed digits map back to a date in the locale's field order.
| Call | Result |
|---|---|
buildDateMaskPattern('en-US') | "00/00/0000" |
getDefaultDatePlaceholder('en-US') | "mm/dd/yyyy" |
applyDateMask('08212002', '00/00/0000') | "08/21/2002" |
maskedDigitsToParts('08212002', 'en-US') | {"month":8,"day":21,"year":2002} |
partsToDate({ year: 2002, month: 8, day: 21 }) | Date(2002-08-21) |