Move props accessors to util, add asserts for accessors in core

This commit is contained in:
Dan Holmsand 2014-02-14 11:13:30 +01:00
parent 695383c9a1
commit effcbe6615
4 changed files with 48 additions and 43 deletions

View File

@ -64,33 +64,39 @@ Everything is optional, except :render.
(defn state
"Returns the state of a component, as set with replace-state or set-state."
[this]
(assert (util/reagent-component? this))
(comp/state this))
(defn replace-state
"Set state of a component."
[this new-state]
(assert (util/reagent-component? this))
(comp/replace-state this new-state))
(defn set-state
"Merge component state with new-state."
[this new-state]
(assert (util/reagent-component? this))
(comp/set-state this new-state))
(defn props
"Returns the props passed to a component."
[this]
(comp/get-props this))
(assert (util/reagent-component? this))
(util/get-props this))
(defn children
"Returns the children passed to a component."
[this]
(comp/get-children this))
(assert (util/reagent-component? this))
(util/get-children this))
(defn argv
"Returns the entire Hiccup form passed to the component."
[this]
(comp/get-argv this))
(assert (util/reagent-component? this))
(util/get-argv this))
(defn dom-node
"Returns the root DOM node of a mounted component."

View File

@ -10,7 +10,7 @@
(def cljs-state "cljsState")
(def cljs-render "cljsRender")
;;; Accessors
;;; State
(defn state [this]
(aget this cljs-state))
@ -26,37 +26,14 @@
(defn set-state [this new-state]
(replace-state this (merge (state this) new-state)))
(defn js-props [C]
(aget C "props"))
(defn extract-props [v]
(let [p (get v 1)]
(if (map? p) p)))
(defn extract-children [v]
(let [p (get v 1)
first-child (if (or (nil? p) (map? p)) 2 1)]
(if (> (count v) first-child)
(subvec v first-child))))
(defn get-argv [C]
(-> C js-props (aget cljs-argv)))
(defn get-props [C]
(-> C get-argv extract-props))
(defn get-children [C]
(-> C get-argv extract-children))
;;; Rendering
(defn do-render [C]
(binding [*current-component* C]
(let [f (aget C cljs-render)
_ (assert (ifn? f))
p (js-props C)
p (util/js-props C)
res (if (nil? (aget C "componentFunction"))
(f C)
(let [argv (aget p cljs-argv)
@ -77,7 +54,7 @@
res)))))
;;; Function wrapping
;;; Method wrapping
(defn custom-wrapper [key f]
(case key
@ -100,7 +77,7 @@
(this-as C
;; Don't care about nextstate here, we use forceUpdate
;; when only when state has changed anyway.
(let [inprops (js-props C)
(let [inprops (util/js-props C)
old-argv (aget inprops cljs-argv)
new-argv (aget nextprops cljs-argv)]
(if (nil? f)

View File

@ -43,13 +43,6 @@
(coll? v) (clj->js v)
:else (fn [& args] (apply v args)))))
(defn extract-props [v]
(let [p (get v 1)]
(if (map? p) p)))
(defn get-props [this]
(-> this (aget "props") (aget cljs-argv) extract-props))
(defn undash-prop-name [n]
(or (attr-aliases n)
(util/dash-to-camel n)))
@ -94,12 +87,12 @@
;;; Specialization for input components
(defn input-initial-state [this]
(let [props (get-props this)]
(let [props (util/get-props this)]
#js {:value (:value props)
:checked (:checked props)}))
(defn input-handle-change [this e]
(let [props (get-props this)
(let [props (util/get-props this)
on-change (or (props :on-change) (props "onChange"))]
(when-not (nil? on-change)
(let [target (.-target e)]
@ -108,7 +101,7 @@
(on-change e))))
(defn input-will-receive-props [this new-props]
(let [props (-> new-props (aget cljs-argv) extract-props)]
(let [props (-> new-props (aget cljs-argv) util/extract-props)]
(.setState this #js {:value (:value props)
:checked (:checked props)})))
@ -127,7 +120,7 @@
(declare as-component)
(defn wrapped-render [this comp id-class input-setup]
(let [inprops (aget this "props")
(let [inprops (util/js-props this)
argv (aget inprops cljs-argv)
props (get argv 1)
hasprops (or (nil? props) (map? props))
@ -144,7 +137,7 @@
(.apply comp nil jsargs)))
(defn wrapped-should-update [C nextprops nextstate]
(let [inprops (aget C "props")
(let [inprops (util/js-props C)
a1 (aget inprops cljs-argv)
a2 (aget nextprops cljs-argv)]
(not (util/equal-args a1 a2))))

View File

@ -6,9 +6,38 @@
(def is-client (not (nil? (try (.-document js/window)
(catch js/Object e nil)))))
(def React reactimport/React)
;;; Props accessors
(def props "props")
(def cljs-level "cljsLevel")
(def cljs-argv "cljsArgv")
(def React reactimport/React)
(defn js-props [C]
(aget C props))
(defn extract-props [v]
(let [p (get v 1)]
(if (map? p) p)))
(defn extract-children [v]
(let [p (get v 1)
first-child (if (or (nil? p) (map? p)) 2 1)]
(if (> (count v) first-child)
(subvec v first-child))))
(defn get-argv [C]
(-> C (aget props) (aget cljs-argv)))
(defn get-props [C]
(-> C (aget props) (aget cljs-argv) extract-props))
(defn get-children [C]
(-> C (aget props) (aget cljs-argv) extract-children))
(defn reagent-component? [C]
(-> C get-argv nil? not))
;; Misc utilities