Skip to content

Commit

Permalink
Support for multi-arity render fns (closes #23)
Browse files Browse the repository at this point in the history
  • Loading branch information
tonsky committed Jun 14, 2015
1 parent 751ca91 commit 5a690d5
Show file tree
Hide file tree
Showing 5 changed files with 367 additions and 354 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -319,6 +319,7 @@ This is a detailed breakdown of what happens inside of Rum. By using `rum/defc`,
### 0.2.7

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

### 0.2.6

Expand Down
12 changes: 8 additions & 4 deletions examples/examples.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -331,9 +331,13 @@

;; Self-referencing component

(rum/defc tree [form]
(if (sequential? form)
[:.branch (map tree form)]
[:.leaf (str form)]))
(rum/defc tree < rum/static
([form] (tree form 0))
([form depth]
(let [offset {:style {:margin-left (* 10 depth)}}]
(if (sequential? form)
[:.branch offset (map #(tree % (inc depth)) form)]
[:.leaf offset (str form)]))))

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

22 changes: 15 additions & 7 deletions src/rum.clj
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@
(:require
[sablono.compiler :as s]))

(defn- fn-body? [form]
(and (list? form)
(vector? (first form))))

(defn- parse-defc [xs]
(loop [res {}
xs xs
Expand All @@ -11,20 +15,23 @@
(cond
(and (empty? res) (symbol? x))
(recur {:name x} next nil)
(vector? x) (assoc res :argvec x
:render next)
(string? x) (recur (assoc res :doc x) next nil)
(= '< x) (recur res next :mixins)
(fn-body? xs) (assoc res :bodies (list xs))
(every? fn-body? xs) (assoc res :bodies xs)
(string? x) (recur (assoc res :doc x) next nil)
(= '< x) (recur res next :mixins)
(= mode :mixins)
(recur (update-in res [:mixins] (fnil conj []) x) next :mixins)
:else
(throw (IllegalArgumentException. (str "Syntax error at " xs)))))))

(defn- compile-body [[argvec & body]]
(list argvec (s/compile-html `(do ~@body))))

(defn- -defc [render-ctor body]
(let [{:keys [name doc mixins argvec render]} (parse-defc body)]
(let [{:keys [name doc mixins bodies]} (parse-defc body)
render-fn (map compile-body bodies)]
`(def ~name ~doc
(let [render-fn# (fn ~argvec ~(s/compile-html `(do ~@render)))
render-mixin# (~render-ctor render-fn#)
(let [render-mixin# (~render-ctor (fn ~@render-fn))
class# (rum/build-class (concat [render-mixin#] ~mixins) ~(str name))
ctor# (fn [& args#]
(let [state# (args->state args#)]
Expand Down Expand Up @@ -72,3 +79,4 @@
(mapcat (fn [[k v]] [(props k) v])))]
`(rum/element (ctor->class ~ctor) (args->state [~@as]) (cljs.core/js-obj ~@ps))))


684 changes: 343 additions & 341 deletions target/main.js

Large diffs are not rendered by default.

2 changes: 0 additions & 2 deletions web/style.css
Original file line number Diff line number Diff line change
Expand Up @@ -20,5 +20,3 @@ input:focus { outline: 2px solid #a3ccf7; }
.artboard { -webkit-user-select: none; line-height: 10px; }
.art-cell { width: 12px; height: 12px; margin: 0 1px 1px 0; display: inline-block; background-color: #EEE; }
.artboard .stats { font-size: 12px; line-height: 14px; margin-top: 8px; }

.branch > .branch { margin-left: 20px; }

0 comments on commit 5a690d5

Please sign in to comment.