Skip to content

Mobile Form 移动端表单 ​

以移动端分组和标签布局组织 Field、Stepper、开关及自定义输入,并显示帮助与错误信息。

何时使用 ​

  • 移动端需要标签、输入控件、帮助或错误提示时使用 Form、FormItem 和 Field。
  • 只有一两个简单输入时可以直接使用 Field,不必为了包装而创建 Form。
  • 多步骤流程或桌面后台表单应使用对应的 Form Pro / Steps 组件。
  • Form 只负责布局和呈现,不替业务执行校验、提交或网络请求。

在线体验 ​

真实 gallery 展示左右标签、顶部标签、错误提示、Stepper 和提交按钮。

Mobile · 表单:Form / Field / Stepper / Button · 在线体验真实 Go 组件
在独立页面打开 ↗

查看此场景的 Go 源码 ↗

基本用法 ​

Field 通过 *string 绑定值;FormItem 的 Body 负责放置具体控件。

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

var name string

func page(c *ui.Context) {
    mobile.Form(c).Header("账户").Body(func() {
        mobile.FormItem(c, "姓名").Required().Body(func() {
            mobile.Field(c, &name).Placeholder("请输入姓名")
        })
    })
    _ = ui.ID(0)
}

常见用法 ​

左侧标签和必填 ​

默认标签在左侧,Required 用于表达必填语义;实际校验仍由业务代码完成。

go
var phone string

func horizontal(c *ui.Context) {
    mobile.Form(c).Header("基本资料").Body(func() {
        mobile.FormItem(c, "手机号").Required().Body(func() {
            mobile.Field(c, &phone).Placeholder("请输入手机号").MaxLength(11)
        })
    })
}

垂直标签和帮助文字 ​

Vertical 将标签放到输入上方,Help 适合说明格式或长度限制。

go
var note string

func vertical(c *ui.Context) {
    mobile.Form(c).Vertical().Body(func() {
        mobile.FormItem(c, "备注").Help("最多 100 字").Body(func() {
            mobile.Field(c, &note).Placeholder("选填").MaxLength(100)
        })
    })
}

错误和提交 ​

错误文本由页面状态决定;按钮点击只触发提交动作,不会自动发送请求。

go
var email, errorText string

func submit(c *ui.Context) {
    mobile.Form(c).Body(func() {
        mobile.FormItem(c, "邮箱").Error(errorText).Body(func() {
            mobile.Field(c, &email).Placeholder("name@example.com")
        })
    })
    if mobile.Button(c, "提交").Primary().Block().Clicked() {
        if email == "" {
            errorText = "请输入邮箱"
        }
    }
}

数字输入和密码 ​

Stepper 绑定 *float64,Field 可用 Password 切换密码输入。

go
var quantity float64
var password string

func controls(c *ui.Context) {
    mobile.Form(c).Body(func() {
        mobile.FormItem(c, "数量").Body(func() {
            mobile.Stepper(c, &quantity).Min(1).Max(10).Step(1)
        })
        mobile.FormItem(c, "密码").Body(func() {
            mobile.Field(c, &password).Password().Placeholder("请输入密码")
        })
    })
}

带箭头的选择项 ​

FormItem 可以用 Arrow 表示会打开 Picker 或下一页,并读取 Clicked。

go
var pickerOpen bool

func choice(c *ui.Context) {
    item := mobile.FormItem(c, "生日").Arrow().Body(func() { ui.Text(c, "1990-06-01") })
    if item.Clicked() {
        pickerOpen = true
    }
    _ = pickerOpen
}

注意事项 ​

输入字符串、数字、错误文本和弹层开关放在页面闭包或其他稳定状态中,不能在每帧重置。Changed、Submitted 和 Clicked 是当前帧事件;异步校验应在 UI 外完成,再把结果写回状态。密码字段不要把值写入日志。给没有可见标签的自定义控件补充无障碍标签,并在 RTL 下让布局自动镜像。

示例代码 ​

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

Mobile · 表单:Form / Field / Stepper / Button

gallery 演示 MobileForm 的 Render 函数,源码 page_mobile_gallery.go#L134。

加载中…

API ​

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

入口函数 ​

mobile.Form ​

go
func Form(c *ui.Context) *FormW

Form starts a mobile form; add FormItems in Body.

ConfigProvider defaults ("mobile.Form"): "layout" ("horizontal" or "vertical"), "mode" ("default" or "card").

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

