github.com/soypat/vectytemplater@v0.0.0-20220501050640-d40b24e35168/_templates/default/views/newitem.go (about) 1 package views 2 3 import ( 4 "vecty-templater-project/store/actions" 5 "vecty-templater-project/store/dispatcher" 6 7 "github.com/hexops/vecty" 8 "github.com/hexops/vecty/elem" 9 "github.com/hexops/vecty/event" 10 ) 11 12 type NewItem struct { 13 vecty.Core 14 input *vecty.HTML 15 } 16 17 func (l *NewItem) Render() vecty.ComponentOrHTML { 18 l.input = elem.Input() 19 20 return elem.Form( 21 vecty.Markup( 22 // PreventDefault prevents the Form from navigating away from page. 23 event.Submit(l.addItem).PreventDefault(), 24 ), 25 elem.Label(vecty.Text("Press enter to add item.")), 26 l.input, 27 ) 28 } 29 30 func (l *NewItem) addItem(e *vecty.Event) { 31 val := l.input.Node().Get("value").String() 32 if val == "" { 33 return // do not add empty items. 34 } 35 dispatcher.Dispatch(&actions.NewItem{Item: val}) // add new item 36 dispatcher.Dispatch(&actions.Back{}) // Back to previous page. 37 }