跳到正文

Checkbox

二态或不确定状态的选择控件,指示器可独立设置样式。

正在加载交互演示…

#导入

从 github.com/egoist/quickgui/go/ui 导入 ui。

import (
	"github.com/egoist/quickgui/go/native"
	"github.com/egoist/quickgui/go/ui"
)

#用法

func CheckboxExample() *ui.Element {
	checked, setChecked := ui.CreateSignal(false)
	checkbox := ui.NewCheckbox(ui.CheckboxProps{Checked: func() ui.CheckedState {
		return checked()
	}, OnCheckedChange: func(next bool, _ *native.Event) {
		setChecked(next)
	}})
	return checkbox.Root().Children(
		checkbox.Indicator().
			Width(16).Height(16).
			BorderWidth(1).BorderRadius(4).BorderColor("#2563eb").
			Display("flex").AlignItems("center").JustifyContent("center").
			Child(ui.Show(checked, func() *ui.Element {
				return ui.SVG().Value(`<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 12 12"><path d="M3 6l2 2 4-4" fill="none" stroke="#2563eb" stroke-width="1.6" stroke-linecap="round" stroke-linejoin="round"/></svg>`).Width(12).Height(12)
			})),
		ui.Text("Remember me"),
	)
}

指示器不附带样式。此示例保留外框,仅在选中时显示 SVG。设置 ReadOnly: true 后,控件仍可获得焦点,但指针或键盘操作不会改变其值。

#结构

  • checkbox.Root
  • checkbox.Indicator

#API 参考

展开条目查看说明和完整签名。短横线表示声明中未记录默认值。

NewCheckbox

查看源码
ui.NewCheckbox(props ...CheckboxProps) *CheckboxComponent

NewCheckbox declares a Checkbox instance. Optional props provide initial settings.

Reffunc(*native.Node)—

Receives the retained native node after construction. Keep imperative work in event or lifecycle callbacks.

func(*native.Node)
查看源码
Styleany—

Reusable layout, typography, color, and interaction-state styles. Build with ui.Style() and compose with Merge.

any
查看源码
ChildrenPartChildren—

Content mounted inside the component. Compound child callbacks run after the parent context exists.

PartChildren
查看源码
Disabledany—

Prevents activation and exposes the disabled state to accessibility. Compound parts inherit a disabled root.

any
查看源码
Rolestring—

Accessibility role override. Prefer the component’s built-in semantics.

string
查看源码
FocusOnPointerany—

Whether a pointer press moves keyboard focus to the control.

any
查看源码
Groupany—

Names this element as a group for descendant interaction-state styling.

any
查看源码
AriaLabelany—

Accessible name, especially for icon-only controls or controls without a visible label.

any
查看源码
TabIndex*float64—

Controls sequential keyboard focus order. Omit to retain the component’s default focus behavior.

*float64
查看源码
OnClickfunc(*native.Event)—

Called after QuickGUI resolves an activation, including supported keyboard activation.

func(*native.Event)
查看源码
OnDoubleClickfunc(*native.Event)—

Callback for double click. The signature below lists the event payload.

func(*native.Event)
查看源码
OnContextMenufunc(*native.Event)—

Callback for context menu. The signature below lists the event payload.

func(*native.Event)
查看源码
OnPointerfunc(*native.Event)—

Callback for pointer. The signature below lists the event payload.

func(*native.Event)
查看源码
OnMouseEnterfunc(*native.Event)—

Callback for mouse enter. The signature below lists the event payload.

func(*native.Event)
查看源码
OnMouseLeavefunc(*native.Event)—

Callback for mouse leave. The signature below lists the event payload.

func(*native.Event)
查看源码
OnMouseDownfunc(*native.Event)—

Callback for mouse down. The signature below lists the event payload.

func(*native.Event)
查看源码
OnMouseUpfunc(*native.Event)—

Callback for mouse up. The signature below lists the event payload.

func(*native.Event)
查看源码
OnKeyDownfunc(*native.Event)—

Callback for key down. The signature below lists the event payload.

func(*native.Event)
查看源码
OnKeyUpfunc(*native.Event)—

Callback for key up. The signature below lists the event payload.

func(*native.Event)
查看源码
OnFocusfunc(*native.Event)—

Callback for focus. The signature below lists the event payload.

func(*native.Event)
查看源码
OnBlurfunc(*native.Event)—

Callback for blur. The signature below lists the event payload.

func(*native.Event)
查看源码
Valuestring—

The current controlled value. Update application state from the change callback to accept a new value.

string
查看源码
Parentbool—

Marks a checkbox as a parent whose state is derived from its children.

bool
查看源码
ChildrenCheckedfunc() []bool—

Reads the checked states used to derive a parent checkbox’s on, off, or mixed state.

func() []bool
查看源码
ReadOnlybool—

Preserves the current value while keeping the control available for focus and inspection.

bool
查看源码
Checkedfunc() CheckedState—

Controlled selection state. A checkbox also accepts the indeterminate state where supported.

func() CheckedState
查看源码
DefaultCheckedCheckedState—

Initial checked state when no controlled Checked accessor is supplied.

CheckedState
查看源码
OnCheckedChangefunc(bool, *native.Event)—

Reports the next checked state after user interaction.

func(bool, *native.Event)
查看源码

Checkbox.Disabled

