mirror of https://github.com/status-im/reagent.git
Move props accessors to util, add asserts for accessors in core
This commit is contained in:
parent
695383c9a1
commit
effcbe6615
|
@ -64,33 +64,39 @@ Everything is optional, except :render.
|
||||||
(defn state
|
(defn state
|
||||||
"Returns the state of a component, as set with replace-state or set-state."
|
"Returns the state of a component, as set with replace-state or set-state."
|
||||||
[this]
|
[this]
|
||||||
|
(assert (util/reagent-component? this))
|
||||||
(comp/state this))
|
(comp/state this))
|
||||||
|
|
||||||
(defn replace-state
|
(defn replace-state
|
||||||
"Set state of a component."
|
"Set state of a component."
|
||||||
[this new-state]
|
[this new-state]
|
||||||
|
(assert (util/reagent-component? this))
|
||||||
(comp/replace-state this new-state))
|
(comp/replace-state this new-state))
|
||||||
|
|
||||||
(defn set-state
|
(defn set-state
|
||||||
"Merge component state with new-state."
|
"Merge component state with new-state."
|
||||||
[this new-state]
|
[this new-state]
|
||||||
|
(assert (util/reagent-component? this))
|
||||||
(comp/set-state this new-state))
|
(comp/set-state this new-state))
|
||||||
|
|
||||||
|
|
||||||
(defn props
|
(defn props
|
||||||
"Returns the props passed to a component."
|
"Returns the props passed to a component."
|
||||||
[this]
|
[this]
|
||||||
(comp/get-props this))
|
(assert (util/reagent-component? this))
|
||||||
|
(util/get-props this))
|
||||||
|
|
||||||
(defn children
|
(defn children
|
||||||
"Returns the children passed to a component."
|
"Returns the children passed to a component."
|
||||||
[this]
|
[this]
|
||||||
(comp/get-children this))
|
(assert (util/reagent-component? this))
|
||||||
|
(util/get-children this))
|
||||||
|
|
||||||
(defn argv
|
(defn argv
|
||||||
"Returns the entire Hiccup form passed to the component."
|
"Returns the entire Hiccup form passed to the component."
|
||||||
[this]
|
[this]
|
||||||
(comp/get-argv this))
|
(assert (util/reagent-component? this))
|
||||||
|
(util/get-argv this))
|
||||||
|
|
||||||
(defn dom-node
|
(defn dom-node
|
||||||
"Returns the root DOM node of a mounted component."
|
"Returns the root DOM node of a mounted component."
|
||||||
|
|
|
@ -10,7 +10,7 @@
|
||||||
(def cljs-state "cljsState")
|
(def cljs-state "cljsState")
|
||||||
(def cljs-render "cljsRender")
|
(def cljs-render "cljsRender")
|
||||||
|
|
||||||
;;; Accessors
|
;;; State
|
||||||
|
|
||||||
(defn state [this]
|
(defn state [this]
|
||||||
(aget this cljs-state))
|
(aget this cljs-state))
|
||||||
|
@ -26,37 +26,14 @@
|
||||||
(defn set-state [this new-state]
|
(defn set-state [this new-state]
|
||||||
(replace-state this (merge (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
|
;;; Rendering
|
||||||
|
|
||||||
|
|
||||||
(defn do-render [C]
|
(defn do-render [C]
|
||||||
(binding [*current-component* C]
|
(binding [*current-component* C]
|
||||||
(let [f (aget C cljs-render)
|
(let [f (aget C cljs-render)
|
||||||
_ (assert (ifn? f))
|
_ (assert (ifn? f))
|
||||||
p (js-props C)
|
p (util/js-props C)
|
||||||
res (if (nil? (aget C "componentFunction"))
|
res (if (nil? (aget C "componentFunction"))
|
||||||
(f C)
|
(f C)
|
||||||
(let [argv (aget p cljs-argv)
|
(let [argv (aget p cljs-argv)
|
||||||
|
@ -77,7 +54,7 @@
|
||||||
res)))))
|
res)))))
|
||||||
|
|
||||||
|
|
||||||
;;; Function wrapping
|
;;; Method wrapping
|
||||||
|
|
||||||
(defn custom-wrapper [key f]
|
(defn custom-wrapper [key f]
|
||||||
(case key
|
(case key
|
||||||
|
@ -100,7 +77,7 @@
|
||||||
(this-as C
|
(this-as C
|
||||||
;; Don't care about nextstate here, we use forceUpdate
|
;; Don't care about nextstate here, we use forceUpdate
|
||||||
;; when only when state has changed anyway.
|
;; when only when state has changed anyway.
|
||||||
(let [inprops (js-props C)
|
(let [inprops (util/js-props C)
|
||||||
old-argv (aget inprops cljs-argv)
|
old-argv (aget inprops cljs-argv)
|
||||||
new-argv (aget nextprops cljs-argv)]
|
new-argv (aget nextprops cljs-argv)]
|
||||||
(if (nil? f)
|
(if (nil? f)
|
||||||
|
|
|
@ -43,13 +43,6 @@
|
||||||
(coll? v) (clj->js v)
|
(coll? v) (clj->js v)
|
||||||
:else (fn [& args] (apply v args)))))
|
: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]
|
(defn undash-prop-name [n]
|
||||||
(or (attr-aliases n)
|
(or (attr-aliases n)
|
||||||
(util/dash-to-camel n)))
|
(util/dash-to-camel n)))
|
||||||
|
@ -94,12 +87,12 @@
|
||||||
;;; Specialization for input components
|
;;; Specialization for input components
|
||||||
|
|
||||||
(defn input-initial-state [this]
|
(defn input-initial-state [this]
|
||||||
(let [props (get-props this)]
|
(let [props (util/get-props this)]
|
||||||
#js {:value (:value props)
|
#js {:value (:value props)
|
||||||
:checked (:checked props)}))
|
:checked (:checked props)}))
|
||||||
|
|
||||||
(defn input-handle-change [this e]
|
(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"))]
|
on-change (or (props :on-change) (props "onChange"))]
|
||||||
(when-not (nil? on-change)
|
(when-not (nil? on-change)
|
||||||
(let [target (.-target e)]
|
(let [target (.-target e)]
|
||||||
|
@ -108,7 +101,7 @@
|
||||||
(on-change e))))
|
(on-change e))))
|
||||||
|
|
||||||
(defn input-will-receive-props [this new-props]
|
(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)
|
(.setState this #js {:value (:value props)
|
||||||
:checked (:checked props)})))
|
:checked (:checked props)})))
|
||||||
|
|
||||||
|
@ -127,7 +120,7 @@
|
||||||
(declare as-component)
|
(declare as-component)
|
||||||
|
|
||||||
(defn wrapped-render [this comp id-class input-setup]
|
(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)
|
argv (aget inprops cljs-argv)
|
||||||
props (get argv 1)
|
props (get argv 1)
|
||||||
hasprops (or (nil? props) (map? props))
|
hasprops (or (nil? props) (map? props))
|
||||||
|
@ -144,7 +137,7 @@
|
||||||
(.apply comp nil jsargs)))
|
(.apply comp nil jsargs)))
|
||||||
|
|
||||||
(defn wrapped-should-update [C nextprops nextstate]
|
(defn wrapped-should-update [C nextprops nextstate]
|
||||||
(let [inprops (aget C "props")
|
(let [inprops (util/js-props C)
|
||||||
a1 (aget inprops cljs-argv)
|
a1 (aget inprops cljs-argv)
|
||||||
a2 (aget nextprops cljs-argv)]
|
a2 (aget nextprops cljs-argv)]
|
||||||
(not (util/equal-args a1 a2))))
|
(not (util/equal-args a1 a2))))
|
||||||
|
|
|
@ -6,9 +6,38 @@
|
||||||
(def is-client (not (nil? (try (.-document js/window)
|
(def is-client (not (nil? (try (.-document js/window)
|
||||||
(catch js/Object e nil)))))
|
(catch js/Object e nil)))))
|
||||||
|
|
||||||
|
(def React reactimport/React)
|
||||||
|
|
||||||
|
;;; Props accessors
|
||||||
|
|
||||||
|
(def props "props")
|
||||||
(def cljs-level "cljsLevel")
|
(def cljs-level "cljsLevel")
|
||||||
(def cljs-argv "cljsArgv")
|
(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
|
;; Misc utilities
|
||||||
|
|
Loading…
Reference in New Issue