Skip to content

Commit

Permalink
Use dom-node from reagent.dom in template
Browse files Browse the repository at this point in the history
And make sure we don't try to access unmounted component's dom
node.
  • Loading branch information
holmsand committed Sep 16, 2016
1 parent a5ff3b2 commit b65afde
Show file tree
Hide file tree
Showing 5 changed files with 22 additions and 11 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@

# Changelog

## Upcoming

- Fix :ref on inputs


## 0.6.0

- React updated to 15.2.1
Expand Down
2 changes: 1 addition & 1 deletion project.clj
Original file line number Diff line number Diff line change
@@ -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"
Expand Down
2 changes: 2 additions & 0 deletions src/reagent/dom.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
17 changes: 14 additions & 3 deletions src/reagent/impl/template.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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))
Expand Down Expand Up @@ -172,18 +176,24 @@
(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")
(doto jsprops
($! :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
Expand All @@ -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*]
Expand Down
7 changes: 0 additions & 7 deletions src/reagent/impl/util.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -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)))

Expand Down

0 comments on commit b65afde

Please sign in to comment.