Number Input
The number input component provides a structured way for users to input numerical data with built-in validation and controls. It combines a text input field with stepper buttons and can include unit selection functionality for measurements, currency, or other quantifiable values.
import {NumberInput} from "@qualcomm-ui/react/number-input"Examples
Simple
The simple API bundles all subcomponents together into a single component.
<NumberInput className="w-72" label="Label" placeholder="Enter a number" />
Composite
The composite pattern provides full control over the component's elements by composing individual subcomponents.
<NumberInput.Root className="w-72">
<NumberInput.Label>Label</NumberInput.Label>
<NumberInput.InputGroup>
<NumberInput.Input placeholder="Enter a number" />
<NumberInput.Control />
<NumberInput.ErrorIndicator />
</NumberInput.InputGroup>
<NumberInput.ErrorText>Error</NumberInput.ErrorText>
</NumberInput.Root>
Min and Max values
Pass the min and max props to the root component to set the minimum and maximum values of the number input.
If the value entered is less than min or greater than max, it will be clamped to the nearest boundary on blur or enter.
<NumberInput
aria-label="input min-max demo"
className="w-72"
defaultValue="5"
max={10}
min={5}
/>
Step interval
Pass the step prop to the root component to set the increment or decrement step interval.
<NumberInput
aria-label="Steps demo"
className="w-72"
placeholder="Enter a number"
step={3}
/>
States
The following shows how the TextInput component appears in each interactive state.
<NumberInput disabled label="Disabled" placeholder="Disabled" />
<NumberInput label="Read only" placeholder="Read only" readOnly />
<NumberInput label="Required" placeholder="Required" required />
<NumberInput
errorText="Invalid"
invalid
label="Invalid"
placeholder="Invalid"
/>
Controlled Value
Use the value, onValueChange and defaultValue props to control the value of the input field. These props follow our controlled state pattern.
import {type ReactElement, useState} from "react"
import {NumberInput} from "@qualcomm-ui/react/number-input"
export function NumberInputControlledDemo(): ReactElement {
const [value, setValue] = useState<string>("")
return (
<NumberInput
className="w-72"
label="Controlled Value"
onValueChange={({value}) => setValue(value)}
placeholder="Placeholder"
value={value}
/>
)
}Sizes
Customize size using the size prop. The default size is md.
<NumberInput
aria-label="sm demo"
className="w-56"
placeholder="sm"
size="sm"
startIcon={Sigma}
/>
<NumberInput
aria-label="md demo"
className="w-64"
placeholder="md"
startIcon={Sigma}
/>
<NumberInput
aria-label="lg demo"
className="w-72"
placeholder="lg"
size="lg"
startIcon={Sigma}
/>
Error Text and Indicator
Error messages are displayed using two props:
The error text and indicator will only render when invalid is true.
<NumberInput
className="w-72"
errorText="You must enter a value"
invalid={!value}
label="Label"
onValueChange={({value}) => setValue(value)}
placeholder="Enter a value"
value={value}
/>
API
<NumberInput.Root>
| Prop | Type | Default |
|---|---|---|
Whether to allow mouse wheel to change the value | boolean | |
Whether to allow the value to overflow the min/max range | boolean | true |
Whether to clamp the value when the input loses focus (blur) | boolean | true |
The initial value of the input when rendered.
Use when you don't need to control the value of the input. | string | |
The document's text/writing direction. | 'ltr' | 'rtl' | 'ltr' |
Whether the input is disabled. When true, prevents user interaction and
applies visual styling to indicate the disabled state. | boolean | |
| LucideIcon | ||
Whether to focus the input when the value changes | boolean | true |
The associate form of the input element. | string | |
The options to pass to the Intl.NumberFormat constructor | NumberFormatOptions | |
A root node to correctly resolve document in custom environments. i.e.,
Iframes, Electron. | () => | |
The ids of the elements that are associated with the input. These will be
automatically generated if omitted. | Partial<{ | |
Hints at the type of data that might be entered by the user. It also determines
the type of keyboard shown to the user on mobile devices | | 'text' | "decimal" |
Controls the visual error state of the input. When true, applies semantic error
styling to indicate validation failure. | boolean | |
The current locale. Based on the BCP 47 definition. | string | 'en-US' |
The maximum value of the number input | number | Number.MAX_SAFE_INTEGER |
The minimum value of the number input | number | Number.MIN_SAFE_INTEGER |
The name attribute of the number input. Useful for form submission. | string | |
Function invoked when the number input is focused | ( | |
Function invoked when the value changes | ( | |
Function invoked when the value overflows or underflows the min/max range | ( | |
The pattern used to check the element's value against | string | "[0-9]*(.[0-9]+)?" |
Whether the input is read-only. When true, prevents user interaction while
keeping the input focusable and visible. | boolean | |
Allows you to replace the component's HTML element with a different tag or component. Learn more | | ReactElement | |
Whether the input is required. When true, the input must have a value for form
validation to pass. | boolean | |
The size of the input field and its elements. Governs properties like font size,
item padding, and icon sizes. | | 'sm' | 'md' |
Whether to spin the value when the increment/decrement button is pressed | boolean | true |
| LucideIcon | ||
Amount to increment/decrement the value when using stepper buttons or arrow keys. | number | 1 |
Specifies the localized strings that identify the accessibility elements and
their states | { | |
The controlled value of the input | string |
booleanbooleanbooleanstring'ltr' | 'rtl'
booleanbooleanstringNumberFormatOptionsIntl.NumberFormat constructor() =>
| Node
| ShadowRoot
| Document
Partial<{
decrementTrigger: string
errorText: string
hint: string
incrementTrigger: string
input: string
label: string
}>
| 'text'
| 'tel'
| 'numeric'
| 'decimal'
booleanstringnumbernumberstring(
details: NumberInputFocusChangeDetails,
) => void
(
details: NumberInputValueChangeDetails,
) => void
(
details: NumberInputValueInvalidDetails,
) => void
stringboolean| ReactElement
| ((
props: object,
) => ReactElement)
boolean| 'sm'
| 'md'
| 'lg'
boolean| LucideIcon
| ReactNode
number{
decrementLabel?: string
incrementLabel?: string
valueText?: (
value: string,
) => string
}
string<NumberInput>
The NumberInput component supports all props from NumberInput.Root, plus the additional props listed below.
| Prop | Type |
|---|---|
Props applied to the control element. | { |
Props applied to the decrement trigger button. | { |
string | |
Props applied to the error text element. | { |
Optional hint describing the element. This element is automatically
associated with the component's input element for accessibility. | |
Props applied to the label element. | { |
Props applied to the increment trigger button. | { |
Props applied to the input group element. | { |
Props applied to the input element. | any |
Optional label describing the element. Recommended. This element is
automatically associated with the component's input element for
accessibility. | |
Props applied to the label element. | { |
HTML placeholder attribute,
passed to the underlying input element. | string |
{
children?: ReactNode
render?:
| Element
| ((
props: Props,
) => Element)
}
{
render?:
| Element
| ((
props: Props,
) => Element)
}
{
icon?:
| LucideIcon
| ReactNode
render?:
| Element
| ((
props: Props,
) => Element)
}
{
id?: string
render?:
| Element
| ((
props: Props,
) => Element)
}
{
render?:
| Element
| ((
props: Props,
) => Element)
}
{
render?:
| Element
| ((
props: Props,
) => Element)
}
any{
children?: ReactNode
id?: string
required?: boolean
}
string