apps/www/content/docs/components/number-input.mdx
import { NumberInput } from "@chakra-ui/react"
<NumberInput.Root>
<NumberInput.Label />
<NumberInput.ValueText />
<NumberInput.Control>
<NumberInput.IncrementTrigger />
<NumberInput.DecrementTrigger />
</NumberInput.Control>
<NumberInput.Scrubber />
<NumberInput.Input />
</NumberInput.Root>
:::info
If you prefer a closed component composition, check out the snippet below.
:::
The NumberInput component provides a set of shortcuts for common use cases
This component renders the NumberInput.IncrementTrigger and
NumberInput.DecrementTrigger within it by default.
Writing this:
<NumberInput.Control />
is shorthand for writing this if you don't need to customize the triggers:
<NumberInput.Control>
<NumberInput.IncrementTrigger />
<NumberInput.DecrementTrigger />
</NumberInput.Control>
Pass the size prop to the NumberInput.Root component to change the size of
the number input.
Pass the formatOptions prop to the NumberInput.Root component to format the
number input value. The value of this maps to Intl.NumberFormatOptions and is
applied based on the current locale.
Pass the min and max props to the NumberInput.Root component to set the
minimum and maximum values of the number input.
If value entered is less than min or greater than max, the value will be
clamped to the nearest boundary on blur, or enter key press.
Pass the step prop to the NumberInput.Root component to change the increment
or decrement interval of the number input.
Pass the value and onValueChange props to the NumberInput.Root component
to control the value of the number input.
Here's an example of how to compose the number input as a mobile stepper.
<ExampleTabs name="number-input-with-stepper" />Pass the allowMouseWheel prop to the NumberInput.Root component to enable or
disable the mouse wheel to change
Pass the disabled prop to the NumberInput.Root component to disable the
number input.
Use the Field component and the invalid prop to indicate that the number
input is invalid.
Compose the Field and Field.HelperText components to add helper text to the
number input.
Here's an example of how to compose the number input with the input group component to add an element on either the left or right.
<ExampleTabs name="number-input-with-element" />Use the NumberInput.Scrubber component to make the number input supports
scrubber interactions.
Here is an example of how to use the NumberInput component with
react-hook-form.
Here's how to setup the Number Input for a closed component composition.
<ExampleCode name="number-input-closed-component" />If you want to automatically add the closed component to your project, run the command:
npx @chakra-ui/cli snippet add number-input
Here's how to use the it
<NumberInputRoot>
<NumberInputField />
</NumberInputRoot>
When controlling the NumberInput component, use string values instead of
converting to numbers. This preserves locale-specific formatting, especially for
currencies with different decimal and thousands separators (e.g., 1.523,30 vs
1,523.30).
const [value, setValue] = useState("0")
<NumberInput.Root
value={value}
onValueChange={(details) => setValue(details.value)}
>
</NumberInput.Root>
If you need a numeric value for form submission, use NumberInput.Context to
access valueAsNumber:
<NumberInput.Root
value={value}
onValueChange={(details) => setValue(details.value)}
>
<NumberInput.Input />
<NumberInput.Context>
{(context) => (
<input type="hidden" name="amount" value={context.valueAsNumber} />
)}
</NumberInput.Context>
</NumberInput.Root>
Explore the NumberInput component parts interactively. Click on parts in the
sidebar to highlight them in the preview.