Number Input
A field for quantities, prices and measurements, with step buttons, locale formatting and drag-to-scrub. Built on ui-sdk’s Number Input.
Usage
import {
NumberInput,
NumberInputControl,
NumberInputDecrement,
NumberInputGroup,
NumberInputIncrement,
} from 'nyctoui/number-input'
<NumberInput defaultValue={1} min={0}>
<NumberInputGroup>
<NumberInputControl aria-label="Quantity" />
<NumberInputDecrement />
<NumberInputIncrement />
</NumberInputGroup>
</NumberInput>The group draws the same box as InputGroup, so a number field lines up with the inputs and buttons beside it. The step buttons sit inside it in the order you write them, repeat while held, and stay out of the tab order: Arrow Up and Arrow Down do the same from the input, Shift for ten steps. The value is a number, or null while the field is empty.
Good for
- Quantity in a shopping cart or a stock adjustment form.
- Price and discount fields in an invoicing app, formatted as currency.
- Width, height and opacity in a design tool’s inspector, with drag to scrub.
- Guest count on a restaurant reservation.
Anatomy
<NumberInput>
<NumberInputScrubArea />
<NumberInputGroup>
<NumberInputDecrement />
<NumberInputControl />
<NumberInputIncrement />
</NumberInputGroup>
</NumberInput>Examples
Format
format takes Intl.NumberFormat options. The field shows and reads that format while the value stays a plain number; percentages are fractions, so 15% is 0.15.
Scrub
Drag a NumberInputScrubArea sideways to change the value, as in a design tool. Here a one-letter InputAddon inside the group is the handle; it can also wrap the field’s label.
Sizes
The same 24, 32 and 40px heights as Input and Button.
Disabled
Best practices
- Set
min,maxandstepso the buttons and arrow keys land on valid values. - Show the unit with
formator beside the field, such as “%”, “GB” or a currency. - Turn off
wheelin a panel that scrolls, so scrolling the page doesn’t change the value.
- Use a number input for digits that aren’t a quantity, like a phone number, a ZIP code or a one-time code. Use a text input.
- Use it for a value people pick roughly, such as volume. A slider is easier.
API reference
NumberInput
Holds the value. Renders a <div>.
| Prop | Type | Default |
|---|---|---|
sizeControl height for the group, the input and the step buttons. | 'sm' | 'md' | 'lg' | 'md' |
defaultValueThe value at first, for uncontrolled use. | number | null | — |
valueThe value, for controlled use. | number | null | — |
onValueChangeCalled as a typed number becomes valid, and on every step. | (value, details) => void | — |
onValueCommitCalled once an interaction ends: Enter, blur, a released button or the end of a scrub. For saving. | (value, details) => void | — |
min / maxBounds. Typed values outside them are clamped when committed. | number | — |
stepOne step. Committed values snap to it. | number | 1 |
largeStepShift, Page Up and Page Down. | number | step × 10 |
formatHow the value is shown and read back. | Intl.NumberFormatOptions | — |
localeLocale for formatting and parsing. Defaults to the page’s lang. | string | — |
wheelLets the mouse wheel step the focused field. | boolean | false |
disabled / readOnly / requiredAs on a native input. | boolean | — |
nameSubmits the plain number under this name. | string | — |
sxStyleX styles merged after the built-in ones. | StyleXStyles | — |
NumberInputGroup
The field’s box around the input and step buttons. Renders a <div>.
| Prop | Type | Default |
|---|---|---|
renderReplaces the rendered element. | ReactElement | function | — |
sxStyleX styles merged after the built-in ones. | StyleXStyles | — |
NumberInputControl
The text input, with tabular digits. Renders an <input>.
| Prop | Type | Default |
|---|---|---|
placeholderShown while empty. | string | — |
sxStyleX styles merged after the built-in ones. | StyleXStyles | — |
NumberInputDecrement / NumberInputIncrement
Ghost icon buttons that step the value and repeat while held. Dimmed at the bound.
| Prop | Type | Default |
|---|---|---|
aria-labelNames the button. | string | 'Decrease' / 'Increase' |
childrenReplaces the minus or plus glyph. | ReactNode | — |
sxStyleX styles merged after the built-in ones. | StyleXStyles | — |
NumberInputScrubArea
Changes the value while dragged, and draws a cursor of its own while it does. Renders a <span>.
| Prop | Type | Default |
|---|---|---|
orientationWhich way to drag. Right and up increase. | 'horizontal' | 'vertical' | 'horizontal' |
pixelsPerStepHow far the pointer moves for one step. | number | — |
sxStyleX styles merged after the built-in ones. | StyleXStyles | — |