github.com/soypat/vectytemplater@v0.0.0-20220501050640-d40b24e35168/_templates/websocket-cli/app/views/newitem.go (about) 1 package views 2 3 import ( 4 "vecty-templater-project/app/store/actions" 5 "vecty-templater-project/model" 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 actions.Dispatch(&actions.NewItem{Item: model.Item{Title: val}}) // add new item 36 actions.Dispatch(&actions.Back{}) // Back to previous page. 37 } 38 39 func itemToHTML(e model.Item) *vecty.HTML { 40 return elem.Span( 41 elem.Strong(vecty.Text(e.Title)), 42 elem.Paragraph(vecty.Text(e.Description)), 43 ) 44 }