Skip to main content

Common Examples

import { NumberField } from "@servicetitan/anvil2";

function ExampleComponent() {
  return <NumberField label="Quantity" />;
}

Default values

A default value can given to NumberField to use the field in an uncontrolled mode.

Controlled usage

The NumberField can be controlled. This means you can maintain the state outside the component. Listen for new values with onChange and supply state back into the value prop.

Empty

The NumberField allows not only 0, but also the ability for the input to be completely cleared out. This is represented by null in the onChange, value, and defaultValue props.

Step value

The increment and decrement functionality of the NumberField defaults to adding or subtracting 1, but can be modified to add or subtract another number. This value applies to the visual +/- buttons as well as keyboard up/down keys.Note that step does not enforce that the value is a multiple of the step. Instead, it will add to or subtract from the current value.Additionally, the increment and decrement is clamped between the minimum and maximum values. This means if value + step > maxValue, the result of incrementing will be maxValue. Similarly, if value - step < minValue, the result will be minValue.

Negative values

The NumberField defaults to a minimum of 0, but supports negative number inputs by setting a lower minValue.

Fractional values

The NumberField defaults to integers, but supports decimal number inputs with the maximumFractionDigits and minimumFractionDigits props.Be careful! Forgetting to set minimumFractionDigits or maximumFractionDigits can result in your numbers being rounded unexpectedly.
Caution

Errors

Error messages can be applied to the component via the error prop.The component does not do validation internally. For example, it may be possible for a user to enter a value outside the minValue/maxValue without knowing it unless you provide an error message.
Caution
Last modified on January 23, 2026