forked from reactive-python/reactpy-django
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcomponents.py
382 lines (303 loc) · 9.22 KB
/
components.py
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
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
import inspect
from django.http import HttpRequest
from idom import component, hooks, html, web
from test_app.models import TodoItem
import django_idom
from django_idom.components import view_to_component
from django_idom.hooks import use_mutation, use_query
from . import views
@component
def hello_world():
return html._(html.h1({"id": "hello-world"}, "Hello World!"), html.hr())
@component
def button():
count, set_count = hooks.use_state(0)
return html._(
html.div(
html.button(
{"id": "counter-inc", "onClick": lambda event: set_count(count + 1)},
"Click me!",
),
html.p(
{"id": "counter-num", "data-count": count},
f"Current count is: {count}",
),
),
html.hr(),
)
@component
def parameterized_component(x, y):
total = x + y
return html._(
html.h1({"id": "parametrized-component", "data-value": total}, total),
html.hr(),
)
victory = web.module_from_template("react", "victory-bar", fallback="...")
VictoryBar = web.export(victory, "VictoryBar")
@component
def simple_bar_chart():
return html._(VictoryBar(), html.hr())
@component
def use_websocket():
ws = django_idom.hooks.use_websocket()
success = bool(ws.scope and ws.close and ws.disconnect and ws.view_id)
return html.div(
{"id": "use-websocket", "data-success": success},
f"use_websocket: {ws}",
html.hr(),
)
@component
def use_scope():
scope = django_idom.hooks.use_scope()
success = len(scope) >= 10 and scope["type"] == "websocket"
return html.div(
{"id": "use-scope", "data-success": success},
f"use_scope: {scope}",
html.hr(),
)
@component
def use_location():
location = django_idom.hooks.use_location()
success = bool(location)
return html.div(
{"id": "use-location", "data-success": success},
f"use_location: {location}",
html.hr(),
)
@component
def use_origin():
location = django_idom.hooks.use_origin()
success = bool(location)
return html.div(
{"id": "use-origin", "data-success": success},
f"use_origin: {location}",
html.hr(),
)
@component
def django_css():
return html.div(
{"id": "django-css"},
django_idom.components.django_css("django-css-test.css"),
html.div({"style": {"display": "inline"}}, "django_css: "),
html.button("This text should be blue."),
html.hr(),
)
@component
def django_js():
success = False
return html._(
html.div(
{"id": "django-js", "data-success": success},
f"django_js: {success}",
django_idom.components.django_js("django-js-test.js"),
),
html.hr(),
)
@component
@django_idom.decorators.auth_required(
fallback=html.div(
{"id": "unauthorized-user-fallback"},
"unauthorized_user: Success",
html.hr(),
)
)
def unauthorized_user():
return html.div(
{"id": "unauthorized-user"},
"unauthorized_user: Fail",
html.hr(),
)
@component
@django_idom.decorators.auth_required(
auth_attribute="is_anonymous",
fallback=html.div(
{"id": "authorized-user-fallback"},
"authorized_user: Fail",
html.hr(),
),
)
def authorized_user():
return html.div(
{"id": "authorized-user"},
"authorized_user: Success",
html.hr(),
)
def get_items_query():
return TodoItem.objects.all()
def add_item_mutation(text: str):
existing = TodoItem.objects.filter(text=text).first()
if existing:
if existing.done:
existing.done = False
existing.save()
else:
return False
else:
TodoItem(text=text, done=False).save()
def toggle_item_mutation(item: TodoItem):
item.done = not item.done
item.save()
@component
def todo_list():
input_value, set_input_value = hooks.use_state("")
items = use_query(get_items_query)
toggle_item = use_mutation(toggle_item_mutation, refetch=get_items_query)
if items.error:
rendered_items = html.h2(f"Error when loading - {items.error}")
elif items.data is None:
rendered_items = html.h2("Loading...")
else:
rendered_items = html._(
html.h3("Not Done"),
_render_items([i for i in items.data if not i.done], toggle_item),
html.h3("Done"),
_render_items([i for i in items.data if i.done], toggle_item),
)
add_item = use_mutation(add_item_mutation, refetch=get_items_query)
if add_item.loading:
mutation_status = html.h2("Working...")
elif add_item.error:
mutation_status = html.h2(f"Error when adding - {add_item.error}")
else:
mutation_status = ""
def on_submit(event):
if event["key"] == "Enter":
add_item.execute(text=event["target"]["value"])
set_input_value("")
def on_change(event):
set_input_value(event["target"]["value"])
return html.div(
html.label("Add an item:"),
html.input(
{
"type": "text",
"id": "todo-input",
"value": input_value,
"onKeyPress": on_submit,
"onChange": on_change,
}
),
mutation_status,
rendered_items,
html.hr(),
)
def _render_items(items, toggle_item):
return html.ul(
[
html.li(
{"id": f"todo-item-{item.text}"},
item.text,
html.input(
{
"id": f"todo-item-{item.text}-checkbox",
"type": "checkbox",
"checked": item.done,
"onChange": lambda event, i=item: toggle_item.execute(i),
}
),
key=item.text,
)
for item in items
]
)
@component
def view_to_component_sync_func():
return view_to_component(views.view_to_component_sync_func)
@component
def view_to_component_async_func():
return view_to_component(views.view_to_component_async_func)
@component
def view_to_component_sync_class():
return view_to_component(views.ViewToComponentSyncClass)
@component
def view_to_component_async_class():
return view_to_component(views.ViewToComponentAsyncClass)
@component
def view_to_component_template_view_class():
return view_to_component(views.ViewToComponentTemplateViewClass)
@component
def view_to_component_sync_func_compatibility():
return html.div(
{"id": inspect.currentframe().f_code.co_name},
view_to_component(
views.view_to_component_sync_func_compatibility, compatibility=True
),
html.hr(),
)
@component
def view_to_component_async_func_compatibility():
return html.div(
{"id": inspect.currentframe().f_code.co_name},
view_to_component(
views.view_to_component_async_func_compatibility, compatibility=True
),
html.hr(),
)
@component
def view_to_component_sync_class_compatibility():
return html.div(
{"id": inspect.currentframe().f_code.co_name},
view_to_component(
views.ViewToComponentSyncClassCompatibility, compatibility=True
),
html.hr(),
)
@component
def view_to_component_async_class_compatibility():
return html.div(
{"id": inspect.currentframe().f_code.co_name},
view_to_component(
views.ViewToComponentAsyncClassCompatibility, compatibility=True
),
html.hr(),
)
@component
def view_to_component_template_view_class_compatibility():
return html.div(
{"id": inspect.currentframe().f_code.co_name},
view_to_component(
views.ViewToComponentTemplateViewClassCompatibility, compatibility=True
),
html.hr(),
)
@component
def view_to_component_script():
return view_to_component(views.view_to_component_script)
@component
def view_to_component_request():
request, set_request = hooks.use_state(None)
def on_click(_):
post_request = HttpRequest()
post_request.method = "POST"
set_request(post_request)
return html._(
html.button(
{"id": f"{inspect.currentframe().f_code.co_name}_btn", "onClick": on_click},
"Click me",
),
view_to_component(views.view_to_component_request, request=request),
)
@component
def view_to_component_args():
params, set_params = hooks.use_state("false")
def on_click(_):
set_params("")
return html._(
html.button(
{"id": f"{inspect.currentframe().f_code.co_name}_btn", "onClick": on_click},
"Click me",
),
view_to_component(views.view_to_component_args, args=[params]),
)
@component
def view_to_component_kwargs():
params, set_params = hooks.use_state("false")
def on_click(_):
set_params("")
return html._(
html.button(
{"id": f"{inspect.currentframe().f_code.co_name}_btn", "onClick": on_click},
"Click me",
),
view_to_component(views.view_to_component_kwargs, kwargs={"success": params}),
)