NumberInput
NumberInput is a component used to obtain numeric input from the user.
<NumberInput placeholder="placeholder" />
Usage
import { NumberInput } from "@yamada-ui/react"
import { NumberInput } from "@/components/ui"
import { NumberInput } from "@workspaces/ui"
<NumberInput />
Change Variant
<VStack>
<For each={["outline", "filled", "flushed"]}>
{(variant) => (
<NumberInput
key={variant}
variant={variant}
placeholder={toTitleCase(variant)}
/>
)}
</For>
</VStack>
Change Size
<VStack>
<For each={["xs", "sm", "md", "lg", "xl"]}>
{(size) => (
<NumberInput key={size} size={size} placeholder={`Size (${size})`} />
)}
</For>
</VStack>
Set Default Value
To set a default value, set a string or number to defaultValue.
<NumberInput defaultValue={18} placeholder="Order Quantity" />
Set Minimum and Maximum Values
To set minimum and maximum values, set a number to min or max.
<NumberInput defaultValue={18} min={8} max={31} placeholder="Order Quantity" />
Set Step Value
To set a step value, set a number to step.
<NumberInput
defaultValue={15}
step={5}
min={5}
max={30}
placeholder="Order Quantity"
/>
Set Decimal Points
To set decimal points, set a number to precision.
<NumberInput
defaultValue={15}
precision={2}
step={0.2}
placeholder="Order Quantity"
/>
Disable Clamping Value on Blur
By default, the component accepts any numeric input from the user, but values exceeding min or max are automatically adjusted on blur. To disable this automatic adjustment, set clampValueOnBlur to false.
<NumberInput
defaultValue={15}
max={30}
clampValueOnBlur={false}
placeholder="Order Quantity"
/>
Allow Out-of-Range Values
To allow out-of-range values, set keepWithinRange to false.
<NumberInput
defaultValue={15}
max={30}
keepWithinRange={false}
clampValueOnBlur={false}
placeholder="Order Quantity"
/>
Disable
To disable the component, set disabled to true.
<VStack>
<For each={["outline", "filled", "flushed"]}>
{(variant) => (
<NumberInput
key={variant}
disabled
variant={variant}
placeholder={toTitleCase(variant)}
/>
)}
</For>
</VStack>
Read-Only
To make the component read-only, set readOnly to true.
<VStack>
<For each={["outline", "filled", "flushed"]}>
{(variant) => (
<NumberInput
key={variant}
readOnly
variant={variant}
placeholder={toTitleCase(variant)}
/>
)}
</For>
</VStack>
Invalid
To make invalid, set invalid to true.
<VStack>
<For each={["outline", "filled", "flushed"]}>
{(variant) => (
<NumberInput
key={variant}
invalid
variant={variant}
placeholder={toTitleCase(variant)}
/>
)}
</For>
</VStack>
Change Border Color
To change the border color, set a color to focusBorderColor or errorBorderColor.
<VStack>
<NumberInput
focusBorderColor="green.500"
placeholder="Custom focus border color"
/>
<NumberInput
errorBorderColor="orange.500"
invalid
placeholder="Custom invalid border color"
/>
</VStack>
Change Placeholder Color
To change the placeholder color, set _placeholder to the value.
<NumberInput
placeholder="Custom placeholder"
_placeholder={{ color: "blue.500" }}
/>
Customize Stepper
To customize the stepper, set props to incrementProps or decrementProps.
<NumberInput
placeholder="Order Quantity"
incrementProps={{ children: <PlusIcon /> }}
decrementProps={{ children: <MinusIcon /> }}
/>
Customize Component
To customize the component, use useNumberInput.
const { getInputProps, getIncrementProps, getDecrementProps } = useNumberInput({
step: 0.01,
defaultValue: 3.14,
min: 3,
max: 4,
precision: 2,
})
return (
<HStack maxW="xs" gap="sm">
<IconButton
icon={<PlusIcon fontSize="2xl" />}
{...getIncrementProps()}
aria-label="Increment"
/>
<Input {...getInputProps()} aria-label="Number Input" />
<IconButton
icon={<MinusIcon fontSize="2xl" />}
{...getDecrementProps()}
aria-label="Decrement"
/>
</HStack>
)
"use client" to the top of the file.Control
const [value, setValue] = useState(18)
return (
<NumberInput
value={value}
onChange={(_, valueAsNumber) => setValue(valueAsNumber)}
placeholder="Order Quantity"
/>
)
"use client" to the top of the file.Props
Accessibility
The NumberInput follows the WAI-ARIA - Spinbutton Pattern for accessibility.
If you are not using Field.Root, set aria-label or aria-labelledby to NumberInput.
<NumberInput aria-label="Age" />
<VStack gap="sm">
<Text as="h3" id="label">
Age
</Text>
<NumberInput aria-labelledby="label" />
</VStack>
Keyboard Navigation
| Key | Description | State |
|---|---|---|
ArrowUp | Increases the value based on step value. | - |
ArrowDown | Decreases the value based on step value. | - |
Home | Sets to min value. | - |
End | Sets to max value. | - |
Shift + ArrowUp | Increases the value by 10x the step. | - |
Shift + ArrowDown | Decreases the value by 10x the step. | - |
Meta + ArrowUp, Ctrl + ArrowUp | Increases the value by 0.1x the step. | - |
Meta + ArrowDown, Ctrl + ArrowDown | Decreases the value by 0.1x the step. | - |
ARIA Roles and Attributes
| Component | Roles and Attributes | Usage |
|---|---|---|
NumberInput | role="spinbutton" | Indicates that this is a spinbutton. |
aria-valuemin | Sets to min value. | |
aria-valuemax | Sets to max value. | |
aria-valuenow | Sets to current value. | |
aria-valuetext | Sets to current value. | |
aria-invalid | Sets to "true" if invalid is set or if the value is out of range. | |
aria-describedby | If NumberInput is within a Field.Root and Field.Root has an errorMessage, helperMessage, or a Field.ErrorMessage, Field.HelperMessage, sets its id. | |
aria-disabled | Sets to "true" if disabled is set. | |
aria-readonly | Sets to "true" if readOnly is set. | |
aria-required | Sets to "true" if required is set. | |
NumberInputIncrementButton | aria-label | Sets to "Increase". |
NumberInputDecrementButton | aria-label | Sets to "Decrease". |
Similar Components
Editable
Editable is a component used to obtain inline editable text input.
Input
Input is a component used to obtain text input from the user.
PasswordInput
PasswordInput is a component that allows users to input passwords with a visibility toggle.
PinInput
PinInput is a component used to capture pin codes or OTP (One-Time Password) inputs.
Textarea
Textarea is a component used to obtain multi-line text input.
FileInput
FileInput is a component used for users to select files.
Autocomplete
Autocomplete is a component used to display suggestions in response to user text input.
Checkbox
Checkbox is a component used for allowing users to select multiple values from multiple options.
Uses Components & Hooks
Button
Button is an interactive component that allows users to perform actions such as submitting forms and toggling modals.
Input
Input is a component used to obtain text input from the user.
Group
Group is a component that groups and attaches multiple elements together.
Icon
Icon is a general icon component that can be used in your projects.
Field
Field is a component used to group form elements with label, helper message, error message, etc.