Skip to content

Commit

Permalink
Preserve text-inputs default value
Browse files Browse the repository at this point in the history
Signed-off-by: Vitaliy Vlasov <siphiuel@gmail.com>
  • Loading branch information
Vitaliy Vlasov committed Sep 10, 2019
1 parent c3dd950 commit b215292
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 9 deletions.
17 changes: 10 additions & 7 deletions components/src/status_im/ui/components/react.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -126,14 +126,18 @@
[text-class (dissoc options-with-style :nested?)]
nested-text-elements)))

(def text-input-refs (atom #{}))
;; We track all currently mounted text input refs
;; in a ref-to-defaultValue map
;; so that we can clear them (restore their default values)
;; when global react-navigation's onWillBlur event is invoked
(def text-input-refs (atom {}))

(defn text-input
[options text]
(let [input-ref (atom nil)]
(reagent/create-class
{:component-will-unmount #(when @input-ref
(swap! text-input-refs disj @input-ref))
(swap! text-input-refs dissoc @input-ref))
:reagent-render
(fn [options text]
[text-input-class
Expand All @@ -143,11 +147,10 @@
:placeholder-text-color colors/text-gray
:placeholder (i18n/label :t/type-a-message)
:ref (fn [r]
(if r
(when (nil? @input-ref)
(swap! text-input-refs conj r))
(when @input-ref
(swap! text-input-refs disj @input-ref)))
;; Store input and its defaultValue
;; one we receive a non-nil ref
(when (and r (nil? @input-ref))
(swap! text-input-refs assoc r (:default-value options)))
(reset! input-ref r)
(when (:ref options)
((:ref options) r)))
Expand Down
3 changes: 3 additions & 0 deletions src/status_im/ui/screens/add_new/new_chat/views.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,9 @@
(re-frame/dispatch [:contact.ui/contact-code-submitted]))
:placeholder (i18n/label :t/enter-contact-code)
:style (add-new.styles/input @tw)
;; Set default-value as otherwise it will
;; be erased in global `onWillBlur` handler
:default-value new-identity
:accessibility-label :enter-contact-code-input
:return-key-type :go}]]
(when-not platform/desktop?
Expand Down
3 changes: 3 additions & 0 deletions src/status_im/ui/screens/add_new/new_public_chat/view.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,9 @@
:auto-capitalize :none
:auto-focus false
:accessibility-label :chat-name-input
;; Set default-value as otherwise it will
;; be erased in global `onWillBlur` handler
:default-value topic
:placeholder nil
:return-key-type :go
:auto-correct false}]]]
Expand Down
6 changes: 4 additions & 2 deletions src/status_im/ui/screens/routing/core.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,10 @@
:on-will-blur
(fn []
(reset! screen-focused? false)
(doseq [text-input @react/text-input-refs]
(.clear text-input)))}])
;; Reset currently mounted text inputs to their default values
;; on navigating away; this is a privacy measure
(doseq [[text-input default-value] @react/text-input-refs]
(.setNativeProps text-input (clj->js {:text default-value}))))}])

(defn wrap
"Wraps screen with main view and adds navigation-events component"
Expand Down

0 comments on commit b215292

Please sign in to comment.