InputNumber
A numeric field with step controls, decimal precision, bounds, keyboard operation, form validation, and focus methods.
Import
import { HuiInputNumber } from '@nscode/handy-ui'
import type {
InputNumberProps,
InputNumberEmits,
InputNumberExpose,
InputNumberSize
} from '@nscode/handy-ui'
Examples
Internal and Controlled Values
Without modelValue, the component stores its own value, initially null. An update listener alone does not make it controlled; unrelated parent updates retain the value. Passing the property makes it controlled, including explicit undefined/null. Rejected or normalized updates settle to the bound value. Adding or removing the binding emits a development warning and follows the new mode. No defaultValue or valueOnClear prop is added.
<script setup lang="ts">
import { ref } from 'vue'
import { HuiButton, HuiInputNumber, HuiSpace } from '@nscode/handy-ui'
const redraw = ref(0)
const last = ref('No commits yet')
const record = (value: number | null) => {
last.value = value === null ? 'null' : String(value)
}
</script>
<template>
<HuiSpace direction="vertical" fill>
<HuiButton @click="redraw++">Parent update: {{ redraw }}</HuiButton>
<HuiInputNumber
:data-parent-render="redraw"
:min="0"
:max="5"
:precision="2"
placeholder="Omitted model: internal value"
@update:model-value="record"
/>
<span>Last commit: {{ last }}</span>
<HuiInputNumber :model-value="2" placeholder="Fixed controlled value, updates rejected" />
<HuiInputNumber :model-value="undefined" placeholder="Explicit undefined: controlled empty" />
<HuiInputNumber :model-value="null" placeholder="Explicit null: controlled empty" />
</HuiSpace>
</template>
Binding, Bounds, and Steps
Bind a number or null. Explicit undefined is also a controlled empty value. Buttons, ArrowUp, and ArrowDown apply step and bounds. Holding a step button repeats the action. Typed text commits on native change, including Enter or leaving a modified field.
<script setup lang="ts">
import { ref } from 'vue'
import { HuiInputNumber, HuiSpace } from '@nscode/handy-ui'
const count = ref<number | null>(1)
const bounded = ref<number | null>(9)
const stepped = ref<number | null>(0)
</script>
<template>
<HuiSpace direction="vertical" fill>
<HuiInputNumber v-model="count" placeholder="Quantity" />
<HuiInputNumber v-model="bounded" :min="0" :max="10" />
<HuiInputNumber v-model="stepped" :min="-10" :max="10" :step="0.5" />
<span>Values: {{ count }}, {{ bounded }}, {{ stepped }}</span>
</HuiSpace>
</template>
Precision and Empty Values
Precision formats committed input with trailing zeroes; the model remains numeric. Deleting all text and committing requests null, never undefined. An empty field steps from zero and then clamps to its bounds.
<script setup lang="ts">
import { ref } from 'vue'
import { HuiInputNumber, HuiSpace } from '@nscode/handy-ui'
const integer = ref<number | null>(2)
const price = ref<number | null>(1.5)
const rate = ref<number | null>(0.125)
const empty = ref<number | null>(null)
</script>
<template>
<HuiSpace direction="vertical" fill>
<HuiInputNumber v-model="integer" :precision="0" />
<HuiInputNumber v-model="price" :precision="2" :step="0.1" />
<HuiInputNumber v-model="rate" :precision="3" :step="0.001" />
<HuiInputNumber v-model="empty" :min="3" placeholder="Empty: first increase yields 3" />
<span>Empty binding: {{ empty === null ? 'null' : empty }}</span>
</HuiSpace>
</template>
Grouping and custom display
thousandSeparator=true groups digits with commas; strings such as _ or a space use that literal separator. Initial and blurred display are formatted. Focus shows raw digits with precision padding, keeping typing and the caret unchanged. A native change strips the configured separator and commits a number.
formatter(value) receives the precision-rounded number and controls only blurred display, taking precedence over automatic grouping. Native change first removes grouping and parses ordinary decimal input; only a failed decimal parse calls parser(text) as a fallback. A parser for a special syntax therefore does not reject ordinary focused edits. It receives trimmed original input; finite numeric results pass through bounds and precision, while undefined, NaN or Infinity restore the actual value. Empty input bypasses the parser and commits null.
<script setup lang="ts">
import { ref } from 'vue'
import { HuiInputNumber, HuiSpace } from '@nscode/handy-ui'
const amount = ref<number | null>(1234567.8)
const custom = ref<number | null>(1234.5)
const formatted = ref<number | null>(1234.5)
const formatter = (value: number) =>
`Value ${value.toFixed(2).replace(/\B(?=(\d{3})+(?!\d))/g, ',')}`
const parser = (text: string) => {
const raw = text.replace(/^Value\s*/, '').replaceAll(',', '')
return /^[+-]?(\d+(\.\d*)?|\.\d+)$/.test(raw) ? Number(raw) : undefined
}
</script>
<template>
<HuiSpace direction="vertical" fill>
<HuiSpace wrap>
<span>Comma grouping</span>
<HuiInputNumber v-model="amount" :precision="2" thousand-separator style="width: 260px" />
<code>{{ amount === null ? 'null' : amount }}</code>
</HuiSpace>
<HuiSpace wrap>
<span>Custom separator</span>
<HuiInputNumber v-model="custom" :precision="2" thousand-separator="_" style="width: 260px" />
<code>{{ custom === null ? 'null' : custom }}</code>
</HuiSpace>
<HuiSpace wrap>
<span>formatter / parser</span>
<HuiInputNumber
v-model="formatted"
:precision="2"
:formatter="formatter"
:parser="parser"
style="width: 260px"
/>
<code>{{ formatted === null ? 'null' : formatted }}</code>
</HuiSpace>
</HuiSpace>
</template>
Automatic grouping uses . as the decimal point. Separators containing digits, ., + or - are ignored with a development warning to avoid changing numeric meaning. Use paired formatter/parser functions for other numeric syntax. aria-valuenow remains numeric; aria-valuetext follows displayed text.
Units and icons
The prefix/suffix slots are separate from editable text and never enter the parser. prefixIcon/suffixIcon provide icon shortcuts; matching slots take precedence. Clicking a unit focuses the input unless disabled. Units work with both control positions or without controls.
<script setup lang="ts">
import { ref } from 'vue'
import { HuiInputNumber, HuiSpace } from '@nscode/handy-ui'
const amount = ref<number | null>(1234.5)
const weight = ref<number | null>(25)
</script>
<template>
<HuiSpace direction="vertical" fill>
<HuiSpace v-for="size in ['large', 'default', 'small'] as const" :key="size" wrap>
<span>{{ size }}</span>
<HuiInputNumber
v-model="amount"
:size="size"
:precision="2"
thousand-separator
style="width: 260px"
>
<template #prefix>¥</template><template #suffix>元</template>
</HuiInputNumber>
<HuiInputNumber v-model="weight" :size="size" controls-position="right" style="width: 200px">
<template #suffix>kg</template>
</HuiInputNumber>
</HuiSpace>
<HuiSpace wrap>
<HuiInputNumber
v-model="weight"
:controls="false"
prefix-icon="search"
suffix-icon="check"
style="width: 200px"
/>
<HuiInputNumber v-model="weight" disabled style="width: 200px"
><template #suffix>kg</template></HuiInputNumber
>
</HuiSpace>
</HuiSpace>
</template>
Sizes and Controls
Compare all sizes, controls on both sides or stacked on the right, and no controls. Omitted size inherits FormItem, Form, ConfigProvider, then default.
<script setup lang="ts">
import { ref } from 'vue'
import { HuiInputNumber, HuiSpace } from '@nscode/handy-ui'
import type { InputNumberSize } from '@nscode/handy-ui'
const sizes: InputNumberSize[] = ['large', 'default', 'small']
const count = ref<number | null>(3)
</script>
<template>
<HuiSpace direction="vertical" fill>
<HuiSpace v-for="size in sizes" :key="size" wrap>
<span>{{ size }}</span>
<HuiInputNumber v-model="count" :size="size" />
<HuiInputNumber v-model="count" :size="size" controls-position="right" />
<HuiInputNumber v-model="count" :size="size" :controls="false" />
</HuiSpace>
</HuiSpace>
</template>
Disabled, Readonly, and Native Attributes
<script setup lang="ts">
import { HuiInputNumber, HuiSpace } from '@nscode/handy-ui'
</script>
<template>
<HuiSpace wrap>
<HuiInputNumber :model-value="3" disabled />
<HuiInputNumber :model-value="3" readonly />
<HuiInputNumber :model-value="3" name="quantity" aria-label="Quantity" :tabindex="0" />
</HuiSpace>
</template>
Events and Focus Methods
Only committed changes emit update and change. Merely focusing and blurring emits no value changes. Focus methods preserve values and external focus; readonly fields remain focusable.
<script setup lang="ts">
import { ref } from 'vue'
import { HuiButton, HuiInputNumber, HuiSpace } from '@nscode/handy-ui'
import type { InputNumberExpose } from '@nscode/handy-ui'
const control = ref<InputNumberExpose>()
const value = ref<number | null>(1)
const event = ref('Waiting')
</script>
<template>
<HuiSpace direction="vertical" fill>
<HuiSpace wrap>
<HuiButton @mousedown.prevent @click="control?.focus()">focus()</HuiButton>
<HuiButton @mousedown.prevent @click="control?.blur()">blur()</HuiButton>
</HuiSpace>
<HuiInputNumber
ref="control"
v-model="value"
@change="event = 'change: ' + $event"
@focus="event = 'focus'"
@blur="event = 'blur'"
/>
<span>{{ event }}; value: {{ value }}</span>
</HuiSpace>
</template>
Form Validation
Value changes validate change rules; blur validates blur rules. FormItem associates its label with the native input. Disable automatic validation with validate-event=false.
<script setup lang="ts">
import { reactive } from 'vue'
import { HuiForm, HuiFormItem, HuiInputNumber, HuiSpace } from '@nscode/handy-ui'
import type { FormRule } from '@nscode/handy-ui'
const model = reactive<{ count: number | null }>({ count: null })
const rules: Record<string, FormRule[]> = {
count: [{ required: true, message: 'Enter a quantity', trigger: 'blur' }]
}
</script>
<template>
<HuiSpace direction="vertical" fill>
<HuiForm :model="model" :rules="rules" @submit.prevent>
<HuiFormItem label="Quantity" prop="count"
><HuiInputNumber v-model="model.count" :min="1"
/></HuiFormItem>
</HuiForm>
<HuiInputNumber :model-value="1" :validate-event="false" />
</HuiSpace>
</template>
Props
| Prop | Type | Default | Description |
|---|---|---|---|
| modelValue | number | null | undefined | Passing the property controls it; omitted: internal null. Explicit undefined/null displays empty text |
| min | number | -Infinity | Minimum committed value |
| max | number | Infinity | Maximum committed value |
| step | number | 1 | Increment/decrement amount |
| precision | number | — | Number of displayed decimal places after committing |
| thousandSeparator | boolean | string | false | Blurred grouping; true uses commas. Digits, decimal points and signs are disallowed |
| formatter | (value: number) => string | — | Blurred display of the precision-rounded number; overrides automatic grouping |
| parser | (text: string) => number | undefined | — | Fallback after ordinary decimal parsing fails; invalid output restores the actual value, empty input commits null |
| prefixIcon | string | — | Prefix icon; prefix slot takes precedence |
| suffixIcon | string | — | Suffix icon; suffix slot takes precedence |
| disabled | boolean | false | Disable input and controls |
| readonly | boolean | false | Prevent input and stepping, retain focus |
| controls | boolean | true | Show step buttons |
| controlsPosition | '' | 'right' | '' | Controls on both sides or stacked on the right |
| size | 'large' | 'default' | 'small' | — | Inherited control size |
| placeholder | string | — | Empty field placeholder |
| validateEvent | boolean | true | Validate on changes and blur |
Events
| Event | Payload | Description |
|---|---|---|
| update:modelValue | (value: number | null) | A committed user value changes |
| change | (value: number | null) | Follows update:modelValue |
| focus | (event: FocusEvent) | Focus enters the component |
| blur | (event: FocusEvent) | Focus leaves the component |
Slots
| Name | Description |
|---|---|
| prefix | Unit or icon before the number, separate from input text; overrides prefixIcon |
| suffix | Unit or icon after the number, separate from input text; overrides suffixIcon |
Methods
| Name | Type | Description |
|---|---|---|
| focus | () => void | Focus the current or first available control unless disabled or unmounted |
| blur | () => void | Blur only an active element inside this component in its owner document |
| inputRef | HTMLInputElement | undefined | Current native input; undefined after unmount |
Types
import type {
InputNumberSize,
InputNumberProps,
InputNumberEmits,
InputNumberExpose
} from '@nscode/handy-ui'
export type InputNumberSize = 'large' | 'default' | 'small'
export interface InputNumberProps {
modelValue?: number | null
min?: number
max?: number
step?: number
precision?: number
thousandSeparator?: boolean | string
formatter?: (value: number) => string
parser?: (text: string) => number | undefined
prefixIcon?: string
suffixIcon?: string
disabled?: boolean
readonly?: boolean
controls?: boolean
controlsPosition?: '' | 'right'
size?: InputNumberSize
placeholder?: string
validateEvent?: boolean
}
export interface InputNumberEmits {
'update:modelValue': [value: number | null]
change: [value: number | null]
focus: [event: FocusEvent]
blur: [event: FocusEvent]
}
export interface InputNumberExpose {
focus: () => void
blur: () => void
inputRef: HTMLInputElement | undefined
}