forked from gobuffalo/tags
/
common.go
86 lines (68 loc) · 1.73 KB
/
common.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
package bootstrap
import (
"fmt"
"strings"
"github.com/gobuffalo/flect"
"github.com/gobuffalo/tags"
)
func buildOptions(opts tags.Options, err bool) {
if opts["class"] == nil {
opts["class"] = ""
}
if opts["tag_only"] != true {
opts["class"] = strings.Join([]string{fmt.Sprint(opts["class"]), "form-control"}, " ")
}
if err {
opts["class"] = strings.Join([]string{fmt.Sprint(opts["class"]), "is-invalid"}, " ")
}
delete(opts, "hide_label")
}
func divWrapper(opts tags.Options, fn func(opts tags.Options) tags.Body) *tags.Tag {
divClass := "form-group"
hasErrors := false
errors := []string{}
if opts["errors"] != nil && len(opts["errors"].([]string)) > 0 {
divClass = "form-group has-error"
hasErrors = true
errors = opts["errors"].([]string)
delete(opts, "errors")
}
if opts["bootstrap"] != nil {
bopts, ok := opts["bootstrap"].(map[string]interface{})
if ok {
divClass = bopts["form-group-class"].(string)
}
delete(opts, "bootstrap")
}
div := tags.New("div", tags.Options{
"class": divClass,
})
if opts["label"] == nil && opts["tags-field"] != nil {
if tf, ok := opts["tags-field"].(string); ok {
tf = strings.Join(strings.Split(tf, "."), " ")
opts["label"] = flect.Titleize(tf)
}
}
delete(opts, "tags-field")
useLabel := opts["hide_label"] == nil
if useLabel && opts["label"] != nil {
div.Prepend(tags.New("label", tags.Options{
"body": opts["label"],
}))
delete(opts, "label")
}
buildOptions(opts, hasErrors)
if opts["tag_only"] == true {
return fn(opts).(*tags.Tag)
}
div.Append(fn(opts))
if hasErrors {
for _, err := range errors {
div.Append(tags.New("div", tags.Options{
"class": "invalid-feedback help-block",
"body": err,
}))
}
}
return div
}