Skip to content

InputNumber 数字输入框 ​

通过键盘输入或步进按钮输入一个在范围内的数字,支持精度、步长、格式化显示和高精度小数。

何时使用 ​

  • 需要输入数值(数量、金额、比例),并希望限制范围、步长或小数位数时。
  • 只在一个连续区间里粗略取值、对精确数字不敏感时,用 Slider 滑动输入条 更直观。
  • 输入的是编号、电话等“由数字组成的文本”时,用 Input 输入框,不要用数字框。

在线体验 ​

InputNumber 数字输入框 · 在线体验真实 Go 组件
在独立页面打开 ↗

查看此场景的 Go 源码 ↗

基本用法 ​

下面代码放在应用的页面文件中;完整的 main 和窗口初始化见 打开自绘窗口。

go
import (
	"fmt"

	"github.com/tangoui/tango/ui"
)

var count = 3.0

func page(c *ui.Context) {
	if ui.InputNumber(c, &count).Min(1).Max(10).A11yLabel("数量").Changed() {
		fmt.Println("数量变为", count)
	}
}

值绑定到 *float64。输入框获得焦点时可直接输入,也可以点击步进按钮或按方向键 ↑ / ↓ 增减;离开输入框时超出 Min / Max 的值会被修正。Changed() 报告本帧值是否改变。

常见用法 ​

步长与精度 ​

Step 设置每次增减的量,Precision 固定显示和保存的小数位数。Wheel() 允许在获得焦点时用鼠标滚轮步进(对应 antd changeOnWheel)。

go
import (
	"github.com/tangoui/tango/ui"
)

var ratio = 1.5

func page(c *ui.Context) {
	ui.InputNumber(c, &ratio).Min(0).Max(5).Step(0.5).Precision(1).Wheel().Width(120).A11yLabel("比例")
}

格式化显示 ​

Formatter 把数值转换成展示文本(千分位、货币符号),Parser 把展示文本还原成纯数字,两者需要成对使用。只想在数字前加一个固定符号时,用 PrefixText("¥") 或 Prefix(icon) 更简单。

go
import (
	"strings"

	"github.com/tangoui/tango/ui"
)

var price = 1280.0

func withComma(s string) string {
	intPart, frac, hasFrac := strings.Cut(s, ".")
	var b strings.Builder
	for i, r := range intPart {
		if i > 0 && (len(intPart)-i)%3 == 0 {
			b.WriteByte(',')
		}
		b.WriteRune(r)
	}
	if hasFrac {
		return "$ " + b.String() + "." + frac
	}
	return "$ " + b.String()
}

func page(c *ui.Context) {
	ui.InputNumber(c, &price).
		Formatter(withComma).
		Parser(func(s string) string { return strings.NewReplacer("$", "", ",", "", " ", "").Replace(s) }).
		Width(180).A11yLabel("金额")
	ui.InputNumber(c, &price).PrefixText("¥").Width(140).A11yLabel("价格")
}

可为空的值 ​

默认情况下清空文本再离开会恢复原值。需要区分“没填”和“填了 0”时,用 Empty(&empty) 绑定一个布尔值:清空时 empty 为 true,输入框显示 Placeholder。

go
import (
	"github.com/tangoui/tango/ui"
)

var (
	discount      float64
	discountEmpty = true
)

func page(c *ui.Context) {
	ui.InputNumber(c, &discount).Empty(&discountEmpty).Placeholder("不打折").Min(0).Max(100)
	if !discountEmpty {
		ui.Text(c, "已设置折扣")
	}
}

高精度小数 ​

float64 无法精确表示 0.1 + 0.2 或 20 位金额。StringMode(&s) 把值绑定成十进制字符串,步进和范围都按十进制计算(对应 antd stringMode);此时第二个参数可以传 nil。

go
import (
	"github.com/tangoui/tango/ui"
)

var exact = "0.1"

func page(c *ui.Context) {
	ui.InputNumber(c, nil).StringMode(&exact).Step(0.000000000000000001).Width(260).A11yLabel("高精度")
	ui.Text(c, "当前值:"+exact)
}

按钮位置、尺寸与状态 ​

antd 6 的 Mode("spinner") 把 − / + 放在两侧,等同于 ControlsPosition("sides")。Mode("input") 对应 ControlsPosition("right"),按钮在行尾上下堆叠。Size(ui.Small) / Size(ui.Large) 设置尺寸,Variant 设置外观("outlined"、"filled"、"borderless"、"underlined"),Status("error" | "warning") 设置校验状态。

Controls(false) 隐藏步进按钮,Keyboard(false) 关闭方向键步进。ReadOnly() 允许选择和复制数值,并禁止编辑和步进。StepStrictly() 将提交的 float64 值取整到最近的步长倍数。

go
import (
	"github.com/tangoui/tango/ui"
)

var qty = 2.0

func page(c *ui.Context) {
	ui.InputNumber(c, &qty).ControlsPosition("sides").Min(1).Max(10).A11yLabel("数量")
	ui.InputNumber(c, &qty).Size(ui.Small).Variant("filled").A11yLabel("数量(小)")
	ui.InputNumber(c, &qty).Status("warning").A11yLabel("数量(警告)")
	ui.InputNumber(c, &qty).Disabled(true).A11yLabel("数量(禁用)")
}

