Keel v0.5.1 中文 GitHub

Radio

Live example: a real Keel application compiled to WebAssembly. Interact with it here. The initial download is about 10 MB.Open full screen
Example sourceGo · Copyable

This is the complete source for the gallery example above.examples/components/radio.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 (
	"github.com/dyike/keel/ui/core"
	"github.com/dyike/keel/ui/el"
	"github.com/dyike/keel/ui/kit"
	"github.com/dyike/keel/ui/theme"
)

func init() {
	registerSection("radio", "inputs", func() core.Widget {
		// Standalone radios placed freely, kept exclusive by the app.
		express, pickup := kit.Radio(demoText("Delivery", "快递配送")), kit.Radio(demoText("Store pickup", "门店自提"))
		express.SetValue(true)
		express.OnChange(func(bool) { pickup.SetValue(false) })
		pickup.OnChange(func(bool) { express.SetValue(false) })
		card := func(r *kit.RadioView, note string, cx *el.Context) el.Element {
			return el.Div().P(12).Rounded(theme.RadiusMd).Border(1, theme.Outline).Bg(theme.Surface).Gap(4).
				Child(r.Render(cx), el.Text(note).TextSize(theme.TextSm).TextColor(theme.Muted))
		}
		return el.Embed(el.ViewFunc(func(cx *el.Context) el.Element {
			return el.Div().ScrollY().P(24).Gap(16).Items(el.Start).Child(
				el.Text(demoText("Radio", "单选按钮")).TextSize(24).Bold(),
				el.Text(demoText("Placed in separate cards; the application keeps them mutually exclusive in OnChange.", "分散在不同卡片里,由应用在 OnChange 中保持互斥。")).TextColor(theme.Muted),
				el.Div().Row().Wrap().Gap(12).Items(el.Stretch).Child(card(express, demoText("Delivery in 1–2 days", "1–2 天送达"), cx), card(pickup, demoText("Same-day pickup", "当天可取"), cx)))
		}))
	})
}

The options are scattered in different cards and table rows. When RadioGroup is not used, use a single kit.Radio(label):

express, pickup := kit.Radio("Delivery"), kit.Radio("Store pickup")
express.SetValue(true)
express.OnChange(func(bool) { pickup.SetValue(false) })
pickup.OnChange(func(bool) { express.SetValue(false) })
  • Click or space to select; clicking again while already selected will not cancel it, so the mutual exclusion is handled by the application in OnChange. The program call SetValue does not trigger the callback.
  • Appearance is consistent with RadioGroup's options: Size, TextSize, Content Replace text labels with any view (the construction parameter is still the accessibility name).
  • Clicking is invalid after disabling; the Agent role is radio, and selected is true when selected.
  • A common set of options still recommends RadioGroup: it comes with arrow key switching and Tab entry rules.

Verification: go run ./examples/components -section radio.