Getting started

Actions

Forms

Layout

Navigation

Overlays

Feedback

Data display

Controllers

Helpers

Recipes

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,
	extractUnmaskedValue,
	formatUnmaskedValue,
	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, [...] an optional section and \ escapes the next character. Other characters are literals. parseMaskPattern turns a pattern into tokens that the other helpers use.

Two reads, two writes. extractMaskInput and formatWithMask work on a flat string of slot characters. formatWithMask fills eligible slots in pattern order, so it can fill an optional slot that the original display skipped. extractUnmaskedValue and formatUnmaskedValue also carry the {text}. For [0]{AB}-00, the unmasked AB12 keeps the digits after AB and formats as AB-12.

applyInputMask reads raw text in pattern order. It skips an optional slot when the character does not fit and drops a character that fits no available token. For 0[a]0, 12 stays 12. Typing A for {AB}-00 displays A. A following digit completes the fixed text and fills the next slot. Typing the separator in 000[-00] gives 123- before the optional digits are entered.

When RuiInput edits a masked value, it uses each character's position in the pattern. For example, deleting AB-1 from AB-12 in [0]{AB}-00 leaves AB-2. The caret follows filled slots across literals, including supplementary Unicode characters such as 🟢. RuiInput formats IME text when composition ends.

CallResult
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"
extractUnmaskedValue('+7 123', parseMaskPattern('+{7} 000'))"7123"
formatUnmaskedValue('7123', parseMaskPattern('+{7} 000'))"+7 123"

Date masks

The mask and placeholder come from the locale, and typed digits map back to a date in the locale's field order.

CallResult
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)