Skip to content

Checkbox

KeyAction
SpaceToggle checked state

Enter is reserved for form submission.

Checkbox implements FormField. The bound value type is bool. SetFieldValue accepts bool or string ("true"/"false").

Checkbox preview

Checkbox is a boolean toggle component. It implements ValueProvider[bool].

Constructor func NewCheckbox(name string) *Checkbox

Usage

check := components.NewCheckbox("notify").
SetLabel("Enable notifications").
SetChecked(true)
check.SetOnChange(func(e *components.ChangeEvent[bool]) {
fmt.Println("checked:", e.NewValue)
})
fmt.Println(check.Value())

Methods

Method Signature Description
Checked func (c *Checkbox) Checked() bool Checked returns the checked state.
Clear func (c *Checkbox) Clear() Clear resets the checkbox to unchecked.
ClearField func (c *Checkbox) ClearField() ClearField resets the checkbox to false. Implements FormField.
FieldValue func (c *Checkbox) FieldValue() any FieldValue returns the checkbox's current value as an any. Implements FormField.
GetFieldHeight func (c *Checkbox) GetFieldHeight() int GetFieldHeight returns the preferred height for this field.
GetName func (c *Checkbox) GetName() string GetName returns the field name.
GetValue func (c *Checkbox) GetValue() bool GetValue returns the checked state.
HasValue func (c *Checkbox) HasValue() bool HasValue returns true (checkbox always has a value).
SetChecked func (c *Checkbox) SetChecked(checked bool) *Checkbox SetChecked sets the checked state.
SetFieldValue func (c *Checkbox) SetFieldValue(value any) error SetFieldValue sets the checkbox's value from an any. Implements FormField.
SetLabel func (c *Checkbox) SetLabel(label string) *Checkbox SetLabel sets the checkbox label.
SetOnChange func (c *Checkbox) SetOnChange(handler ChangeHandler[bool]) *Checkbox SetOnChange sets the change handler (new API).
Toggle func (c *Checkbox) Toggle() *Checkbox Toggle toggles the checked state.
Value func (c *Checkbox) Value() bool Value returns the checked state (alias for Checked). This method is part of the ValueProvider interface.

Types

type ChangeHandler ChangeHandler handles value changes