-
Notifications
You must be signed in to change notification settings - Fork 6
/
form_fieldset.go
190 lines (166 loc) · 5.12 KB
/
form_fieldset.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
package control
import (
"context"
"github.com/goradd/goradd/pkg/config"
"github.com/goradd/goradd/pkg/page"
"github.com/goradd/goradd/pkg/page/control"
"github.com/goradd/goradd/pkg/pool"
"github.com/goradd/html5tag"
"html"
"io"
)
type FormFieldsetI interface {
control.PanelI
LegendAttributes() html5tag.Attributes
SetAsRow(r bool) FormFieldsetI
SetInstructions(instructions string) FormFieldsetI
}
// FormFieldset is a FormGroup kind of wrapper that is specific to using a fieldset as
// a wrapper. See https://getbootstrap.com/docs/4.3/components/forms/#horizontal-form.
// You will need to coordinate with whatever you are drawing internally to get the formatting right.
type FormFieldset struct {
control.Panel
legendAttributes html5tag.Attributes
asRow bool
instructions string
instructionAttributes html5tag.Attributes
}
func NewFormFieldset(parent page.ControlI, id string) *FormFieldset {
p := new(FormFieldset)
p.Init(p, parent, id)
return p
}
func (c *FormFieldset) Init(self any, parent page.ControlI, id string) {
c.Panel.Init(self, parent, id)
c.Tag = "fieldset"
c.legendAttributes = html5tag.NewAttributes()
c.legendAttributes.AddClass("pt-0") // helps with alignment. Remove if needed
c.instructionAttributes = html5tag.NewAttributes().
SetID(c.ID() + "_inst").
SetClass("form-text")
}
func (c *FormFieldset) this() FormFieldsetI {
return c.Self().(FormFieldsetI)
}
func (c *FormFieldset) LegendAttributes() html5tag.Attributes {
return c.legendAttributes
}
func (c *FormFieldset) SetAsRow(r bool) FormFieldsetI {
c.asRow = r
return c.this()
}
func (c *FormFieldset) SetInstructions(instructions string) FormFieldsetI {
if c.instructions != instructions {
c.instructions = instructions
c.Refresh()
}
return c.this()
}
func (c *FormFieldset) InstructionAttributes() html5tag.Attributes {
return c.instructionAttributes
}
func (c *FormFieldset) DrawingAttributes(ctx context.Context) html5tag.Attributes {
a := c.Panel.DrawingAttributes(ctx)
a.SetData("grctl", "formFieldset")
return a
}
func (c *FormFieldset) DrawInnerHtml(ctx context.Context, w io.Writer) {
var s string
buf2 := pool.GetBuffer()
defer pool.PutBuffer(buf2)
if c.Text() != "" {
buf2.WriteString(html5tag.RenderTag("legend", c.legendAttributes, html.EscapeString(c.Text())))
}
c.Panel.DrawInnerHtml(ctx, buf2)
if c.instructions != "" {
s = html5tag.RenderTag("div", c.instructionAttributes, html.EscapeString(c.instructions))
buf2.WriteString(s)
}
if c.asRow {
s = html5tag.RenderTag("div", html5tag.NewAttributes().AddClass("row"), buf2.String())
page.WriteString(w, s)
} else {
page.WriteString(w, buf2.String())
}
return
}
func (c *FormFieldset) Serialize(e page.Encoder) {
c.Panel.Serialize(e)
if err := e.Encode(c.legendAttributes); err != nil {
panic(err)
}
if err := e.Encode(c.asRow); err != nil {
panic(err)
}
if err := e.Encode(c.instructions); err != nil {
panic(err)
}
if err := e.Encode(c.instructionAttributes); err != nil {
panic(err)
}
}
func (c *FormFieldset) Deserialize(d page.Decoder) {
c.Panel.Deserialize(d)
if err := d.Decode(&c.legendAttributes); err != nil {
panic(err)
}
if err := d.Decode(&c.asRow); err != nil {
panic(err)
}
if err := d.Decode(&c.instructions); err != nil {
panic(err)
}
if err := d.Decode(&c.instructionAttributes); err != nil {
panic(err)
}
}
// FormFieldsetCreator creates a bootstrap fieldset,
// which wraps a control group with a fieldset.
// The Child item should be a panel or a control that groups other controls,
// like a RadioList or CheckboxList
type FormFieldsetCreator struct {
// ID is the control id on the html form.
ID string
// Legend is the text that will be in the html label tag associated with the Child control.
Legend string
// Child should be a panel, or a control that draws a group of controls,
// like a RadioList or CheckboxList
Child page.Creator
// LegendAttributes are additional attributes to add to the label tag.
LegendAttributes html5tag.Attributes
// Instructions contains help text that accompanies the control
Instructions string
// Set AsRow to true to put the legend on the same row as the content
AsRow bool
// ControlOptions are additional options for the wrapper tag
ControlOptions page.ControlOptions
}
func (f FormFieldsetCreator) Create(ctx context.Context, parent page.ControlI) page.ControlI {
id := control.MakeCreatorWrapperID(f.ID, f.Child, config.DefaultFormFieldWrapperIdSuffix)
c := NewFormFieldset(parent, id)
f.Init(ctx, c)
return c
}
func (f FormFieldsetCreator) Init(ctx context.Context, c FormFieldsetI) {
if f.Legend != "" {
c.SetText(f.Legend)
}
if f.LegendAttributes != nil {
c.LegendAttributes().Merge(f.LegendAttributes)
}
if f.AsRow {
c.SetAsRow(true)
}
if f.Instructions != "" {
c.SetInstructions(f.Instructions)
}
c.AddControls(ctx, f.Child)
c.ApplyOptions(ctx, f.ControlOptions)
}
// GetFormFieldset is a convenience method to return the fieldset with the given id from the page.
func GetFormFieldset(c page.ControlI, id string) *FormFieldset {
return c.Page().GetControl(id).(*FormFieldset)
}
func init() {
page.RegisterControl(&FormFieldset{})
}