mobile.FormItem ​

go
func FormItem(c *ui.Context, label string) *FormItemW

FormItem starts a labelled field; build the control in Body.

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

mobile.Field ​

go
func Field(c *ui.Context, val *string) *FieldW

Field adds a borderless text input bound to *val. It reuses ui.Input (editing, IME, clipboard) and hides its frame.

ConfigProvider defaults ("mobile.Field"): "clearable" (bool), "maxLength" (int), "placeholder" (string).

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

go
func SearchBar(c *ui.Context, q *string, placeholder, cancel string) *SearchBarW

SearchBar adds a search field bound to *q. placeholder "" uses the localized "搜索". Pass a non-empty cancel text to show a cancel action while the field has text. In the shadcn style the field is a muted, borderless rounded-lg input on the bar background with a ring on focus.

ConfigProvider defaults ("mobile.SearchBar"): "placeholder" (string, used when placeholder is ""), "cancelText" (string, used when cancel is ""), "shape" ("round" or "default").

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

mobile.Stepper ​

go
func Stepper(c *ui.Context, v *float64) *StepperW

Stepper adds a stepper bound to *v (step 1, no bounds). Set options, then read Changed (applying the tap happens on the first query or at paint, whichever comes first, so bounds always apply). In the shadcn style it is an outlined button group (h-10) with ghost − / + buttons.

ConfigProvider defaults ("mobile.Stepper"): "step", "min", "max", "digits" (numbers), "disabled" (bool).

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

FormW 方法 ​

mobile.FormW:FormW lays out labelled fields in a white group, labels on the left (default) or on top.

方法说明签名
BodyBody builds the items.Body(fn func())
CardCard insets the group with rounded corners.Card() *FormW
HeaderHeader shows a group caption.Header(s string) *FormW
VerticalVertical puts labels above their fields.Vertical() *FormW

FormItemW 方法 ​

mobile.FormItemW:FormItemW is one labelled field of a Form.

方法说明签名
ArrowArrow shows a navigation arrow (pickers opened from the row).Arrow() *FormItemW
BodyBody builds the field control.Body(fn func()) *FormItemW
ClickedClicked reports a tap on the row (open a picker from an Arrow item).Clicked() bool
ErrorError shows a validation message under the field ("" for none).Error(msg string) *FormItemW
HelpHelp shows a hint under the field.Help(s string) *FormItemW
RequiredRequired marks the label with an asterisk.Required() *FormItemW

FieldW 方法 ​

mobile.FieldW:FieldW is a borderless single-line text input for forms and cells.

方法说明签名
ChangedChanged reports an edit this frame.Changed() bool
EnterKeyEnterKey labels the soft keyboard's return key (input.EnterNext, input.EnterDone, …); pressing it still reports Submitted.EnterKey(k input.EnterKey) *FieldW
InputInput returns the underlying ui.InputW.Input() *ui.InputW
MaxLengthMaxLength limits the length.MaxLength(n int) *FieldW
PasswordPassword masks the text.Password() *FieldW
PlaceholderPlaceholder sets the hint text.Placeholder(s string) *FieldW
SubmittedSubmitted reports Enter.Submitted() bool
TypeType tells the soft keyboard what the field holds (input.TextNumber, input.TextEmail, input.TextPhone, …): its layout and suggestions.Type(k input.TextKind) *FieldW

SearchBarW 方法 ​

mobile.SearchBarW:SearchBarW is a rounded search field with an optional cancel action.

方法说明签名
CancelledCancelled reports a tap on the cancel action.Cancelled() bool
ChangedChanged reports an edit this frame.Changed() bool
SubmittedSubmitted reports Enter (search).Submitted() bool

StepperW 方法 ​

mobile.StepperW:StepperW is a − value + control.

方法说明签名
A11yLabelA11yLabel sets the accessible name (inside a FormItem the label is used).A11yLabel(s string) *StepperW
ChangedChanged reports a step this frame.Changed() bool
DigitsDigits fixes the decimals shown.Digits(n int) *StepperW
DisabledDisabled disables the control.Disabled(on bool) *StepperW
MaxMax sets the upper bound.Max(v float64) *StepperW
MinMin sets the lower bound.Min(v float64) *StepperW
StepStep sets the increment.Step(s float64) *StepperW

相关链接 ​

以 MIT 许可发布