-
Notifications
You must be signed in to change notification settings - Fork 11
/
react_events.go
248 lines (214 loc) · 6.33 KB
/
react_events.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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
// Copyright 2018-20 PJ Engineering and Business Solutions Pty. Ltd. All rights reserved.
package react
import (
"github.com/gopherjs/gopherjs/js"
)
// SyntheticEvent represents a SyntheticEvent.
//
// See: https://reactjs.org/docs/events.html#overview
type SyntheticEvent struct {
// O represents the original React SyntheticEvent.
O *js.Object
}
// Bubbles ...
//
// See: https://reactjs.org/docs/events.html#overview
func (s *SyntheticEvent) Bubbles() bool {
return s.O.Get("bubbles").Bool()
}
// Cancelable ...
//
// See: https://reactjs.org/docs/events.html#overview
func (s *SyntheticEvent) Cancelable() bool {
return s.O.Get("cancelable").Bool()
}
// CurrentTarget ...
//
// See: https://reactjs.org/docs/events.html#overview
//
// Example:
//
// import "honnef.co/go/js/dom"
//
// dom.WrapHTMLElement(e.CurrentTarget())
func (s *SyntheticEvent) CurrentTarget() *js.Object {
return s.O.Get("currentTarget")
}
// DefaultPrevented ...
//
// See: https://reactjs.org/docs/events.html#overview
func (s *SyntheticEvent) DefaultPrevented() bool {
return s.O.Get("defaultPrevented").Bool()
}
// EventPhase ...
//
// See: https://reactjs.org/docs/events.html#overview
func (s *SyntheticEvent) EventPhase() int {
return s.O.Get("eventPhase").Int()
}
// IsTrusted ...
//
// See: https://reactjs.org/docs/events.html#overview
func (s *SyntheticEvent) IsTrusted() bool {
return s.O.Get("isTrusted").Bool()
}
// NativeEvent ...
//
// See: https://reactjs.org/docs/events.html#overview
//
// Example:
//
// import "honnef.co/go/js/dom"
//
// dom.WrapEvent(e.NativeEvent())
func (s *SyntheticEvent) NativeEvent() *js.Object {
return s.O.Get("nativeEvent")
}
// PreventDefault ...
//
// See: https://reactjs.org/docs/events.html#overview
func (s *SyntheticEvent) PreventDefault() {
s.O.Call("preventDefault")
}
// IsDefaultPrevented ...
// See: https://reactjs.org/docs/events.html#overview
func (s *SyntheticEvent) IsDefaultPrevented() bool {
return s.O.Call("isDefaultPrevented").Bool()
}
// StopPropagation ...
//
// See: https://reactjs.org/docs/events.html#overview
func (s *SyntheticEvent) StopPropagation() {
s.O.Call("stopPropagation")
}
// IsPropagationStopped ...
//
// See: https://reactjs.org/docs/events.html#overview
func (s *SyntheticEvent) IsPropagationStopped() bool {
return s.O.Call("isPropagationStopped").Bool()
}
// Target ...
//
// See: https://reactjs.org/docs/events.html#overview
//
// Example:
//
// import "honnef.co/go/js/dom"
//
// dom.WrapHTMLElement(e.Target())
func (s *SyntheticEvent) Target() *js.Object {
return s.O.Get("target")
}
// TimeStamp ...
//
// See: https://reactjs.org/docs/events.html#overview
func (s *SyntheticEvent) TimeStamp() float64 {
return s.O.Get("timeStamp").Float()
}
// Type ...
//
// See: https://reactjs.org/docs/events.html#overview
func (s *SyntheticEvent) Type() string {
return s.O.Get("type").String()
}
// Persist is used if you want to access properties in an asynchronous way.
//
// See: https://reactjs.org/docs/events.html#event-pooling
func (s *SyntheticEvent) Persist() *SyntheticEvent {
p := s.O.Call("persist")
return &SyntheticEvent{p}
}
// SetEventHandler allows a custom event handler to be attached.
// By passing nil for f, the handler can also be detached (cleared).
//
// It can be used like this: "onClick": this.Get("clickhandler")
//
func (def ClassDef) SetEventHandler(name string, f func(this *js.Object, e *SyntheticEvent, props, state Map, setState SetState)) {
h := func(this *js.Object, props, state Map, setState SetState, arguments []*js.Object) interface{} {
syntheticEvent := &SyntheticEvent{arguments[0]}
f(this, syntheticEvent, props, state, setState)
return nil
}
def.setMethod(false, name, h)
}
// SetMultiArgEventHandler allows for you to pass custom arguments to a custom
// event handler. By passing nil for f, the handler can also be detached (cleared).
//
// It can be used like this: "onClick": this.Get("clickhandler").Invoke(5)
//
// See: https://reactjs.org/docs/handling-events.html#passing-arguments-to-event-handlers
func (def ClassDef) SetMultiArgEventHandler(name string, f func(this *js.Object, arguments []*js.Object) func(this *js.Object, e *SyntheticEvent, props, state Map, setState SetState)) {
if f == nil {
// Clear handler
delete(def, name)
return
}
if name == "statics" {
panic("can't have function name called 'statics'")
}
if name == "mixins" {
panic("can't have function name called 'mixins'")
}
x := func(this *js.Object, arguments []*js.Object) interface{} {
props := func(key string) *js.Object {
return this.Get("props").Get(key)
}
state := func(key string) *js.Object {
return this.Get("state").Get(key)
}
setState := func(updater interface{}, callback ...func()) {
if updater == nil {
return
}
if len(callback) > 0 && callback[0] != nil {
switch updater := updater.(type) {
case func(props, state Map) interface{}:
this.Call("setState", func(state *js.Object, props *js.Object) interface{} {
return SToMap(updater(func(key string) *js.Object {
return props.Get(key)
}, func(key string) *js.Object {
return state.Get(key)
}))
}, callback[0])
case UpdaterFunc:
this.Call("setState", func(state *js.Object, props *js.Object) interface{} {
return SToMap(updater(func(key string) *js.Object {
return props.Get(key)
}, func(key string) *js.Object {
return state.Get(key)
}))
}, callback[0])
default:
this.Call("setState", SToMap(updater), callback[0])
}
} else {
switch updater := updater.(type) {
case func(props, state Map) interface{}:
this.Call("setState", func(state *js.Object, props *js.Object) interface{} {
return SToMap(updater(func(key string) *js.Object {
return props.Get(key)
}, func(key string) *js.Object {
return state.Get(key)
}))
})
case UpdaterFunc:
this.Call("setState", func(state *js.Object, props *js.Object) interface{} {
return SToMap(updater(func(key string) *js.Object {
return props.Get(key)
}, func(key string) *js.Object {
return state.Get(key)
}))
})
default:
this.Call("setState", SToMap(updater))
}
}
}
z := f(this, arguments)
return func(e *js.Object) {
syntheticEvent := &SyntheticEvent{e}
z(this, syntheticEvent, props, state, setState)
}
}
def[name] = js.MakeFunc(x)
}