-
Notifications
You must be signed in to change notification settings - Fork 0
/
textarea.go
145 lines (125 loc) · 3.34 KB
/
textarea.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
135
136
137
138
139
140
141
142
143
144
145
package textarea
import (
"github.com/maxence-charriere/go-app/v9/pkg/app"
"github.com/mlctrez/goapp-mdc/pkg/base"
)
type TextArea struct {
app.Compo
base.JsUtil
id string
label string
placeholder string
outlined bool
Value string
resizable bool
rows int
cols int
maxlength int
callback func(in app.HTMLTextarea)
onmount func(ctx app.Context)
}
func New(id string) *TextArea {
return &TextArea{id: id, rows: 8, cols: 40}
}
func (t *TextArea) Label(label string) *TextArea {
t.label = label
return t
}
func (t *TextArea) Placeholder(placeholder string) *TextArea {
t.placeholder = placeholder
return t
}
func (t *TextArea) Outlined(outlined bool) *TextArea {
t.outlined = outlined
return t
}
//
//func (t *TextArea) Value(val string) *TextArea {
// t.value = val
// return t
//}
func (t *TextArea) Resizeable(r bool) *TextArea {
t.resizable = r
return t
}
func (t *TextArea) Size(rows, cols int) *TextArea {
t.rows = rows
t.cols = cols
return t
}
func (t *TextArea) MaxLength(l int) *TextArea {
t.maxlength = l
return t
}
func (t *TextArea) WithCallback(cb func(in app.HTMLTextarea)) *TextArea {
t.callback = cb
return t
}
func (t *TextArea) WithOnMount(cb func(ctx app.Context)) *TextArea {
t.onmount = cb
return t
}
func (t *TextArea) buildClasses() []string {
classes := []string{"mdc-text-field"}
if t.outlined {
classes = append(classes, "mdc-text-field--outlined")
} else {
classes = append(classes, "mdc-text-field--filled")
}
classes = append(classes, "mdc-text-field--textarea")
if t.label == "" {
classes = append(classes, "mdc-text-field--no-label")
} else {
if t.Value != "" {
classes = append(classes, "mdc-text-field--label-floating")
}
}
return classes
}
func (t *TextArea) Render() app.UI {
classes := t.buildClasses()
var input app.HTMLTextarea
var labelSpan app.HTMLSpan
input = app.Textarea().Class("mdc-text-field__input").ID(t.id + "-input").Text(t.Value)
input.MaxLength(t.maxlength).Rows(t.rows).Cols(t.cols)
if t.label == "" {
if t.placeholder != "" {
input.Placeholder(t.placeholder).Aria("label", "Label").Text(t.Value)
}
} else {
labelId := t.id + "-label"
labelSpan = app.Span().Class("mdc-floating-label").ID(labelId).Text(t.label)
if t.Value != "" {
labelSpan.Class("mdc-floating-label", "mdc-floating-label--float-above")
}
input.Aria("labelledby", labelId).ID(t.id + "-input")
}
if t.callback != nil {
t.callback(input)
}
return app.Label().ID(t.id).Class(classes...).Body(
app.If(t.outlined,
app.Span().Class("mdc-notched-outline").Body(
app.Span().Class("mdc-notched-outline__leading"),
app.If(labelSpan != nil,
app.Span().Class("mdc-notched-outline__notch").Body(labelSpan)),
app.Span().Class("mdc-notched-outline__trailing"),
),
app.If(t.resizable, app.Span().Class("mdc-text-field__resizer").Body(input)).Else(input),
).Else(
app.Span().Class("mdc-text-field__ripple"),
app.If(labelSpan != nil, labelSpan),
app.If(t.resizable, app.Span().Class("mdc-text-field__resizer").Body(input)).Else(input),
app.Span().Class("mdc-line-ripple"),
),
)
}
func (t *TextArea) OnMount(ctx app.Context) {
value := t.JsValueAtPath("mdc.textField.MDCTextField")
if !value.IsUndefined() {
value.Call("attachTo", app.Window().GetElementByID(t.id))
}
if t.onmount != nil {
t.onmount(ctx)
}
}