主题与样式 ​

  • 步进按钮的默认位置随设计体系变化:Element Plus 下为两侧(sides),Ant Design 与 shadcn 下为右侧堆叠(right)。
  • 组件令牌:theme.Components.InputNumber(InputNumberToken)控制默认宽度 ControlWidth、步进按钮宽度与颜色(HandleWidth、HandleColor、HandleHoverBg、HandleBorderColor…)以及两侧按钮(SideHandleBg、SideHandleColor);边框、文字、占位符沿用 Input 令牌。
  • ConfigProvider.Defaults("InputNumber", ui.Props{...}) 支持 controlsPosition、mode、variant、wheel、controls 和 keyboard。

注意事项 ​

  • 状态:数值就是你绑定的变量;正在编辑的文本由组件内部按调用位置保存。在列表里渲染多个数字框时用 ui.Key 包住每一行,避免增删行后编辑状态错位。
  • 表单校验:放进 Form 时用 ui.RulesOf(f.Item("年龄"), &age, ui.RangeValue(18.0, 60.0, "...")) 绑定数值规则;在出错的表单项里,数字框自动显示错误状态。
  • 无障碍:在表单项里自动使用表单项标签作为名称;单独使用时设置 A11yLabel。键盘 ↑ / ↓ 按步长增减。
  • 格式化:Formatter 与 Parser 必须互为逆运算,否则编辑后的文本无法还原成数字。

示例代码 ​

以下是上方在线演示运行的 Go 源码(来自官方 gallery),点击标题展开。函数接收的 c *ui.Context 由窗口的 UI 循环每帧传入,接入方式见 Go GUI 入门。

InputNumber 数字输入框

gallery 演示 InputNumber 的 Render 函数,源码 page_form.go#L70。

加载中…

InputNumber 模式、控制按钮与只读

gallery 演示 InputNumberAlign 的 Render 函数,源码 page_b1b.go#L26。

加载中…

InputNumber 格式化、前缀、两侧按钮与高精度

gallery 演示 InputNumberMore 的 Render 函数,源码 page_p2d_entry.go#L138。

加载中…

API ​

签名与说明由 site/tools/compdoc 从 Go 源码和文档注释提取;点击名称跳到 Go API 参考 的对应条目。

入口函数 ​

ui.InputNumber ​

go
func InputNumber(c *Context, val *float64) *InputNumberW

InputNumber adds a numeric input bound to *val (val may be nil with StringMode).

返回 *InputNumberW,可链式调用的方法见 InputNumberW 方法。

InputNumberW 方法 ​

ui.InputNumberW:InputNumberW is a numeric input with step controls.

方法说明签名
A11yLabelA11yLabel sets the accessible name of the numeric input.A11yLabel(s string) *InputNumberW
ChangedChanged() bool
ControlsControls shows (true, the default) or hides the step buttons (antd controls, Element controls).Controls(on bool) *InputNumberW
ControlsPositionControlsPosition places the step buttons: "right" (stacked at the right, Ant's look and Element's controls-position="right") or "sides" (− before and + after the value).ControlsPosition(pos string) *InputNumberW
DisabledDisabled(on bool) *InputNumberW
DownDisabledDownDisabled reports whether the − / down button is disabled.DownDisabled() bool
EmptyEmpty lets the field hold no value, like a nullable number: *empty reports whether it is empty.Empty(empty *bool) *InputNumberW
FormatterFormatter formats the value for display ("$ 1,000"); the text being edited keeps the formatted form.Formatter(fn func(value string) string) *InputNumberW
KeyboardKeyboard turns the Up / Down arrow keys stepping on (the default) or off (antd keyboard).Keyboard(on bool) *InputNumberW
MaxMax(v float64) *InputNumberW
MinMin(v float64) *InputNumberW
ModeMode chooses where the step buttons go (antd 6 mode): "input" (stacked at the inline end, the default) or "spinner" (− before and + after the value, which is centred).Mode(m string) *InputNumberW
ParserParser turns the displayed text back into a plain number ("1000").Parser(fn func(display string) string) *InputNumberW
PlaceholderPlaceholder is shown while the field is empty (see Empty).Placeholder(s string) *InputNumberW
PrecisionPrecision(n int) *InputNumberW
PrefixPrefix shows an icon before the value.Prefix(ic *icon.Icon) *InputNumberW
PrefixTextPrefixText shows short static text before the value ("¥").PrefixText(s string) *InputNumberW
ReadOnlyReadOnly shows the value without letting it change: the text can be selected and copied, the step buttons are hidden and the arrow keys and the wheel do nothing (antd readOnly, Element readonly).ReadOnly() *InputNumberW
SizeSize(s ControlSize) *InputNumberW
StatusStatus overrides the validation status ("error" | "warning"); inside a form item it defaults to the item's status.Status(s string) *InputNumberW
StepStep(v float64) *InputNumberW
StepStrictlyStepStrictly only accepts multiples of Step (Element step-strictly): a typed value is rounded to the nearest multiple when it is committed.StepStrictly() *InputNumberW
StringModeStringMode binds the value as a decimal string instead of a float64, so it keeps every digit ("0.1" + "0.2" = "0.3", 20-digit amounts; Ant stringMode).StringMode(s *string) *InputNumberW
SubmittedSubmitted reports Enter after committing the numeric value.Submitted() bool
UpDisabledUpDisabled reports whether the + / up button is disabled (a value at Max, a disabled or read-only field, or no step buttons).UpDisabled() bool
VariantVariant sets the frame look (see InputW.Variant).Variant(v string) *InputNumberW
WheelWheel lets the mouse wheel step the value while the field has focus (Ant changeOnWheel).Wheel() *InputNumberW
WidthWidth(px float32) *InputNumberW

相关链接 ​

以 MIT 许可发布