MaskedField
A Reactberry Field with numeric or pattern input masking from react-number-format, with built-in presets.
MaskedField is a client component that formats input as the user types, for amounts, phone numbers, dates and similar values. It renders react-number-format's NumericFormat or PatternFormat with Reactberry's Field (as="input") as the input. Masks come from a named preset or from react-number-format props.
Import#
import { MaskedField, maskPresets } from "@reactberry/system/blocks";
import type { MaskedFieldProps, MaskPresetType } from "@reactberry/system/blocks";
Usage#
import { useState } from "react";
import { MaskedField } from "@reactberry/system/blocks";
import { Box, Text } from "@reactberry/system/elements";
export default function BudgetInput() {
const [amount, setAmount] = useState<number | undefined>();
return (
<Box display="flex" flexDirection="column" gap="xs">
<Text as="label" htmlFor="budget" fontWeight={600}>
Budget
</Text>
<MaskedField
id="budget"
preset="currency"
variant="default"
value={amount}
onValueChange={(values) => setAmount(values.floatValue)}
placeholder="$0.00"
/>
</Box>
);
}
Examples#
Presets#
Pass preset to apply a ready-made mask. Pattern presets such as phone, date and creditCard use PatternFormat; percentage uses NumericFormat and only allows values from 0 to 100.
Custom formats#
Props are merged after the preset, so they override its options. Use maskType="pattern" with format for a mask that has no preset.
API#
MaskedField#
| Prop | Type | Default | Description |
|---|---|---|---|
preset | MaskPresetType | — | A key of |
maskType | "numeric""pattern" | — | Selects the formatter. Takes precedence over the preset's type. Falls back to |
variant | string | "ghost" |
|
$size | string | "medium" |
|
width | string | "100%" |
|
shape | string | — | Corner shape, forwarded to |
bg | string | — | Background colour, forwarded to |
fontSize | string | — | Text size, forwarded to |
fontWeight | number | — | Text weight, forwarded to |
textAlign | string | — | Text alignment, forwarded to |
All other NumericFormatProps or PatternFormatProps from react-number-format (except customInput and getInputRef) are accepted, including value, onValueChange, onChange, format, mask, prefix, suffix, decimalScale, and isAllowed. They are merged after the preset, so they override preset options. Props that react-number-format does not use are passed through to Field.
maskPresets#
An object of preset configurations. MaskPresetType is keyof typeof maskPresets.
Numeric presets (NumericFormat):
currency— prefix$, thousand separator,, decimal separator., 2 fixed decimals, no negatives.percentage— suffix%, up to 2 decimals, no negatives, values limited to 0–100.decimal— up to 4 decimals, negatives allowed, thousand separator,.integer— no decimals, no negatives, thousand separator,.year— no decimals, no negatives, values limited to 1900–2100.
Pattern presets (PatternFormat, mask character _, allowEmptyFormatting: false):
phone—(###) ###-####date—##/##/####, placeholderMM/DD/YYYYzip—#####zipPlus4—#####-####ssn—###-##-####ein—##-#######creditCard—#### #### #### ####time12—##:## ##, placeholderHH:MM AMtime24—##:##, placeholderHH:MM
Accessibility#
MaskedFieldrenders a nativeinputwith no label. Pass anidand pair it with alabel(htmlFor), or passaria-label.
Notes#
- Any
typeprop is removed before rendering (it is stripped together with the preset's internaltypekey), so the input always usesField's defaulttype="text". MaskedFielddoes not forward refs, andgetInputRefis omitted from its props.- Pattern presets only restrict input to digits;
date,time12, andtime24do not validate ranges, andtime12accepts digits only in the AM/PM slot. maskType="pattern"without a preset requires aformatprop.formatWithPresetexists in the module source but is not exported from@reactberry/system/blocks.- The preset
placeholdervalues (date,time12,time24) are passed to the input and can be overridden.