Skip to Content
Onevra

TextBox

A text field, single-line or multiline, with optional icons and prefixes.

Loading the live example…

text_box.go
import (
	"github.com/egoist/mygo/ui"
	"github.com/oneo-me/onevra/source/components"
	"github.com/oneo-me/onevra/source/icons/lucide"
)

type accountForm struct {
	name, query, price, id string
}

func (f *accountForm) View(c *ui.Context) {
	ui.Column(c).Width(320).MaxWidthPercent(100).Gap(12).Children(func() {
		components.TextBox(c, &f.name, components.TextBoxOptions{}).
			Label("Name").Placeholder("Your name").FillWidth()
		components.TextBox(c, &f.query, components.TextBoxOptions{LeadingIcon: lucide.Search()}).
			Label("Search").Placeholder("Search").FillWidth()
		components.TextBox(c, &f.price, components.TextBoxOptions{LeadingText: "$"}).
			Label("Price").Placeholder("0.00").FillWidth()
		components.TextBox(c, &f.id, components.TextBoxOptions{ReadOnly: true}).
			Label("Account ID").FillWidth()
	})
}
Source

components.TextBox edits a *string. Chain MyGo's input methods on it, such as Label, Placeholder and Password. LeadingIcon and LeadingText add a decoration or a prefix that is not part of the value, and ReadOnly keeps the text selectable but not editable.

Multiline

Multiline accepts line breaks. Lines(min, max) grows the field with its text and scrolls past the maximum.

Loading the live example…

text_box_multiline.go
import (
	"fmt"
	"unicode/utf8"

	"github.com/egoist/mygo/ui"
	"github.com/oneo-me/onevra/source/components"
)

type feedback struct {
	message string
}

func (f *feedback) View(c *ui.Context) {
	ui.Column(c).Width(360).MaxWidthPercent(100).Gap(8).Children(func() {
		components.TextBox(c, &f.message, components.TextBoxOptions{Multiline: true}).
			Label("Feedback").Placeholder("Tell us what you think…").Lines(4, 8).FillWidth()
		components.Text(c, fmt.Sprintf("%d characters", utf8.RuneCountInString(f.message))).
			TextColor(c.Theme().TextMuted)
	})
}
Source

Buffers

components.TextBoxBuffer edits a *ui.TextBuffer the application keeps, so editing a long document does not copy it into a string every frame.

Loading the live example…

text_box_buffer.go
import (
	"fmt"

	"github.com/egoist/mygo/ui"
	"github.com/oneo-me/onevra/source/components"
)

type notes struct {
	// buffer is kept across frames; editing does not copy the whole text.
	buffer *ui.TextBuffer
}

func (n *notes) View(c *ui.Context) {
	ui.Column(c).Width(360).MaxWidthPercent(100).Gap(8).Children(func() {
		components.TextBoxBuffer(c, n.buffer, components.TextBoxOptions{Multiline: true}).
			Label("Notes").Lines(4, 8).FillWidth()
		components.Text(c, fmt.Sprintf("%d bytes", n.buffer.Len())).TextColor(c.Theme().TextMuted)
	})
}
Source

API

TextBox

func TextBox(c *ui.Context, value *string, options TextBoxOptions) ui.Element

TextBox creates a text input editing *value. Configure mode and read-only state through options, not the returned Element's ReadOnly method, so styling matches behavior. Other MyGo input properties can be chained normally, such as Label, Placeholder, Password (single-line only) and Lines (multiline only). For stable identity, key a parent container rather than this element: interaction styling is resolved during construction.

pkg.go.dev

TextBoxOptions

TextBoxOptions configures TextBox and TextBoxBuffer editing behavior and appearance.

Prop

Type

pkg.go.dev

TextBoxBuffer

func TextBoxBuffer(c *ui.Context, value *ui.TextBuffer, options TextBoxOptions) ui.Element

TextBoxBuffer creates a text input editing an application-owned buffer. Keep the non-nil buffer across builds; ordinary editing does not materialize the whole document as a string. Options and chained input properties follow TextBox, including its parent-key requirement for stable identity.

pkg.go.dev
Edit this page on GitHub

On this page