Number input
A number input takes a number: a quantity, an amount, a percentage. Carbon's − and + buttons sit at the end of the field, and ↑ / ↓ step from the keyboard.
Installation
npm gives you every component from the package. shadcn copies the source into your app instead: a copy pinned to this version that you can change. The shadcn commands need the @kanok-ui registry in your components.json once, see Installation.
Use it
- The value is text, so the field can be empty:
"12","0.5", or"". The element'svalueAsNumbergives the number (NaNwhile empty). - The element fires
inputon every keystroke or step, andchangewhen the value is committed (leaving the field, Enter, a step). The React wrapper hasonInputandonChange. - The React + Tailwind
onChangegets the new value as text, not an event.
Step, minimum and maximum
step (1 by default) is how far − / + and ↑ / ↓ move. A step stays within min and max, and the button at the end of the range turns off. A value typed outside the range is an error.
From an empty field, the first step goes to step-start-value if you set one; otherwise to min when only a minimum is set, else to 1 or −1, as in Carbon.
Decimals
A step rounds to the decimals of the value or of step, whichever has more, so 0.1 + 0.2 shows 0.3, not 0.30000000000000004. With the default type="number", step is also the precision the browser accepts: with step="0.01", 12.345 is an error.
Currency, units and number formats
prefix-text and suffix-text put text inside the field, before or after the number: ฿, kg, baht a month. They stay in view while the user types and aren't part of the value. They have no Carbon counterpart.
For Carbon's formatted field, set type="text" with format-options, which takes any Intl.NumberFormat options: thousands separators, currency, percent, units. The text is free while the user types and is formatted when they leave. The value stays a plain number: ฿1,234.50 submits 1234.5, and a percent style gives the fraction (0.5 for 50%). The locale is the page language, or locale.
Percent and units
With format-options, the percent sign or the unit is part of the formatted text, which is Carbon's way. A percent style stores the fraction: 7.5% is 0.075, so give step, min and max as fractions too. locale picks the number format when it must not follow the page.
Empty fields
As in Carbon, an empty number input counts as missing, even without required: the form won't submit until there is a number. Set allow-empty on a field that may stay empty.
Steppers and the mouse wheel
hide-steppersremoves the − / + buttons; ↑ / ↓ still step.disable-wheelstops the mouse wheel from changing a focused field, which is easy to do by accident while scrolling a long form.decrement-labelandincrement-labelrename the buttons for screen readers ("Decrement number" and "Increment number" by default, translated).
Sizes, messages, read-only and disabled
size:sm32px,md40px (the default),lg48px.helper-text,warn+warn-textandinvalid+invalid-textwork as on the text input. A value outsidemin/maxgets the browser's or the built-in message unless you setinvalid-text. More in Forms and validation.readonlyshows and submits the number, and the steppers turn off.disabledgreys it out; it isn't submitted.
Fluid
fluid (Carbon's FluidNumberInput), or a <ui-fluid-form> around the field, puts the label inside a 64px box; an error or a warning shows inside the box, and there is no helper text. See fluid forms.
Outlined (Kanok design)
In the Kanok design, fields are filled by default: a soft grey fill, no edge. Put data-field-variant="outlined" on an element around the field (a <form>, or <html> for the whole app) for a 1px edge on no fill; focus is still the soft blue halo. The attribute does nothing in the Carbon design, so the example below sits inside data-design="kanok". See Kanok design for the details.
Work out a total
Listen to input to update a total on every keystroke and every step. valueAsNumber gives the number, or NaN while the field is empty.
Total:
In a form
With a name, the field submits a plain number: the formatted rent ฿18,500.00 below submits 18500. A value outside min / max, or an empty field without allow-empty, blocks the submit and shows its error. Try 72 months.
Sent:
Skeleton
While the data of a form loads, <ui-number-input-skeleton> keeps the field's place: a label bar and a field bar at its size, or a fluid box with fluid. Screen readers skip it, so say "loading" on the form (for example aria-busy="true").
Keyboard
| Key | What it does |
|---|---|
| ↑ / ↓ | One step up or down, within min / max |
| Enter | Commits the value and submits the form the field sits in |
The − / + buttons are out of the Tab order, as in Carbon: the arrow keys do the same from the field.
Accessibility
- The label is a real
<label>for the inner<input>; the prefix, the suffix and the message line are its description. - The − / + buttons have names ("Decrement number", "Increment number") and turn off at the end of the range.
inputmode="decimal"brings up a keyboard with a decimal point on phones.
Unstyled React parts with the same behaviour: Headless Number input.
API
Generated from the package. In React, pass a property as a prop of the same name; events are props too (the React column).
<ui-number-input> · React UiNumberInput
Attributes
| Attribute | Property | Type | Default | Description |
|---|---|---|---|---|
label |
label |
string |
'' |
Visible label text; the label slot replaces it when markup is needed. |
hide-label |
hideLabel |
boolean |
false |
Keep the label for screen readers but don't show it. |
helper-text |
helperText |
string |
'' |
Helper text under the field, shown while there is no error or warning. |
invalid-text |
invalidText |
string |
'' |
Error text shown while invalid or while the field fails validation. |
invalid |
invalid |
boolean |
false |
Forces the error state, e.g. after server-side validation. |
warn |
warn |
boolean |
false |
Non-blocking warning; shown when there is no error. |
warn-text |
warnText |
string |
'' |
Text of the warning shown while warn. |
value |
value |
string |
'' |
The field text; valueAsNumber gives the parsed number (NaN while empty or invalid). |
name |
name |
string |
'' |
Reflected: form-associated elements submit under the name attribute. |
placeholder |
placeholder |
string |
'' |
|
prefix-text |
prefixText |
string |
'' |
Text inside the field before the value, e.g. a currency (฿); not part of value. Part of the field's description for screen readers. |
suffix-text |
suffixText |
string |
'' |
Text inside the field after the value, e.g. a unit (kg); not part of value. Part of the field's description for screen readers. |
min |
min |
number | undefined |
||
max |
max |
number | undefined |
||
step |
step |
number |
1 |
Step of the ± buttons and ↑ / ↓; with type="number" also the allowed precision. |
step-start-value |
stepStartValue |
number |
0 |
Where the steppers start from an empty field (Carbon stepStartValue); 0 = Carbon's rule: ±1, or min when only a minimum is set. |
type |
type |
NumberInputType |
'number' |
number: a native number field. text: a text field formatted for locale with format-options (Carbon type="text"). |
locale |
locale |
string |
'' |
BCP 47 locale of the formatting with type="text"; empty = the page language (lang on the element, an ancestor or <html>), as ui-date-picker. |
format-options |
formatOptions |
Intl.NumberFormatOptions |
{} |
Intl.NumberFormat options with type="text", e.g. {"style":"currency","currency":"THB"} or {"maximumFractionDigits":2}. |
allow-empty |
allowEmpty |
boolean |
false |
An empty field is valid (Carbon allowEmpty); otherwise empty counts as missing. |
hide-steppers |
hideSteppers |
boolean |
false |
Remove the − / + buttons (keyboard stepping still works). |
disable-wheel |
disableWheel |
boolean |
false |
Ignore mouse-wheel changes while focused (Carbon disableWheel). |
size |
size |
NumberInputSize |
'md' |
Field height: sm 32px, md 40px, lg 48px (tokens --ui-field-height-*). |
disabled |
disabled |
boolean |
false |
Disables the control; a disabled <fieldset> ancestor does the same. |
readonly |
readonly |
boolean |
false |
Shows and submits the value but blocks changes. |
fluid |
fluid |
boolean |
false |
Carbon FluidNumberInput: the label inside a 64px box, an error / warning inside it. <ui-fluid-form> sets it for every field inside. |
required |
required |
boolean |
false |
Blocks form submission while the field is empty. |
decrement-label |
decrementLabel |
string |
'' |
Accessible name of the − button. |
increment-label |
incrementLabel |
string |
'' |
Accessible name of the + button. |
Properties
| Property | Type | Description |
|---|---|---|
valueAsNumber |
(read-only) The parsed value, NaN while empty or not a number. | |
willValidate |
(read-only) Whether the field takes part in constraint validation (not while disabled), as natively. | |
labels |
(read-only) The <label>s that point at this field. |
Methods
| Method | Description |
|---|---|
setCustomValidity(message) |
Like <input>.setCustomValidity(): a non-empty message makes the field invalid (and is shown as its error once touched) until cleared with ''. |
Events
| Event | React | Description |
|---|---|---|
change |
onChange |
After a committed change (blur / Enter / stepper; with type="text", on blur when the number changed) |
input |
onInput |
On every keystroke or stepper click |
Slots
| Slot | Description |
|---|---|
label |
Label content with markup; the label attribute is the plain-text fallback |
helper-text |
Helper text with markup (links, bold); overrides helper-text |
warn-text |
Warning text shown while warn; overrides warn-text |
invalid-text |
Error text shown while invalid; overrides invalid-text |
CSS parts
| Part | Description |
|---|---|
base |
The field box |
label |
The label |
input |
The native input |
prefix |
The text before the value |
suffix |
The text after the value |
controls |
The stepper group |
decrement-button |
The − button |
increment-button |
The + button |
status-icon |
The error / warning icon |
helper-text |
The helper line |
warning-text |
The warning line |
warn-text |
Same line as warning-text (named after the warn-text attribute / slot) |
error-text |
The error line |
invalid-text |
Same line as error-text (named after the invalid-text attribute / slot) |
CSS custom properties
| Custom property | Default | Description |
|---|---|---|
--ui-field-border |
Border on every side of the box (default none: Carbon's underline only; Kanok: a 1px outline) | |
--ui-field-disabled-border-color |
Border colour when disabled (default: the underline transparent) | |
--ui-field-readonly-border-color |
Border colour when read-only (default: a hairline underline) | |
--ui-field-radius |
Corner radius of the box (default 0; Kanok: 6px) | |
--ui-field-background |
Fill of the box (default --ui-surface-1; Kanok: transparent) |
|
--ui-label-font |
Font of the label (default --ui-font-caption; Kanok: 14px semibold) |
|
--ui-label-color |
Colour of the label (default --ui-ink-muted; Kanok: --ui-ink) |
|
--ui-field-readonly-background |
The box's fill when read-only (default transparent; Kanok: a faint ink tint, so read-only reads as set and never weaker than disabled) | |
--ui-fluid-background |
/ --ui-fluid-gap - A fluid box's fill and the space under it (default the layer's fill, 0: boxes that touch; Kanok: transparent, 8px) | |
--ui-icon-control-radius |
Corners of its small icon buttons, their hover fill and ring (default 0; Kanok: round) |