You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
53 lines
975 B
Go
53 lines
975 B
Go
1 year ago
|
package input
|
||
|
|
||
|
import (
|
||
|
"strconv"
|
||
|
|
||
|
"github.com/vugu/vugu"
|
||
|
"golang.org/x/exp/slices"
|
||
|
)
|
||
|
|
||
|
// Tags provides a way for the user to add/remove entries from a slice.
|
||
|
type Tags struct {
|
||
|
AttrMap vugu.AttrMap
|
||
|
|
||
|
Bind ListBinder // Binds the shown tags to some slice.
|
||
|
|
||
|
fieldValue string
|
||
|
}
|
||
|
|
||
|
func (c *Tags) keyValuePairs() []ListKeyValuePair {
|
||
|
if c.Bind == nil {
|
||
|
return nil
|
||
|
}
|
||
|
|
||
|
return c.Bind.ListKeyValuePairs()
|
||
|
}
|
||
|
|
||
|
func (c *Tags) handleButtonAdd(event vugu.DOMEvent) {
|
||
|
if c.Bind == nil {
|
||
|
return
|
||
|
}
|
||
|
|
||
|
// Don't allow empty entries.
|
||
|
if c.fieldValue == "" {
|
||
|
return
|
||
|
}
|
||
|
|
||
|
// Don't allow duplicate entries.
|
||
|
values := c.Bind.ListKeyValuePairs()
|
||
|
if slices.ContainsFunc(values, func(k ListKeyValuePair) bool { return k.Value == c.fieldValue }) {
|
||
|
return
|
||
|
}
|
||
|
|
||
|
c.Bind.ListAddKeyValuePair(strconv.Itoa(c.Bind.ListLen()), c.fieldValue)
|
||
|
}
|
||
|
|
||
|
func (c *Tags) handleButtonDelete(event vugu.DOMEvent, index int) {
|
||
|
if c.Bind == nil {
|
||
|
return
|
||
|
}
|
||
|
|
||
|
c.Bind.ListDeleteKey(strconv.Itoa(index))
|
||
|
}
|