Select
Example sourceGo · Copyable
This is the complete source for the gallery example above.examples/components/select.go
Reuse the component construction and Render methods in a scaffolded project. The example’s registerSection and shared helpers are provided by the component gallery.
package main
import (
"fmt"
"github.com/dyike/keel/ui/core"
"github.com/dyike/keel/ui/el"
"github.com/dyike/keel/ui/kit"
)
func init() {
registerSection("select", "inputs", func() core.Widget {
status := kit.Select(demoText("Status", "状态"), demoText("Awaiting payment", "待付款"), demoText("Paid", "已付款"), demoText("Shipped", "已发货"), demoText("Completed", "已完成")).Clearable(true).TitlePrefix(demoText("Status: ", "状态:"))
city := kit.Select(demoText("City", "城市"), demoText("Beijing", "北京"), demoText("Shanghai", "上海"), demoText("Guangzhou", "广州"), demoText("Shenzhen", "深圳"), demoText("Hangzhou", "杭州"), demoText("Chengdu", "成都"), demoText("Chicago", "芝加哥")).Searchable()
city.Clearable(true).MenuWidth(360).MenuMaxHeight(200).RowHeight(40).
RenderItem(func(cx *el.Context, row kit.SelectItemContext) el.Element {
return el.Div().Row().Gap(8).Child(kit.Icon(kit.IconInfo).Size(16).Render(cx), el.Text(row.Option.Label), el.Text(row.Option.Value))
}).
Empty(el.ViewFunc(func(*el.Context) el.Element {
return el.Text(demoText("No matching cities; try Beijing or Chicago", "没有匹配的城市,试试北京或芝加哥"))
}))
city.SetEntries(kit.SelectOption{Value: "bj", Label: demoText("Beijing", "北京"), Group: demoText("China", "中国")}, kit.SelectOption{Value: "sh", Label: demoText("Shanghai (unavailable)", "上海(暂不可用)"), Group: demoText("China", "中国"), Disabled: true}, kit.SelectOption{Value: "sz", Label: demoText("Shenzhen", "深圳"), Group: demoText("China", "中国")}, kit.SelectOption{Value: "chi", Label: demoText("Chicago", "芝加哥"), Group: demoText("United States", "美国")})
multiple := kit.Select(demoText("Multiselect · 10,000 options", "多选 · 一万条选项")).Multiple().Searchable()
entries := make([]kit.SelectOption, 10000)
for i := range entries {
entries[i] = kit.SelectOption{Value: fmt.Sprintf("id-%d", i), Label: fmt.Sprintf(demoText("Option %05d", "选项 %05d"), i), Group: fmt.Sprintf(demoText("Group %d", "分组 %d"), i/1000+1), Disabled: i%100 == 5}
}
multiple.SetEntries(entries...)
return el.Root(el.ViewFunc(func(cx *el.Context) el.Element {
return el.Div().P(24).Items(el.Start).Child(el.Div().Gap(14).W(el.Dp(300)).MaxW(el.Full).Child(status.Render(cx), city.Render(cx), multiple.Render(cx)))
}))
})
}
Choose an item from the list.
status := kit.Select("Status", "Awaiting payment", "Paid", "Shipped").OnChange(func(s string) { … })
city := kit.Select("City", cities...).Searchable()
- Click, Enter, Space or ↓ to open the list. ↑ ↓ moves in the list (looping from beginning to end), Home / End jumps to the beginning and end, Enter selects, Esc closes; after closing, the focus returns to the drop-down box.
Searchable()Add a search box at the top of the list. When opening, focus on the search box and press Enter to select the first matching item. Press ↓ in the search box to enter the list.Hint(s)is the text displayed when no selection is made. By default, the locale's "Please select" is used.Value()/SetValue,SetOptions(cleared when the original selection is not in the new option),SetDisabled,SetError.el.RootREQUIRED.
Agent: drop-down box roles select, value are the current options; after opening, the list is listbox, each item is option, selected represents the currently selected item.
Verify: go run ./examples/components -section select, add -theme dark to check the dark theme.
SetEntries(...SelectOption) supports standalone Value, Label, Group and Disabled. Display the title before consecutive groups, and use Value for empty Label. Value must be non-empty and unique. Illegal data will panic before modification; Entries() returns a copy. The old Select(label, options...) / SetOptions still take literals as values. Copy data for all entries and clean up removed selections after dynamic replacement. SetOptionDisabled(value, on) controls a single option; clicks and keyboard skip over disabled items, programmatic assignment still allows it.
Multiple() Enables multi-selection, click, space or enter to switch the current item and keep the drop-down box open, Esc or external click to close. Values() returns an independent copy in option order, SetValues replaces the selection without triggering a callback, and OnValuesChange receives the user-modified copy. Value() is the primary value, multiple selections should use Values(); SetValue will be replaced with a single value. Use Label for display and Value for callback.
Search for matching tags or values. List virtualization only builds rows near the viewport, and scrolls to the current option when opened; the arrow keys bypass titles and disabled items, Home/End jumps to the beginning and end, and PageUp/PageDown turns pages. Keyboard focus remains on the option container, preventing long lists from losing focus after recycling rows; returning to fields after closing. Examples include grouping, disabled items, and 10,000 multiple-select options.
Customized displays and menus
RenderItem(func(*el.Context, SelectItemContext) el.Element) Customizes visible row content; context includes Entries index, option copy, selection, and keyboard activity state. Return nil to use the default text, the outer layer is still responsible for option semantics, disabling, checking and selecting. Content should be presented as display elements, and interactions should be placed in independent controls. RenderValue receives a copy of the selected item and customizes the display when closed; the placeholder prompt remains the default and returns nil to restore the label. The value of Agent continues to use the stored value and does not follow the custom drawn text.
TitlePrefix("City: ") Adds prefix only when selected, up to half field width and truncated. Empty(view) replaces no matching content, nil returns to default; empty content area can be scrolled. Match(func(SelectOption,string) bool) Replaces the search match on the original query received; nil restores the tag/value containment match. Recalling Match after the data the closure depends on changes will invalidate the cache.
Clearable(true) displays an independent clear button, clears selections, queries and errors and closes the menu. The focus returns to the field; the multi-select callback receives nil, and OnChange receives an empty string when the main value changes. Empty selections do not send callbacks repeatedly, the procedure SetValue/SetValues remains silent, and disabling inheritance blocks the button.
The trigger behaves like a button: keyboard focus uses a subtle background fill by default, with a focus border only when the palette explicitly enables Outline. After a click or a pointer pick, focus still returns to the field for the keyboard, without the ring.
MenuWidth(dp) sets the menu width, 0 follows the field; MenuMaxHeight(dp) sets the height budget including search/blank space, 0 defaults to 240dp, the minimum positive number is 64dp, and the window available space takes priority. Size(dp) scales fields, text, spacing and default line height, recommended 28/36/48, minimum positive number is 20; RowHeight(dp) independently sets unified options/group height, minimum positive number is 16, 0 follows Size. The custom content must fit within the row height, and the excess part will be clipped by the virtual list. The above interface ignores negative numbers and non-finite values.
Appearance(false) Removes field background and borders, retaining labels, error text, keyboard, options, and disabled behavior. The menu still uses the theme overlay appearance.
MenuAlign(el.End) aligns the popup with the right edge of the field, useful for wider menus in right-aligned settings controls. The default is el.Start.
Default virtual row heights also follow accessibility text scaling. An explicit
RowHeight remains an application-owned fixed dp height.