Skip to content

NumberBox 步进器

介绍

用于通过点击按钮或直接输入来调整数值,支持步长、范围限制、长按连续调整及自定义样式。

引入

typescript
import { TnNumberBox } from "@tuniao/tn-ui";

代码演示

基础用法

通过 modelValue 属性绑定当前数值,点击加减按钮调整。

基础用法

点我查看代码
typescript
@Local numberValue: number = 5;

TnNumberBox({
  modelValue: this.numberValue,
  onChange: (value: number) => {
    this.numberValue = value;
  },
})

步长 / 范围限制

通过 step 属性设置每次点击按钮时的步进值。通过 minmax 属性限制数值的输入范围。

点我查看代码
typescript
@Local numberValue: number = 0;

TnNumberBox({
  modelValue: this.numberValue,
  step: 5,
  min: 5,
  max: 50,
  onChange: (value: number) => {
    this.numberValue = value;
  },
})

长按连续

通过 longPress 属性开启长按连续调整,通过 longPressInterval 设置触发间隔(毫秒)。

点我查看代码
typescript
@Local numberValue: number = 0;

TnNumberBox({
  modelValue: this.numberValue,
  longPress: true,
  longPressInterval: 150,
  onChange: (value: number) => {
    this.numberValue = value;
  },
})

禁用输入

通过 inputDisabled 属性禁用中间输入框,只允许通过按钮调整数值。

禁用输入

点我查看代码
typescript
@Local numberValue: number = 5;

TnNumberBox({
  modelValue: this.numberValue,
  inputDisabled: true,
  onChange: (value: number) => {
    this.numberValue = value;
  },
})

自定义样式

通过 sizebgColortextColor 属性自定义步进器的外观样式。

点我查看代码
typescript
@Local numberValue: number = 5;

Column({ space: 10 }) {
  // 自定义大小
  TnNumberBox({
    modelValue: this.numberValue,
    componentSize: 36,
    onChange: (value: number) => {
      this.numberValue = value;
    },
  })
  // 自定义颜色
  TnNumberBox({
    modelValue: this.numberValue,
    bgColor: "#01beff",
    textColor: "#FFFFFF",
    onChange: (value: number) => {
      this.numberValue = value;
    },
  })
}

禁用状态

通过 disabled 属性禁用整个步进器。

禁用状态

点我查看代码
typescript
TnNumberBox({
  modelValue: 3,
  disabled: true,
})

API

Props

参数说明类型默认值
modelValue当前数值number0
min最小值number0
max最大值number100
step步长number1
disabled是否禁用booleanfalse
inputDisabled是否禁用输入框booleanfalse
longPress是否开启长按连续调整booleantrue
longPressInterval长按触发间隔(毫秒)number250
componentSize步进器大小number0
bgColor背景颜色ResourceColor$r("app.color.tn_color_gray_light")
textColor文字颜色ResourceColor$r("app.color.tn_text_color_primary")

Events

事件名说明回调参数
onChange数值变化时触发(value: number) => void