Skip to content

InputNumber 数字输入框

介绍

通过点击按钮控制数字增减。

基础用法

初始化一个默认值

html
<template>
  <mot-input-number v-model="value" />
</template>
<script lang="ts">
  import { ref } from 'vue';
  export default {
    setup() {
      const value = ref(1);
      return { value };
    },
  };
</script>

步长设置

设置步长 step 5

html
<template>
  <mot-input-number v-model="value" step="5" />
</template>
<script lang="ts">
  import { ref } from 'vue';
  export default {
    setup() {
      const value = ref(1);
      return { value };
    },
  };
</script>

限制输入范围

minmax 属性分别表示最小值和最大值

html
<template>
  <mot-input-number v-model="value" min="10" max="20" />
</template>
<script lang="ts">
  import { ref } from 'vue';
  export default {
    setup() {
      const value = ref(10);
      return { value };
    },
  };
</script>

禁用状态

disabled 禁用状态下无法点击按钮或修改输入框。

html
<template>
  <mot-input-number v-model="value" disabled />
</template>
<script lang="ts">
  import { ref } from 'vue';
  export default {
    setup() {
      const value = ref(1);
      return { value };
    },
  };
</script>

只读禁用输入框

readonly 设置只读禁用输入框输入行为

html
<template>
  <mot-input-number v-model="value" readonly />
</template>
<script lang="ts">
  import { ref } from 'vue';
  export default {
    setup() {
      const value = ref(1);
      return { value };
    },
  };
</script>

支持小数点

设置步长 step 0.1 decimal-places 小数保留1位

html
<template>
  <mot-input-number v-model="value" step="0.1" decimal-places="1" />
</template>
<script lang="ts">
  import { ref } from 'vue';
  export default {
    setup() {
      const value = ref(1);
      return { value };
    },
  };
</script>

支持异步修改

通过 change 事件和 model-value 进行异步修改

html
<template>
  <mot-input-number :model-value="value" @change="onChange" />
</template>
<script lang="ts">
  import { reactive, toRefs } from 'vue';
  export default {
    setup() {
      const state = reactive({
        value: 1
      });
      const onChange = (value: number) => {
        console.log('异步演示 2 秒后更改');
        setTimeout(() => {
          state.value = value;
        }, 2000);
      };
      return { ...toRefs(state), onChange };
    },
  };
</script>

自定义按钮大小

html
<template>
  <mot-input-number v-model="value"  button-size="30" input-width="50" />
</template>
<script lang="ts">
  import { ref } from 'vue';
  export default {
    setup() {
      const value = ref(1);
      return { value };
    },
  };
</script>

自定义按钮图标

html
<template>
  <mot-input-number v-model="value">
    <template #leftIcon>
      <mot-icon  name="left"  />
    </template>
    <template #rightIcon>
      <mot-icon  name="right"  />
    </template>
  </mot-input-number>
</template>
<script lang="ts">
  import { ref } from 'vue';
  export default {
    setup() {
      const value = ref(1);
      return { value };
    },
  };
</script>

API

Props

参数说明类型默认值
v-model初始值string | number-
input-width输入框宽度string``
button-size操作符+、-尺寸string``
min最小值限制string | number1
max最大值限制string | number9999
step步长string | number1
decimal-places设置保留的小数位string | number0
disabled禁用所有功能booleanfalse
readonly只读状态禁用输入框操作行为booleanfalse

Attrs version

支持透传属性至组件内部的 input 元素, 仅支持H5。

Slots

名称说明
leftIcon自定义左侧按钮
rightIcon自定义右侧按钮

Events

事件名说明回调参数
add点击增加按钮时触发(event: Event)
reduce点击减少按钮时触发(event: Event)
overlimit点击不可用的按钮时触发(event: Event,type:string (reduce or add) )
change值改变时触发(value: number , event : Event)
blur输入框失去焦点时触发(event: Event)
focus输入框获得焦点时触发(event: Event )

主题定制

样式变量

组件提供了下列 CSS 变量,可用于自定义样式,使用方法请参考 ConfigProvider 组件

名称默认值
--mot-inputnumber-icon-colorvar(--mot-title-color)
--mot-inputnumber-icon-void-colorvar(--mot-disable-color)
--mot-inputnumber-icon-size20px
--mot-inputnumber-input-width40px
--mot-inputnumber-input-font-size12px
--mot-inputnumber-input-font-colorvar(--mot-title-color)
--mot-inputnumber-input-background-colorvar(--mot-help-color)
--mot-inputnumber-input-border-radius4px
--mot-inputnumber-input-margin0 6px
--mot-inputnumber-input-border0
--mot-inputnumber-border0
--mot-inputnumber-border-radius0
--mot-inputnumber-heightauto
--mot-inputnumber-line-heightnormal
--mot-inputnumber-border-boxcontent-box
--mot-inputnumber-displayinline-flex

MIT Licensed