Skip to content

Commit

Permalink
Add preserve-input flag to text-input
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 Oct 9, 2019
1 parent d2ff665 commit 227127f
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 28 deletions.
54 changes: 29 additions & 25 deletions components/src/status_im/ui/components/react.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -137,31 +137,35 @@

(defn text-input
[options text]
(let [input-ref (atom nil)]
(reagent/create-class
{:component-will-unmount #(when @input-ref
(swap! text-input-refs dissoc @input-ref))
:reagent-render
(fn [options text]
[text-input-class
(merge
{:underline-color-android :transparent
:max-font-size-multiplier max-font-size-multiplier
:placeholder-text-color colors/text-gray
:placeholder (i18n/label :t/type-a-message)
:ref (fn [r]
;; 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)))
:value text}
(-> options
(dissoc :ref)
(update :style typography/get-style)
(update :style dissoc :line-height)))])})))
(let [render-fn (fn [options text]
[text-input-class
(merge
{:underline-color-android :transparent
:max-font-size-multiplier max-font-size-multiplier
:placeholder-text-color colors/text-gray
:placeholder (i18n/label :t/type-a-message)
:value text}
(-> options
(dissoc :preserve-input?)
(update :style typography/get-style)
(update :style dissoc :line-height)))])]
(if (:preserve-input? options)
render-fn
(let [input-ref (atom nil)]
(reagent/create-class
{:component-will-unmount #(when @input-ref
(swap! text-input-refs dissoc @input-ref))
:reagent-render
(fn [options text]
(render-fn (assoc options :ref
(fn [r]
;; 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)))) text))})))))

(defn i18n-text
[{:keys [style key]}]
Expand Down
7 changes: 4 additions & 3 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,9 +37,10 @@
(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
;; This input is fine to preserve inputs
;; so its contents will not be erased
;; in onWillBlur navigation event handler
:preserve-input? true
:accessibility-label :enter-contact-code-input
:return-key-type :go}]]
(when-not platform/desktop?
Expand Down

0 comments on commit 227127f

Please sign in to comment.