Use dom-node from reagent.dom in template

And make sure we don't try to access unmounted component's dom
node.
This commit is contained in:
Dan Holmsand 2016-09-16 12:14:10 +02:00
parent a5ff3b23a7
commit b65afde4d7
5 changed files with 22 additions and 11 deletions

View File

@ -1,6 +1,11 @@
# Changelog
## Upcoming
- Fix :ref on inputs
## 0.6.0
- React updated to 15.2.1

View File

@ -1,4 +1,4 @@
(defproject reagent "0.6.0"
(defproject reagent "0.6.1-SNAPSHOT"
:url "http://github.com/reagent-project/reagent"
:license {:name "MIT"}
:description "A simple ClojureScript interface to React"

View File

@ -61,6 +61,8 @@
[this]
($ (module) findDOMNode this))
(set! tmpl/find-dom-node dom-node)
(defn force-update-all
"Force re-rendering of all mounted Reagent components. This is
probably only useful in a development environment, when you want to

View File

@ -98,6 +98,9 @@
;;; Specialization for input components
;; This gets set from reagent.dom
(defonce find-dom-node nil)
;; <input type="??" >
;; The properites 'selectionStart' and 'selectionEnd' only exist on some inputs
;; See: https://html.spec.whatwg.org/multipage/forms.html#do-not-apply
@ -109,10 +112,11 @@
(contains? these-inputs-have-selection-api input-type))
(defn input-set-value [this]
(when-some [node ($ util/react-dom findDOMNode this)]
(when ($ this :cljsInputLive)
($! this :cljsInputDirty false)
(let [rendered-value ($ this :cljsRenderedValue)
dom-value ($ this :cljsDOMValue)]
dom-value ($ this :cljsDOMValue)
node (find-dom-node this)]
(when (not= rendered-value dom-value)
(if-not (and (identical? node ($ js/document :activeElement))
(has-selection-api? ($ node :type))
@ -172,11 +176,14 @@
(when (and (some? jsprops)
(.hasOwnProperty jsprops "onChange")
(.hasOwnProperty jsprops "value"))
(assert find-dom-node
"reagent.dom needs to be loaded for controlled input to work")
(let [v ($ jsprops :value)
value (if (nil? v) "" v)
on-change ($ jsprops :onChange)]
(when (nil? ($ this :cljsDOMValue))
(when-not ($ this :cljsInputLive)
;; set initial value
($! this :cljsInputLive true)
($! this :cljsDOMValue value))
($! this :cljsRenderedValue value)
(js-delete jsprops "value")
@ -184,6 +191,9 @@
($! :defaultValue value)
($! :onChange #(input-handle-change this on-change %))))))
(defn input-unmount [this]
($! this :cljsInputLive nil))
(defn ^boolean input-component? [x]
(case x
("input" "textarea") true
@ -196,6 +206,7 @@
(def input-spec
{:display-name "ReagentInput"
:component-did-update input-set-value
:component-will-unmount input-unmount
:reagent-render
(fn [argv comp jsprops first-child]
(let [this comp/*current-component*]

View File

@ -10,13 +10,6 @@
(throw (js/Error. "require('react') failed")))
:else (throw (js/Error. "js/React is missing"))))
(defonce react-dom
(cond (exists? js/ReactDOM) js/ReactDOM
(exists? js/require) (or (js/require "react-dom")
(throw (js/Error. "require('react-dom') failed")))
:else
(throw (js/Error. "js/ReactDOM is missing"))))
(def is-client (and (exists? js/window)
(-> js/window ($ :document) nil? not)))