mirror of https://github.com/status-im/reagent.git
Create component without create-react-class
This commit is contained in:
parent
bc922f7319
commit
2f1c72882f
|
@ -1,5 +1,5 @@
|
||||||
(ns reagent.impl.component
|
(ns reagent.impl.component
|
||||||
(:require [create-react-class :as create-react-class]
|
(:require [goog.object :as gobj]
|
||||||
[react :as react]
|
[react :as react]
|
||||||
[reagent.impl.util :as util]
|
[reagent.impl.util :as util]
|
||||||
[reagent.impl.batching :as batch]
|
[reagent.impl.batching :as batch]
|
||||||
|
@ -149,9 +149,10 @@
|
||||||
:getDefaultProps
|
:getDefaultProps
|
||||||
(throw (js/Error. "getDefaultProps not supported"))
|
(throw (js/Error. "getDefaultProps not supported"))
|
||||||
|
|
||||||
|
;; In ES6 React, this is now part of the constructor
|
||||||
:getInitialState
|
:getInitialState
|
||||||
(fn getInitialState []
|
(fn getInitialState [c]
|
||||||
(this-as c (reset! (state-atom c) (.call f c c))))
|
(reset! (state-atom c) (.call f c c)))
|
||||||
|
|
||||||
:componentWillReceiveProps
|
:componentWillReceiveProps
|
||||||
(fn componentWillReceiveProps [nextprops]
|
(fn componentWillReceiveProps [nextprops]
|
||||||
|
@ -250,7 +251,6 @@
|
||||||
{} fmap)]
|
{} fmap)]
|
||||||
(assoc fmap
|
(assoc fmap
|
||||||
:displayName name
|
:displayName name
|
||||||
:autobind false
|
|
||||||
:cljsLegacyRender legacy-render
|
:cljsLegacyRender legacy-render
|
||||||
:reagentRender render-fun
|
:reagentRender render-fun
|
||||||
:render (:render static-fns))))
|
:render (:render static-fns))))
|
||||||
|
@ -265,14 +265,44 @@
|
||||||
(-> body
|
(-> body
|
||||||
camelify-map-keys
|
camelify-map-keys
|
||||||
add-obligatory
|
add-obligatory
|
||||||
wrap-funs
|
wrap-funs))
|
||||||
map-to-js))
|
|
||||||
|
;; Credits to Paulus Esterhazy, Thomas Heller
|
||||||
|
;; https://gist.github.com/pesterhazy/2a25c82db0519a28e415b40481f84554
|
||||||
|
;; https://gist.github.com/thheller/7f530b34de1c44589f4e0671e1ef7533#file-es6-class-cljs-L18
|
||||||
|
(defn make-component
|
||||||
|
"Creates a React Component class
|
||||||
|
`m` is a js-obj of class methods
|
||||||
|
`s` is a js-obj of static methods"
|
||||||
|
([display-name m s] (make-component display-name nil m s))
|
||||||
|
([display-name construct m s]
|
||||||
|
(let [cmp (fn [props context updater]
|
||||||
|
(cljs.core/this-as this
|
||||||
|
(.call react/Component this props context updater)
|
||||||
|
(when construct
|
||||||
|
(construct this))
|
||||||
|
this))]
|
||||||
|
(gobj/extend (.-prototype cmp) (.-prototype react/Component) m)
|
||||||
|
(gobj/extend cmp react/Component s)
|
||||||
|
|
||||||
|
(when display-name
|
||||||
|
(set! (.-displayName cmp) display-name)
|
||||||
|
(set! (.-cljs$lang$ctorStr cmp) display-name)
|
||||||
|
(set! (.-cljs$lang$ctorPrWriter cmp)
|
||||||
|
(fn [this writer opt]
|
||||||
|
(cljs.core/-write writer display-name))))
|
||||||
|
(set! (.-cljs$lang$type cmp) true)
|
||||||
|
(set! (.. cmp -prototype -constructor) cmp))))
|
||||||
|
|
||||||
(defn create-class [body]
|
(defn create-class [body]
|
||||||
{:pre [(map? body)]}
|
{:pre [(map? body)]}
|
||||||
(->> body
|
(let [body (cljsify body)
|
||||||
cljsify
|
m (dissoc body :displayName :getInitialState :contextTypes :childContextTypes)
|
||||||
create-react-class))
|
s (select-keys body [:childContextTypes :contextTypes])]
|
||||||
|
(make-component (:displayName body)
|
||||||
|
(:getInitialState body)
|
||||||
|
(map-to-js m)
|
||||||
|
(map-to-js s))))
|
||||||
|
|
||||||
(defn fiber-component-path [fiber]
|
(defn fiber-component-path [fiber]
|
||||||
(let [name (some-> fiber
|
(let [name (some-> fiber
|
||||||
|
|
Loading…
Reference in New Issue