查看源码
checkbox.Disabled(value any) *CheckboxComponent

Prevents activation and exposes the disabled state to accessibility. Compound parts inherit a disabled root.

Checkbox.Value

查看源码
checkbox.Value(value string) *CheckboxComponent

The current controlled value. Update application state from the change callback to accept a new value.

Checkbox.Parent

查看源码
checkbox.Parent(value bool) *CheckboxComponent

Marks a checkbox as a parent whose state is derived from its children.

Checkbox.ChildrenChecked

查看源码
checkbox.ChildrenChecked(value any) *CheckboxComponent

Reads the checked states used to derive a parent checkbox’s on, off, or mixed state.

Checkbox.ReadOnly

查看源码
checkbox.ReadOnly(value bool) *CheckboxComponent

Preserves the current value while keeping the control available for focus and inspection.

Checkbox.Checked

查看源码
checkbox.Checked(value any) *CheckboxComponent

Controlled selection state. A checkbox also accepts the indeterminate state where supported.

Checkbox.DefaultChecked

查看源码
checkbox.DefaultChecked(value CheckedState) *CheckboxComponent

Initial checked state when no controlled Checked accessor is supplied.

Checkbox.OnCheckedChange

查看源码
checkbox.OnCheckedChange(value func(bool, *native.Event)) *CheckboxComponent

Reports the next checked state after user interaction.

Checkbox.Root

查看源码
checkbox.Root() *Element

Configures root for this part. The declared type and source define the accepted value.

Checkbox.Indicator

查看源码
checkbox.Indicator(options ...PartProps) *Element

Configures indicator for this part. The declared type and source define the accepted value.

Reffunc(*native.Node)—

Receives the retained native node after construction. Keep imperative work in event or lifecycle callbacks.

func(*native.Node)
查看源码
Styleany—

Reusable layout, typography, color, and interaction-state styles. Build with ui.Style() and compose with Merge.

any
查看源码
ChildrenPartChildren—

Content mounted inside the component. Compound child callbacks run after the parent context exists.

PartChildren
查看源码
Disabledany—

Prevents activation and exposes the disabled state to accessibility. Compound parts inherit a disabled root.

any
查看源码
Rolestring—

Accessibility role override. Prefer the component’s built-in semantics.

string
查看源码
FocusOnPointerany—

Whether a pointer press moves keyboard focus to the control.

any
查看源码
Groupany—

Names this element as a group for descendant interaction-state styling.

any
查看源码
AriaLabelany—

Accessible name, especially for icon-only controls or controls without a visible label.

any
查看源码
TabIndex*float64—

Controls sequential keyboard focus order. Omit to retain the component’s default focus behavior.

*float64
查看源码
OnClickfunc(*native.Event)—

Called after QuickGUI resolves an activation, including supported keyboard activation.

func(*native.Event)
查看源码
OnDoubleClickfunc(*native.Event)—

Callback for double click. The signature below lists the event payload.

func(*native.Event)
查看源码
OnContextMenufunc(*native.Event)—

Callback for context menu. The signature below lists the event payload.

func(*native.Event)
查看源码
OnPointerfunc(*native.Event)—

Callback for pointer. The signature below lists the event payload.

func(*native.Event)
查看源码
OnMouseEnterfunc(*native.Event)—

Callback for mouse enter. The signature below lists the event payload.

func(*native.Event)
查看源码
OnMouseLeavefunc(*native.Event)—

Callback for mouse leave. The signature below lists the event payload.

func(*native.Event)
查看源码
OnMouseDownfunc(*native.Event)—

Callback for mouse down. The signature below lists the event payload.

func(*native.Event)
查看源码
OnMouseUpfunc(*native.Event)—

Callback for mouse up. The signature below lists the event payload.

func(*native.Event)
查看源码
OnKeyDownfunc(*native.Event)—

Callback for key down. The signature below lists the event payload.

func(*native.Event)
查看源码
OnKeyUpfunc(*native.Event)—

Callback for key up. The signature below lists the event payload.

func(*native.Event)
查看源码
OnFocusfunc(*native.Event)—

Callback for focus. The signature below lists the event payload.

func(*native.Event)
查看源码
OnBlurfunc(*native.Event)—

Callback for blur. The signature below lists the event payload.

func(*native.Event)
查看源码
part.Child(content).Style(style)

Every part returns a fluent element. Strings, numbers, elements, and deferred child factories compose directly.

ChildChild(child any) *Element—

Child appends content and returns this element. Content can be an element, native node, scalar, accessor, or node-returning construction callback.

Child(child any) *Element
查看源码
ChildrenChildren(children ...any) *Element—

Children appends multiple children in order without replacing existing content. It also accepts []any, []*Element, and []*native.Node slices as arguments. Read the retained child nodes through element.Node.Children.

Children(children ...any) *Element
查看源码
StyleStyle(style StyleBuilder) *Element—

Style merges a reusable style. Scalar accessors retain independent native bindings.

Style(style StyleBuilder) *Element
查看源码
OnClickOnClick(handler func()) *Element—

OnClick handles a click without an unused event parameter.

OnClick(handler func()) *Element
查看源码
RefRef(handler func(*native.Node)) *Element—

Ref exposes the native handle once at this point in the declaration chain.

Ref(handler func(*native.Node)) *Element
查看源码