package editor
import (
"bytes"
"fmt"
"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 {
// Name string `json:"name"`
// }
//
// func (p *Person) MarshalEditor() ([]byte, error) {
// view, err := editor.Form(p,
// editor.Field{
// View: editor.InputRepeater("Name", p, map[string]string{
// "label": "Name",
// "type": "text",
// "placeholder": "Enter the 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{}
html.WriteString(``)
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"]
}
html.Write(domElementSelfClose(el))
}
html.WriteString(``)
return append(html.Bytes(), RepeatController(fieldName, p, "input", ".input-field")...)
}
// SelectRepeater returns the []byte of a