package ui import ( "slices" "github.com/egoist/mygo/internal/text" ) // formBuild is a Form being built: the boxes of its fields' labels, which // its layout makes as wide as the widest. type formBuild struct { labels []*node aligned bool } // fieldParts are the parts of a Field: its label, the column holding its // control and the texts below it, and the control, which the label names // and the texts describe. type fieldParts struct { label, content, control *node // errors are those of controls inside that check what is typed, as // DecimalInput, which show below the control (fieldError). errors []string } // Form creates a column of the fields that fn builds (Field, Fieldset), // whose labels sit in a column of their own, beside the controls, as in // macOS's forms: aligned to the right, as wide as the widest, and level // with the first line of their control. // // ui.Form(c, func() { // ui.Field(c, "Name", func() { ui.TextInput(c, &app.name) }) // ui.Field(c, "Size", func() { ui.Select(c, &app.size, sizes) }) // }) func coreForm(c *context, fn func()) *node { f := coreColumn(c).Gap(c.theme.Space(3)) f.widget = "Form" fb := &formBuild{} f.form = fb f.special = true // it lays its labels out saved := c.form c.form = fb f.Children(fn) c.form = saved return f } // alignLabels makes the labels of a form as wide as the widest, once a // frame, before the form is measured or laid out. func (fb *formBuild) alignLabels() { if fb.aligned { return } fb.aligned = true var w float32 for _, l := range fb.labels { if !l.inHidden() { // A Hidden field's label sets no width. w = max(w, intrinsic(l, true)) } } for _, l := range fb.labels { journalKeep(l.c.rt, &l.width) l.width = px(w) l.parent.forgetIntrinsic() } } // Field creates a field: label, above the control that fn builds, or // beside it in a Form. The label names the control for assistive // technology, unless the control names itself, as a check box with its own // text does, when it names the field around it; a click on the label // focuses the control, or clicks a check box, a switch or a radio button. // Description and Error add texts below the control, which describe it: // // ui.Field(c, "Email", func() { // ui.TextInput(c, &app.email) // }).Description("For receipts.").Error(app.emailError) // // The control is the first element fn builds that takes the focus, or a // group of them, such as a RadioGroup. func coreField(c *context, label string, fn func()) *node { t := c.theme fp := &fieldParts{} var f *node if c.form != nil { f = coreRow(c).Gap(t.Space(3)).AlignItems(Start) f.baselines = true } else { f = coreColumn(c).Gap(t.Space(1.5)) } f.widget, f.field = "Field", fp f.Children(func() { if c.form != nil { box := coreRow(c).Justify(End).Shrink(0) c.form.labels = append(c.form.labels, box) box.Children(func() { fp.label = coreText(c, label) }) } else { fp.label = coreText(c, label).FontWeight(500) } fp.content = coreColumn(c).Gap(t.Space(1.5)) if c.form != nil { fp.content.Grow(1).MinWidth(0) // the rest of the row } fp.content.Children(fn) }) ctrl := fieldControl(fp.content) fp.control = ctrl for _, msg := range fp.errors { f.fieldText(msg, t.Danger) } l := fp.label if ctrl == nil { return f } if namesItself(ctrl) { f.Role(RoleGroup).Label(label) } else if ctrl.nameFrom == nil || ctrl.nameFrom.label == "" { ctrl.nameFrom = l } l.labels(ctrl) l.styleFn = func(l *node) { if ctrl.IsDisabled() && !l.IsDisabled() { l.Opacity(0.5) } } return f } // LabelledBy has label, a text the view draws as the element's caption, // name the element for assistive technology, in place of its Label or the // text inside it, as ARIA's aria-labelledby does, and gives a click on // label to the element, as HTML's