Get rid of old wrapping code

Inputs are now backed by a proper Reagent component. The same class
is now shared by every input instance.
This commit is contained in:
Dan Holmsand 2014-11-30 16:41:00 +01:00
parent 2cfd616dd2
commit 4e62b415ba
1 changed files with 29 additions and 58 deletions

View File

@ -124,50 +124,28 @@
(.! this :cljsInputValue nil))) (.! this :cljsInputValue nil)))
(defn input-component? [x] (defn input-component? [x]
(let [DOM (.' js/React :DOM)]
(or (= x "input") (or (= x "input")
(= x "textarea") (= x "textarea")))
(identical? x (.' DOM :input))
(identical? x (.' DOM :textarea)))))
(def reagent-input-class nil)
;;; Wrapping of native components
(declare make-element) (declare make-element)
(defn wrapped-render [this comp id-class input-setup] (def input-spec
(let [inprops (.' this :props) {:display-name "ReagentInput"
argv (.' inprops :argv) :component-did-update input-set-value
props (nth argv 1 nil) :component-will-unmount input-unmount
hasprops (or (nil? props) (map? props)) :component-function
jsprops (convert-props (if hasprops props) id-class false)] (fn [argv comp jsprops first-child]
(when-not (nil? input-setup) (let [this comp/*current-component*]
(input-setup this jsprops)) (input-render-setup this jsprops)
(make-element argv comp jsprops (make-element argv comp jsprops first-child)))})
(if hasprops 2 1))))
(defn wrapped-should-update [c nextprops nextstate] (defn reagent-input [argv comp jsprops first-child]
(or util/*always-update* (when (nil? reagent-input-class)
(let [a1 (.' c :props.argv) (set! reagent-input-class
a2 (.' nextprops :argv)] (comp/create-class input-spec)))
(not (util/equal-args a1 a2))))) (reagent-input-class argv comp jsprops first-child))
(defn add-input-methods [spec]
(doto spec
(.! :componentDidUpdate #(this-as c (input-set-value c)))
(.! :componentWillUnmount #(this-as c (input-unmount c)))))
(defn wrap-component [comp extras name]
(let [input? (input-component? comp)
input-setup (if input? input-render-setup)
spec #js{:render
#(this-as C (wrapped-render C comp extras input-setup))
:shouldComponentUpdate
#(this-as C (wrapped-should-update C %1 %2))
:displayName (or name "ComponentWrapper")}]
(when input?
(add-input-methods spec))
(.' js/React createClass spec)))
;;; Conversion from Hiccup forms ;;; Conversion from Hiccup forms
@ -180,12 +158,6 @@
[tag (when (or id class') [tag (when (or id class')
[id class'])])) [id class'])]))
(defn get-wrapper [tag]
(let [[comp id-class] (parse-tag tag)]
(wrap-component comp id-class (str tag))))
(def cached-wrapper (util/memoize-1 get-wrapper))
(defn fn-to-class [f] (defn fn-to-class [f]
(assert (ifn? f) (str "Expected a function, not " (pr-str f))) (assert (ifn? f) (str "Expected a function, not " (pr-str f)))
(let [spec (meta f) (let [spec (meta f)
@ -196,25 +168,24 @@
wrapf)) wrapf))
(defn as-class [tag] (defn as-class [tag]
(if (hiccup-tag? tag)
(cached-wrapper tag)
(let [cached-class (util/cached-react-class tag)] (let [cached-class (util/cached-react-class tag)]
(if-not (nil? cached-class) (if-not (nil? cached-class)
cached-class cached-class
(fn-to-class tag))))) (fn-to-class tag))))
(def cached-parse (util/memoize-1 parse-tag)) (def cached-parse (util/memoize-1 parse-tag))
(defn native-element [tag argv] (defn native-element [tag argv]
(when (hiccup-tag? tag) (when (hiccup-tag? tag)
(let [[comp id-class] (cached-parse tag)] (let [[comp id-class] (cached-parse tag)]
(when-not (input-component? comp)
(let [props (nth argv 1 nil) (let [props (nth argv 1 nil)
hasprops (or (nil? props) (map? props)) hasprops (or (nil? props) (map? props))
jsprops (convert-props (if hasprops props) id-class true)] jsprops (convert-props (if hasprops props) id-class true)
first-child (if hasprops 2 1)]
;; TODO: Meta key ;; TODO: Meta key
(make-element argv comp jsprops (if (input-component? comp)
(if hasprops 2 1))))))) (reagent-input argv comp jsprops first-child)
(make-element argv comp jsprops first-child))))))
(defn get-key [x] (defn get-key [x]
(when (map? x) (get x :key))) (when (map? x) (get x :key)))