github.com/egonelbre/exp@v0.0.0-20240430123955-ed1d3aa93911/view/cmd/todolist/main.go (about) 1 package main 2 3 import ui "github.com/egonelbre/exp/view/cli" 4 5 type Item struct { 6 Done bool 7 Value string 8 } 9 10 type App struct { 11 Items []Item 12 } 13 14 func (app *App) Delete(i int) { 15 app.Items = append(app.Items[:i], app.Items[i+1:]) 16 } 17 18 func (app *App) RenderItem(i int) ui.Element { 19 item := app.Items[i] 20 return ui.Node( 21 "item", 22 ui.Text(item.Value), 23 ui.Button( 24 "Delete", 25 ui.On("click", func(s *ui.State) { 26 app.Delete(i) 27 }), 28 ), 29 ) 30 } 31 32 func (app *App) Render() ui.Element { 33 return ui.Node( 34 "application", 35 ui.Class("application"), 36 ui.Input( 37 "", 38 ui.Ref("new-item"), 39 ui.Attr{"placeholder": "Todo item..."}, 40 ), 41 ui.Button( 42 "Add", 43 ui.On("click", func(s *ui.State) { 44 input := s.ByRef("new-item") 45 text := input.Attr("value") 46 app.Items = append(app.Items, Item{Value: text}) 47 }), 48 ), 49 ui.Each(len(app.Items), app.RenderItem), 50 ) 51 } 52 53 func main() { 54 55 }