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> );}
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:
notation
property 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', }} /> );}
API reference
Section titled “API reference”NumberField
Section titled “NumberField”Allows users to edit a number with a keyboard or increment/decrement buttons.
- ref Ref<HTMLDivElement>
-
The
ref
type for this component.
NumberFieldProps
- value null | number
-
The current value (controlled). A
null
value means an empty field. - defaultValue null | number
-
The default value (uncontrolled). A
null
value means an empty field. - onChange (value: null | number) => void
-
Handler that is called when the value changes. A
null
value means an empty field. - id string
-
The element's unique identifier. See MDN.
- data-cim-style-root boolean
-
Use this attribute to "claim" the component tree for exclusive Cimpress UI usage.
- UNSAFE_className string
-
Sets the CSS className for the element. Only use as a last resort. Use style props instead.
See styling guide.
- UNSAFE_style CSSProperties
-
Sets the CSS style for the element. Only use as a last resort. Use style props instead.
See styling guide.
- label string
-
The content to display as the label.
- aria-label string
-
Defines a string value that labels the current element.
- aria-labelledby string
-
Identifies the element (or elements) that labels the current element.
- aria-describedby string
-
Identifies the element (or elements) that describes the object.
- aria-details string
-
Identifies the element (or elements) that provide a detailed, extended description for the object.
- name string
-
The name of the input element, used when submitting an HTML form. See MDN.
- description string
-
A description for the field. Provides a hint such as specific requirements for what to choose.
- error FieldError
-
An error message for the field.
- validate (value: number) => undefined | string | true | string[]
-
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 theerror
prop instead. - placeholder string
-
The placeholder text displayed in the input field.
- formatOptions NumberFormatOptions
-
Formatting options for the value displayed in the number field. This also affects what characters are allowed to be typed by the user.
- onFocus (e: FocusEvent<Element>) => void
-
Handler that is called when the element receives focus.
- onBlur (e: FocusEvent<Element>) => void
-
Handler that is called when the element loses focus.
- autoFocus boolean
-
Whether the element should receive focus on render.
- isRequired boolean
-
Whether user input is required on the input before form submission.
- isInvalid boolean
-
Whether the input value is invalid.
- isDisabled boolean
-
Whether the input is disabled.
- isReadOnly boolean
-
Whether the input can be selected but not changed by the user.
- minValue number
-
The smallest value allowed for the input.
- maxValue number
-
The largest value allowed for the input.
- step number
-
The amount that the input value changes with each increment or decrement "tick".
StyleProps
- margin Responsive<Spacing | "auto">
-
The amount of margin applied to all edges of this component.
- marginX Responsive<Spacing | "auto">
-
The amount of margin applied to the left and right edges of this component. Takes priority over
margin
. - marginY Responsive<Spacing | "auto">
-
The amount of margin applied to the top and bottom edges of this component. Takes priority over
margin
. - marginTop Responsive<Spacing | "auto">
-
The amount of margin applied to the top edge of this component. Takes priority over
marginY
andmargin
. - marginRight Responsive<Spacing | "auto">
-
The amount of margin applied to the right edge of this component. Takes priority over
marginX
andmargin
. - marginBottom Responsive<Spacing | "auto">
-
The amount of margin applied to the bottom edge of this component. Takes priority over
marginY
andmargin
. - marginLeft Responsive<Spacing | "auto">
-
The amount of margin applied to the left edge of this component. Takes priority over
marginX
andmargin
.