Skip to content

Mask editor ​

cellEditor: 'mask' makes people type a value in a fixed shape such as 010-1234-5678. Each key press refits the text to the pattern, and empty positions show as _. It suits phone numbers, business numbers, date text and color codes.

ts
{ id: 'tel', editable: true, cellEditor: 'mask',
  editorParams: { pattern: '###-####-####' } }

It needs the optional editors bundle (maxgrid/editors in the modular build). Without it, creating the grid fails with invalid-option.

Pattern ​

CharacterMeaning
#One digit
AOne letter (Latin letters and Hangul syllables)
*One letter or digit
\Use the next character as it is (\# is a #)
Any otherA fixed character shown as it is (-, (, ), space)

slot adds or replaces slot codes. The value is the source of a regular expression that matches one character (a single atom such as [0-9a-f], \d or \p{L}). Quantifiers (+, *), groups, | and more than one atom are an invalid-option. A key must be one character and not \.

ts
editorParams: { pattern: 'HH:HH:HH', slot: { H: '[0-9a-fA-F]' } }   // hex color

Options ​

All go in editorParams. Only the pattern is required.

OptionValueDefaultEffect
patternstringrequiredThe pattern; empty, without a slot or ending in \ is an invalid-option
slot{ code: regex source }noneAdd or replace slot codes
keepMaskbooleantruetrue stores the masked text (010-1234-5678), false only the slot characters (01012345678)
placeholderCharone character_Shown for empty slots while editing; a character a slot of this pattern accepts is an invalid-option
overwritebooleanfalsetrue makes typing replace the character at the caret instead of inserting
autoCommitbooleanfalsetrue commits the moment every slot is filled

inputMode, align and placeholder work as well; placeholder shows only before anything is typed. A pattern with only digit slots (#) asks for the numeric keypad (inputMode: 'numeric').

While typing ​

  • Typed characters that fit the slot go in; others are ignored.
  • Inserting in the middle pushes the rest along, and the caret stays right after the inserted character.
  • Backspace and Delete skip fixed characters such as - and erase slot characters. Selecting a range and deleting or typing over it works too.
  • Paste keeps only the characters that fit the slots.
  • While an input method (IME) is composing a character, the input is left alone; the pattern is applied the moment composition ends. This includes Hangul in A slots.
  • Pressing a letter key on a selected cell to start the edit puts that letter in the first slot.

Stored values that do not fit the pattern ​

If the stored value fits the pattern nowhere (for example garbage), the input is not emptied: the text is shown as it is. Committing without touching it is refused by the incomplete-value check, so the value is not cleared silently. To clear it, delete it yourself or type a new value.

Incomplete values ​

Committing before every slot is filled does not write the value and shows Fill in every position. Committing with nothing typed gives an empty value (emptyAs decides what that means). Paste and validateRows use the same rule.

A value stored with keepMask: false goes to the grid display, copy and export as the slot characters (01012345678). To show it in the masked shape, call applyMask in formatValue.

applyMask and stripMask ​

The same rules work outside the editor. Both functions are exported from maxgrid and maxgrid/core.

ts
import { applyMask, stripMask } from 'maxgrid';

applyMask('01012345678', '###-####-####'); // '010-1234-5678'
applyMask('010', '###-####-####', { partial: true }); // '010-____-____'
stripMask('010-1234-5678', '###-####-####'); // '01012345678'
FunctionWhat it does
applyMask(raw, pattern, opts?)Fits characters into the pattern; characters that do not fit a slot are skipped
stripMask(masked, pattern, opts?)Returns only the slot characters (fixed characters and blanks are dropped)

opts takes slot, placeholderChar (default _) and partial. With partial: false (default) the result is cut at the last filled slot; with true empty slots show placeholderChar and every fixed character is included. A bad pattern is an invalid-option.

See also ​