Skip to content
Cynosurev0.5.0
SearchStorybookGitHubnpmColour mode: System. Switch to Light

Components / Controls

NumberField

A field for numbers. People type in their own format or step the value with the keyboard or the − and + buttons; your code gets a number.

Spin button with its value

When to useLink to "When to use"

  • Quantities, amounts, limits, measurements: values people may want to nudge up and down.

Use something else when:

  • the value is picked on a scale and the exact number matters less: Slider
  • it is digits that are not a quantity (a phone number, a code, a card number): TextField with inputMode="numeric"

UsageLink to "Usage"

tsx
Copy
import { NumberField } from "@arshad-shah/cynosure";

<NumberField label="Seats" value={seats} onChange={setSeats} min={1} max={50} />

onChange fires when a value is committed: on each step, on Enter, and when leaving the field after typing. It receives a number, or null when the field is emptied.

Formats and localesLink to "Formats and locales"

Numbers are read and written with Intl.NumberFormat for locale (default: the browser's) and formatOptions: currencies, percents, units, fixed digits. People can type group separators, the local decimal mark and a typographic minus. Percents are typed as people say them: 20 means 20%, stored as 0.2.

FormsLink to "Forms"

With name, the form receives the plain number ("1234.5"), whatever the text shows. Reset restores defaultValue; disabled fields are not submitted; required makes an empty field invalid.

Forms get the plain number

PropsLink to "Props"

Every prop of NumberField
PropWhat it doesTypeDefault
labelRequired
Visible label; it names the field.ReactNode
labelHidden
Visually hide the label (it still names the field). Prefer visible labels.boolean
description
Help text shown below the field.ReactNode
error
Error message shown below the field; it marks the field invalid.ReactNode
value
null is empty.number | null
defaultValue
Starting value when uncontrolled; null is empty.number | null
onChange
Called with the new value when it is committed: a step, Enter, or leaving the field.((value: number | null) => void)
min
Smallest value. Typed and stepped values are kept at or above it, and Home jumps to it.number
max
Largest value. Typed and stepped values are kept at or below it, and End jumps to it.number
step
Arrow keys and steppers move by this; values they produce snap to it. Default 1.number1
largeStep
Page Up and Page Down move by this. Default: 10 steps.number10 steps
locale
Locale for reading and writing numbers. Default: the browser's.stringthe browser's
formatOptions
How the value is written: currency, percent, units, digits… (Intl.NumberFormat options).Intl.NumberFormatOptions
hideSteppers
Hide the − and + buttons (the keyboard still steps).boolean
name
Name the plain number is submitted under in a form.string
form
Id of the form it belongs to, when it is not inside that form.string
required
Marks the field as required for form validation.boolean
disabled
Turns off the field and its stepper buttons.boolean
readOnly
The value can be read but not changed.boolean
placeholder
Hint shown while the field is empty.string
size
Size of the field. Default "md".Size"md"
variant
outline or soft (tinted, borderless until focus). Default "outline"."soft" | "outline""outline"
labels
Replace the built-in English text: the stepper button names.Partial<NumberFieldLabels>
scrub
Drag the label sideways to change the value, as design tools do: one step every few pixels, Shift for ten. Escape mid-drag puts it back. The keys and the steppers still work as before.boolean
className
Added to the root element, for layout.string
style
Layout only.CSSProperties
layout
stack puts the label above the control and the description and error below it. split puts the label and description at the start and the control at the end of one row (a settings row); when the field is too narrow for both, the control moves under the text. Default "stack"."stack" | "split""stack"
width
Width of the control, so the controls of a column of fields line up: "sm", "md" and "lg" are the --cy-field-control-width-* tokens (never wider than the field), "full" is the whole field. Default: the control's own width (the whole field in the stacked layout for TextField and the other fields).FieldWidth

Generated from the type definitions in @arshad-shah/cynosure 0.5.0.

Behaviour and accessibilityLink to "Behaviour and accessibility"

  • Role: role="spinbutton", named by its label, with aria-valuenow, aria-valuemin, aria-valuemax and the formatted value as aria-valuetext.
  • Keyboard:
Behaviour and accessibility, table 1
KeyAction
Arrow Up / Arrow DownOne step up or down
Page Up / Page DownlargeStep (10 steps) up or down
Home / EndTo min / max (when set)
EnterCommit the typed text
  • Stepping: stepped values snap to step, counted from min, and never pick up floating-point noise (0.1 + 0.2 is 0.3). The steppers are disabled at the limits.
  • Typing: unreadable text goes back to the last value; values outside min and max are clamped.
  • Steppers: not tab stops (the field is one stop, as in the APG pattern); their hit areas are at least 24px, 44px on touch screens; pressing one keeps focus in the field.
  • Mobile keyboards: inputmode is numeric for whole non-negative numbers and decimal otherwise.
  • Labels: "Decrease" and "Increase" are in labels.

ThemingLink to "Theming"

NumberField uses TextField's tokens (--cy-text-field-*); digits are tabular so columns of numbers line up.

TextField · Slider · Field

These examples run Cynosure 0.5.0 (commit 50ce30b), the version on npm.

Getting started
Theming
Icons
Accessibility
Code editors and highlighters
Using Cynosure without React
Build-tool adapters
Components
Accordion
AppShell
AspectRatio
Breadcrumbs
Calendar
Card
Container
Disclosure
Divider
DocumentViewport
Grid
Inline
PageHeader
PanZoomCanvas
ScrollArea
SideNav
SortableList
SplitPane
Stack
StatusBar
Tabs
TitleBar
Toolbar
Tree
VirtualList
ZoomControls
Announcer
Avatar
Badge
CodeBlock
DescriptionList
EmptyState
ErrorBoundary
Field
Heading
IconTile
Image
Kbd
List
LoadingDots
Notice
ProgressBar
Prose
Skeleton
Stat
Steps
Table
Text
Button
Checkbox
Checklist
ColorField
ColorSwatches
Combobox
CompareSlider
CopyButton
DateField
FileDrop
IconButton
KeyValueEditor
Link
NumberField
Pagination
PathField
RadioGroup
SearchField
SegmentedControl
Select
SelectionFrame
SignaturePad
Slider
Switch
TagInput
TextArea
TextField
ThemeToggle
TimeField
ButtonBase
ChoiceBase
DialogBase
FileInputBase
HiddenValue
Icon
InputBase
LinkBase
LiveRegion
TextAreaBase
ChatMessage
Choice
DataGrid
DeviceFrame
HighlightedText
HotkeyField
ListRow
OptionList
OverlayFrame
QRCode
RowActions
Section
SiteFooter
SiteHeader
SkipLink
SortButton
VisuallyHidden
CommandPalette
ContextMenu
Dialog
FloatingToolbar
HoverCard
Menu
Menubar
Popover
ProgressOverlay
Sheet
Toast
Tooltip
Changelog