More property access to component.cljs

This commit is contained in:
Dan Holmsand 2015-10-06 10:40:11 +02:00
parent 52e729a294
commit eb359b301d
3 changed files with 34 additions and 32 deletions

View File

@ -136,21 +136,21 @@ Everything is optional, except either :reagent-render or :render.
(defn state-atom
"Returns an atom containing a components state."
[this]
(assert (util/reagent-component? this))
(assert (comp/reagent-component? this))
(comp/state-atom this))
(defn state
"Returns the state of a component, as set with replace-state or set-state.
Equivalent to (deref (r/state-atom this))"
[this]
(assert (util/reagent-component? this))
(assert (comp/reagent-component? this))
(deref (state-atom this)))
(defn replace-state
"Set state of a component.
Equivalent to (reset! (state-atom this) new-state)"
[this new-state]
(assert (util/reagent-component? this))
(assert (comp/reagent-component? this))
(assert (or (nil? new-state) (map? new-state)))
(reset! (state-atom this) new-state))
@ -158,7 +158,7 @@ Equivalent to (reset! (state-atom this) new-state)"
"Merge component state with new-state.
Equivalent to (swap! (state-atom this) merge new-state)"
[this new-state]
(assert (util/reagent-component? this))
(assert (comp/reagent-component? this))
(assert (or (nil? new-state) (map? new-state)))
(swap! (state-atom this) merge new-state))
@ -176,20 +176,20 @@ Equivalent to (swap! (state-atom this) merge new-state)"
(defn props
"Returns the props passed to a component."
[this]
(assert (util/reagent-component? this))
(util/get-props this))
(assert (comp/reagent-component? this))
(comp/get-props this))
(defn children
"Returns the children passed to a component."
[this]
(assert (util/reagent-component? this))
(util/get-children this))
(assert (comp/reagent-component? this))
(comp/get-children this))
(defn argv
"Returns the entire Hiccup form passed to the component."
[this]
(assert (util/reagent-component? this))
(util/get-argv this))
(assert (comp/reagent-component? this))
(comp/get-argv this))
(defn dom-node
"Returns the root DOM node of a mounted component."

View File

@ -9,6 +9,30 @@
(declare ^:dynamic *non-reactive*)
(defn extract-props [v]
(let [p (nth v 1 nil)]
(if (map? p) p)))
(defn extract-children [v]
(let [p (nth v 1 nil)
first-child (if (or (nil? p) (map? p)) 2 1)]
(if (> (count v) first-child)
(subvec v first-child))))
(defn get-argv [c]
(.' c :props.argv))
(defn get-props [c]
(-> (.' c :props.argv) extract-props))
(defn get-children [c]
(-> (.' c :props.argv) extract-children))
(defn reagent-component? [c]
(-> (.' c :props.argv) nil? not))
;;; State
(defn state-atom [this]

View File

@ -8,28 +8,6 @@
;;; Props accessors
(defn extract-props [v]
(let [p (nth v 1 nil)]
(if (map? p) p)))
(defn extract-children [v]
(let [p (nth v 1 nil)
first-child (if (or (nil? p) (map? p)) 2 1)]
(if (> (count v) first-child)
(subvec v first-child))))
(defn get-argv [c]
(.' c :props.argv))
(defn get-props [c]
(-> (.' c :props.argv) extract-props))
(defn get-children [c]
(-> (.' c :props.argv) extract-children))
(defn reagent-component? [c]
(-> (.' c :props.argv) nil? not))
(defn cached-react-class [c]
(.' c :cljsReactClass))