Skip to content

Commit d5c4115

Browse files
committed
Add solution placeholders for most of the examples
1 parent 318d5f2 commit d5c4115

File tree

2 files changed

+70
-10
lines changed

2 files changed

+70
-10
lines changed

demo/reagentdemo/intro.cljs

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -139,13 +139,15 @@
139139

140140
[:p "You can build new components using other components as
141141
building blocks. Like this:"]
142-
[demo-component {:comp simple-parent
142+
[demo-component {:expected simple-parent
143+
:comp solutions/simple-parent
143144
:src (s/src-of [:simple-parent])}]
144145

145146
[:p "Data is passed to child components using plain old Clojure
146147
data types. Like this:"]
147148

148-
[demo-component {:comp say-hello
149+
[demo-component {:expected say-hello
150+
:comp solutions/say-hello
149151
:src (s/src-of [:hello-component :say-hello])}]
150152

151153
[:p [:strong "Note: "]
@@ -160,7 +162,8 @@
160162
[:p "Here is another example that shows items in a "
161163
[:code "seq"] ":" ]
162164

163-
[demo-component {:comp lister-user
165+
[demo-component {:expected lister-user
166+
:comp solutions/lister-user
164167
:src (s/src-of [:lister :lister-user])}]
165168

166169
[:p [:strong "Note: "]
@@ -184,7 +187,8 @@
184187
re-rendered when its value changes."]
185188

186189
[:p "Let’s demonstrate that with a simple example:"]
187-
[demo-component {:comp counting-component
190+
[demo-component {:expected counting-component
191+
:comp solutions/counting-component
188192
:src [:pre
189193
ns-src
190194
(s/src-of [:click-count :counting-component])]}]
@@ -196,7 +200,8 @@
196200
[:code "setTimeout"] " every time the component is rendered to
197201
update a counter:"]
198202

199-
[demo-component {:comp timer-component
203+
[demo-component {:expected timer-component
204+
:comp solutions/timer-component
200205
:src (s/src-of [:timer-component])}]
201206

202207
[:p "The previous example also uses another feature of Reagent: a
@@ -210,7 +215,8 @@
210215
[:p "By simply passing an "[:code "atom"]" around you can share
211216
state management between components, like this:"]
212217

213-
[demo-component {:comp shared-state
218+
[demo-component {:expected shared-state
219+
:comp solutions/shared-state
214220
:src [:pre
215221
ns-src
216222
(s/src-of [:atom-input :shared-state])]}]
@@ -292,7 +298,8 @@
292298
[:p "Data is kept in a single " [:code "reagent.core/atom"] ": a map
293299
with height, weight and BMI as keys."]
294300

295-
[demo-component {:comp bmi-component
301+
[demo-component {:expected bmi-component
302+
:comp solutions/bmi-component
296303
:src [:pre
297304
ns-src
298305
(s/src-of [:bmi-data :calc-bmi :slider
@@ -306,7 +313,7 @@
306313
Leiningen project files and everything. Here’s one of them in
307314
action:"]
308315

309-
[demo-component {:comp simple/simple-example
316+
[demo-component {:expected simple/simple-example
310317
:complete true
311318
:src (s/src-of nil "simpleexample/core.cljs")}]])
312319

@@ -318,7 +325,7 @@
318325
Reagent (cheating a little bit by skipping routing and
319326
persistence):"]
320327

321-
[demo-component {:comp todo/todo-app
328+
[demo-component {:expected todo/todo-app
322329
:complete true
323330
:src (s/src-of nil "todomvc/core.cljs")}]])
324331

demo/reagentdemo/solutions.cljs

Lines changed: 54 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,58 @@
11
(ns reagentdemo.solutions
2-
"Solutions for the tutorial examples. Your code goes here.")
2+
"Solutions for the tutorial examples. Your code goes here."
3+
(:require
4+
[reagent.core :as r]))
35

46
(defn simple-component []
57
[:div "Hello!"])
8+
9+
(defn simple-parent []
10+
[:div "Fix me in reagentdemo.solutions/simple-parent"])
11+
12+
(defn say-hello []
13+
[:div "Fix me in reagentdemo.solutions/say-hello"])
14+
15+
(defn lister [items]
16+
[:ul
17+
(for [_ items]
18+
:fix-me)])
19+
20+
(defn lister-user []
21+
[:div
22+
"Fix me in reagentdemo.solutions/lister-user"
23+
[lister (range 1)]])
24+
25+
(defn counting-component []
26+
[:div "Fix me in reagentdemo.solutions/counting-component"])
27+
28+
(defn timer-component []
29+
[:div "Fix me in reagentdemo.solutions/timer-component"])
30+
31+
(defn shared-state []
32+
[:div "Fix me in reagentdemo.solutions/shared-state"])
33+
34+
(def bmi-data (r/atom {:height 180 :weight 80}))
35+
36+
(defn calc-bmi []
37+
(let [{:keys [height weight bmi] :as data} @bmi-data
38+
h (/ height 100)]
39+
(if (nil? bmi)
40+
(assoc data :bmi (/ weight (* h h)))
41+
(assoc data :weight (* bmi h h)))))
42+
43+
(defn slider [param value min max]
44+
[:input {:type "range" :value value :min min :max max
45+
:style {:width "100%"}
46+
:on-change (fn [e]
47+
;; fix me
48+
)}])
49+
50+
(defn bmi-component []
51+
(let [{:keys [weight height bmi]} (calc-bmi)
52+
[color diagnose] (cond
53+
(< bmi 18.5) ["orange" "underweight"]
54+
(< bmi 25) ["inherit" "normal"]
55+
(< bmi 30) ["orange" "overweight"]
56+
:else ["red" "obese"])]
57+
[:div
58+
[:h3 "Fix me in reagentdemo.solutions/bmi-component"]]))

0 commit comments

Comments
 (0)