-
Notifications
You must be signed in to change notification settings - Fork 0
/
input.go
78 lines (65 loc) · 1.53 KB
/
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
package beleine
//INPUT
import (
"errors"
"fmt"
)
type Input struct {
id string
text string
hint string
size string
inputType string
js string
}
//Create new Input struct
func NewInput() Input {
return Input{id: GetGlobalID(), inputType: "text"}
}
//Returns ID of component
func (i *Input) GetID() string {
return i.id
}
//Return JS code as string, that sets Input text
func (i *Input) SetTextJS(text string) string {
i.text = text
return fmt.Sprintf(`%s.value="%s"`, i.id, i.text)
}
//Sets input hint
func (i *Input) SetHint(hint string) {
i.hint = hint
}
//Size in 0-2 number, 1 - default
//Throws error when size is not in 0-2 range
func (i *Input) SetSize(size int) error {
switch size {
case 0:
i.size = "form-control-sm"
case 1:
i.size = ""
case 2:
i.size = "form-control-lg"
default:
return errors.New("value must be in 0-2 range")
}
return nil
}
//Sets input type
//TODO:Make switch instead of html code
func (i *Input) SetInputType(inputType string) {
i.inputType = inputType
}
//Return JS code as string, that returns value of input
func (i *Input) GetTextJS() string {
return fmt.Sprintf("%s.value", i.id)
}
//Sets js code, to be executed after click
func (i *Input) SetOnClickListener(listener string) {
i.js += fmt.Sprintf("%s.onclick = function(){%s}", i.id, listener)
}
func (i *Input) Render() string {
return fmt.Sprintf(`<input id="%s" type="%s" class="form-control %s" placeholder="%s">`, i.id, i.inputType, i.size, i.hint)
}
func (i *Input) RenderJS() string {
return i.js
}