-
Notifications
You must be signed in to change notification settings - Fork 2
/
checkbox-input.go
134 lines (113 loc) · 3.18 KB
/
checkbox-input.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
package goschemaform
import (
"bytes"
"log"
"strconv"
"text/template"
)
// NewCheckBoxInput returns an initialized CheckBoxInput
func NewCheckBoxInput(key string) *CheckBoxInput {
return &CheckBoxInput{
key: key,
title: "",
required: false,
condition: "",
condFlip: false,
}
}
// CheckBoxInput represents a form checkxbox
type CheckBoxInput struct {
key string
title string
required bool
condition string
condFlip bool
}
// Form returns the "form" section of the JSON Schema Form definition
func (c *CheckBoxInput) Form() string {
// Compile the template for generating the form section
var tmplForm = template.Must(template.New("form").Parse(tmplCheckBoxForm))
var cCheck = false
if c.condition != "" {
cCheck = true
}
tmplData := struct {
Key string
Title string
ConditionCheck bool
Condition string
ConditionFlip bool
}{
Key: c.key,
Title: c.title,
ConditionCheck: cCheck,
Condition: c.condition,
ConditionFlip: c.condFlip,
}
form := bytes.NewBuffer([]byte{})
err := tmplForm.Execute(form, tmplData)
if err != nil {
log.Println(err.Error())
return ""
}
return form.String()
}
// Schema returns the "schema" section of the JSON Schema Form definition
func (c *CheckBoxInput) Schema() string {
// Compile the template for generating the Schema section
var tmplSchema = template.Must(template.New("schema").Parse(tmplCheckBoxSchema))
tmplData := struct {
Key string
Title string
}{
Key: c.key,
Title: c.title,
}
schema := bytes.NewBuffer([]byte{})
err := tmplSchema.Execute(schema, tmplData)
if err != nil {
log.Println(err.Error())
return ""
}
return schema.String()
}
// Inputs returns the DropDownInput structure, which matches the Input interface.
// It only returns itself in the array as it holds no other Inputs.
func (c *CheckBoxInput) Inputs() []Input {
return []Input{c}
}
// Key returns the name/id of the form input that will be used
func (c *CheckBoxInput) Key() string {
return c.key
}
// Required returns a boolean value to determine if the Input is required
func (c *CheckBoxInput) Required() bool {
return c.required
}
// MapDefinition returns the internal structure of the Input in a Go friendly
// format.
func (c *CheckBoxInput) MapDefinition() map[string]string {
return map[string]string{
"key": c.key,
"title": c.title,
"required": strconv.FormatBool(c.required),
"type": "checkbox",
}
}
// SetTitle sets the Input's title text
func (c *CheckBoxInput) SetTitle(title string) {
c.title = title
}
// IsRequired configures the input to be required (true) or to not be required
// (false).
func (c *CheckBoxInput) IsRequired(req bool) {
c.required = req
}
// SetCondition will set whether this item displays on the form based on if the provided
// key has a value or not. You can reverse the behaivor with the defaultHide switch. False
// for this option is the default and will make something only appear if the condition is
// is set, while true flips this and shows the control until the condition is met.
func (c *CheckBoxInput) SetCondition(text string, defaultHide bool) {
c.condition = text
c.condFlip = defaultHide
}