github.com/jgarto/itcv@v0.0.0-20180826224514-4eea09c1aa0d/examples/immtodoapp/todo_app.go (about) 1 package immtodoapp // import "myitcv.io/react/examples/immtodoapp" 2 3 import ( 4 "fmt" 5 6 "honnef.co/go/js/dom" 7 "myitcv.io/react" 8 ) 9 10 //go:generate reactGen 11 //go:generate immutableGen 12 13 // TodoAppDef is the definition fo the TodoApp component 14 type TodoAppDef struct { 15 react.ComponentDef 16 } 17 18 type _Imm_item struct { 19 name string 20 } 21 22 type _Imm_itemS []*item 23 24 // TodoAppState is the state type for the TodoApp component 25 type TodoAppState struct { 26 items *itemS 27 currItem string 28 } 29 30 // TodoApp creates instances of the TodoApp component 31 func TodoApp() *TodoAppElem { 32 return buildTodoAppElem() 33 } 34 35 func (t TodoAppDef) GetInitialState() TodoAppState { 36 return TodoAppState{ 37 items: new(itemS), 38 } 39 } 40 41 // Render renders the TodoApp component 42 func (t TodoAppDef) Render() react.Element { 43 var entries []react.RendersLi 44 45 for _, v := range t.State().items.Range() { 46 entries = append(entries, Entry(v.name())) 47 } 48 49 return react.Div(nil, 50 react.H3(nil, react.S("TODO")), 51 react.Ul(nil, entries...), 52 react.Form(&react.FormProps{ClassName: "form-inline"}, 53 react.Div( 54 &react.DivProps{ClassName: "form-group"}, 55 react.Label(&react.LabelProps{ClassName: "sr-only", For: "todoText"}, react.S("Todo Item")), 56 react.Input(&react.InputProps{ 57 Type: "text", 58 ClassName: "form-control", 59 ID: "todoText", 60 Placeholder: "Todo Item", 61 Value: t.State().currItem, 62 OnChange: inputChange{t}, 63 }), 64 react.Button(&react.ButtonProps{ 65 Type: "submit", 66 ClassName: "btn btn-default", 67 OnClick: add{t}, 68 }, react.S(fmt.Sprintf("Add #%v", t.State().items.Len()+1))), 69 ), 70 ), 71 ) 72 } 73 74 type inputChange struct{ t TodoAppDef } 75 type add struct{ t TodoAppDef } 76 77 func (i inputChange) OnChange(se *react.SyntheticEvent) { 78 target := se.Target().(*dom.HTMLInputElement) 79 80 ns := i.t.State() 81 ns.currItem = target.Value 82 83 i.t.SetState(ns) 84 } 85 86 func (a add) OnClick(se *react.SyntheticMouseEvent) { 87 ns := a.t.State() 88 89 ns.items = ns.items.Append(new(item).setName(ns.currItem)) 90 ns.currItem = "" 91 92 a.t.SetState(ns) 93 94 se.PreventDefault() 95 }