-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathform_render.go
More file actions
363 lines (338 loc) · 9.94 KB
/
Copy pathform_render.go
File metadata and controls
363 lines (338 loc) · 9.94 KB
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
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
package gomukit
import (
"fmt"
"strconv"
g "maragu.dev/gomponents"
h "maragu.dev/gomponents/html"
"github.com/techthos/gomukit/internal/assets"
"github.com/techthos/gomukit/internal/htmlx"
)
// Document implements Widget. Field structure is fully SSR'd (it is static
// config); prefill values and server-side field errors are runtime state.
func (f *Form) Document() (string, error) {
if err := f.Validate(); err != nil {
return "", err
}
var data any
if len(f.InitialData) > 0 {
data = f.InitialData
}
return htmlx.Document(htmlx.DocConfig{
Title: docTitle(f.Title, "Form"),
CSS: assets.StylesCSS,
ThemeCSS: f.Theme.CSS(),
Body: f.shell(),
Config: f.config(),
Data: data,
RuntimeJS: assets.RuntimeJS,
})
}
func (f *Form) shell() g.Node {
var body []g.Node
if f.Title != "" {
body = append(body, h.Div(h.Class("gomu-toolbar"),
h.H2(h.Class("gomu-title"), g.Text(f.Title)),
))
}
// novalidate: the runtime runs checkValidity itself and renders inline
// errors; native validation would swallow the submit event and rely on
// browser bubbles that hosts' sandboxed iframes may not show.
formClass := "gomu-form"
// The form's own width follows its widest grid: one column is a reading
// measure, four need the room to be four.
if widest := f.widestGroup(); widest > 1 {
formClass += " gomu-form--cols-" + strconv.Itoa(widest)
}
formChildren := []g.Node{h.Class(formClass), htmlx.Data("form", ""), g.Attr("novalidate")}
for i, gr := range f.groups() {
formChildren = append(formChildren, groupNode(gr, i))
}
submitLabel := f.Submit.Label
if submitLabel == "" {
submitLabel = "Submit"
}
actions := []g.Node{h.Class("gomu-form-actions")}
if f.Cancel != nil {
cancelLabel := f.Cancel.Label
if cancelLabel == "" {
cancelLabel = "Cancel"
}
actions = append(actions, h.Button(h.Type("button"), h.Class("gomu-btn"), htmlx.Data("cancel", ""), g.Text(cancelLabel)))
}
// type=button, not submit: hosts sandbox the widget iframe without
// allow-forms, which blocks native form submission outright (the submit
// event never fires). The runtime drives submission from this click.
actions = append(actions, h.Button(
h.Type("button"),
h.Class("gomu-btn gomu-btn--primary"),
htmlx.Data("submit", ""),
g.Text(submitLabel),
))
formChildren = append(formChildren, h.Div(actions...))
body = append(body, h.Form(formChildren...), statusNode(f.Brand))
return h.Div(h.Class("gomu-root"), htmlx.Data("widget", "form"),
h.Div(append([]g.Node{h.Class("gomu-card")}, body...)...),
)
}
// groupNode renders one block of fields: a bare grid for the form's ungrouped
// fields, or a <fieldset> around the same grid for a FieldSet.
//
// The title is a heading the fieldset points at with aria-labelledby rather
// than a <legend>: a legend is laid out inside the fieldset's border by the
// UA, which no grid or panel chrome survives, while aria-labelledby names the
// group just as well.
func groupNode(gr fieldGroup, i int) g.Node {
grid := gridNode(gr)
if gr.set == nil {
return grid
}
titleID := fmt.Sprintf("gomu-fs-%d", i)
class := "gomu-fieldset"
if gr.set.Boxed {
class += " gomu-fieldset--boxed"
}
head := []g.Node{
h.Class("gomu-fieldset-head"),
h.H3(h.Class("gomu-fieldset-title"), h.ID(titleID), g.Text(gr.set.Title)),
}
if gr.set.Description != "" {
head = append(head, h.P(h.Class("gomu-fieldset-desc"), g.Text(gr.set.Description)))
}
return h.FieldSet(
h.Class(class),
h.Aria("labelledby", titleID),
h.Div(head...),
grid,
)
}
// gridNode lays a group's fields out in its columns. The column count travels
// as a class rather than an inline style: there are four of them, and the
// narrow tiers below override the same classes.
func gridNode(gr fieldGroup) g.Node {
class := "gomu-form-grid"
if gr.cols > 1 {
class += " gomu-cols-" + strconv.Itoa(gr.cols)
}
children := []g.Node{h.Class(class)}
for _, fd := range gr.fields {
children = append(children, fieldNode(fd, gr.cols))
}
return h.Div(children...)
}
func fieldNode(fd Field, cols int) g.Node {
ft := fd.fieldType()
id := "gomu-f-" + fd.Name
if ft == FHidden {
return h.Input(h.Type("hidden"), h.Name(fd.Name), h.Value(defaultString(fd.Default)))
}
var control g.Node
switch ft {
case FTextarea:
rows := fd.Rows
if rows <= 0 {
rows = 3
}
control = h.Textarea(append(controlAttrs(fd, id),
h.Rows(strconv.Itoa(rows)),
g.Text(defaultString(fd.Default)),
)...)
case FSelect, FMultiSelect:
attrs := controlAttrs(fd, id)
if ft == FMultiSelect {
attrs = append(attrs, h.Multiple())
}
selected := selectedSet(fd.Default)
for _, opt := range fd.Options {
optAttrs := []g.Node{h.Value(opt.Value), g.Text(opt.Label)}
if selected[opt.Value] {
optAttrs = append(optAttrs, h.Selected())
}
attrs = append(attrs, h.Option(optAttrs...))
}
control = h.Select(attrs...)
case FDateRange:
control = dateRangeNode(fd, id)
case FCheckbox:
// Not controlAttrs: the box draws itself (ui/css/check.css) rather than
// wearing .gomu-input, and none of the text/number validation
// attributes mean anything on a checkbox.
attrs := []g.Node{h.ID(id), h.Name(fd.Name)}
if fd.Required {
attrs = append(attrs, h.Required())
}
if b, ok := fd.Default.(bool); ok && b {
attrs = append(attrs, h.Checked())
}
control = checkboxNode(attrs...)
default: // text, number, date, time, readonly
attrs := controlAttrs(fd, id)
if ft == FDate {
attrs = append(attrs, dateBounds(fd.Calendar)...)
}
attrs = append(attrs, h.Type(inputType(ft)))
if ft == FReadonly {
attrs = append(attrs, h.ReadOnly())
}
if v := defaultString(fd.Default); v != "" {
attrs = append(attrs, h.Value(v))
}
control = h.Input(attrs...)
}
labelText := fd.Label
if labelText == "" {
labelText = fd.Name
}
var labelChildren []g.Node
labelChildren = append(labelChildren, h.For(id), g.Text(labelText))
if fd.Required {
labelChildren = append(labelChildren, h.Span(h.Class("gomu-required"), h.Aria("hidden", "true"), g.Text(" *")))
}
class := "gomu-field gomu-field--" + string(ft)
if span := fd.span(cols); span > 1 {
class += " gomu-span-" + strconv.Itoa(span)
}
nodes := []g.Node{h.Class(class)}
if ft == FCheckbox {
// Checkbox: control first, label after.
nodes = append(nodes, control, h.Label(labelChildren...))
} else {
nodes = append(nodes, h.Label(labelChildren...), control)
}
if fd.Description != "" {
nodes = append(nodes, h.P(h.Class("gomu-field-desc"), g.Text(fd.Description)))
}
nodes = append(nodes, h.P(h.Class("gomu-field-error"), htmlx.Data("error-for", fd.Name), g.Attr("hidden")))
return h.Div(nodes...)
}
// dateRangeNode renders a range field as its two value holders: a native date
// input per end, named after the two tool arguments. The runtime hides them
// behind one trigger and a range calendar (ui/src/calendar.ts) — but they stay
// in the DOM, so the field keeps native constraint validation, and a document
// whose script never runs still asks for both dates.
func dateRangeNode(fd Field, id string) g.Node {
start, end := fd.rangeDefaults()
bounds := dateBounds(fd.Calendar)
label := fd.Label
if label == "" {
label = fd.Name
}
part := func(id, name, value, class, aria string) g.Node {
attrs := []g.Node{
h.Type("date"),
h.Name(name),
h.Class("gomu-input " + class),
h.Aria("label", aria),
}
if id != "" {
attrs = append(attrs, h.ID(id))
}
if fd.Required {
attrs = append(attrs, h.Required())
}
if value != "" {
attrs = append(attrs, h.Value(value))
}
return h.Input(append(attrs, bounds...)...)
}
// The field's own label addresses the start input by id, and the runtime
// moves that id onto the trigger it builds — the same handover the
// dropdown performs over a <select>. Each input also names itself, since
// without the script the two are all the reader gets.
return h.Div(
h.Class("gomu-daterange"),
htmlx.Data("daterange", fd.Name),
part(id, fd.Name, start, "gomu-daterange-start", label+" start date"),
part("", fd.endName(), end, "gomu-daterange-end", label+" end date"),
)
}
// dateBounds are the native min/max attributes a calendar's window implies.
// The grid enforces the same window, but the attributes make the fallback
// control and form.checkValidity() agree with it.
func dateBounds(c *Calendar) []g.Node {
if c == nil {
return nil
}
var attrs []g.Node
if c.Min != "" {
attrs = append(attrs, h.Min(c.Min))
}
if c.Max != "" {
attrs = append(attrs, h.Max(c.Max))
}
return attrs
}
// controlAttrs renders the shared attributes incl. native validation.
func controlAttrs(fd Field, id string) []g.Node {
attrs := []g.Node{h.ID(id), h.Name(fd.Name), h.Class("gomu-input")}
if fd.Placeholder != "" {
attrs = append(attrs, h.Placeholder(fd.Placeholder))
}
if fd.Required {
attrs = append(attrs, h.Required())
}
if v := fd.Validation; v != nil {
if v.Pattern != "" {
attrs = append(attrs, h.Pattern(v.Pattern))
}
if v.Min != nil {
attrs = append(attrs, h.Min(formatFloat(*v.Min)))
}
if v.Max != nil {
attrs = append(attrs, h.Max(formatFloat(*v.Max)))
}
if v.Step != nil {
attrs = append(attrs, h.Step(formatFloat(*v.Step)))
}
if v.MinLen != nil {
attrs = append(attrs, h.MinLength(strconv.Itoa(*v.MinLen)))
}
if v.MaxLen != nil {
attrs = append(attrs, h.MaxLength(strconv.Itoa(*v.MaxLen)))
}
}
return attrs
}
func inputType(ft FieldType) string {
switch ft {
case FNumber:
return "number"
case FDate:
return "date"
case FTime:
return "time"
default:
return "text"
}
}
func defaultString(v any) string {
switch x := v.(type) {
case nil:
return ""
case string:
return x
case bool:
if x {
return "true"
}
return ""
default:
return fmt.Sprint(x)
}
}
func selectedSet(v any) map[string]bool {
out := map[string]bool{}
switch x := v.(type) {
case string:
if x != "" {
out[x] = true
}
case []string:
for _, s := range x {
out[s] = true
}
}
return out
}
func formatFloat(f float64) string {
return strconv.FormatFloat(f, 'f', -1, 64)
}