with a label.
// IMPORTANT:
// The `fieldName` argument will cause a panic if it is not exactly the string
// form of the struct field that this editor input is representing
func Checkbox(fieldName string, p interface{}, attrs, options map[string]string) []byte {
if _, ok := attrs["class"]; ok {
attrs["class"] += "input-field col s12"
} else {
attrs["class"] = "input-field col s12"
}
div := NewElement("div", attrs["label"], fieldName, p, attrs)
var opts []*Element
// get the pre-checked options if this is already an existing post
checkedVals := ValueFromStructField(fieldName, p)
checked := strings.Split(checkedVals, "__ponzu")
i := 0
for k, v := range options {
inputAttrs := map[string]string{
"type": "checkbox",
"value": k,
"id": strings.Join(strings.Split(v, " "), "-"),
}
// check if k is in the pre-checked values and set to checked
for _, x := range checked {
if k == x {
inputAttrs["checked"] = "checked"
}
}
// create a *element manually using the modified TagNameFromStructFieldMulti
// func since this is for a multi-value name
input := &Element{
TagName: "input",
Attrs: inputAttrs,
Name: TagNameFromStructFieldMulti(fieldName, i, p),
Label: v,
Data: "",
ViewBuf: &bytes.Buffer{},
}
opts = append(opts, input)
i++
}
return DOMElementWithChildrenCheckbox(div, opts)
}
// Tags returns the []byte of a tag input (in the style of Materialze 'Chips') with a label.
// IMPORTANT:
// The `fieldName` argument will cause a panic if it is not exactly the string
// form of the struct field that this editor input is representing
func Tags(fieldName string, p interface{}, attrs map[string]string) []byte {
name := TagNameFromStructField(fieldName, p)
// get the saved tags if this is already an existing post
values := ValueFromStructField(fieldName, p)
var tags []string
if strings.Contains(values, "__ponzu") {
tags = strings.Split(values, "__ponzu")
}
// case where there is only one tag stored, thus has no separator
if len(values) > 0 && !strings.Contains(values, "__ponzu") {
tags = append(tags, values)
}
html := `
`
return []byte(html + script)
}