Skip to content
This repository was archived by the owner on Mar 28, 2025. It is now read-only.

Commit 5a690d5

Browse files
committed
Support for multi-arity render fns (closes #23)
1 parent 751ca91 commit 5a690d5

File tree

5 files changed

+367
-354
lines changed

5 files changed

+367
-354
lines changed

README.md

+1
Original file line numberDiff line numberDiff line change
@@ -319,6 +319,7 @@ This is a detailed breakdown of what happens inside of Rum. By using `rum/defc`,
319319
### 0.2.7
320320

321321
- Allow components to refer to themselves (thx [Kevin Lynagh](https://github.com/lynaghk), pull request #30)
322+
- Support for multi-arity render fns (issue #23)
322323

323324
### 0.2.6
324325

examples/examples.cljs

+8-4
Original file line numberDiff line numberDiff line change
@@ -331,9 +331,13 @@
331331

332332
;; Self-referencing component
333333

334-
(rum/defc tree [form]
335-
(if (sequential? form)
336-
[:.branch (map tree form)]
337-
[:.leaf (str form)]))
334+
(rum/defc tree < rum/static
335+
([form] (tree form 0))
336+
([form depth]
337+
(let [offset {:style {:margin-left (* 10 depth)}}]
338+
(if (sequential? form)
339+
[:.branch offset (map #(tree % (inc depth)) form)]
340+
[:.leaf offset (str form)]))))
338341

339342
(rum/mount (tree [:a [:b [:c :d [:e] :g]]]) (el "selfie"))
343+

src/rum.clj

+15-7
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,10 @@
22
(:require
33
[sablono.compiler :as s]))
44

5+
(defn- fn-body? [form]
6+
(and (list? form)
7+
(vector? (first form))))
8+
59
(defn- parse-defc [xs]
610
(loop [res {}
711
xs xs
@@ -11,20 +15,23 @@
1115
(cond
1216
(and (empty? res) (symbol? x))
1317
(recur {:name x} next nil)
14-
(vector? x) (assoc res :argvec x
15-
:render next)
16-
(string? x) (recur (assoc res :doc x) next nil)
17-
(= '< x) (recur res next :mixins)
18+
(fn-body? xs) (assoc res :bodies (list xs))
19+
(every? fn-body? xs) (assoc res :bodies xs)
20+
(string? x) (recur (assoc res :doc x) next nil)
21+
(= '< x) (recur res next :mixins)
1822
(= mode :mixins)
1923
(recur (update-in res [:mixins] (fnil conj []) x) next :mixins)
2024
:else
2125
(throw (IllegalArgumentException. (str "Syntax error at " xs)))))))
2226

27+
(defn- compile-body [[argvec & body]]
28+
(list argvec (s/compile-html `(do ~@body))))
29+
2330
(defn- -defc [render-ctor body]
24-
(let [{:keys [name doc mixins argvec render]} (parse-defc body)]
31+
(let [{:keys [name doc mixins bodies]} (parse-defc body)
32+
render-fn (map compile-body bodies)]
2533
`(def ~name ~doc
26-
(let [render-fn# (fn ~argvec ~(s/compile-html `(do ~@render)))
27-
render-mixin# (~render-ctor render-fn#)
34+
(let [render-mixin# (~render-ctor (fn ~@render-fn))
2835
class# (rum/build-class (concat [render-mixin#] ~mixins) ~(str name))
2936
ctor# (fn [& args#]
3037
(let [state# (args->state args#)]
@@ -72,3 +79,4 @@
7279
(mapcat (fn [[k v]] [(props k) v])))]
7380
`(rum/element (ctor->class ~ctor) (args->state [~@as]) (cljs.core/js-obj ~@ps))))
7481

82+

target/main.js

+343-341
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

web/style.css

-2
Original file line numberDiff line numberDiff line change
@@ -20,5 +20,3 @@ input:focus { outline: 2px solid #a3ccf7; }
2020
.artboard { -webkit-user-select: none; line-height: 10px; }
2121
.art-cell { width: 12px; height: 12px; margin: 0 1px 1px 0; display: inline-block; background-color: #EEE; }
2222
.artboard .stats { font-size: 12px; line-height: 14px; margin-top: 8px; }
23-
24-
.branch > .branch { margin-left: 20px; }

0 commit comments

Comments
 (0)