-
Notifications
You must be signed in to change notification settings - Fork 6
/
form_group.go
289 lines (252 loc) · 8.42 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
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
package control
import (
"context"
"html"
"io"
"strings"
"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"
"github.com/goradd/html5tag"
)
type FormGroupI interface {
control.FormFieldWrapperI
SetUseTooltips(use bool) FormGroupI
UseTooltips() bool
InnerDivAttributes() html5tag.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 html5tag.Attributes
useTooltips bool // uses tooltips for the error class
}
func NewFormGroup(parent page.ControlI, id string) *FormGroup {
p := &FormGroup{}
p.Self = p
p.Init(parent, id)
return p
}
func (c *FormGroup) Init(parent page.ControlI, id string) {
c.FormFieldWrapper.Init(parent, id)
c.innerDivAttr = html5tag.NewAttributes()
c.InstructionAttributes().AddClass("form-text")
}
func (c *FormGroup) this() FormGroupI {
return c.Self.(FormGroupI)
}
func (c *FormGroup) Validate(ctx context.Context) bool {
c.setChildValidation()
c.FormFieldWrapper.Validate(ctx)
return true
}
func (c *FormGroup) ChildValidationChanged() {
c.setChildValidation()
c.FormFieldWrapper.ChildValidationChanged()
}
func (c *FormGroup) setChildValidation() {
child := c.Page().GetControl(c.For())
if child.ValidationState() == page.ValidationWaiting {
child.RemoveClass("is-valid")
child.RemoveClass("is-invalid")
} else if child.ValidationMessage() != "" {
child.RemoveClass("is-valid")
child.AddClass("is-invalid")
} else {
child.AddClass("is-valid")
child.RemoveClass("is-invalid")
}
}
// 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(ctx context.Context) html5tag.Attributes {
a := c.FormFieldWrapper.DrawingAttributes(ctx)
a.SetData("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, w io.Writer) {
log.FrameworkDebug("Drawing FormFieldWrapper: " + c.ID())
attributes := c.this().DrawingAttributes(ctx)
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 = html.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("visually-hidden") // make a hidden label for screen readers
}
}
if text != "" {
c.LabelAttributes().Set("for", c.For())
buf.WriteString(html5tag.RenderTag("label", c.LabelAttributes(), html.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(">")
}
c.this().DrawInnerHtml(ctx, buf)
if hasInnerDiv {
buf.WriteString("</")
buf.WriteString(c.SubTag())
buf.WriteString(">")
}
if c.Instructions() != "" {
buf.WriteString(html5tag.RenderTag("div", c.InstructionAttributes(), html.EscapeString(c.Instructions())))
}
if subControl.ValidationState() != page.ValidationNever {
c.ErrorAttributes().SetClass(c.getValidationClass(subControl))
buf.WriteString(html5tag.RenderTag(c.SubTag(), c.ErrorAttributes(), errorMessage))
}
if _, err := io.WriteString(w, html5tag.RenderTag(c.Tag, attributes, buf.String())); err != nil {
panic(err)
}
}
func (c *FormGroup) getValidationClass(subcontrol page.ControlI) (class string) {
switch subcontrol.ValidationState() {
case page.ValidationWaiting:
fallthrough
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() html5tag.Attributes {
return c.innerDivAttr
}
func (c *FormGroup) Serialize(e page.Encoder) {
c.FormFieldWrapper.Serialize(e)
if err := e.Encode(c.innerDivAttr); err != nil {
panic(err)
}
if err := e.Encode(c.useTooltips); err != nil {
panic(err)
}
}
func (c *FormGroup) Deserialize(dec page.Decoder) {
c.FormFieldWrapper.Deserialize(dec)
if err := dec.Decode(&c.innerDivAttr); err != nil {
panic(err)
}
if err := dec.Decode(&c.useTooltips); err != nil {
panic(err)
}
}
// FormGroupCreator creates 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 contains 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 html5tag.Attributes
// ErrorAttributes are additional attributes to add to the tag that displays the error.
ErrorAttributes html5tag.Attributes
// InstructionAttributes are additional attributes to add to the tag that displays the instructions.
InstructionAttributes html5tag.Attributes
// 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 html5tag.Attributes
// 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.MakeCreatorWrapperID(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)
}
func init() {
page.RegisterControl(&FormGroup{})
}