Skip to Content
Onevra

TagInput

Edits a list of short values as removable pills before a draft field.

Loading the live example…

tag_input.go
import (
	"strings"

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

type labels struct {
	tags []string
}

func (l *labels) View(c *ui.Context) {
	ui.Column(c).Width(380).MaxWidthPercent(100).Gap(8).Children(func() {
		tags := components.TagInput(c, &l.tags).Label("Labels").Placeholder("Add a label")
		tags.FillWidth()
		if tags.Changed() {
			// Applications normalize tags after a change.
			for i, tag := range l.tags {
				l.tags[i] = strings.ToLower(tag)
			}
		}
		components.Text(c, "Type and press Enter or a comma. Backspace removes the last label.").
			TextColor(c.Theme().TextMuted)
	})
}
Source

Enter or a comma adds the draft as a tag, and pasted comma-separated text adds each part. A duplicate pulses the existing tag instead. Backspace at the start of the draft picks the last tag, and pressing it again removes it.

Validate or normalize tags after Changed().

API

TagInput

func TagInput(c *ui.Context, tags *[]string) *TagInputControl

TagInput edits application-owned *tags, distinct plain strings shown as removable pills before a draft field. Enter or a comma adds the trimmed draft, as does leaving the field; pasted comma-separated text adds each part. A duplicate, compared without case, pulses the existing tag instead; Enter keeps such a draft for correction. Backspace at the start of the draft picks the last tag and removes it when pressed again; Delete removes a picked tag, the arrow keys move the pick and Escape clears it. Clicking a tag picks it. Announcements report additions, removals and picks.

Tags wrap onto new rows while the field's height follows on a spring. New tags blur and scale in, removed tags leave and the rest glide into place; Reduced Motion skips the movement. Applications validate or normalize tags after Changed. Each tag animates individually, which suits dozens of tags, not hundreds. Key a parent for stable identity; construction resolves interaction state.

pkg.go.dev

TagInputControl

TagInputControl exposes layout on Element and editing on the draft field. Keep this handle to query Changed after applying Element layout methods.

Prop

Type

Methods
func (n *TagInputControl) AutoFocus() *TagInputControl

AutoFocus focuses the draft field.

func (n *TagInputControl) Changed() bool

Changed reports a tag added or removed by the user in this frame.

func (n *TagInputControl) Label(label string) *TagInputControl

Label names the draft field. Remove buttons are named after their tags.

func (n *TagInputControl) Placeholder(text string) *TagInputControl

Placeholder shows text in the field while it holds no tags.

pkg.go.dev
Edit this page on GitHub

On this page