Skip to content

Number field API

import { NumberField } from '@cimpress-ui/react';

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.

This component exposes an imperative API through the apiRef prop. This API allows triggering behaviors that can’t be expressed by props.

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.

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:

  • notation property only supports the 'standard' value

The following example demonstrates a number field that displays a percentage value:

Number field can integrate with native HTML forms. See our forms guide to learn how to work with forms.

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.

Allows users to edit a number with a keyboard or increment/decrement buttons.

See number field usage guidelines.

Ref<HTMLDivElement>

The ref type for this component.

NumberFieldProps
number | null

The current value (controlled). A null value means an empty field.

number | null

The default value (uncontrolled). A null value means an empty field.

(value: number | null) => void

Handler that is called when the value changes. A null value means an empty field.

string

The element's unique identifier. See MDN.

boolean

Use this attribute to "claim" the component tree for exclusive Cimpress UI usage.

string

Sets the CSS className for the element. Only use as a last resort. Use style props instead.

See styling guide.

CSSProperties

Sets the CSS style for the element. Only use as a last resort. Use style props instead.

See styling guide.

string

The content to display as the label.

string

Defines a string value that labels the current element.

string

Identifies the element (or elements) that labels the current element.

string

Identifies the element (or elements) that describes the object.

string

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

A description for the field. Provides a hint such as specific requirements for what to choose.

FieldError

An error message for the field.

(value: number) => string | true | string[] | undefined

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 the error prop instead.

string

The placeholder text displayed in the input field.

RefObject<NumberFieldApi | null>

A React ref that allows access to the imperative API of this component.

NumberFormatOptions

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

Handler that is called when a key is pressed.

(e: FocusEvent<Element>) => void

Handler that is called when the element receives focus.

(e: FocusEvent<Element>) => void

Handler that is called when the element loses focus.

boolean

Whether the element should receive focus on render.

boolean

Whether user input is required on the input before form submission.

boolean

Whether the input value is invalid.

boolean

Whether the input is disabled.

boolean

Whether the input can be selected but not changed by the user.

number

The smallest value allowed for the input.

number

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">

The amount of margin applied to all edges of this component.

Responsive<Spacing | "auto">

The amount of margin applied to the left and right edges of this component. Takes priority over margin.

Responsive<Spacing | "auto">

The amount of margin applied to the top and bottom edges of this component. Takes priority over margin.

Responsive<Spacing | "auto">

The amount of margin applied to the top edge of this component. Takes priority over marginY and margin.

Responsive<Spacing | "auto">

The amount of margin applied to the right edge of this component. Takes priority over marginX and margin.

Responsive<Spacing | "auto">

The amount of margin applied to the bottom edge of this component. Takes priority over marginY and margin.

Responsive<Spacing | "auto">

The amount of margin applied to the left edge of this component. Takes priority over marginX and margin.