Say I have the following map:
(def m {"a" {"d" {}
"e" {}}
"b" {"f" {}
"g" {"h" {}
"i" {}}}
"c" {}})
I need to render it like this:
(om.dom/div #js {} "a"
(om.dom/div #js {} "d")
(om.dom/div #js {} "e"))
(om.dom/div #js {} "b"
(om.dom/div #js {} "f")
(om.dom/div #js {} "g"
(om.dom/div #js {} "h")
(om.dom/div #js {} "i")))
(om.dom/div #js {} "c")
How would I go about doing this? I have messed around with clojure.walk
, but couldn't get it to call om.dom/div
on the leaves first, then the direct parents, etc.
I am thinking that the solution might involve mapping a recursive function over the vals
of a given sub-map. It would break the map apart until it sees a leaf and then bubble the om.dom/div
calls back up the map.
So far I have this function:
(defn build-tree [data]
(apply dom/div #js {}
(->> (clojure.walk/postwalk
#(cond (map? %) (vec %)
(vector? %) %
(string? %) %) data)
(clojure.walk/postwalk
#(if (and (vector? %) (string? (first %)))
(apply dom/div #js {} %) %)))))
Which results in:
With this in the inspector:
Bonus points for generating nested dom/ul
and dom/li
elements..