Skip to content

Input Number 数字输入框

仅允许输入标准的数字值,可定义范围

基础用法

要使用它,只需要在 <s-input-number> 元素中使用 v-model 绑定变量即可,变量的初始值即为默认值。

<template>
   <InputNumber v-model="num" :min="1" :max="10" @change="handleChange"/>
</template>

<script setup>
import { ref } from 'vue' 
import InputNumber from '@/components/InputNumber'
const num = ref(1)
const handleChange = (val) => {
  console.log(val)
}
</script>

<style lang="scss" scoped>

</style>

TIP

当输入无效的字符串到输入框时,由于错误,输入值将把 NaN 导入到上层

禁用状态

disabled属性接受一个 Boolean,设置为true即可禁用整个组件。 ,如果你只需要控制数值在某一范围内,可以设置 min 属性和 max 属性, 默认最小值为 0。

<template>
   <InputNumber v-model="num" :min="1" :max="10" @change="handleChange" disabled/>
</template>

<script setup>
import { ref } from 'vue' 
import InputNumber from '@/components/InputNumber'
const num = ref(1)
const handleChange = (val) => {
  console.log(val)
}
</script>

<style lang="scss" scoped>

</style>

步进

允许定义递增递减的步进控制

设置 step 属性可以控制步长。

<template>
   <InputNumber v-model="num" @change="handleChange" :step="2"/>
</template>

<script setup>
import { ref } from 'vue' 
import InputNumber from '@/components/InputNumber'
const num = ref(1)
const handleChange = (val) => {
  console.log(val)
}
</script>

<style lang="scss" scoped>

</style>

严格步进

step-strictly属性接受一个Boolean。 如果这个属性被设置为 true,则只能输入步进的倍数。

<template>
   <InputNumber v-model="num" @change="handleChange" :step="2" step-strictly/>
</template>

<script setup>
import { ref } from 'vue' 
import InputNumber from '@/components/InputNumber'
const num = ref(1)
const handleChange = (val) => {
  console.log(val)
}
</script>

<style lang="scss" scoped>

</style>

精度

设置 precision 属性可以控制数值精度,接收一个 Number。

<template>
   <InputNumber v-model="num" :precision="2" :step="0.01" :max="10"/>
</template>

<script setup>
import { ref } from 'vue' 
import InputNumber from '@/components/InputNumber'
const num = ref(1)
</script>

<style lang="scss" scoped>

</style>

TIP

precision 的值必须是一个非负整数,并且不能小于 step 的小数位数。

不同的输入框尺寸

使用 size 属性额外配置尺寸,可选的尺寸大小为:largesmall

<template>
   <InputNumber v-model="num1" size="large"/>
   <InputNumber v-model="num2" class="mx-4"/>
   <InputNumber v-model="num3" size="small"/>
</template>

<script setup>
import { ref } from 'vue' 
import InputNumber from '@/components/InputNumber'
const num1 = ref(1)
const num2 = ref(1)
const num3 = ref(1)
</script>

<style scoped>

</style>

按钮位置

设置 controls-position 属性可以控制按钮位置。

<template>
   <InputNumber v-model="num1" size="large" controls-position="right"/>
   <InputNumber v-model="num2" class="mx-4" controls-position="right"/>
   <InputNumber v-model="num3" size="small" controls-position="right"/>
</template>

<script setup>
import { ref } from 'vue' 
import InputNumber from '@/components/InputNumber'
const num1 = ref(1)
const num2 = ref(1)
const num3 = ref(1)

</script>

<style scoped>

</style>

API

Attributes

NameDescriptionTypeDefault
model-value / v-modelbinding valuenumber
minthe minimum allowed valuenumber-Infinity
maxthe maximum allowed valuenumberInfinity
stepincremental stepnumber1
step-strictlywhether input value can only be multiple of stepbooleanfalse
precisionprecision of input valuenumber
sizesize of the componentenum 'large' | 'default' | 'small'default
readonly ^(2.2.16)same as readonly in native inputbooleanfalse
disabledwhether the component is disabledbooleanfalse
controlswhether to enable the control buttonsbooleantrue
controls-positionposition of the control buttonsenum '' | 'right'
namesame as name in native inputstring
label ^(a11y) ^(deprecated)same as aria-label in native inputstring
aria-label ^(a11y) ^(2.7.2)same as aria-label in native inputstring
placeholdersame as placeholder in native inputstring
idsame as id in native inputstring
value-on-clear ^(2.2.0)value should be set when input box is clearednumber / null / enum 'min' | 'max'
validate-eventwhether to trigger form validationbooleantrue

Slots

NameDescription
decrease-icon ^(2.6.3)custom input box button decrease icon
increase-icon ^(2.6.3)custom input box button increase icon

Events

NameDescriptionType
changetriggers when the value changesFunction (currentValue: number | undefined, oldValue: number | undefined) => void
blurtriggers when Input blursFunction (event: FocusEvent) => void
focustriggers when Input focusesFunction (event: FocusEvent) => void

Exposes

NameDescriptionType
focusget focus the input componentFunction () => void
blurremove focus the input componentFunction () => void

Released under the MIT License.