Input.Search 搜索框
带有搜索触发按钮的单行输入框,用于提交关键字查询或筛选条件。
何时使用
- 需要用户输入关键字,并通过 Enter 或按钮明确提交查询时。
- 只需要编辑文本、不需要搜索触发器时,用 Input 输入框。
- 需要从固定选项中选择时,用 Select 选择器 或 AutoComplete 自动完成。
- 需要把搜索框放进窗口标题栏或工具栏时,可直接复用同一个
Search控件。
在线体验
基本用法
下面代码放在应用的页面文件中;完整的 main 和窗口初始化见 打开自绘窗口。
import (
"github.com/tangoui/tango/ui"
)
var query string
func page(c *ui.Context) {
search := ui.Search(c, &query).Placeholder("请输入关键字").AllowClear()
if search.Searched() {
ui.Text(c, "正在查询:"+query)
}
}Search 绑定 *string。Searched() 只在本帧由 Enter 或搜索按钮触发时返回 true;输入内容本身的变化用 Changed() 读取。
常见用法
文字搜索按钮与加载状态
EnterButton 把默认的图标按钮替换成主按钮,Loading(true) 显示加载状态并阻止重复触发。异步请求结束后,把页面状态设回 false 并让页面重新构建。
import "github.com/tangoui/tango/ui"
var (
searchText string
searching bool
)
func page(c *ui.Context) {
w := ui.Search(c, &searchText).EnterButton("搜索").Loading(searching).Width(320)
if w.Searched() && !searching {
searching = true
// 在异步请求完成后设置 searching = false,并调用 c.Invalidate()。
}
}监听编辑与提交
用 Changed() 更新本地的筛选提示,用 Searched() 执行代价较高的查询。两者可以在同一帧分别判断。
import "github.com/tangoui/tango/ui"
var (
filterText string
lastQuery string
)
func page(c *ui.Context) {
w := ui.Search(c, &filterText).Placeholder("过滤列表")
if w.Changed() {
ui.Text(c, "当前输入:"+filterText)
}
if w.Searched() {
lastQuery = filterText
}
ui.Text(c, "已提交:"+lastQuery)
}状态、尺寸与宽度
Status("error") 或 Status("warning") 用于校验反馈;Disabled(true) 禁止编辑和提交,Size 与 Width 调整控件尺寸。
import "github.com/tangoui/tango/ui"
var errorQuery string
func page(c *ui.Context) {
ui.Search(c, &errorQuery).Status("error").Size(ui.Large).Width(360).A11yLabel("错误状态的搜索")
ui.Search(c, &errorQuery).Disabled(true).Size(ui.Small).A11yLabel("禁用的搜索")
}在工具栏中使用
Layout 接受 TanGo 的布局选项;需要把控件放进标题栏时,也可以直接在标题栏的 Center 或工具栏回调中创建它。
import (
"github.com/tangoui/tango/ui"
"github.com/tangoui/tango/ui/layout"
)
var toolbarQuery string
func page(c *ui.Context) {
ui.Row(c, func() {
ui.Text(c, "文档").Strong()
ui.Search(c, &toolbarQuery).Placeholder("搜索文档").Size(ui.Small).
Layout(layout.W(280))
}, layout.Gap(12))
}主题与样式
- 搜索框的输入区域沿用
Input的输入框令牌;默认图标按钮使用Button的按钮令牌,文字按钮由EnterButton创建,同一页面切换 Ant Design、Element Plus 或 shadcn 时会随主题变化。 - 相关令牌是
theme.Components.Input与theme.Components.Button;搜索控件自身没有单独的SearchToken。 ConfigProvider.Defaults("Input", ui.Props{...})可为搜索框提供输入框级默认值;搜索专属的默认键包括placeholder、allowClear和enterButton,组件级名称是"Search"。
注意事项
- 状态:查询字符串放在页面函数外,或放进
ui.State;不要在每帧把它重新赋成初始值。加载标志和上次提交的查询也应由应用保存。 - 事件时机:
Changed()是编辑事件,Searched()是提交事件;Loading(true)时不会报告搜索触发。 - 无障碍:有可见表单标签时优先使用
FormItem;独立使用时用A11yLabel命名搜索框。Tab 聚焦后可用 Enter 提交。 - 输入焦点:需要从外部聚焦内部编辑器时,读取
InputID()后传给c.SetFocus;不要依赖每帧新建的临时 ID。 - RTL:搜索按钮和输入区域跟随当前
ConfigProvider的方向设置,避免手工交换前后边距。
示例代码
以下是上方在线演示运行的 Go 源码(来自官方 gallery),点击标题展开。函数接收的 c *ui.Context 由窗口的 UI 循环每帧传入,接入方式见 Go GUI 入门。
API
签名与说明由 site/tools/compdoc 从 Go 源码和文档注释提取;点击名称跳到 Go API 参考 的对应条目。
入口函数
ui.Search
func Search(c *Context, val *string) *SearchWSearch adds a search box bound to *val.
返回 *SearchW,可链式调用的方法见 SearchW 方法。
SearchW 方法
ui.SearchW:SearchW is an input with a search button (Ant Input.Search, Element input with an append search button).
| 方法 | 说明 | 签名 |
|---|---|---|
A11yLabel | A11yLabel sets the accessible name of the search box (inside a FormItem the item label is used; the placeholder is the last resort). | A11yLabel(s string) *SearchW |
AllowClear | AllowClear() *SearchW | |
Changed | Changed reports an edit of the text. | Changed() bool |
Disabled | Disabled(on bool) *SearchW | |
EnterButton | EnterButton replaces the icon button with a primary button labelled s. | EnterButton(s string) *SearchW |
InputID | InputID returns the ID of the inner editor (focus it with c.SetFocus). | InputID() ID |
Layout | Layout(o ...layout.Option) *SearchW | |
Loading | Loading shows a spinner in the button. | Loading(on bool) *SearchW |
Placeholder | Placeholder(s string) *SearchW | |
Searched | Searched reports a search request (button click or Enter). | Searched() bool |
Size | Size(s ControlSize) *SearchW | |
Status | Status(s string) *SearchW | |
Variant | Variant sets the look of the field like InputW.Variant (antd Input.Search variant). | Variant(v string) *SearchW |
Width | Width(px float32) *SearchW |