FormField

为控件添加标签、帮助和错误说明。

实时预览

正在加载预览…

参数配置

全部字段 13

FormFieldOptions

Styles · 0

Go 代码

Go
// Inside a view; c is *ui.Context, t is nui.Theme.
// Keep s (example state) outside the view. See the complete example.
nui.FormField(c.Key("field"), t, nui.FormFieldOptions{Label: "Project", Help: "Use a name your team will recognize.", Required: false, Disabled: false, Error: "", Size: nui.SizeDefault}, func(ft nui.Theme) {
	nui.Input(c.Key("input"), ft, &s.Value, nui.InputOptions{Placeholder: "Project name"})
})

使用说明

这个示例为 Input 添加标签、帮助、必填标记和可编辑的错误说明。将回调收到的主题继续传给输入框,可保持父字段与子控件的状态一致。

API 参考

FormField

源码
func FormField(c *ui.Context, t Theme, opts FormFieldOptions, content func(Theme)) ui.Element

FormFieldOptions

源码
type FormFieldOptions struct {
	// Name and Value register a serialized value with the enclosing Form.
	Name, Value                           string
	Label, Description, Help, Hint, Error string
	Required, Disabled                    bool
	Size                                  Size
	Orientation                           Optional[Orientation] // Defaults to Vertical.
	Decoration
}
字段类型说明
Namestring

Go 零值: ""

表单字段或对象的名称。

Valuestring

Go 零值: ""

控件值、字段值或数据读取函数。

Labelstring

Go 零值: ""

控件标签或可访问名称。

Descriptionstring

Go 零值: ""

补充说明文字。

Helpstring

Go 零值: ""

字段下方的帮助文字。

Hintstring

Go 零值: ""

字段的简短辅助提示。

Errorstring

Go 零值: ""

错误状态或错误说明。

Requiredbool

Go 零值: false

将字段标记为必填。

Disabledbool

Go 零值: false

禁用组件及其交互。

SizeSize

Go 零值: SizeDefault

组件尺寸;默认值继承主题或组件尺寸。

OrientationOptional[Orientation]

Go 零值: unset

水平或垂直方向;未设置时使用组件默认方向。

UIStyles

Go 零值: zero value

以部件名称为键覆盖外观和间距。

Stylefunc(Slot, ui.Element)

Go 零值: nil

同步配置当前构建的部件;不要保留 Element。