Merge branch 'poernahi-extends-component'

This commit is contained in:
Juho Teperi 2018-12-31 10:42:58 +02:00
commit 3a69df6f2c
7 changed files with 638 additions and 705 deletions

View File

@ -5,6 +5,7 @@
**[compare](https://github.com/reagent-project/reagent/compare/v0.8.1...master)** **[compare](https://github.com/reagent-project/reagent/compare/v0.8.1...master)**
- Fix using metadata to set React key with Fragment shortcut (`:<>`) ([#401](https://github.com/reagent-project/reagent/issues/401)) - Fix using metadata to set React key with Fragment shortcut (`:<>`) ([#401](https://github.com/reagent-project/reagent/issues/401))
- Create React Component without `create-react-class`
## 0.8.1 (2018-05-15) ## 0.8.1 (2018-05-15)

View File

@ -42,7 +42,6 @@
:install-deps true :install-deps true
:npm-deps {react "16.6.0" :npm-deps {react "16.6.0"
react-dom "16.6.0" react-dom "16.6.0"
create-react-class "15.6.3"
"@material-ui/core" "3.1.1" "@material-ui/core" "3.1.1"
"@material-ui/icons" "3.0.1"} "@material-ui/icons" "3.0.1"}
:process-shim true}}}}) :process-shim true}}}})

1247
package-lock.json generated

File diff suppressed because it is too large Load Diff

View File

@ -2,7 +2,6 @@
"private": true, "private": true,
"dependencies": { "dependencies": {
"@cljs-oss/module-deps": "1.1.1", "@cljs-oss/module-deps": "1.1.1",
"create-react-class": "15.6.3",
"prop-types": "15.6.2", "prop-types": "15.6.2",
"react": "16.6.0", "react": "16.6.0",
"react-dom": "16.6.0" "react-dom": "16.6.0"

View File

@ -9,13 +9,12 @@
;; like react-leaflet might have closer dependency to a other version. ;; like react-leaflet might have closer dependency to a other version.
[cljsjs/react "16.6.0-0"] [cljsjs/react "16.6.0-0"]
[cljsjs/react-dom "16.6.0-0"] [cljsjs/react-dom "16.6.0-0"]
[cljsjs/react-dom-server "16.6.0-0"] [cljsjs/react-dom-server "16.6.0-0"]]
[cljsjs/create-react-class "15.6.3-1"]]
:plugins [[lein-cljsbuild "1.1.7"] :plugins [[lein-cljsbuild "1.1.7"]
[lein-doo "0.1.10"] [lein-doo "0.1.10"]
[lein-codox "0.10.3"] [lein-codox "0.10.3"]
[lein-figwheel "0.5.17"]] [lein-figwheel "0.5.18"]]
:source-paths ["src"] :source-paths ["src"]
@ -24,7 +23,7 @@
:source-paths ["src"]} :source-paths ["src"]}
:profiles {:dev {:dependencies [[org.clojure/clojurescript "1.10.439"] :profiles {:dev {:dependencies [[org.clojure/clojurescript "1.10.439"]
[figwheel "0.5.17"] [figwheel "0.5.18"]
[doo "0.1.10"] [doo "0.1.10"]
[cljsjs/prop-types "15.6.2-0"]] [cljsjs/prop-types "15.6.2-0"]]
:source-paths ["demo" "test" "examples/todomvc/src" "examples/simple/src" "examples/geometry/src"] :source-paths ["demo" "test" "examples/todomvc/src" "examples/simple/src" "examples/geometry/src"]

View File

@ -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]
@ -156,9 +156,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]
@ -257,7 +258,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))))
@ -272,14 +272,50 @@
(-> body (-> body
camelify-map-keys camelify-map-keys
add-obligatory add-obligatory
wrap-funs wrap-funs))
map-to-js))
(defn create-class [body] ;; Idea from:
;; https://gist.github.com/pesterhazy/2a25c82db0519a28e415b40481f84554
;; https://gist.github.com/thheller/7f530b34de1c44589f4e0671e1ef7533#file-es6-class-cljs-L18
(def built-in-static-method-names
[:childContextTypes :contextTypes
:getDerivedStateFromProps :getDerivedStateFromError])
(defn create-class
"Creates JS class based on provided Clojure map.
Map keys should use `React.Component` method names (https://reactjs.org/docs/react-component.html).
Constructor function is defined using key `:getInitialState`.
React built-in static methods or properties are automatically defined as statics."
[body]
{:pre [(map? body)]} {:pre [(map? body)]}
(->> body (let [body (cljsify body)
cljsify methods (map-to-js (apply dissoc body :displayName :getInitialState built-in-static-method-names))
create-react-class)) static-methods (map-to-js (select-keys body built-in-static-method-names))
display-name (:displayName body)
construct (:getInitialState body)
cmp (fn [props context updater]
(this-as this
(.call react/Component this props context updater)
(when construct
(construct this))
this))]
(gobj/extend (.-prototype cmp) (.-prototype react/Component) methods)
(gobj/extend cmp react/Component static-methods)
(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)
cmp))
(defn fiber-component-path [fiber] (defn fiber-component-path [fiber]
(let [name (some-> fiber (let [name (some-> fiber

View File

@ -1,7 +1,6 @@
(ns reagenttest.testreagent (ns reagenttest.testreagent
(:require [cljs.test :as t :refer-macros [is deftest testing]] (:require [cljs.test :as t :refer-macros [is deftest testing]]
[react :as react] [react :as react]
[create-react-class :as create-react-class]
[reagent.ratom :as rv :refer-macros [reaction]] [reagent.ratom :as rv :refer-macros [reaction]]
[reagent.debug :as debug :refer-macros [dbg println log dev?]] [reagent.debug :as debug :refer-macros [dbg println log dev?]]
[reagent.interop :refer-macros [$ $!]] [reagent.interop :refer-macros [$ $!]]
@ -441,15 +440,18 @@
(is (= (rstr (ae [:div [:div "foo"]])) (is (= (rstr (ae [:div [:div "foo"]]))
(rstr (ae [:div (ce "div" nil "foo")])))))) (rstr (ae [:div (ce "div" nil "foo")]))))))
(def ndiv (create-react-class (def ndiv (let [cmp (fn [])]
#js {:displayName "ndiv" (gobj/extend
:render (.-prototype cmp)
(fn [] (.-prototype react/Component)
(this-as #js {:render (fn []
this (this-as
(r/create-element this
"div" #js{:className ($ this :props.className)} (r/create-element
($ this :props.children))))})) "div" #js {:className ($ this :props.className)}
($ this :props.children))))})
(gobj/extend cmp react/Component)
cmp))
(deftest test-adapt-class (deftest test-adapt-class
(let [d1 (r/adapt-react-class ndiv) (let [d1 (r/adapt-react-class ndiv)
@ -990,7 +992,13 @@
comp4 (fn comp4 [] comp4 (fn comp4 []
(for [i (range 0 1)] (for [i (range 0 1)]
[:p "foo"])) [:p "foo"]))
nat (create-react-class #js {:render (fn [])}) nat (let [cmp (fn [])]
(gobj/extend
(.-prototype cmp)
(.-prototype react/Component)
#js {:render (fn [])})
(gobj/extend cmp react/Component)
cmp)
pkg "reagenttest.testreagent." pkg "reagenttest.testreagent."
stack1 (str "in " pkg "comp1") stack1 (str "in " pkg "comp1")
stack2 (str "in " pkg "comp2 > " pkg "comp1") stack2 (str "in " pkg "comp2 > " pkg "comp1")