autocomplete

package
v0.1.5 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Apr 27, 2026 License: MIT Imports: 4 Imported by: 0

Documentation

Overview

Package autocomplete provides typeahead/autocomplete input components.

Available variants:

  • New() creates a basic autocomplete (template: "lvt:autocomplete:default:v1")
  • NewMulti() creates a multi-select autocomplete (template: "lvt:autocomplete:multi:v1")

Required lvt-* attributes: lvt-on:input, name, lvt-on:keydown, lvt-el:removeClass:on:click-away

Example usage:

// In your controller/state
Search: autocomplete.New("search",
    autocomplete.WithPlaceholder("Search cities..."),
    autocomplete.WithSuggestions([]autocomplete.Suggestion{
        {Value: "nyc", Label: "New York City"},
        {Value: "la", Label: "Los Angeles"},
    }),
)

// In your template
{{template "lvt:autocomplete:default:v1" .Search}}

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Templates

func Templates() *base.TemplateSet

Templates returns the autocomplete component's template set for registration with the LiveTemplate framework.

Example usage in main.go:

import "github.com/livetemplate/lvt/components/autocomplete"

tmpl, err := livetemplate.New("app",
    livetemplate.WithComponentTemplates(autocomplete.Templates()),
)

Available templates:

  • "lvt:autocomplete:default:v1" - Basic autocomplete
  • "lvt:autocomplete:multi:v1" - Multi-select autocomplete

Types

type Autocomplete

type Autocomplete struct {
	base.Base

	// Query is the current input text
	Query string

	// Selected is the currently selected suggestion (nil if none)
	Selected *Suggestion

	// Suggestions is the full list of available suggestions
	Suggestions []Suggestion

	// FilteredSuggestions is the currently shown suggestions
	FilteredSuggestions []Suggestion

	// Placeholder text shown when empty
	Placeholder string

	// MinChars is the minimum characters before showing suggestions (default: 1)
	MinChars int

	// MaxSuggestions limits shown suggestions (0 for unlimited)
	MaxSuggestions int

	// Open indicates whether suggestions are visible
	Open bool

	// HighlightedIndex is the currently highlighted suggestion (-1 for none)
	HighlightedIndex int

	// Loading indicates an async search is in progress
	Loading bool

	// AllowCustom allows values not in suggestions
	AllowCustom bool

	// ClearOnSelect clears input after selection (useful for multi)
	ClearOnSelect bool
	// contains filtered or unexported fields
}

Autocomplete is a typeahead input component. Use template "lvt:autocomplete:default:v1" to render.

func New

func New(id string, opts ...Option) *Autocomplete

New creates a basic autocomplete.

Example:

ac := autocomplete.New("search",
    autocomplete.WithPlaceholder("Search..."),
    autocomplete.WithSuggestions(suggestions),
)

func (*Autocomplete) Clear

func (ac *Autocomplete) Clear()

Clear clears the selection and query.

func (*Autocomplete) DisplayValue

func (ac *Autocomplete) DisplayValue() string

DisplayValue returns the display text (selected label or query).

func (*Autocomplete) Filter

func (ac *Autocomplete) Filter()

Filter filters suggestions based on the current query.

func (*Autocomplete) HasSelection

func (ac *Autocomplete) HasSelection() bool

HasSelection returns true if a suggestion is selected.

func (*Autocomplete) HighlightNext

func (ac *Autocomplete) HighlightNext()

HighlightNext moves highlight to the next suggestion.

func (*Autocomplete) HighlightPrevious

func (ac *Autocomplete) HighlightPrevious()

HighlightPrevious moves highlight to the previous suggestion.

func (*Autocomplete) IsHighlighted

func (ac *Autocomplete) IsHighlighted(index int) bool

IsHighlighted checks if a suggestion index is highlighted.

func (*Autocomplete) Select

func (ac *Autocomplete) Select(s Suggestion) bool

Select selects a suggestion.

func (*Autocomplete) SelectHighlighted

func (ac *Autocomplete) SelectHighlighted() bool

SelectHighlighted selects the currently highlighted suggestion.

