# Number field API

> Explore the API of Cimpress UI's number field component.

Source: https://ui.cimpress.io/components/number-field/api/

## Import

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

## Controlled/uncontrolled usage

```tsx
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

```tsx
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

Number field follows the [ARIA spinbutton pattern](https://www.w3.org/WAI/ARIA/apg/patterns/spinbutton/). 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](/dev-guides/accessibility#labelling) for more details.

## 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](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Intl/NumberFormat/NumberFormat#options), with the following limitations:

- `notation` property only supports the `'standard'` value

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

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

export default function Demo() {
  return (
    <NumberField
      label="Tax"
      defaultValue={0.05}
      minValue={0}
      formatOptions={{
        style: 'percent',
      }}
    />
  );
}
```

## Forms

Number field can integrate with native HTML forms. See our [forms guide](/dev-guides/forms) to learn how to work with forms.

## `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

### NumberField

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

See [number field usage guidelines](https://ui.cimpress.io/components/number-field/).

- `ref`: `Ref<HTMLDivElement>`

  The `ref` type for this component.

**Props: `NumberFieldProps`**

- `value`: `number | null`

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

- `defaultValue`: `number | null`

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

- `onChange`: `(value: number | null) => 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](https://developer.mozilla.org/en-US/docs/Web/HTML/Global_attributes/id).

- `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](https://ui.cimpress.io/dev-guides/styling/).

- `UNSAFE_style`: `CSSProperties`

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

  See [styling guide](https://ui.cimpress.io/dev-guides/styling/).

- `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.

- `form`: `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](https://developer.mozilla.org/en-US/docs/Web/HTML/Reference/Elements/input#form).

- `name`: `string`

  The name of the input element, used when submitting an HTML form. See [MDN](https://developer.mozilla.org/en-US/docs/Web/HTML/Reference/Elements/input#name).

- `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) => 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.

- `placeholder`: `string`

  The placeholder text displayed in the input field.

- `apiRef`: `RefObject<NumberFieldApi | null>`

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

- `onKeyDown`: `(e: KeyboardEvent) => void`

  Handler that is called when a key is pressed.

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

- `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.

**Props: `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` and `margin`.

- `marginRight`: `Responsive<Spacing | "auto">`

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

- `marginBottom`: `Responsive<Spacing | "auto">`

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

- `marginLeft`: `Responsive<Spacing | "auto">`

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