-
Notifications
You must be signed in to change notification settings - Fork 6
/
form_group.go
248 lines (219 loc) · 7.63 KB
/
form_group.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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
package control
import (
"context"
"github.com/goradd/goradd/pkg/html"
"github.com/goradd/goradd/pkg/log"
"github.com/goradd/goradd/pkg/page"
"github.com/goradd/goradd/pkg/page/control"
"github.com/goradd/goradd/pkg/pool"
html2 "html"
"strings"
)
type FormGroupI interface {
control.FormFieldWrapperI
SetUseTooltips(use bool) FormGroupI
UseTooltips() bool
InnerDivAttributes() html.Attributes
}
// FormGroup is a Goradd control that wraps other controls, and provides common companion
// functionality like a form label, validation state display, and help text.
type FormGroup struct {
control.FormFieldWrapper
innerDivAttr html.Attributes
useTooltips bool // uses tooltips for the error class
}
func NewFormGroup(parent page.ControlI, id string) *FormGroup {
p := &FormGroup{}
p.Init(p, parent, id)
return p
}
func (c *FormGroup) Init(self control.FormFieldWrapperI, parent page.ControlI, id string) {
c.FormFieldWrapper.Init(self, parent, id)
c.innerDivAttr = html.NewAttributes()
c.AddClass("form-group") // to get a wrapper without this, just remove it after initialization
c.InstructionAttributes().AddClass("form-text")
}
func (c *FormGroup) this() FormGroupI {
return c.Self.(FormGroupI)
}
func (c *FormGroup) Validate(ctx context.Context) bool {
c.FormFieldWrapper.Validate(ctx)
child := c.Page().GetControl(c.For())
if child.ValidationMessage() != "" {
child.RemoveClass("is-valid")
child.AddClass("is-invalid")
} else {
child.AddClass("is-valid")
child.RemoveClass("is-invalid")
}
return true
}
// SetUseTooltips sets whether to use tooltips to display validation messages.
func (c *FormGroup) SetUseTooltips(use bool) FormGroupI {
c.useTooltips = use
return c
}
func (c *FormGroup) UseTooltips() bool {
return c.useTooltips
}
func (c *FormGroup) ΩDrawingAttributes() html.Attributes {
a := c.FormFieldWrapper.ΩDrawingAttributes()
a.SetDataAttribute("grctl", "formGroup")
if c.useTooltips {
// bootstrap requires that parent of a tool-tipped object has position relative
c.SetStyle("position", "relative")
}
return a
}
func (c *FormGroup) ΩDrawTag(ctx context.Context) string {
log.FrameworkDebug("Drawing FormFieldWrapper: " + c.ID())
attributes := c.this().ΩDrawingAttributes()
if c.For() == "" {
panic("a FormGroup MUST have a sub control")
}
subControl := c.Page().GetControl(c.For())
errorMessage := subControl.ValidationMessage()
if errorMessage != "" {
attributes.AddClass("error")
errorMessage = html2.EscapeString(errorMessage)
} else {
errorMessage = " "
}
buf := pool.GetBuffer()
defer pool.PutBuffer(buf)
text := c.Text()
if text == "" {
text = subControl.Attribute("placeholder")
if text != "" {
c.LabelAttributes().SetClass("sr-only") // make a hidden label for screen readers
}
}
if text != "" {
c.LabelAttributes().Set("for", c.For())
buf.WriteString(html.RenderTag("label", c.LabelAttributes(), html2.EscapeString(text)))
}
var describedBy string
if errorMessage != "" {
describedBy = c.ID() + "_err"
}
if c.Instructions() != "" {
describedBy += " " + c.ID() + "_inst"
}
describedBy = strings.TrimSpace(describedBy)
if describedBy != "" {
subControl.SetAttribute("aria-describedby", describedBy)
}
hasInnerDiv := c.innerDivAttr.Len() > 0
if hasInnerDiv {
buf.WriteString("<")
buf.WriteString(c.Subtag)
buf.WriteString(" ")
buf.WriteString(c.innerDivAttr.String())
buf.WriteString(">")
}
if err := c.this().ΩDrawInnerHtml(ctx, buf); err != nil {
panic(err)
}
if hasInnerDiv {
buf.WriteString("</")
buf.WriteString(c.Subtag)
buf.WriteString(">")
}
if c.Instructions() != "" {
buf.WriteString(html.RenderTag("small", c.InstructionAttributes(), html2.EscapeString(c.Instructions())))
}
if subControl.ValidationState() != page.ValidationNever {
c.ErrorAttributes().SetClass(c.getValidationClass(subControl))
buf.WriteString(html.RenderTag(c.Subtag, c.ErrorAttributes(), errorMessage))
}
return html.RenderTag(c.Tag, attributes, buf.String())
}
func (c *FormGroup) getValidationClass(subcontrol page.ControlI) (class string) {
switch subcontrol.ValidationState() {
case page.ValidationWaiting: fallthrough // we need to correctly style
case page.ValidationValid:
if c.UseTooltips() {
class = "valid-tooltip"
} else {
class = "valid-feedback"
}
case page.ValidationInvalid:
if c.UseTooltips() {
class = "invalid-tooltip"
} else {
class = "invalid-feedback"
}
}
return
}
func (c *FormGroup) InnerDivAttributes() html.Attributes {
return c.innerDivAttr
}
// Use FormGroupCreator to create a FormGroup,
// which wraps a control with a div or span that also has a label, validation error
// text and optional instructions. Pass the creator of the control you
// are wrapping as the Child item.
type FormGroupCreator struct {
// ID is the optional control id on the html form. If you do not specify this, it
// will create on for you that is the ID of the child control + "-fg"
ID string
// Label is the text that will be in the html label tag associated with the Child control.
Label string
// Child is the creator of the child control you want to wrap
Child page.Creator
// Instructions is help text that will follow the control and that further describes its purpose or use.
Instructions string
// For specifies the id of the control that the label is for, and that is the control that we are wrapping.
// You normally do not need to specify this, as it will default to the first child control, but if for some reason
// that control is wrapped, you should explicitly specify the For control id here.
For string
// LabelAttributes are additional attributes to add to the label tag.
LabelAttributes html.AttributeCreator
// ErrorAttributes are additional attributes to add to the tag that displays the error.
ErrorAttributes html.AttributeCreator
// InstructionAttributes are additional attributes to add to the tag that displays the instructions.
InstructionAttributes html.AttributeCreator
// Set IsInline to true to use a "span" instead of a "div" in the wrapping tag.
IsInline bool
// ControlOptions are additional options for the wrapper tag
ControlOptions page.ControlOptions
// InnerDivAttributes are the attributes for the additional div wrapper of the control
// To achieve certain effects, Bootstrap needs this addition div. To display the div, you
// must specify its attributes here. Otherwise no inner div will be displayed.
InnerDivAttributes html.AttributeCreator
// UseTooltips will cause validation errors to be displayed with tooltips, a specific
// feature of Bootstrap
UseTooltips bool
}
// Create is called by the framework to create the control. You do not
// normally need to call it.
func (f FormGroupCreator) Create(ctx context.Context, parent page.ControlI) page.ControlI {
id := control.CalcWrapperID(f.ID, f.Child, "fg")
c := NewFormGroup(parent, id)
f.Init(ctx, c)
return c
}
// Init is called by implementations of a FormFieldWrapper to initialize
// the creator. You do not normally need to call this.
func (f FormGroupCreator) Init(ctx context.Context, c FormGroupI) {
// Reuse parent creator
ff := control.FormFieldWrapperCreator{
ControlOptions: f.ControlOptions,
Label: f.Label,
For: f.For,
Instructions: f.Instructions,
LabelAttributes: f.LabelAttributes,
ErrorAttributes: f.ErrorAttributes,
InstructionAttributes: f.InstructionAttributes,
Child: f.Child,
}
ff.Init(ctx, c)
if f.InnerDivAttributes != nil {
c.InnerDivAttributes().Merge(f.InnerDivAttributes)
}
c.SetUseTooltips(f.UseTooltips)
}
// GetFormGroup is a convenience method to return the form group with the given id from the page.
func GetFormGroup(c page.ControlI, id string) *FormGroup {
return c.Page().GetControl(id).(*FormGroup)
}