Number field API
Import
Section titled “Import”import { NumberField } from '@cimpress-ui/react';Controlled/uncontrolled usage
Section titled “Controlled/uncontrolled usage”This component can be used in a controlled or uncontrolled way.
In the controlled way, this component doesn’t maintain its own internal state, and its value is provided by the parent component. Use the controlled way when you need to be able to change the state of this component in other parts of your application.
In the uncontrolled way, this component maintains its own internal state, and can optionally notify the parent component when its internal state changes. Use the uncontrolled way when you don’t need to change the state of this component in other parts of your application.
import { NumberField, Stack } from '@cimpress-ui/react';import { useState } from 'react';
export default function Demo() { const [value, setValue] = useState<number | null>(10);
return ( <Stack gap={32}> <NumberField label="Width (controlled)" value={value} onChange={setValue} /> <NumberField label="Width (uncontrolled)" defaultValue={10} /> </Stack> );}Imperative API
Section titled “Imperative API”This component exposes an imperative API through the apiRef prop. This API allows triggering behaviors that can’t be expressed by props.
import { Button, NumberField, type NumberFieldApi, Stack } from '@cimpress-ui/react';import { useRef } from 'react';
export default function Demo() { const apiRef = useRef<NumberFieldApi>(null);
return ( <Stack gap={16}> <NumberField label="Width" defaultValue={10} apiRef={apiRef} />
<Stack gap={16} direction={{ md: 'horizontal' }}> <Button onPress={() => apiRef.current?.focus()}>Focus</Button> <Button onPress={() => apiRef.current?.select()}>Select text</Button> </Stack> </Stack> );}Accessibility notes
Section titled “Accessibility notes”Number field follows the ARIA spinbutton pattern. See the linked page for a list of available keyboard interactions.
NumberField requires a textual label to remain accessible to assistive technologies. See our accessibility guide for more details.
Value formatting
Section titled “Value formatting”Number field displays its value as a decimal number by default. This can be overridden using the formatOptions prop. This prop has the same syntax as the options parameter of the Intl.NumberFormat constructor, with the following limitations:
notationproperty only supports the'standard'value
The following example demonstrates a number field that displays a percentage value:
import { NumberField } from '@cimpress-ui/react';
export default function Demo() { return ( <NumberField label="Tax" defaultValue={0.05} minValue={0} formatOptions={{ style: 'percent', }} /> );}Number field can integrate with native HTML forms. See our forms guide to learn how to work with forms.
onChange behavior
Section titled “onChange behavior”Unlike TextField, NumberField does not trigger onChange while typing. Instead, it triggers onChange when:
- Using keyboard arrow keys or a mouse scroll wheel to increment/decrement the value
- Pressing the Enter key
- Blurring (leaving) the input
- Clicking the spin buttons
This behavior ensures proper handling of validation constraints like minValue and maxValue. For example, if a user types “-200” when minValue is -100, the value will be clamped to -100 only after one of the above actions occurs, rather than during typing which could interfere with input.
API reference
Section titled “API reference”NumberField
Section titled “NumberField”Allows users to edit a number with a keyboard or increment/decrement buttons.
- Ref<HTMLDivElement>
-
The
reftype for this component.
NumberFieldProps
- number | null
value
Section titled “ value ” -
The current value (controlled). A
nullvalue means an empty field. - number | null
defaultValue
Section titled “ defaultValue ” -
The default value (uncontrolled). A
nullvalue means an empty field. - (value: number | null) => void
onChange
Section titled “ onChange ” -
Handler that is called when the value changes. A
nullvalue means an empty field. - string
-
The element's unique identifier. See MDN.
- boolean
data-cim-style-root
Section titled “ data-cim-style-root ” -
Use this attribute to "claim" the component tree for exclusive Cimpress UI usage.
- string
UNSAFE_className
Section titled “ UNSAFE_className ” -
Sets the CSS className for the element. Only use as a last resort. Use style props instead.
See styling guide.
- CSSProperties
UNSAFE_style
Section titled “ UNSAFE_style ” -
Sets the CSS style for the element. Only use as a last resort. Use style props instead.
See styling guide.
- string
label
Section titled “ label ” -
The content to display as the label.
- string
aria-label
Section titled “ aria-label ” -
Defines a string value that labels the current element.
- string
aria-labelledby
Section titled “ aria-labelledby ” -
Identifies the element (or elements) that labels the current element.
- string
aria-describedby
Section titled “ aria-describedby ” -
Identifies the element (or elements) that describes the object.
- string
aria-details
Section titled “ aria-details ” -
Identifies the element (or elements) that provide a detailed, extended description for the object.
- string
-
The
<form>element to associate the input with. The value of this attribute must be the id of a<form>in the same document. See MDN. - string
-
The name of the input element, used when submitting an HTML form. See MDN.
- string
description
Section titled “ description ” -
A description for the field. Provides a hint such as specific requirements for what to choose.
- FieldError
error
Section titled “ error ” -
An error message for the field.
- (value: number) => string | true | string[] | undefined
validate
Section titled “ validate ” -
A function that returns an error message (or
true) if a given value is invalid. Validation errors are displayed to the user when the form is submitted. For real-time validation, use theerrorprop instead. - string
placeholder
Section titled “ placeholder ” -
The placeholder text displayed in the input field.
- RefObject<NumberFieldApi | null>
apiRef
Section titled “ apiRef ” -
A React ref that allows access to the imperative API of this component.
- NumberFormatOptions
formatOptions
Section titled “ formatOptions ” -
Formatting options for the value displayed in the number field. This also affects what characters are allowed to be typed by the user.
- (e: KeyboardEvent) => void
onKeyDown
Section titled “ onKeyDown ” -
Handler that is called when a key is pressed.
- (e: FocusEvent<Element>) => void
onFocus
Section titled “ onFocus ” -
Handler that is called when the element receives focus.
- (e: FocusEvent<Element>) => void
onBlur
Section titled “ onBlur ” -
Handler that is called when the element loses focus.
- boolean
autoFocus
Section titled “ autoFocus ” -
Whether the element should receive focus on render.
- boolean
isRequired
Section titled “ isRequired ” -
Whether user input is required on the input before form submission.
- boolean
isInvalid
Section titled “ isInvalid ” -
Whether the input value is invalid.
- boolean
isDisabled
Section titled “ isDisabled ” -
Whether the input is disabled.
- boolean
isReadOnly
Section titled “ isReadOnly ” -
Whether the input can be selected but not changed by the user.
- number
minValue
Section titled “ minValue ” -
The smallest value allowed for the input.
- number
maxValue
Section titled “ maxValue ” -
The largest value allowed for the input.
- number
-
The amount that the input value changes with each increment or decrement "tick".
StyleProps
- Responsive<Spacing | "auto">
margin
Section titled “ margin ” -
The amount of margin applied to all edges of this component.
- Responsive<Spacing | "auto">
marginX
Section titled “ marginX ” -
The amount of margin applied to the left and right edges of this component. Takes priority over
margin. - Responsive<Spacing | "auto">
marginY
Section titled “ marginY ” -
The amount of margin applied to the top and bottom edges of this component. Takes priority over
margin. - Responsive<Spacing | "auto">
marginTop
Section titled “ marginTop ” -
The amount of margin applied to the top edge of this component. Takes priority over
marginYandmargin. - Responsive<Spacing | "auto">
marginRight
Section titled “ marginRight ” -
The amount of margin applied to the right edge of this component. Takes priority over
marginXandmargin. - Responsive<Spacing | "auto">
marginBottom
Section titled “ marginBottom ” -
The amount of margin applied to the bottom edge of this component. Takes priority over
marginYandmargin. - Responsive<Spacing | "auto">
marginLeft
Section titled “ marginLeft ” -
The amount of margin applied to the left edge of this component. Takes priority over
marginXandmargin.