Components

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

Do
  • Set min, max and step so the buttons and arrow keys land on valid values.
  • Show the unit with format or beside the field, such as “%”, “GB” or a currency.
  • Turn off wheel in a panel that scrolls, so scrolling the page doesn’t change the value.
Don’t
  • 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>.

PropTypeDefault
size
Control height for the group, the input and the step buttons.
'sm' | 'md' | 'lg''md'
defaultValue
The value at first, for uncontrolled use.
number | null—
value
The value, for controlled use.
number | null—
onValueChange
Called as a typed number becomes valid, and on every step.
(value, details) => void—
onValueCommit
Called once an interaction ends: Enter, blur, a released button or the end of a scrub. For saving.
(value, details) => void—
min / max
Bounds. Typed values outside them are clamped when committed.
number—
step
One step. Committed values snap to it.
number1
largeStep
Shift, Page Up and Page Down.
numberstep × 10
format
How the value is shown and read back.
Intl.NumberFormatOptions—
locale
Locale for formatting and parsing. Defaults to the page’s lang.
string—
wheel
Lets the mouse wheel step the focused field.
booleanfalse
disabled / readOnly / required
As on a native input.
boolean—
name
Submits the plain number under this name.
string—
sx
StyleX styles merged after the built-in ones.
StyleXStyles—

NumberInputGroup

The field’s box around the input and step buttons. Renders a <div>.

PropTypeDefault
render
Replaces the rendered element.
ReactElement | function—
sx
StyleX styles merged after the built-in ones.
StyleXStyles—

NumberInputControl

The text input, with tabular digits. Renders an <input>.

PropTypeDefault
placeholder
Shown while empty.
string—
sx
StyleX 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.

PropTypeDefault
aria-label
Names the button.
string'Decrease' / 'Increase'
children
Replaces the minus or plus glyph.
ReactNode—
sx
StyleX 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>.

PropTypeDefault
orientation
Which way to drag. Right and up increase.
'horizontal' | 'vertical''horizontal'
pixelsPerStep
How far the pointer moves for one step.
number—
sx
StyleX styles merged after the built-in ones.
StyleXStyles—