~samwhited/xmpp

ref: 8a516e9e064619dcb3d84499b66e1562546be918 xmpp/form/options.go -rw-r--r-- 2.2 KiB
8a516e9eSam Whited .builds: validate license headers 10 months ago
                                                                                
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
// Copyright 2017 The Mellium Contributors.
// Use of this source code is governed by the BSD 2-clause
// license that can be found in the LICENSE file.

package form

// An Field is used to define the behavior and appearance of a data form.
type Field func(*Data)

// Title sets a form's title.
func Title(s string) Field {
	return func(data *Data) {
		data.title.Text = s
	}
}

// Instructions adds new textual instructions to the form.
// Multiple uses of the option result in multiple sets of instructions.
func Instructions(s string) Field {
	return func(data *Data) {
		data.children = append(data.children, instructions{Text: s})
	}
}

func getOpts(data *Data, o ...Field) {
	for _, f := range o {
		f(data)
	}
	return
}

// A Option is used to define the behavior and appearance of a form field.
type Option func(*field)

var (
	// Required flags the field as required in order for the form to be considered
	// valid.
	Required Option = required
)

var (
	required Option = func(f *field) {
		f.Required = &struct{}{}
	}
)

// Desc provides a natural-language description of the field, intended for
// presentation in a user-agent (e.g., as a "tool-tip", help button, or
// explanatory text provided near the field).
// Desc should not contain newlines (the \n and \r characters), since layout is
// the responsibility of a user agent.
// However, it does nothing to prevent them from being added.
func Desc(s string) Option {
	return func(f *field) {
		f.Desc = s
	}
}

// Value defines the default value for the field (according to the
// form-processing entity) in a data form of type "form", the data provided by a
// form-submitting entity in a data form of type "submit", or a data result in a
// data form of type "result".
// Fields of type ListMulti, JidMulti, TextMulti, and Hidden may contain more
// than one Value; all other field types will only use the first Value.
func Value(s string) Option {
	return func(f *field) {
		f.Value = append(f.Value, s)
	}
}

// ListField is one of the values in a list.
// It has no effect on any non-list field type.
func ListField(s string) Option {
	return func(f *field) {
		f.Field = append(f.Field, fieldopt{
			Value: s,
		})
	}
}

func getFieldOpts(f *field, o ...Option) {
	for _, opt := range o {
		opt(f)
	}
	return
}