~samwhited/xmpp

xmpp/form/options.go -rw-r--r-- 2.2 KiB
c9743d9fSam Whited docs: update changelog to include deprecations 3 days 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
87
88
89
90
91
92
93
94
// 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 = s
	}
}

// Instructions adds new textual instructions to the form.
func Instructions(s string) Field {
	return func(data *Data) {
		data.instructions = s
	}
}

var (
	// Result marks a form as the result type.
	// For more information see TypeResult.
	Result Field = result
)

var (
	result Field = func(data *Data) {
		data.typ = TypeResult
	}
)

// 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 = true
	}
)

// 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.
// 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)
	}
}

// Label defines a human-readable name for the field.
func Label(s string) Option {
	return func(f *field) {
		f.label = s
	}
}

// ListItem adds a list item with the provided label and value.
// It has no effect on any non-list field type.
func ListItem(label, value string) Option {
	return func(f *field) {
		f.option = append(f.option, fieldOpt{
			Label: label,
			Value: value,
		})
	}
}

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