ModalForm 弹窗表单
把 ProForm 放进 Modal 或 Drawer,用确认按钮完成校验和提交,保存成功后自动关闭。
何时使用
- 新建、编辑规则或配置项等字段较少、需要聚焦完成的表单。
- 需要校验、提交中状态和保存成功后自动关闭,而不想自己拼接 Modal 与 ProForm 时。
- 内容较长或需要从右侧展开时,用同一 API 的
DrawerForm;完整页面表单用 ProForm 高级表单。 - 只需要一句确认而没有字段时,用 Confirm 确认框。
在线体验
基本用法
import "github.com/tangoui/tango/ui"
var open bool
var fields = []ui.ProField{
{Key: "name", Label: "规则名称", Required: true, Placeholder: "请输入名称"},
{Key: "owner", Label: "负责人", Required: true},
}
func page(c *ui.Context) {
if ui.Button(c, "新建规则").Primary().Clicked() {
open = true
}
ui.ModalForm(c, &open, "新建规则", fields).
OnFinish(func(v ui.ProValues, done func(error)) { done(nil) }).
Show()
}点击 Modal 的确认按钮后,内置 ProForm 先校验字段,再调用 OnFinish;done(nil) 后对话框关闭。
常见用法
绑定表单值与异步保存
Values 让页面外可以读取和预填 ProValues。保存请求完成后调用 done;返回错误会保留弹窗并显示表单错误。
import "github.com/tangoui/tango/ui"
var (
editOpen bool
editVals = ui.ProValues{"name": "TradeCode 99"}
editFields = []ui.ProField{{Key: "name", Label: "规则名称", Required: true}}
)
func page(c *ui.Context) {
if ui.Button(c, "编辑").Clicked() {
editOpen = true
}
form := ui.ModalForm(c, &editOpen, "编辑规则", editFields).
Values(&editVals).
OnFinish(func(v ui.ProValues, done func(error)) {
go func() {
// 在真实应用中完成保存后回调 done(err)。
done(nil)
}()
}).Show()
if form.Finished() {
ui.Message(c, "success", "保存成功")
}
}DrawerForm 抽屉表单
DrawerForm 使用相同的字段、校验和提交回调,只把容器换成右侧抽屉;Width 设置抽屉宽度。
import "github.com/tangoui/tango/ui"
var drawerOpen bool
var drawerFields = []ui.ProField{
{Key: "title", Label: "标题", Required: true},
{Key: "description", Label: "说明", ValueType: ui.ValueTextArea},
}
func page(c *ui.Context) {
if ui.Button(c, "打开抽屉").Primary().Clicked() {
drawerOpen = true
}
ui.DrawerForm(c, &drawerOpen, "编辑内容", drawerFields).
Width(520).
OnFinish(func(v ui.ProValues, done func(error)) { done(nil) }).
Show()
}多列和保留值
Columns 把字段排成栅格列。关闭后默认保留字段值,符合 Pro Components v3 的默认行为。DestroyOnHidden(true) 在关闭时把值恢复到首次打开时的初始快照,显式绑定的 Values 也会被重置。
import "github.com/tangoui/tango/ui"
var openAdvanced bool
var advancedFields = []ui.ProField{
{Key: "name", Label: "名称", Required: true},
{Key: "code", Label: "编码", Required: true},
{Key: "owner", Label: "负责人"},
{Key: "status", Label: "状态"},
}
func page(c *ui.Context) {
if ui.Button(c, "高级配置").Clicked() {
openAdvanced = true
}
ui.ModalForm(c, &openAdvanced, "高级配置", advancedFields).
Columns(2).Show()
}只读详情
Readonly(true) 把字段显示为只读值;Modal 的确认按钮只负责关闭,DrawerForm 会移除确认按钮,适合复用编辑字段作为详情视图。
import "github.com/tangoui/tango/ui"
var detailOpen bool
var detailFields = []ui.ProField{
{Key: "name", Label: "名称"},
{Key: "amount", Label: "金额", ValueType: ui.ValueMoney},
}
var detailValues = ui.ProValues{"name": "TradeCode 99", "amount": 12800.0}
func page(c *ui.Context) {
if ui.Button(c, "查看详情").Clicked() {
detailOpen = true
}
ui.ModalForm(c, &detailOpen, "规则详情", detailFields).
Values(&detailValues).Readonly(true).Show()
}主题与样式
- ModalForm 使用 Modal、ProForm 和字段控件的令牌;DrawerForm 额外使用 Drawer 的宽度、页脚和滑入动画令牌。
Width控制 Modal 宽度或 Drawer 尺寸,两者默认均为 800px。Columns控制表单栅格列数,默认 1 列。ConfigProvider.Defaults("ModalForm", ...)和Defaults("DrawerForm", ...)支持width、columns;链式方法优先。
注意事项
open和ProValues必须放在稳定状态中;ModalForm 会根据标题生成内部状态 key,多个同标题表单要用不同标题或外层Key区分。- 调用
KeepValues()可恢复默认的保留行为。 OnFinish的done可以异步调用;done(nil)才会关闭,错误应传给done以保留弹窗并显示错误。Finished应在Show()后读取,且只报告最近一次成功提交;不要在OnFinishgoroutine 中直接操作 UI。- 弹窗标题、字段标签和校验错误会进入无障碍结构;只读模式仍应提供明确的关闭操作。
示例代码
以下是上方在线演示运行的 Go 源码(来自官方 gallery),点击标题展开。函数接收的 c *ui.Context 由窗口的 UI 循环每帧传入,接入方式见 Go GUI 入门。
ModalForm / DrawerForm 弹窗与抽屉表单
gallery 演示 ModalForm 的注册调用(含 Render 函数),源码 page_form_pro_steps.go#L45。
加载中…
API
签名与说明由 site/tools/compdoc 从 Go 源码和文档注释提取;点击名称跳到 Go API 参考 的对应条目。
入口函数
ui.ModalForm
func ModalForm(c *Context, open *bool, title string, fields []ProField) *ModalFormWModalForm starts a dialog form bound to open; chain options, then Show.
ConfigProvider.Defaults("ModalForm" / "DrawerForm", ...) keys: "width", "columns" (number).
返回 *ModalFormW,可链式调用的方法见 ModalFormW 方法。
ui.DrawerForm
func DrawerForm(c *Context, open *bool, title string, fields []ProField) *ModalFormWDrawerForm is ModalForm in a right-side drawer (Ant Design Pro DrawerForm).
返回 *ModalFormW,可链式调用的方法见 ModalFormW 方法。
ModalFormW 方法
ui.ModalFormW:ModalFormW is a ProForm inside a Modal (Ant Design Pro ModalForm): the dialog's OK button submits (spinning while OnFinish runs) and the dialog closes once the values are saved.
| 方法 | 说明 | 签名 |
|---|---|---|
Columns | Columns lays the fields out in n grid columns. | Columns(n int) *ModalFormW |
DestroyOnHidden | DestroyOnHidden resets the field values when the form is opened again (Pro modalProps/drawerProps.destroyOnHidden). | DestroyOnHidden(on bool) *ModalFormW |
Finished | Finished reports a completed submit (the dialog just closed), once. | Finished() bool |
KeepValues | KeepValues keeps the values after closing (the default, as in Pro v3). | KeepValues() *ModalFormW |
OnFinish | OnFinish receives the validated values; call done (from any goroutine) when saved. | OnFinish(fn func(vals ProValues, done func(error))) *ModalFormW |
Readonly | Readonly shows the values read-only (a details dialog): the drawer drops its OK button and the modal's OK just closes it. | Readonly(on bool) *ModalFormW |
Show | Show builds the dialog (nothing while closed). | Show() *ModalFormW |
Values | Values binds the values. | Values(v *ProValues) *ModalFormW |
Width | Width sets the dialog width (drawer: its size). | Width(px float32) *ModalFormW |
ProField 参数类型
ui.ProField:ProField describes a form field; ProForm, QueryFilter, StepsForm, ModalForm and DrawerForm render fields from it.
| 字段 | 说明 | 类型 |
|---|---|---|
Key | string | |
Label | string | |
ValueType | see the Value* constants (default text) | string |
ValueEnum | options of select / radio / checkbox / status / tag | []ProEnum |
Placeholder | string | |
Help | hint under the control | string |
Span | grid columns out of 24 (default: 24 / form columns) | int |
Group | Group puts the field in a titled ProFormGroup with the consecutive fields of the same Group (ProForm, ModalForm, StepsForm). | string |
Width | fixed control width (default: fill the column) | float32 |
Initial | value when the form starts or resets | any |
Required | Validation: 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 | |
Validate | func(v any, all ProValues) string | |
AsyncValidate | func(v any, done func(msg string)) | |
Dependencies | Linkage: Dependencies lists keys whose change re-validates this field, refreshes Options and drops a value Options no longer offers. | []string |
Hide | func(all ProValues) bool | |
Disable | func(all ProValues) bool | |
Options | func(all ProValues) []ProEnum | |
Min | Number fields. | float64 |
Max | Number fields. | float64 |
Precision | int | |
Pick | Pick 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)) |
Dragger | Dragger 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 |
Render | Render replaces the built-in control (bind it to vals[Key]). | func(c *Context, vals ProValues) |
Readonly | Readonly shows the value as read-only text (ProFormW.Readonly does it for every field); a read-only field is not validated. | bool |
OnCaptcha | OnCaptcha 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)) |
CaptchaSeconds | int | |
Captcha | Captcha configures the phone key, button text and timing callback. | *ProFieldCaptcha |
Segmented | Segmented configures the ValueSegmented editor (custom item content, direction, size, icons, etc.) before it is built. | func(w *SegmentedW) |
List | List gives the row fields of a formList field. | *ProFieldList |