-
Notifications
You must be signed in to change notification settings - Fork 6
/
textbox_integer.go
109 lines (98 loc) · 4.07 KB
/
textbox_integer.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
package control
import (
"context"
"github.com/goradd/goradd/pkg/page"
"github.com/goradd/goradd/pkg/page/control/textbox"
"github.com/goradd/html5tag"
)
type IntegerTextboxI interface {
textbox.IntegerI
}
type IntegerTextbox struct {
textbox.IntegerTextbox
}
func NewIntegerTextbox(parent page.ControlI, id string) *IntegerTextbox {
t := new(IntegerTextbox)
t.Self = t
t.Init(parent, id)
return t
}
func (t *IntegerTextbox) DrawingAttributes(ctx context.Context) html5tag.Attributes {
a := t.IntegerTextbox.DrawingAttributes(ctx)
a.AddClass("form-control")
return a
}
// IntegerTextboxCreator creates an integer textbox.
// Pass it to AddControls of a control, or as a Child of
// a FormFieldWrapper.
type IntegerTextboxCreator struct {
// ID is the control id of the html widget and must be unique to the page
ID string
// Placeholder is the placeholder attribute of the textbox and shows as help text inside the field
Placeholder string
// Type is the type attribute of the textbox
Type string
// MinLength is the minimum number of characters that the user is required to enter. If the
// length is less than this number, a validation error will be shown.
MinLength int
// MaxLength is the maximum number of characters that the user is required to enter. If the
// length is more than this number, a validation error will be shown.
MaxLength int
// ColumnCount is the number of characters wide the textbox will be, and becomes the width attribute in the tag.
// The actual width is browser dependent. For better control, use a width style property.
ColumnCount int
// RowCount creates a multi-line textarea with the given number of rows. By default the
// textbox will expand vertically by this number of lines. Use a height style property for
// better control of the height of a textbox.
RowCount int
// ReadOnly sets the readonly attribute of the textbox, which prevents it from being changed by the user.
ReadOnly bool
// SaveState will save the text in the textbox, to be restored if the user comes back to the page.
// It is particularly helpful when the textbox is being used to filter the results of a query, so that
// when the user comes back to the page, he does not have to type the filter text again.
SaveState bool
// MinValue is the minimum value the user can enter. If the user does not
// enter at least this amount, or enters something that is not an integer, it will fail validation
// and the FormFieldWrapper will show an error.
MinValue *textbox.IntegerLimit
// MaxValue is the maximum value the user can enter. If the user enter more
// than this amount, or enters something that is not an integer, it will fail validation
// and the FormFieldWrapper will show an error.
MaxValue *textbox.IntegerLimit
// Value is the initial value of the textbox. Often its best to load the value in a separate Load step after creating the control.
Value interface{}
page.ControlOptions
}
// Create is called by the framework to create a new control from the Creator. You
// do not normally need to call this.
func (c IntegerTextboxCreator) Create(ctx context.Context, parent page.ControlI) page.ControlI {
ctrl := NewIntegerTextbox(parent, c.ID)
c.Init(ctx, ctrl)
return ctrl
}
// Init is called by implementations of Textboxes to initialize a control with the
// creator. You do not normally need to call this.
func (c IntegerTextboxCreator) Init(ctx context.Context, ctrl IntegerTextboxI) {
// Reuse subclass
sub := textbox.IntegerTextboxCreator{
Placeholder: c.Placeholder,
Type: c.Type,
MinLength: c.MinLength,
MaxLength: c.MaxLength,
RowCount: c.RowCount,
ReadOnly: c.ReadOnly,
ControlOptions: c.ControlOptions,
SaveState: c.SaveState,
MinValue: c.MinValue,
MaxValue: c.MaxValue,
Value: c.Value,
}
sub.Init(ctx, ctrl)
}
// GetIntegerTextbox is a convenience method to return the control with the given id from the page.
func GetIntegerTextbox(c page.ControlI, id string) *IntegerTextbox {
return c.Page().GetControl(id).(*IntegerTextbox)
}
func init() {
page.RegisterControl(&IntegerTextbox{})
}