trackby

package
v0.0.2 Latest Latest
Warning

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

Go to latest
Published: Mar 8, 2026 License: Apache-2.0 Imports: 1 Imported by: 0

README

TrackBy Test Components

This directory tests both formats of the trackBy clause:

  1. Bare Variable - For primitive types (string, int, etc.)
  2. Dot-Notation - For struct fields (user.ID, etc.)

Components

TagList

Tests bare variable trackBy with a slice of primitive strings:

type TagList struct {
    Tags []string  // Slice of primitive strings
}

Template:

{@for _, tag := range Tags trackBy tag}
    <li>Tag: {tag}</li>
{@endfor}
ProductList

Tests dot-notation trackBy with a slice of structs:

type Product struct {
    ID   int
    Name string
}

type ProductList struct {
    Products []Product  // Slice of structs
}

Template:

{@for _, product := range Products trackBy product.ID}
    <li>Product: {product.Name}</li>
{@endfor}

Building

Both test components can be compiled together:

cd compiler
go run . -in ../testcomponents/trackby

Expected output:

🎉 Compilation completed successfully!

Both trackBy formats compile without errors and generate proper Render() methods.

Key Differences

Feature Bare Variable Dot-Notation
Use Case Primitive types (string, int, bool) Struct fields
Syntax trackBy varName trackBy varName.Field
Example trackBy tag trackBy product.ID
Validation Validates match with loop variable Validates field exists on struct type

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Item

type Item struct {
	ID   int
	Name string
}

Item represents a data item with ID for trackBy

type MultiItemList

type MultiItemList struct {
	runtime.ComponentBase
	Items []Item
}

MultiItemList demonstrates trackBy with multiple sibling child elements per loop iteration. This tests a critical compiler edge case where multiple children in a loop body previously caused variable shadowing errors.

func (*MultiItemList) AddItem

func (m *MultiItemList) AddItem(name string)

func (*MultiItemList) ClearItems

func (m *MultiItemList) ClearItems()

func (*MultiItemList) OnMount

func (m *MultiItemList) OnMount()

type Product

type Product struct {
	ID   int
	Name string
}

Product represents an item with an ID for trackBy

type ProductList

type ProductList struct {
	runtime.ComponentBase
	Products []Product
}

ProductList demonstrates trackBy with struct slice using dot-notation

func (*ProductList) AddProduct

func (p *ProductList) AddProduct(name string)

func (*ProductList) ClearProducts

func (p *ProductList) ClearProducts()

func (*ProductList) OnMount

func (p *ProductList) OnMount()

type TagList

type TagList struct {
	runtime.ComponentBase
	Tags []string
}

TagList demonstrates trackBy with primitive string slice

func (*TagList) AddTag

func (t *TagList) AddTag(newTag string)

func (*TagList) ClearTags

func (t *TagList) ClearTags()

func (*TagList) OnMount

func (t *TagList) OnMount()

Jump to

Keyboard shortcuts

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