package editor
import (
"bytes"
"fmt"
"log"
"strings"
)
// InputRepeater returns the []byte of an HTML element with a label.
// It also includes repeat controllers (+ / -) so the element can be
// dynamically multiplied or reduced.
// 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
// type Person struct {
// item.Item
// editor editor.Editor
//
// Names []string `json:"names"`
// //...
// }
//
// func (p *Person) MarshalEditor() ([]byte, error) {
// view, err := editor.Form(p,
// editor.Field{
// View: editor.InputRepeater("Names", p, map[string]string{
// "label": "Names",
// "type": "text",
// "placeholder": "Enter a Name here",
// }),
// }
// )
// }
func InputRepeater(fieldName string, p interface{}, attrs map[string]string) []byte {
// find the field values in p to determine pre-filled inputs
fieldVals := ValueFromStructField(fieldName, p)
vals := strings.Split(fieldVals, "__ponzu")
scope := TagNameFromStructField(fieldName, p)
html := bytes.Buffer{}
_, err := html.WriteString(``)
if err != nil {
log.Println("Error writing HTML string to InputRepeater buffer")
return nil
}
for i, val := range vals {
el := &Element{
TagName: "input",
Attrs: attrs,
Name: TagNameFromStructFieldMulti(fieldName, i, p),
Data: val,
ViewBuf: &bytes.Buffer{},
}
// only add the label to the first input in repeated list
if i == 0 {
el.Label = attrs["label"]
}
_, err := html.Write(DOMElementSelfClose(el))
if err != nil {
log.Println("Error writing DOMElementSelfClose to InputRepeater buffer")
return nil
}
}
_, err = html.WriteString(``)
if err != nil {
log.Println("Error writing HTML string to InputRepeater buffer")
return nil
}
return append(html.Bytes(), RepeatController(fieldName, p, "input", ".input-field")...)
}
// SelectRepeater returns the []byte of a