Skip to content

ProFormGroup 表单分组 ​

把相关表单项放进带标题、说明、额外操作和可选折叠状态的分组容器,并可按列数组织字段。

何时使用 ​

  • 一个页面有基本信息、联系信息、结算信息等清晰的字段区块时。
  • 需要让用户折叠不常用字段,或在分组标题右侧放校验、帮助等操作时。
  • 同一份字段由 ProForm 自动分组,或需要在自定义 Children 布局中手动控制分组时。
  • 只有一两个字段、没有独立标题或折叠需求时,直接使用 ProForm 的 Columns 更简单。

在线体验 ​

ProFormGroup 表单分组 · 在线体验真实 Go 组件
在独立页面打开 ↗

查看此场景的 Go 源码 ↗

基本用法 ​

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

go
package main

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

var groupValues ui.ProValues

func page(c *ui.Context) {
    fields := []ui.ProField{
        {Key: "name", Label: "姓名", Group: "基本信息", Required: true},
        {Key: "phone", Label: "手机号", Group: "基本信息"},
        {Key: "city", Label: "城市", Group: "联系信息", ValueType: ui.ValueSelect,
            ValueEnum: []ui.ProEnum{{Value: "bj", Label: "北京"}, {Value: "sh", Label: "上海"}}},
        {Key: "note", Label: "备注", Group: "联系信息", ValueType: ui.ValueTextArea, Span: 24},
    }
    ui.ProForm(c, fields).Values(&groupValues).Columns(2).Show()
}

func main() {}

相邻且 Group 相同的字段会被 ProForm 自动包进一个 ProFormGroup;字段换组时开始新的分组。Group 不是数据键,不会写进提交值。

常见用法 ​

手动创建带说明和额外操作的分组 ​

Children 适合需要自己决定字段顺序或混入自定义控件的页面。ProFormItems 会在指定的 FormW 中渲染标准 ProField 项。

go
package main

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

var (
    accountValues ui.ProValues
    accountFields = []ui.ProField{
        {Key: "bank", Label: "开户行", Required: true},
        {Key: "number", Label: "账号", Required: true},
        {Key: "holder", Label: "户名", Required: true},
    }
)

func page(c *ui.Context) {
    ui.ProForm(c, nil).Values(&accountValues).NoSubmitter().Children(func(f *ui.FormW) {
        ui.ProFormGroup(c, "结算账户").
            Description("用于每月结算打款").
            Columns(3).
            Extra(func() { ui.Button(c, "校验").Small() }).
            Body(func() { ui.ProFormItems(c, f, accountFields, accountValues) })
    }).Show()
}

func main() {}

Description 显示在标题下方,Extra 在标题右侧执行即时模式回调;按钮自己的点击状态仍由页面变量或闭包保存。

可折叠并绑定状态 ​

Collapsed(&folded) 是受控模式:页面状态决定当前是否折叠,用户点击标题后组件会更新这个布尔值,并在当前帧让 Toggled() 返回 true。

go
package main

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

var folded bool
var contactValues ui.ProValues
var contactFields = []ui.ProField{
    {Key: "email", Label: "邮箱", Required: true},
    {Key: "address", Label: "地址"},
}

func page(c *ui.Context) {
    var group *ui.ProFormGroupW
    ui.ProForm(c, nil).Values(&contactValues).NoSubmitter().Children(func(f *ui.FormW) {
        group = ui.ProFormGroup(c, "联系信息").
            Collapsed(&folded).
            Body(func() { ui.ProFormItems(c, f, contactFields, contactValues) })
    }).Show()
    if group != nil && group.Toggled() {
        auditFoldState(folded)
    }
}

func auditFoldState(open bool) {}

func main() {}

如果不需要外部控制,可用 Collapsible();希望首次打开就折叠时用 DefaultCollapsed()。空标题 ProFormGroup(c, "") 仍可使用折叠行为,但会使用语言包中的展开 / 收起名称。

栅格与间距 ​

Columns(n) 按可用宽度把组内项目排成 n 列,Gutter 设置列间距,Size 设置项目行之间的间距。字段的 Span 仍可覆盖自己的栅格宽度。

go
package main

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

var values ui.ProValues
var fields = []ui.ProField{
    {Key: "first", Label: "名"},
    {Key: "last", Label: "姓"},
    {Key: "department", Label: "部门"},
    {Key: "description", Label: "说明", ValueType: ui.ValueTextArea, Span: 24},
}

func page(c *ui.Context) {
    ui.ProForm(c, nil).Values(&values).NoSubmitter().Children(func(f *ui.FormW) {
        ui.ProFormGroup(c, "人员资料").Columns(3).Gutter(16).Size(8).
            Body(func() { ui.ProFormItems(c, f, fields, values) })
    }).Show()
}

func main() {}

列数是组内布局意图,不是固定像素宽度;窗口变窄时项目会按可用宽度重新计算。长文本字段可以用 Span: 24 独占整行。

使用 Extra 放置即时操作 ​

分组标题的 Extra 只负责渲染,不会自动提交表单。需要校验或保存时,保留 ProFormW 句柄并调用 Submit,或在 Submitter 中集中管理按钮。

go
package main

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

var (
    values ui.ProValues
    form   *ui.ProFormW
)

func page(c *ui.Context) {
    fields := []ui.ProField{{Key: "title", Label: "标题", Required: true}}
    form = ui.ProForm(c, nil).Values(&values).NoSubmitter().Children(func(f *ui.FormW) {
        ui.ProFormGroup(c, "内容").Extra(func() {
            if ui.Button(c, "保存").Primary().Clicked() {
                form.Submit()
            }
        }).Body(func() { ui.ProFormItems(c, f, fields, values) })
    }).OnFinish(func(v ui.ProValues, done func(error)) { done(nil) }).Show()
}

