NumberField is a form component that allows users to enter numeric values with optional increment/decrement buttons. It extends TextField with number-specific features.
Basic usage
import { NumberField } from 'reshaped';
function Example() {
return (
<NumberField
name="quantity"
increaseAriaLabel="Increase quantity"
decreaseAriaLabel="Decrease quantity"
/>
);
}
Controlled component
import { useState } from 'react';
import { NumberField } from 'reshaped';
function ControlledExample() {
const [value, setValue] = useState(0);
return (
<NumberField
name="quantity"
value={value}
onChange={({ value }) => setValue(value)}
increaseAriaLabel="Increase"
decreaseAriaLabel="Decrease"
/>
);
}
Uncontrolled component
import { NumberField } from 'reshaped';
function UncontrolledExample() {
return (
<NumberField
name="count"
defaultValue={10}
onChange={({ name, value }) => console.log(name, value)}
increaseAriaLabel="Increase count"
decreaseAriaLabel="Decrease count"
/>
);
}
With min and max
import { NumberField } from 'reshaped';
<NumberField
name="age"
min={0}
max={120}
defaultValue={25}
increaseAriaLabel="Increase age"
decreaseAriaLabel="Decrease age"
/>
With step
import { NumberField } from 'reshaped';
<NumberField
name="price"
step={0.01}
min={0}
defaultValue={9.99}
prefix="$"
increaseAriaLabel="Increase price"
decreaseAriaLabel="Decrease price"
/>
With FormControl
import { FormControl, NumberField } from 'reshaped';
function FormExample() {
return (
<FormControl>
<FormControl.Label>Quantity</FormControl.Label>
<NumberField
name="quantity"
min={1}
max={99}
defaultValue={1}
increaseAriaLabel="Increase quantity"
decreaseAriaLabel="Decrease quantity"
/>
<FormControl.Helper>
Select a quantity between 1 and 99.
</FormControl.Helper>
</FormControl>
);
}
Validation
import { FormControl, NumberField } from 'reshaped';
import { useState } from 'react';
function ValidationExample() {
const [value, setValue] = useState(null);
const hasError = value !== null && (value < 1 || value > 10);
return (
<FormControl hasError={hasError}>
<FormControl.Label>Team size</FormControl.Label>
<NumberField
name="teamSize"
value={value}
onChange={({ value }) => setValue(value)}
min={1}
max={10}
increaseAriaLabel="Increase team size"
decreaseAriaLabel="Decrease team size"
/>
<FormControl.Error>
Team size must be between 1 and 10.
</FormControl.Error>
</FormControl>
);
}
Sizes and variants
import { NumberField } from 'reshaped';
<NumberField
size="small"
name="small"
increaseAriaLabel="Increase"
decreaseAriaLabel="Decrease"
/>
<NumberField
variant="faded"
name="faded"
increaseAriaLabel="Increase"
decreaseAriaLabel="Decrease"
/>
Currency input
import { NumberField } from 'reshaped';
import { useState } from 'react';
function CurrencyExample() {
const [value, setValue] = useState(0);
return (
<NumberField
name="budget"
value={value}
onChange={({ value }) => setValue(value)}
min={0}
step={0.01}
prefix="$"
suffix="USD"
increaseAriaLabel="Increase budget"
decreaseAriaLabel="Decrease budget"
/>
);
}
Accessibility
NumberField follows accessibility best practices:
- Uses semantic HTML number input
- Increment/decrement buttons have required ARIA labels
- Keyboard accessible (Arrow up/down to adjust)
- Screen reader announces current value and constraints
- Works with FormControl for proper label associations
Name of the number field input element
aria-label attribute for the increase button
aria-label attribute for the decrease button
Unique identifier for the number field
Value of the form field, enables controlled mode
Default value of the form field, enables uncontrolled mode
Minimum value supported in the form field
Maximum value supported in the form field
Step at which the value will increase or decrease when clicking the button controls
Placeholder text when there is no value
size
'small' | 'medium' | 'large' | 'xlarge'
default:"medium"
Component size. Supports responsive values.
variant
'outline' | 'faded' | 'ghost' | 'headless'
default:"outline"
Component render variant
Disable the number field user interaction
Render in the focused state
Show an error state. Automatically inherited when used inside FormControl.
Icon of the number field at the start position
Node for inserting text content before the number field value
Node for inserting text content after the number field value
Node for inserting content before the number field value
Padding inline end, base unit token number multiplier
onChange
(args: { name: string, value: number }) => void
Callback when the component value is changed
onFocus
(e: React.FocusEvent<HTMLInputElement>) => void
Callback when the number field is focused
onBlur
(e: React.FocusEvent<HTMLInputElement>) => void
Callback when the number field is blurred
Additional classname for the root element
Additional attributes for the root element
Additional attributes for the input element. Use for ARIA attributes.