NumericFormatElement
Use NumericFormat with react-hook-form for currency, thousand separators and decimals. NumericFormatElement binds the value and validation.
Import
import { NumericFormatElement } from '@piplup/rhf-adapters/react-number-format';Usage
'use client';import Stack from '@mui/material/Stack';import { MuiButtonElement } from '@piplup/rhf-adapters/mui-material';import { NumericFormatElement } from '@piplup/rhf-adapters/react-number-format';import { useForm } from 'react-hook-form';export default function Page() { const { control, handleSubmit } = useForm({ defaultValues: { price: '1200' }, }); return ( <form onSubmit={handleSubmit((values) => alert(JSON.stringify(values, null, 2)))}> <Stack direction="column" spacing={2}> <NumericFormatElement control={control} name="price" prefix="$" thousandSeparator /> <div> <MuiButtonElement control={control} type="submit" variant="contained"> Submit </MuiButtonElement> </div> </Stack> </form> );}Props
Prop
Type
NumericFormatElement supports all standard props of the NumericFormat component, along with additional adapter-specific props.
The
refis forwarded to the underlying<NumericFormat />component.
useNumericFormatAdapter
useNumericFormatAdapter connects a field to React Hook Form and returns props you spread onto NumericFormat from react-number-format. It's what NumericFormatElement uses internally. It calls useNumberFormatBaseAdapter with the same props, so the wiring is identical; this page covers what matters for amounts and quantities.
Use the element for one-off fields. Reach for the hook when you want a reusable money or quantity field with the currency format, range and messages fixed in one place, or when your form should hold a number rather than a string.
Import
import { useNumericFormatAdapter } from '@piplup/rhf-adapters/react-number-format';This BudgetField shows $12,500.00-style input, stores a number (or null when empty), and limits the range with its own messages. Try 200 or 75000, then submit.
'use client';import Stack from '@mui/material/Stack';import TextField from '@mui/material/TextField';import { MuiButtonElement } from '@piplup/rhf-adapters/mui-material';import { useNumericFormatAdapter } from '@piplup/rhf-adapters/react-number-format';import { useForm } from 'react-hook-form';import { NumericFormat } from 'react-number-format';import type { Control } from 'react-hook-form';import type { NumberFormatValues } from 'react-number-format';type FormValues = { budget: null | number };// Stores a real number (or null) instead of the default numeric string,// and enforces a range with project-specific messages.function BudgetField({ control }: { control: Control<FormValues> }) { const adapter = useNumericFormatAdapter<number | string, FormValues, 'budget'>({ composeHelperText: true, control, helperText: 'Between $500 and $50,000', max: 50000, messages: { max: 'Budgets over $50,000 need approval', min: 'The minimum budget is $500', required: 'Enter a budget', }, min: 500, name: 'budget', required: true, transform: { input: (value) => value ?? '', output: (values: NumberFormatValues) => values.floatValue ?? null, }, }); return ( <NumericFormat allowNegative={false} customInput={TextField} decimalScale={2} label="Project budget" prefix="$" thousandSeparator {...adapter} /> );}export default function Page() { const { control, handleSubmit } = useForm<FormValues>({ defaultValues: { budget: null }, }); return ( <form onSubmit={handleSubmit((values) => alert(JSON.stringify(values, null, 2)))} noValidate> <Stack direction="column" spacing={2}> <BudgetField control={control} /> <div> <MuiButtonElement control={control} type="submit" variant="contained"> Submit </MuiButtonElement> </div> </Stack> </form> );}How it works
Stored value is a string by default. The form receives values.value from onValueChange: the number as a string, without prefix, suffix or thousand separators, and with . as the decimal point. Typing $12,500.5 stores "12500.5". An empty input stores "".
Storing a number. Pass transform.output: (values) => values.floatValue ?? null, as in the demo. floatValue is undefined when the input is empty, hence the fallback. Pair it with transform.input: (value) => value ?? "": NumericFormat treats a null value as "not controlled" and keeps showing its last text, so without it reset() to null would not clear the input.
Range checks. min and max work with both storage styles. React Hook Form converts the stored string to a number before comparing, so "12500.5" is checked as 12500.5. Formatting characters never reach the check.
| Key | Default message |
|---|---|
required | Please fill out this field. |
min | Value must be min or later. |
max | Value must be max or earlier. |
The default min/max wording is shared with date inputs, so most money fields set their own messages. minLength and maxLength count characters of the stored string ("12500.5" is 7), not of the formatted text.
Message functions get the limit wrapped in an array: min: (min) => ... receives [500]. Inside a template string that prints as 500, but call number methods on min[0].
Errors and helper text. Set composeHelperText: true when rendering through customInput={TextField}. You then get error and a helperText that shows the error message, or your helperText when valid. Without it, neither prop is returned. Like the base hook, the field ref ends up on the TextField root, so focus-on-error does not reach the input.
Props
Prop
Type
Return value
Prop
Type
Notes
- Use this component when numeric display formatting should stay separate from stored
react-hook-formvalues.
NumberFormatBaseElement
Use NumberFormatBase with react-hook-form for fully custom number formatting. NumberFormatBaseElement binds the value and validation.
PatternFormatElement
Use PatternFormat with react-hook-form for masked inputs like phone numbers, card numbers and dates. PatternFormatElement binds value and validation.