func main() {}

主题与样式 ​

ProFormGroup 使用表单和布局的主题令牌绘制标题、描述、焦点环和间距。ConfigProvider.Defaults("ProFormGroup", ui.Props{...}) 支持 collapsible、columns、gutter、size;实例上的 Columns、Gutter、Size 等链式方法优先。三套主题会分别应用自己的标题字重、边框和折叠图标风格。

注意事项 ​

  • Group 只对连续字段生效;同名字段被其他组隔开时会生成两个分组。
  • Collapsed(&state) 的布尔变量必须放在稳定页面状态中;每帧重新声明会让折叠状态无法保存。
  • 手动 Children 中,ProFormItems 使用的 ProValues 应与外层 Values 相同,避免控件写入另一份状态。
  • 折叠标题具有按钮语义和键盘操作;Extra 中的图标按钮要用 A11yLabel 命名,避免只有图标没有可访问名称。

示例代码 ​

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

ProFormGroup 表单分组

gallery 演示 ProFormGroup 的具名 Render 函数,源码 page_shad2d_pro4.go#L94。

加载中…

API ​

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

入口函数 ​

ui.ProFormGroup ​

go
func ProFormGroup(c *Context, title string) *ProFormGroupW

ProFormGroup starts a group titled title ("" = no title row); chain options, then Body.

ConfigProvider.Defaults("ProFormGroup", ...) keys: "collapsible" (bool), "columns", "gutter", "size" (gap between rows, number).

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

ui.ProFormItems ​

go
func ProFormItems(c *Context, f *FormW, fields []ProField, vals ProValues)

ProFormItems builds the form items of fields inside f (for use in ProForm.Children, ProFormGroup bodies and custom form layouts).

相关类型与成员 ​

ProField.Group ​

go
// 字段
Group string

Group puts the field in a titled ProFormGroup with the consecutive fields of the same Group (ProForm, ModalForm, StepsForm).

ProFormGroupW 方法 ​

ui.ProFormGroupW:ProFormGroupW builds a ProFormGroup.

方法说明签名
BodyBody builds the group with fn as its items.Body(fn func()) *ProFormGroupW
CollapsedCollapsed binds the folded state (implies Collapsible).Collapsed(v *bool) *ProFormGroupW
CollapsibleCollapsible lets a click on the title fold the items.Collapsible() *ProFormGroupW
ColumnsColumns lays the items out n per line (0: stacked).Columns(n int) *ProFormGroupW
DefaultCollapsedDefaultCollapsed starts an uncontrolled collapsible group folded.DefaultCollapsed() *ProFormGroupW
DescriptionDescription adds a muted line under the title.Description(s string) *ProFormGroupW
ExtraExtra builds content at the right of the title row.Extra(fn func()) *ProFormGroupW
GutterGutter sets the horizontal space between columns (default 24).Gutter(px float32) *ProFormGroupW
IDID returns the group's widget ID (after Body).ID() ID
SizeSize sets the vertical space between item rows (default 0: the items' own margins).Size(px float32) *ProFormGroupW
ToggledToggled reports a fold / unfold by the user this frame.Toggled() bool

ProField 类型 ​

ui.ProField:ProField describes a form field; ProForm, QueryFilter, StepsForm, ModalForm and DrawerForm render fields from it.

字段说明类型
Keystring
Labelstring
ValueTypesee the Value* constants (default text)string
ValueEnumoptions of select / radio / checkbox / status / tag[]ProEnum
Placeholderstring
Helphint under the controlstring
Spangrid columns out of 24 (default: 24 / form columns)int
GroupGroup puts the field in a titled ProFormGroup with the consecutive fields of the same Group (ProForm, ModalForm, StepsForm).string
Widthfixed control width (default: fill the column)float32
Initialvalue when the form starts or resetsany
RequiredValidation: Required adds a required rule with the locale message; Rules check the value's text form; Validate checks the typed value against all values (cross-field); AsyncValidate runs after them.bool
Rules[]Rule
Validatefunc(v any, all ProValues) string
AsyncValidatefunc(v any, done func(msg string))
DependenciesLinkage: Dependencies lists keys whose change re-validates this field, refreshes Options and drops a value Options no longer offers.[]string
Hidefunc(all ProValues) bool
Disablefunc(all ProValues) bool
Optionsfunc(all ProValues) []ProEnum
MinNumber fields.float64
MaxNumber fields.float64
Precisionint
PickPick handles the upload field's trigger (open a file dialog); call done from any goroutine with the files to add.func(done func(files []UploadFile))
DraggerDragger shows the upload field as a drag area (Upload.Dragger): it highlights while files are dragged over it and takes dropped files (DropZone inside Upload).bool
RenderRender replaces the built-in control (bind it to vals[Key]).func(c *Context, vals ProValues)
ReadonlyReadonly shows the value as read-only text (ProFormW.Readonly does it for every field); a read-only field is not validated.bool
OnCaptchaOnCaptcha sends the code of a captcha field (call done from any goroutine; nil starts the countdown); the button then counts down CaptchaSeconds (default 60).func(vals ProValues, done func(error))
CaptchaSecondsint
CaptchaCaptcha configures the phone key, button text and timing callback.*ProFieldCaptcha
SegmentedSegmented configures the ValueSegmented editor (custom item content, direction, size, icons, etc.) before it is built.func(w *SegmentedW)
ListList gives the row fields of a formList field.*ProFieldList

相关链接 ​

以 MIT 许可发布