From 09b06ed0e0188b8559620e4e0c4f7e54599044af Mon Sep 17 00:00:00 2001 From: Steve Manuel Date: Tue, 25 Oct 2016 11:35:10 -0700 Subject: adding Tags input type and implementing a test on post --- management/editor/elements.go | 68 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 68 insertions(+) (limited to 'management/editor') diff --git a/management/editor/elements.go b/management/editor/elements.go index 4d829ad..9f679ac 100644 --- a/management/editor/elements.go +++ b/management/editor/elements.go @@ -332,6 +332,74 @@ func Checkbox(fieldName string, p interface{}, attrs, options map[string]string) 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) // returns refelct.Value + tags := values.Slice(0, values.Len()).Interface().([]string) // casts reflect.Value to []string + + html := ` +
+
+
+
+ ` + + var initial []string + i := 0 + for _, tag := range tags { + tagName := tagNameFromStructFieldMulti(fieldName, i, p) + html += `` + initial = append(initial, `{tag: `+tag+`}`) + i++ + } + + script := ` + + ` + + html += `
` + + return []byte(html + script) +} + // domElementSelfClose is a special DOM element which is parsed as a // self-closing tag and thus needs to be created differently func domElementSelfClose(e *element) []byte { -- cgit v1.2.3