func (*Autocomplete) SelectIndex

func (ac *Autocomplete) SelectIndex(index int) bool

SelectIndex selects the suggestion at the given index.

func (*Autocomplete) SetLoading

func (ac *Autocomplete) SetLoading(loading bool)

SetLoading sets the loading state.

func (*Autocomplete) SetQuery

func (ac *Autocomplete) SetQuery(query string)

SetQuery updates the search query and filters suggestions.

func (*Autocomplete) SetSuggestions

func (ac *Autocomplete) SetSuggestions(suggestions []Suggestion)

SetSuggestions updates the suggestions and re-filters.

func (*Autocomplete) Styles

Styles returns the resolved AutocompleteStyles for this component.

type MultiAutocomplete

type MultiAutocomplete struct {
	Autocomplete

	// SelectedItems contains all selected suggestions
	SelectedItems []Suggestion
}

MultiAutocomplete allows selecting multiple suggestions. Use template "lvt:autocomplete:multi:v1" to render.

func NewMulti

func NewMulti(id string, opts ...Option) *MultiAutocomplete

NewMulti creates a multi-select autocomplete.

func (*MultiAutocomplete) ClearMulti

func (mac *MultiAutocomplete) ClearMulti()

ClearMulti clears all selected items.

func (*MultiAutocomplete) FilteredExcludingSelected

func (mac *MultiAutocomplete) FilteredExcludingSelected() []Suggestion

FilteredExcludingSelected returns filtered suggestions excluding already selected items.

func (*MultiAutocomplete) HasSelectedItems

func (mac *MultiAutocomplete) HasSelectedItems() bool

HasSelectedItems returns true if any items are selected.

func (*MultiAutocomplete) IsSelectedMulti

func (mac *MultiAutocomplete) IsSelectedMulti(value string) bool

IsSelectedMulti checks if a value is in selected items.

func (*MultiAutocomplete) RemoveSelected

func (mac *MultiAutocomplete) RemoveSelected(value string) bool

RemoveSelected removes a suggestion from selected items by value.

func (*MultiAutocomplete) SelectMulti

func (mac *MultiAutocomplete) SelectMulti(s Suggestion) bool

SelectMulti adds a suggestion to selected items.

func (*MultiAutocomplete) SelectedValues

func (mac *MultiAutocomplete) SelectedValues() []string

SelectedValues returns the values of all selected items.

type Option

type Option func(*Autocomplete)

Option is a functional option for configuring autocomplete components.

func WithAllowCustom

func WithAllowCustom(allow bool) Option

WithAllowCustom allows values not in the suggestions list.

func WithClearOnSelect

func WithClearOnSelect(clear bool) Option

WithClearOnSelect clears the input after selection.

func WithFilterFunc

func WithFilterFunc(fn func(query string, suggestions []Suggestion) []Suggestion) Option

WithFilterFunc sets a custom filter function.

func WithMaxSuggestions

func WithMaxSuggestions(max int) Option

WithMaxSuggestions sets the maximum number of shown suggestions.

func WithMinChars

func WithMinChars(min int) Option

WithMinChars sets the minimum characters before showing suggestions.

func WithPlaceholder

func WithPlaceholder(placeholder string) Option

WithPlaceholder sets the placeholder text.

func WithQuery

func WithQuery(query string) Option

WithQuery sets the initial query.

func WithSelected

func WithSelected(s Suggestion) Option

WithSelected sets the initially selected suggestion.

func WithStyled

func WithStyled(styled bool) Option

WithStyled enables Tailwind CSS styling for the component.

func WithSuggestions

func WithSuggestions(suggestions []Suggestion) Option

WithSuggestions sets the initial suggestions.

type Suggestion

type Suggestion struct {
	// Value is the internal value
	Value string
	// Label is the display text
	Label string
	// Description is optional secondary text
	Description string
	// Icon is an optional icon class/name
	Icon string
	// Disabled prevents selection
	Disabled bool
	// Data holds arbitrary custom data
	Data map[string]any
}

Suggestion represents an autocomplete suggestion.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL