A field that allows users to enter a number and increment or decrement the value using stepper buttons.
import { NumberField } from "@opengovsg/oui"<NumberField aria-label="Enter quantity" />The NumberField component provides an accessible way for users to enter numeric values with optional stepper buttons for incrementing and decrementing the value.
If the component does not have a visible label (by passing a label prop), an aria-label or aria-labelledby prop must be passed instead to identify it to assistive technology.
Provide clear instructions to users with labels and descriptions.
Use the inputProps.placeholder to show placeholder text.
Use the hideSteppers prop to hide the increment and decrement buttons.
Add custom content to the start or end of the input field using startContent and endContent props.
Use minValue and maxValue props to restrict the range of acceptable values.
Use the step prop to define the increment/decrement step size.
Use the formatOptions prop to format the number display with internationalization support.
Use the value and onChange props to control the number field value programmatically.
Current value: 25
Combine isInvalid and errorMessage props to show validation errors.
Use the size prop to change the size of the number field.
Use the isDisabled prop to disable the number field.
Use the isReadOnly prop to make the number field read-only.
Use the isRequired prop to mark the field as required.
By default, users can change the value using the mouse wheel. Use isWheelDisabled to prevent this.
The NumberField component supports built-in validation:
minValue and maxValueisRequired to make the field mandatoryvalidate prop for custom validation logic<input type="number"> elementNumberField has the following attributes on the base element, which you can use to style the component based on its state (e.g. group-[data-invalid=true]:bg-red-500):
isInvalid prop.isRequired prop.isReadOnly prop.isDisabled prop.hideSteppers prop.startContent prop.endContent prop.You can customize the NumberField component by passing custom Tailwind CSS classes to the component slots via the classNames prop.
<NumberField
label="Price"
classNames={{
base: "max-w-xs",
input: "text-right",
label: "font-bold",
}}
/>| Prop | Type | Default | Description |
|---|---|---|---|
label | string | - | The label for the number field |
description | string | - | The description text shown below the field |
errorMessage | string | ((validation: ValidationResult) => string) | - | The error message to display when validation fails |
value | number | - | The current value (controlled) |
defaultValue | number | - | The default value (uncontrolled) |
onChange | (value: number) => void | - | Callback fired when the value changes |
minValue | number | - | The minimum allowed value |
maxValue | number | - | The maximum allowed value |
step | number | 1 | The amount to increment/decrement when using steppers |
formatOptions | Intl.NumberFormatOptions | - | Formatting options for number display |
hideSteppers | boolean | false | Whether to hide the stepper buttons |
startContent | React.ReactNode | - | Content to display at the start of the input |
endContent | React.ReactNode | - | Content to display at the end of the input |
size | "xs" | "sm" | "md" | "md" | The size of the component |
isDisabled | boolean | false | Whether the field is disabled |
isReadOnly | boolean | false | Whether the field is read-only |
isRequired | boolean | false | Whether the field is required |
isInvalid | boolean | false | Whether the field should display as invalid |
isWheelDisabled | boolean | false | Whether to disable value changes via mouse wheel |
validate | (value: number) => ValidationError | true | null | undefined | - | Custom validation function |
inputProps | Partial<InputProps> | - | Additional props to pass to the input element |
classNames | SlotsToClasses<NumberFieldSlots> | - | Custom CSS classes for component slots |