Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Handle sequences in hiccup like hiccup does #186

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
69 changes: 20 additions & 49 deletions src/reagent/impl/template.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@

(defn valid-tag? [x]
(or (hiccup-tag? x)
(ifn? x)
(fn? x)
(instance? NativeWrapper x)))


Expand Down Expand Up @@ -287,60 +287,31 @@

:else (reag-element tag v))))

(declare expand-seq)
(declare expand-seq-check)
(defn nested? [x]
(and (sequential? x) (not (valid-tag? (first x)))))

(defn as-element [x]
(defn as-element [x]
(cond (string? x) x
(vector? x) (vec-to-elem x)
(seq? x) (if (dev?)
(expand-seq-check x)
(expand-seq x))
(and (vector? x) (not (nested? x))) (vec-to-elem x)
(or (nested? x) (sequential? x)) (map as-element x)
true x))

(defn expand-seq [s]
(let [a (into-array s)]
(dotimes [i (alength a)]
(aset a i (as-element (aget a i))))
a))

(defn expand-seq-dev [s o]
(let [a (into-array s)]
(dotimes [i (alength a)]
(let [val (aget a i)]
(when (and (vector? val)
(nil? (key-from-vec val)))
(.! o :no-key true))
(aset a i (as-element val))))
a))

(defn expand-seq-check [x]
(let [ctx #js{}
res (if (nil? ratom/*ratom-context*)
(expand-seq-dev x ctx)
(ratom/capture-derefed #(expand-seq-dev x ctx)
ctx))]
(when (ratom/captured ctx)
(warn "Reactive deref not supported in lazy seq, "
"it should be wrapped in doall"
(comp/comp-name) ". Value:\n" (pr-str x)))
(when (and (not comp/*non-reactive*)
(.' ctx :no-key))
(warn "Every element in a seq should have a unique "
":key" (comp/comp-name) ". Value: " (pr-str x)))
res))

(defn make-element [argv comp jsprops first-child]
(case (- (count argv) first-child)
;; Optimize cases of zero or one child
;; Optimize case of no children
0 (.' js/React createElement comp jsprops)

1 (.' js/React createElement comp jsprops
(as-element (nth argv first-child)))

(.apply (.' js/React :createElement) nil
(reduce-kv (fn [a k v]
(when (>= k first-child)
(.push a (as-element v)))
a)
#js[comp jsprops] argv))))
(reduce-kv
(fn [a k v]
(when (>= k first-child)
(if (and (not (string? v))
(not (and (vector? v) (not (nested? v))))
(or (sequential? v) (nested? v)))
(->> (map as-element v)
(flatten)
(into-array)
(js/Array.prototype.push.apply a))
(.push a (as-element v))))
a)
#js[comp jsprops] argv))))
9 changes: 8 additions & 1 deletion test/reagenttest/testreagent.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -434,7 +434,14 @@
(is (= (rstr (ae [:div [:div "foo"]]))
(rstr (ce "div" nil (ae [:div "foo"])))))
(is (= (rstr (ae [:div [:div "foo"]]))
(rstr (ae [:div (ce "div" nil "foo")]))))))
(rstr (ae [:div (ce "div" nil "foo")]))))

(is (= (rstr (ae [:div (map str (range 3))]))
(rstr (ce "div" nil "0" "1" "2"))))
(is (= (rstr (ae [:div [[:div "a"] [[:div "b"]]]]))
(rstr (ce "div" nil (ce "div" nil "a") (ce "div" nil "b")))))
(is (= (rstr (ae [:div [[:div] [:div]]]))
(rstr (ce "div" nil (ce "div" nil) (ce "div" nil)))))))

(def ndiv (.' js/React
createClass
Expand Down