-
Notifications
You must be signed in to change notification settings - Fork 0
/
render.cljs
40 lines (31 loc) · 999 Bytes
/
render.cljs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
(ns elm-cljs.render
(:require-macros [cljs.core.async.macros :refer [go]])
(:require [cljs.core.async :refer [>!]]
[clojure.string :as str]
[elm-cljs.channels :refer [messages]]
[react :refer [createElement]]
[react-dom :as react-dom]))
(defn- callback? [key]
(str/starts-with? (name key) "on"))
(defn- wrap-callback [callback]
(fn [e]
(let [message (callback e)]
(go (>! messages message)))))
(defn- to-prop [key value]
(if (callback? key)
(wrap-callback value)
value))
(defn- props-reducer [acc [key value]]
(aset acc (name key) (to-prop key value))
acc)
(defn- to-props [props]
(reduce props-reducer (js-obj) props))
(declare to-react)
(defn- create-element [[tag props & children]]
(apply createElement (name tag) (to-props props) (map to-react children)))
(defn- to-react [view]
(if (vector? view)
(create-element view)
view))
(defn render [view el]
(react-dom/render (to-react view) el))